Programmable real-time audio signal processing application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmdIf.h 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef cmdIf_h
  2. #define cmdIf_h
  3. class Fl_Output;
  4. // This class allows the 'model' to run independently
  5. // from the user interface. This eliminates problems with
  6. // application unresponsiveness which would arise from
  7. // executing compute intensive operations from UI control or
  8. // menu callback functions. It also allows a progress
  9. // or status bar to be shown while the model operation
  10. // is in progress.
  11. // The class works by enqueueing all incoming commands
  12. // into a thread-safe queue. An internal worker thread
  13. // executes the commands in the order they are received
  14. // and thereby changes the state of the model.
  15. // The results of model changes are posted to a second
  16. // thread-safe queue. These responses are picked
  17. // up by the onIdle() member and dispatched to an
  18. // object with a cmIfRspdr interface. Note that because
  19. // the onIdle() function is called from the applications
  20. // idle handler the callbacks to the cmIfRspdr are
  21. // always in the application thread and can therefore
  22. // safely interact with any application data constructs.
  23. // All calls to this object and all callbacks from it,
  24. // therefore occur in the application thread.
  25. class cmdIfRspdr
  26. {
  27. public:
  28. virtual ~cmdIfRspdr(){}
  29. virtual void cmdIfShowStatusMsg( const char* msg ) = 0;
  30. virtual void cmdIfHideStatus() = 0;
  31. virtual void cmdIfErrorMsg( const char* msg ) = 0;
  32. virtual void cmdIfTimeLineMsg( const void* msg, unsigned msgByteCnt ) = 0;
  33. virtual void cmdIfAudioFileLoad( unsigned fileId ) = 0;
  34. virtual void cmdIfScoreMsg( const void* msg, unsigned msgByteCnt ) = 0;
  35. virtual void cmdIfOnTimeLineMarkerSelect( unsigned markerId ) = 0;
  36. virtual void cmdIfOnTimeLineMidiEvtSelect(unsigned midiEvtId ) = 0;
  37. virtual void cmdIfOnScoreBarSelect( unsigned scoreIndex ) = 0;
  38. };
  39. class cmdIf
  40. {
  41. public:
  42. typedef enum
  43. {
  44. kOkRC,
  45. kCmdFailRC,
  46. kCmdEnqueueFailRC
  47. } rc_t;
  48. cmdIf( cmCtx_t* ctx, cmdIfRspdr* rspdr, const cmChar_t* audioPath=NULL );
  49. virtual ~cmdIf();
  50. // Open a time line.
  51. rc_t open( const cmChar_t* fn );
  52. // Close the time line.
  53. rc_t close();
  54. // Time line interface
  55. const cmChar_t* tlFileName() const;
  56. const cmTlObj_t* tlObjIdToPtr( unsigned tlObjId ) const;
  57. const cmTlMidiFile_t* tlMidiFileObjPtr( const cmTlObj_t* op ) const;
  58. const cmTlAudioFile_t* tlAudioFileObjPtr( const cmTlObj_t* op ) const;
  59. const cmTlMidiEvt_t* tlMidiEvtObjPtr( const cmTlObj_t* op ) const;
  60. const cmTlAudioEvt_t* tlAudioEvtObjPtr( const cmTlObj_t* op ) const;
  61. const cmTlMarker_t* tlMarkerObjPtr( const cmTlObj_t* op ) const;
  62. const cmChar_t* scoreFileName() const;
  63. const cmScoreEvt_t* scoreEventIdToPtr( unsigned scEvtId ) const;
  64. const cmScoreSection_t* scoreSectionIdToPtr( unsigned scSectId ) const;
  65. // Make a time line sequence active.
  66. rc_t selectSequence( unsigned id );
  67. // Load an audio file into the audio file mgr.
  68. // If an audio file path was set via setAudioFilePath() then
  69. // the directories referenced by 'fn' will be replaced by
  70. // the previously set audio file path.
  71. rc_t audioFileLoad( const cmChar_t* fn, unsigned appFileId );
  72. // Return the audio file handle assigned to appFileId.
  73. cmAfmFileH_t audioFileHandle( unsigned appFileId );
  74. // Set the audio file location.
  75. void setAudioFilePath( const cmChar_t* path );
  76. // Assign a score file
  77. rc_t setScore( const cmChar_t* scoreFn );
  78. // Set the current score location
  79. void setScoreLocation( unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel );
  80. // Set the value of a score variable
  81. void setScoreVarValue( unsigned locIdx, unsigned varId, double value );
  82. // Set the performed dynamic level of a score event.
  83. void setScoreDynLevel( unsigned evtIdx, unsigned dynLvl );
  84. void onTimeLineMarkerSelected( unsigned markerTlId );
  85. void onTimeLineMidiEvtSelected( unsigned midiEvtTlId );
  86. void onScoreBarSelected( unsigned scoreIndex );
  87. // Generate or delete Audio/MIDI onset markers.
  88. rc_t generateOnsetMarks();
  89. rc_t deleteOnsetMarks();
  90. // True if the worker thread is running.
  91. bool isBusy() const;
  92. // Called by the application to dequeue messages via callbacks to
  93. // the cmdIfRspdr.
  94. void onIdle();
  95. void testStub();
  96. private:
  97. // Id's used by cmd_t
  98. typedef enum
  99. {
  100. kInvalidCmdId, // 0
  101. kOpenCmdId, // 1 app->thread
  102. kCloseCmdId, // 2 app->thread
  103. kSelectSeqCmdId, // 3 app->thread
  104. kAfLoadCmdId, // 4 app->thread and thread->app
  105. kScoreCmdId, // 5 app->thread
  106. kShowStatusCmdId, // 6 thread->app
  107. kHideStatusCmdId, // 7 thread->app
  108. kErrMsgCmdId, // 8 thread->app
  109. kTimeLineMsgCmdId, // 9 thread->app
  110. kScoreMsgCmdId, //10 thread->app
  111. kGenOnsetMarksCmdId,
  112. kDelOnsetMarksCmdId
  113. } cmdId_t;
  114. // Messages are passed between the app and the worker thread
  115. // and v.v. using this record format. Note that the u.msg
  116. // field is always dynamically allocated by _enqueue()
  117. // and must be released following the dequeue operation.
  118. typedef struct cmd_str
  119. {
  120. cmdId_t id; // cmdId
  121. unsigned byteCnt; // length of msg
  122. unsigned value; // msg value
  123. union
  124. {
  125. void* msg; // msg[byteCnt]
  126. char* string; // string[byteCnt]
  127. } u;
  128. } cmd_t;
  129. cmCtx_t* _ctx; //
  130. cmErr_t _err; //
  131. cmThreadH_t _thH; // worker thread
  132. cmTs1p1cH_t _cmdQueH; // app->model
  133. cmTs1p1cH_t _outQueH; // model->appl
  134. cmTlH_t _tlH; // time line handle
  135. cmAfmH_t _afmH; // audio file manager
  136. cmScH_t _scH; // score handle
  137. cmChar_t* _afPath; //
  138. cmdIfRspdr* _rspdr; //
  139. unsigned _curSeqId; // seqId of last selected sequence
  140. // Functions called from the app thread.
  141. rc_t _sendCmd( cmdId_t id, unsigned value=0, const char* str=NULL );
  142. void _releaseQue( cmTs1p1cH_t* queHPtr );
  143. // Functions called from the worker thread
  144. static bool _thFunc( void* arg );
  145. void _thDoOpen( const cmd_t* cmd );
  146. void _thDoClose( const cmd_t* cmd );
  147. void _thDoSelectSeq( const cmd_t* cmd );
  148. void _thDoAfLoad( const cmd_t* cmd );
  149. void _thDoScore( const cmd_t* cmd );
  150. void _thDoGenOnsetMarks( const cmd_t* cmd );
  151. void _thDoDelOnsetMarks( const cmd_t* cmd );
  152. //void _thErrorMsg( const char* fmt, va_list vl );
  153. void _thErrorMsg( const char* fmt, ... );
  154. //void _thStatusMsg( const char* fmt, va_list vl );
  155. void _thStatusMsg( const char* fmt, ... );
  156. void _thSendResponse( cmdId_t id, const char* str = NULL, unsigned value=0 );
  157. static void _thSendTimeLineMsg( void* arg, const void* msg, unsigned byteCnt );
  158. static void _thSendScoreMsg( void* arg, const void* msg, unsigned byteCnt );
  159. // Thread independent functions
  160. rc_t _enqueue( cmTs1p1cH_t qH, cmdId_t id, unsigned value, const void* msg, unsigned msgByteCnt );
  161. // Print context information for selected time line objects
  162. void _onTimeLineObjSelected( unsigned tlObjId );
  163. };
  164. #endif