root/trunk/libs/hydrogen/src/action.cpp @ 578

Revision 578, 12.5 KB (checked in by smoors, 5 years ago)

corrected the action list and added actions to control the master volume

Line 
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22#include <QObject>
23
24#include <hydrogen/audio_engine.h>
25#include <hydrogen/event_queue.h>
26#include <hydrogen/hydrogen.h>
27#include <gui/src/HydrogenApp.h>
28
29#include <hydrogen/Preferences.h>
30#include <hydrogen/action.h>
31#include <map>
32
33ActionManager* ActionManager::instance = NULL;
34
35using namespace H2Core;
36
37/* Class Action */
38
39QString Action::getType(){
40        return type;
41}
42
43
44
45void Action::setParameter1( QString text ){
46        parameter1 = text;
47}
48
49void Action::setParameter2( QString text ){
50        parameter2 = text;
51}
52
53QString Action::getParameter1(){
54        return parameter1;
55}
56
57QString Action::getParameter2(){
58        return parameter2;
59}
60
61Action::Action( QString s ) : Object( "Action" ) {
62        type = s;
63        QString parameter1 = "0";
64        QString parameter2 = "0" ;
65}
66
67
68/* Class ActionManager */
69
70
71ActionManager::ActionManager() : Object( "ActionManager" ) {
72        INFOLOG( "ActionManager Init" );
73       
74        actionList <<""
75        << "PLAY"
76        << "PLAY_TOGGLE"
77        << "STOP"
78        << "PAUSE"
79        << "MUTE"
80        << "UNMUTE"
81        << "MUTE_TOGGLE"
82        << ">>_NEXT_BAR"
83        << "<<_PREVIOUS_BAR"
84        << "BPM_INCR"
85        << "BPM_DECR"
86        << "BPM_CC_RELATIVE"
87        << "MASTER_VOLUME_RELATIVE"
88        << "MASTER_VOLUME_ABSOLUTE"
89/*      << "STRIP_VOLUME_RELATIVE"
90        << "STRIP_VOLUME_ABSOLUTE"
91        << "EFFECT1_LEVEL_RELATIVE"
92        << "EFFECT2_LEVEL_RELATIVE"
93        << "EFFECT3_LEVEL_RELATIVE"
94        << "EFFECT4_LEVEL_RELATIVE"
95        << "EFFECT1_LEVEL_ABSOLUTE"
96        << "EFFECT2_LEVEL_ABSOLUTE"
97        << "EFFECT3_LEVEL_ABSOLUTE"
98        << "EFFECT4_LEVEL_ABSOLUTE"
99        << "PAN_RELATIVE"
100        << "PAN_ABSOULTE"*/
101        << "BEATCOUNTER"
102        << "TAP_TEMPO";
103
104        eventList << ""
105        << "MMC_PLAY"
106        << "MMC_DEFERRED_PLAY"
107        << "MMC_STOP"
108        << "MMC_FAST_FORWARD"
109        << "MMC_REWIND"
110        << "MMC_RECORD_STROBE"
111        << "MMC_RECORD_EXIT"
112        << "MMC_PAUSE"
113        << "NOTE"
114        << "CC";
115}
116
117
118ActionManager::~ActionManager(){
119        INFOLOG( "ActionManager delete" );
120        instance = NULL;
121}
122
123
124/// Return an instance of ActionManager
125ActionManager* ActionManager::getInstance()
126{
127        if ( instance == NULL ) {
128                instance = new ActionManager();
129        }
130       
131        return instance;
132}
133
134QStringList ActionManager::getActionList(){
135        return actionList;
136}
137
138QStringList ActionManager::getEventList(){
139        return eventList;
140}
141
142
143/*
144bool setAbsoluteFXLevel( int nLine, int fx_channel , int fx_param)
145{
146        //helper function to set fx levels
147                       
148        Hydrogen::get_instance()->setSelectedInstrumentNumber( nLine );
149
150        Hydrogen *engine = Hydrogen::get_instance();
151        Song *song = engine->getSong();
152        InstrumentList *instrList = song->get_instrument_list();
153        Instrument *instr = instrList->get( nLine );
154        if ( instr == NULL) return false;
155
156        if( fx_param != 0 ){
157                        instr->set_fx_level(  ( (float) (fx_param / 127.0 ) ), fx_channel );
158        } else {
159                        instr->set_fx_level( 0 , fx_channel );
160        }
161               
162        Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
163       
164        return true;
165
166}*/
167
168bool ActionManager::handleAction( Action * pAction ){
169
170        Hydrogen *pEngine = Hydrogen::get_instance();
171
172        /*
173                return false if action is null
174                (for example if no Action exists for an event)
175        */
176        if( pAction == NULL )   return false;
177
178        QString sActionString = pAction->getType();
179
180       
181        if( sActionString == "PLAY" )
182        {
183                int nState = pEngine->getState();
184                if ( nState == STATE_READY ){
185                        pEngine->sequencer_play();
186                }
187                return true;
188        }
189
190        if( sActionString == "PLAY_TOGGLE" )
191        {
192                int nState = pEngine->getState();
193                switch ( nState )
194                {
195                        case STATE_READY:
196                                pEngine->sequencer_play();
197                                break;
198
199                        case STATE_PLAYING:
200                                pEngine->sequencer_stop();
201                                break;
202
203                        default:
204                                ERRORLOG( "[Hydrogen::ActionManager(PLAY): Unhandled case" );
205                }
206
207                return true;
208        }
209
210        if( sActionString == "PAUSE" )
211        {       
212                pEngine->sequencer_stop();
213                return true;
214        }
215
216        if( sActionString == "STOP" )
217        {       
218                pEngine->sequencer_stop();
219                pEngine->setPatternPos( 0 );
220                return true;
221        }
222
223        if( sActionString == "MUTE" ){
224                //mutes the master, not a single strip
225                pEngine->getSong()->__is_muted = true;
226                return true;
227        }
228
229        if( sActionString == "UNMUTE" ){
230                pEngine->getSong()->__is_muted = false;
231                return true;
232        }
233
234        if( sActionString == "MUTE_TOGGLE" ){
235                pEngine->getSong()->__is_muted = !Hydrogen::get_instance()->getSong()->__is_muted;
236                return true;
237        }
238
239        if( sActionString == "BEATCOUNTER" ){
240                pEngine->handleBeatCounter();
241                return true;
242        }
243
244        if( sActionString == "TAP_TEMPO" ){
245                pEngine->onTapTempoAccelEvent();
246                return true;
247        }
248
249
250
251        /*
252        if( sActionString == "EFFECT1_LEVEL_ABSOLUTE" ){
253                bool ok;
254                int nLine = pAction->getParameter1().toInt(&ok,10);
255                int fx_param = pAction->getParameter2().toInt(&ok,10);
256                setAbsoluteFXLevel( nLine, 0 , fx_param );
257        }
258
259        if( sActionString == "EFFECT2_LEVEL_ABSOLUTE" ){
260                bool ok;
261                int nLine = pAction->getParameter1().toInt(&ok,10);
262                int fx_param = pAction->getParameter2().toInt(&ok,10);
263                setAbsoluteFXLevel( nLine, 1 , fx_param );
264        }
265
266        if( sActionString == "EFFECT3_LEVEL_ABSOLUTE" ){
267                bool ok;
268                int nLine = pAction->getParameter1().toInt(&ok,10);
269                int fx_param = pAction->getParameter2().toInt(&ok,10);
270                setAbsoluteFXLevel( nLine, 2 , fx_param );
271        }
272
273        if( sActionString == "EFFECT4_LEVEL_ABSOLUTE" ){
274                bool ok;
275                int nLine = pAction->getParameter1().toInt(&ok,10);
276                int fx_param = pAction->getParameter2().toInt(&ok,10);
277                setAbsoluteFXLevel( nLine, 3 , fx_param );
278        }
279       
280
281
282        */
283
284        if( sActionString == "MASTER_VOLUME_RELATIVE" ){
285                //increments/decrements the volume of the whole song   
286               
287                bool ok;
288                int vol_param = pAction->getParameter2().toInt(&ok,10);
289                       
290                Hydrogen *engine = Hydrogen::get_instance();
291                Song *song = engine->getSong();
292
293
294
295                if( vol_param != 0 ){
296                                if ( vol_param == 1 && song->get_volume() < 1.5 ){
297                                        song->set_volume( song->get_volume() + 0.05 ); 
298                                }  else  {
299                                        if( song->get_volume() >= 0.0 ){
300                                                song->set_volume( song->get_volume() - 0.05 );
301                                        }
302                                }
303                } else {
304                        song->set_volume( 0 );
305                }
306
307        }
308
309       
310
311        if( sActionString == "MASTER_VOLUME_ABSOLUTE" ){
312                //sets the volume of a mixer strip to a given level (percentage)
313
314                bool ok;
315                int nLine = pAction->getParameter1().toInt(&ok,10);
316                int vol_param = pAction->getParameter2().toInt(&ok,10);
317                       
318
319                Hydrogen *engine = Hydrogen::get_instance();
320                Song *song = engine->getSong();
321
322
323                if( vol_param != 0 ){
324                                song->set_volume( 1.5* ( (float) (vol_param / 127.0 ) ));
325                } else {
326                                song->set_volume( 0 );
327                }
328
329        }
330
331        /*
332
333        if( sActionString == "STRIP_VOLUME_RELATIVE" ){
334                //increments/decrements the volume of one mixer strip   
335               
336                bool ok;
337                int nLine = pAction->getParameter1().toInt(&ok,10);
338                int vol_param = pAction->getParameter2().toInt(&ok,10);
339                       
340                Hydrogen::get_instance()->setSelectedInstrumentNumber( nLine );
341
342                Hydrogen *engine = Hydrogen::get_instance();
343                Song *song = engine->getSong();
344                InstrumentList *instrList = song->get_instrument_list();
345
346                Instrument *instr = instrList->get( nLine );
347
348                if ( instr == NULL) return 0;
349
350                if( vol_param != 0 ){
351                                if ( vol_param == 1 && instr->get_volume() < 1.5 ){
352                                        instr->set_volume( instr->get_volume() + 0.1 ); 
353                                }  else  {
354                                        if( instr->get_volume() >= 0.0 ){
355                                                instr->set_volume( instr->get_volume() - 0.1 );
356                                        }
357                                }
358                } else {
359                        instr->set_volume( 0 );
360                }
361
362                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
363        }
364
365        if( sActionString == "STRIP_VOLUME_ABSOLUTE" ){
366                //sets the volume of a mixer strip to a given level (percentage)
367
368                bool ok;
369                int nLine = pAction->getParameter1().toInt(&ok,10);
370                int vol_param = pAction->getParameter2().toInt(&ok,10);
371                       
372                Hydrogen::get_instance()->setSelectedInstrumentNumber( nLine );
373
374                Hydrogen *engine = Hydrogen::get_instance();
375                Song *song = engine->getSong();
376                InstrumentList *instrList = song->get_instrument_list();
377
378                Instrument *instr = instrList->get( nLine );
379
380                if ( instr == NULL) return 0;
381
382                if( vol_param != 0 ){
383                                instr->set_volume( 1.5* ( (float) (vol_param / 127.0 ) ));
384                } else {
385                                instr->set_volume( 0 );
386                }
387
388                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
389        }
390
391       
392        if( sActionString == "PAN_ABSOULTE" ){
393               
394                 // sets the absolute panning of a given mixer channel
395               
396               
397                bool ok;
398                int nLine = pAction->getParameter1().toInt(&ok,10);
399                int pan_param = pAction->getParameter2().toInt(&ok,10);
400
401               
402                float pan_L;
403                float pan_R;
404
405                Hydrogen *engine = Hydrogen::get_instance();
406                engine->setSelectedInstrumentNumber( nLine );
407                Song *song = engine->getSong();
408                InstrumentList *instrList = song->get_instrument_list();
409
410                Instrument *instr = instrList->get( nLine );
411               
412                if( instr == NULL )
413                        return false;
414               
415                pan_L = instr->get_pan_l();
416                pan_R = instr->get_pan_r();
417
418                // pan
419                float fPanValue = 0.0;
420                if (pan_R == 1.0) {
421                        fPanValue = 1.0 - (pan_L / 2.0);
422                }
423                else {
424                        fPanValue = pan_R / 2.0;
425                }
426
427               
428                fPanValue = 1 * ( ((float) pan_param) / 127.0 );
429
430
431                if (fPanValue >= 0.5) {
432                        pan_L = (1.0 - fPanValue) * 2;
433                        pan_R = 1.0;
434                }
435                else {
436                        pan_L = 1.0;
437                        pan_R = fPanValue * 2;
438                }
439
440
441                instr->set_pan_l( pan_L );
442                instr->set_pan_r( pan_R );
443
444                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
445
446                return true;
447        }
448
449        if( sActionString == "PAN_RELATIVE" ){
450               
451                 // changes the panning of a given mixer channel
452                // this is useful if the panning is set by a rotary control knob
453               
454               
455                bool ok;
456                int nLine = pAction->getParameter1().toInt(&ok,10);
457                int pan_param = pAction->getParameter2().toInt(&ok,10);
458
459               
460                float pan_L;
461                float pan_R;
462
463                Hydrogen *engine = Hydrogen::get_instance();
464                engine->setSelectedInstrumentNumber( nLine );
465                Song *song = engine->getSong();
466                InstrumentList *instrList = song->get_instrument_list();
467
468                Instrument *instr = instrList->get( nLine );
469               
470                if( instr == NULL )
471                        return false;
472               
473                pan_L = instr->get_pan_l();
474                pan_R = instr->get_pan_r();
475
476                // pan
477                float fPanValue = 0.0;
478                if (pan_R == 1.0) {
479                        fPanValue = 1.0 - (pan_L / 2.0);
480                }
481                else {
482                        fPanValue = pan_R / 2.0;
483                }
484
485                if( pan_param == 1 && fPanValue < 1 ){
486                        fPanValue += 0.05;
487                }
488
489                if( pan_param != 1 && fPanValue > 0 ){
490                        fPanValue -= 0.05;
491                }
492
493                if (fPanValue >= 0.5) {
494                        pan_L = (1.0 - fPanValue) * 2;
495                        pan_R = 1.0;
496                }
497                else {
498                        pan_L = 1.0;
499                        pan_R = fPanValue * 2;
500                }
501
502
503                instr->set_pan_l( pan_L );
504                instr->set_pan_r( pan_R );
505
506                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
507
508                return true;
509        }
510        */
511
512        if( sActionString == "BPM_CC_RELATIVE" ){
513                /*
514                 * increments/decrements the BPM
515                 * this is useful if the bpm is set by a rotary control knob
516                */
517
518                AudioEngine::get_instance()->lock( "Action::BPM_CC_RELATIVE" );
519
520                int mult = 1;   
521
522                //second parameter of cc command
523                //this value should be 1 to decrement and something other then 1 to increment the bpm
524                int cc_param = 1;
525
526                //this Action should be triggered only by CC commands
527
528                bool ok;
529                mult = pAction->getParameter1().toInt(&ok,10);
530                cc_param = pAction->getParameter2().toInt(&ok,10);
531                       
532
533
534                Song* pSong = pEngine->getSong();
535
536
537               
538                if ( cc_param == 1 && pSong->__bpm  < 300) {
539                        pEngine->setBPM( pSong->__bpm + 1*mult );
540                }
541
542
543                if ( cc_param != 1 && pSong->__bpm  > 40 ) {
544                        pEngine->setBPM( pSong->__bpm - 1*mult );
545                }
546
547
548                AudioEngine::get_instance()->unlock();
549
550                return true;
551        }
552
553
554        if( sActionString == "BPM_INCR" ){
555                AudioEngine::get_instance()->lock( "Action::BPM_INCR" );
556
557                int mult = 1;   
558
559                bool ok;
560                mult = pAction->getParameter1().toInt(&ok,10);
561
562
563                Song* pSong = pEngine->getSong();
564                if (pSong->__bpm  < 300) {
565                        pEngine->setBPM( pSong->__bpm + 1*mult );
566                }
567                AudioEngine::get_instance()->unlock();
568
569                return true;
570        }
571
572
573
574        if( sActionString == "BPM_DECR" ){
575                AudioEngine::get_instance()->lock( "Action::BPM_DECR" );
576
577                int mult = 1;   
578
579                bool ok;
580                mult = pAction->getParameter1().toInt(&ok,10);
581
582                Song* pSong = pEngine->getSong();
583                if (pSong->__bpm  > 40 ) {
584                        pEngine->setBPM( pSong->__bpm - 1*mult );
585                }
586                AudioEngine::get_instance()->unlock();
587               
588                return true;
589        }
590
591        if( sActionString == ">>_NEXT_BAR"){
592                pEngine->setPatternPos(pEngine->getPatternPos() +1 );
593                return true;
594        }
595
596        if( sActionString == "<<_PREVIOUS_BAR"){
597                pEngine->setPatternPos(pEngine->getPatternPos() -1 );
598                return true;
599        }
600       
601        return false;
602}
Note: See TracBrowser for help on using the browser.