root/branches/midiAutosense/libs/hydrogen/src/action.cpp @ 566

Revision 566, 12.4 KB (checked in by smoors, 5 years ago)

removed unnecessary methods

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
168
169bool actionManager::handleAction( action * pAction ){
170
171        Hydrogen *pEngine = Hydrogen::get_instance();
172
173        /*
174                return false if action is null
175                (for example if no action exists for an event)
176        */
177        if( pAction == NULL )   return false;
178
179        QString sActionString = pAction->getType();
180
181       
182        if( sActionString == "PLAY" )
183        {
184                int nState = pEngine->getState();
185                if ( nState == STATE_READY ){
186                        pEngine->sequencer_play();
187                }
188                return true;
189        }
190
191        if( sActionString == "PLAY_TOGGLE" )
192        {
193                int nState = pEngine->getState();
194                switch ( nState )
195                {
196                        case STATE_READY:
197                                pEngine->sequencer_play();
198                                break;
199
200                        case STATE_PLAYING:
201                                pEngine->sequencer_stop();
202                                break;
203
204                        default:
205                                ERRORLOG( "[Hydrogen::actionManager(PLAY): Unhandled case" );
206                }
207
208                return true;
209        }
210
211        if( sActionString == "PAUSE" )
212        {       
213                pEngine->sequencer_stop();
214                return true;
215        }
216
217        if( sActionString == "STOP" )
218        {       
219                pEngine->sequencer_stop();
220                pEngine->setPatternPos( 0 );
221                return true;
222        }
223
224        if( sActionString == "MUTE" ){
225                //mutes the master, not a single strip
226                pEngine->getSong()->__is_muted = true;
227                return true;
228        }
229
230        if( sActionString == "UNMUTE" ){
231                pEngine->getSong()->__is_muted = false;
232                return true;
233        }
234
235        if( sActionString == "MUTE_TOGGLE" ){
236                pEngine->getSong()->__is_muted = !Hydrogen::get_instance()->getSong()->__is_muted;
237                return true;
238        }
239
240        if( sActionString == "BEATCOUNTER" ){
241                pEngine->handleBeatCounter();
242                return true;
243        }
244
245        if( sActionString == "TAP_TEMPO" ){
246                pEngine->onTapTempoAccelEvent();
247                return true;
248        }
249
250
251
252       
253        if( sActionString == "EFFECT1_LEVEL_ABSOLUTE" ){
254                bool ok;
255                int nLine = pAction->getParameter1().toInt(&ok,10);
256                int fx_param = pAction->getParameter2().toInt(&ok,10);
257                setAbsoluteFXLevel( nLine, 0 , fx_param );
258        }
259
260        if( sActionString == "EFFECT2_LEVEL_ABSOLUTE" ){
261                bool ok;
262                int nLine = pAction->getParameter1().toInt(&ok,10);
263                int fx_param = pAction->getParameter2().toInt(&ok,10);
264                setAbsoluteFXLevel( nLine, 1 , fx_param );
265        }
266
267        if( sActionString == "EFFECT3_LEVEL_ABSOLUTE" ){
268                bool ok;
269                int nLine = pAction->getParameter1().toInt(&ok,10);
270                int fx_param = pAction->getParameter2().toInt(&ok,10);
271                setAbsoluteFXLevel( nLine, 2 , fx_param );
272        }
273
274        if( sActionString == "EFFECT4_LEVEL_ABSOLUTE" ){
275                bool ok;
276                int nLine = pAction->getParameter1().toInt(&ok,10);
277                int fx_param = pAction->getParameter2().toInt(&ok,10);
278                setAbsoluteFXLevel( nLine, 3 , fx_param );
279        }
280
281
282
283
284
285        if( sActionString == "MASTER_VOLUME_RELATIVE" ){
286                //increments/decrements the volume of the whole song   
287               
288                bool ok;
289                int vol_param = pAction->getParameter2().toInt(&ok,10);
290                       
291                Hydrogen *engine = Hydrogen::get_instance();
292                Song *song = engine->getSong();
293
294
295
296                if( vol_param != 0 ){
297                                if ( vol_param == 1 && song->get_volume() < 1.5 ){
298                                        song->set_volume( song->get_volume() + 0.05 ); 
299                                }  else  {
300                                        if( song->get_volume() >= 0.0 ){
301                                                song->set_volume( song->get_volume() - 0.05 );
302                                        }
303                                }
304                } else {
305                        song->set_volume( 0 );
306                }
307
308        }
309
310        if( sActionString == "MASTER_VOLUME_ABSOLUTE" ){
311                //sets the volume of a mixer strip to a given level (percentage)
312
313                bool ok;
314                int nLine = pAction->getParameter1().toInt(&ok,10);
315                int vol_param = pAction->getParameter2().toInt(&ok,10);
316                       
317
318                Hydrogen *engine = Hydrogen::get_instance();
319                Song *song = engine->getSong();
320
321
322                if( vol_param != 0 ){
323                                song->set_volume( 1.5* ( (float) (vol_param / 127.0 ) ));
324                } else {
325                                song->set_volume( 0 );
326                }
327
328        }
329
330        if( sActionString == "STRIP_VOLUME_RELATIVE" ){
331                //increments/decrements the volume of one mixer strip   
332               
333                bool ok;
334                int nLine = pAction->getParameter1().toInt(&ok,10);
335                int vol_param = pAction->getParameter2().toInt(&ok,10);
336                       
337                Hydrogen::get_instance()->setSelectedInstrumentNumber( nLine );
338
339                Hydrogen *engine = Hydrogen::get_instance();
340                Song *song = engine->getSong();
341                InstrumentList *instrList = song->get_instrument_list();
342
343                Instrument *instr = instrList->get( nLine );
344
345                if ( instr == NULL) return 0;
346
347                if( vol_param != 0 ){
348                                if ( vol_param == 1 && instr->get_volume() < 1.5 ){
349                                        instr->set_volume( instr->get_volume() + 0.1 ); 
350                                }  else  {
351                                        if( instr->get_volume() >= 0.0 ){
352                                                instr->set_volume( instr->get_volume() - 0.1 );
353                                        }
354                                }
355                } else {
356                        instr->set_volume( 0 );
357                }
358
359                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
360        }
361
362        if( sActionString == "STRIP_VOLUME_ABSOLUTE" ){
363                //sets the volume of a mixer strip to a given level (percentage)
364
365                bool ok;
366                int nLine = pAction->getParameter1().toInt(&ok,10);
367                int vol_param = pAction->getParameter2().toInt(&ok,10);
368                       
369                Hydrogen::get_instance()->setSelectedInstrumentNumber( nLine );
370
371                Hydrogen *engine = Hydrogen::get_instance();
372                Song *song = engine->getSong();
373                InstrumentList *instrList = song->get_instrument_list();
374
375                Instrument *instr = instrList->get( nLine );
376
377                if ( instr == NULL) return 0;
378
379                if( vol_param != 0 ){
380                                instr->set_volume( 1.5* ( (float) (vol_param / 127.0 ) ));
381                } else {
382                                instr->set_volume( 0 );
383                }
384
385                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
386        }
387
388       
389        if( sActionString == "PAN_ABSOULTE" ){
390                /*
391                 * sets the absolute panning of a given mixer channel
392                */
393               
394                bool ok;
395                int nLine = pAction->getParameter1().toInt(&ok,10);
396                int pan_param = pAction->getParameter2().toInt(&ok,10);
397
398               
399                float pan_L;
400                float pan_R;
401
402                Hydrogen *engine = Hydrogen::get_instance();
403                engine->setSelectedInstrumentNumber( nLine );
404                Song *song = engine->getSong();
405                InstrumentList *instrList = song->get_instrument_list();
406
407                Instrument *instr = instrList->get( nLine );
408               
409                if( instr == NULL )
410                        return false;
411               
412                pan_L = instr->get_pan_l();
413                pan_R = instr->get_pan_r();
414
415                // pan
416                float fPanValue = 0.0;
417                if (pan_R == 1.0) {
418                        fPanValue = 1.0 - (pan_L / 2.0);
419                }
420                else {
421                        fPanValue = pan_R / 2.0;
422                }
423
424               
425                fPanValue = 1 * ( ((float) pan_param) / 127.0 );
426
427
428                if (fPanValue >= 0.5) {
429                        pan_L = (1.0 - fPanValue) * 2;
430                        pan_R = 1.0;
431                }
432                else {
433                        pan_L = 1.0;
434                        pan_R = fPanValue * 2;
435                }
436
437
438                instr->set_pan_l( pan_L );
439                instr->set_pan_r( pan_R );
440
441                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
442
443                return true;
444        }
445
446        if( sActionString == "PAN_RELATIVE" ){
447                /*
448                 * changes the panning of a given mixer channel
449                 * this is useful if the panning is set by a rotary control knob
450                */
451               
452                bool ok;
453                int nLine = pAction->getParameter1().toInt(&ok,10);
454                int pan_param = pAction->getParameter2().toInt(&ok,10);
455
456               
457                float pan_L;
458                float pan_R;
459
460                Hydrogen *engine = Hydrogen::get_instance();
461                engine->setSelectedInstrumentNumber( nLine );
462                Song *song = engine->getSong();
463                InstrumentList *instrList = song->get_instrument_list();
464
465                Instrument *instr = instrList->get( nLine );
466               
467                if( instr == NULL )
468                        return false;
469               
470                pan_L = instr->get_pan_l();
471                pan_R = instr->get_pan_r();
472
473                // pan
474                float fPanValue = 0.0;
475                if (pan_R == 1.0) {
476                        fPanValue = 1.0 - (pan_L / 2.0);
477                }
478                else {
479                        fPanValue = pan_R / 2.0;
480                }
481
482                if( pan_param == 1 && fPanValue < 1 ){
483                        fPanValue += 0.05;
484                }
485
486                if( pan_param != 1 && fPanValue > 0 ){
487                        fPanValue -= 0.05;
488                }
489
490                if (fPanValue >= 0.5) {
491                        pan_L = (1.0 - fPanValue) * 2;
492                        pan_R = 1.0;
493                }
494                else {
495                        pan_L = 1.0;
496                        pan_R = fPanValue * 2;
497                }
498
499
500                instr->set_pan_l( pan_L );
501                instr->set_pan_r( pan_R );
502
503                Hydrogen::get_instance()->setSelectedInstrumentNumber(nLine);
504
505                return true;
506        }
507
508        if( sActionString == "BPM_CC_RELATIVE" ){
509                /*
510                 * increments/decrements the BPM
511                 * this is useful if the bpm is set by a rotary control knob
512                */
513
514                AudioEngine::get_instance()->lock( "Action::BPM_CC_RELATIVE" );
515
516                int mult = 1;   
517
518                //second parameter of cc command
519                //this value should be 1 to decrement and something other then 1 to increment the bpm
520                int cc_param = 1;
521
522                //this action should be triggered only by CC commands
523
524                bool ok;
525                mult = pAction->getParameter1().toInt(&ok,10);
526                cc_param = pAction->getParameter2().toInt(&ok,10);
527                       
528
529
530                Song* pSong = pEngine->getSong();
531
532
533               
534                if ( cc_param == 1 && pSong->__bpm  < 300) {
535                        pEngine->setBPM( pSong->__bpm + 1*mult );
536                }
537
538
539                if ( cc_param != 1 && pSong->__bpm  > 40 ) {
540                        pEngine->setBPM( pSong->__bpm - 1*mult );
541                }
542
543
544                AudioEngine::get_instance()->unlock();
545
546                return true;
547        }
548
549
550        if( sActionString == "BPM_INCR" ){
551                AudioEngine::get_instance()->lock( "Action::BPM_INCR" );
552
553                int mult = 1;   
554
555                bool ok;
556                mult = pAction->getParameter1().toInt(&ok,10);
557
558
559                Song* pSong = pEngine->getSong();
560                if (pSong->__bpm  < 300) {
561                        pEngine->setBPM( pSong->__bpm + 1*mult );
562                }
563                AudioEngine::get_instance()->unlock();
564
565                return true;
566        }
567
568
569
570        if( sActionString == "BPM_DECR" ){
571                AudioEngine::get_instance()->lock( "Action::BPM_DECR" );
572
573                int mult = 1;   
574
575                bool ok;
576                mult = pAction->getParameter1().toInt(&ok,10);
577
578                Song* pSong = pEngine->getSong();
579                if (pSong->__bpm  > 40 ) {
580                        pEngine->setBPM( pSong->__bpm - 1*mult );
581                }
582                AudioEngine::get_instance()->unlock();
583               
584                return true;
585        }
586
587        if( sActionString == ">>_NEXT_BAR"){
588                pEngine->setPatternPos(pEngine->getPatternPos() +1 );
589                return true;
590        }
591
592        if( sActionString == "<<_PREVIOUS_BAR"){
593                pEngine->setPatternPos(pEngine->getPatternPos() -1 );
594                return true;
595        }
596       
597        return false;
598}
Note: See TracBrowser for help on using the browser.