libcm is a C development framework with an emphasis on audio signal processing applications.
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.

cmTimeLine.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef cmTimeLine_h
  2. #define cmTimeLine_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef cmHandle_t cmTlH_t;
  7. typedef cmRC_t cmTlRC_t;
  8. enum
  9. {
  10. kOkTlRC = cmOkRC,
  11. kLHeapFailTlRC,
  12. kParseFailTlRC,
  13. kJsonFailTlRC,
  14. kDuplNameTlRC,
  15. kRefNotFoundTlRC,
  16. kAudioFileFailTlRC,
  17. kMidiFileFailTlRC,
  18. kTypeCvtFailTlRC,
  19. kUnknownRecdTypeTlRC,
  20. kFinalizeFailTlRC,
  21. kInvalidSeqIdTlRC
  22. };
  23. typedef enum
  24. {
  25. kMidiFileTlId = 0x01,
  26. kMidiEvtTlId = 0x02,
  27. kAudioFileTlId = 0x03,
  28. kAudioEvtTlId = 0x04,
  29. kMarkerTlId = 0x05
  30. } cmTlObjTypeId_t;
  31. enum
  32. {
  33. kReservedTlFl = 0x01,
  34. kNoWriteTlFl = 0x02 // do not write this object in cmTimeLineWrite()
  35. };
  36. typedef void (*cmTlCb_t)( void* arg, const void* data, unsigned byteCnt );
  37. typedef struct cmTlObj_str
  38. {
  39. void* reserved; // pt's to _cmTlObj_t
  40. unsigned seqId; // sequence this object is assigned to
  41. const cmChar_t* name; // text name of this object
  42. unsigned uid; // generated unique id for this object
  43. cmTlObjTypeId_t typeId; // type of the object
  44. struct cmTlObj_str* ref; // time reference object
  45. int begSmpIdx; // start time of this object as an offset from the start time of the reference object
  46. unsigned durSmpCnt; // duration of this object
  47. int seqSmpIdx; // absolute start time of this object within the sequence
  48. const cmChar_t* text; // points to text assoc'd with this node (file name for audio/midi file, marker text)
  49. unsigned flags; // see kXXXTlFl
  50. void* userDataPtr; // user customizable data pointer
  51. } cmTlObj_t;
  52. typedef struct
  53. {
  54. cmTlObj_t obj;
  55. cmMidiFileH_t h;
  56. unsigned noteOnCnt;
  57. cmChar_t* fn;
  58. } cmTlMidiFile_t;
  59. typedef struct
  60. {
  61. cmTlObj_t obj;
  62. unsigned midiFileObjId;
  63. const cmMidiTrackMsg_t* msg; // w/ dticks converted to microsecs
  64. } cmTlMidiEvt_t;
  65. typedef struct
  66. {
  67. cmTlObj_t obj;
  68. cmAudioFileH_t h;
  69. cmAudioFileInfo_t info;
  70. cmChar_t* fn;
  71. } cmTlAudioFile_t;
  72. typedef struct
  73. {
  74. cmTlObj_t obj;
  75. cmAudioFileH_t h;
  76. unsigned smpIdx;
  77. unsigned smpCnt;
  78. cmChar_t* text;
  79. } cmTlAudioEvt_t;
  80. typedef struct
  81. {
  82. cmTlObj_t obj;
  83. const cmChar_t* text;
  84. } cmTlMarker_t;
  85. extern cmTlH_t cmTimeLineNullHandle;
  86. //
  87. cmTlRC_t cmTimeLineInitialize( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg );
  88. cmTlRC_t cmTimeLineInitializeFromFile( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg, const cmChar_t* fn );
  89. const cmChar_t* cmTimeLineFileName( cmTlH_t h );
  90. cmTlRC_t cmTimeLineFinalize( cmTlH_t* hp );
  91. bool cmTimeLineIsValid( cmTlH_t h );
  92. double cmTimeLineSampleRate( cmTlH_t h );
  93. // Given cmTlObj_t.uid return a pointer to the associated record.
  94. // seqId is optional (dflt:cmInvalidId)
  95. cmTlObj_t* cmTimeLineIdToObj( cmTlH_t h, unsigned seqId, unsigned uid );
  96. // Return the object following 'p' assigned to 'seqId'.
  97. // If 'p' is NULL then return the first object assigned to seqId.
  98. // If 'seqId' is set to cmInvalidId then return the next object on any seq.
  99. // If no objects follow 'p' on the specified sequence then return NULL.
  100. cmTlObj_t* cmTimeLineNextObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId );
  101. // Same as cmTimeLineNextObj() but returns next object whose type matches 'typeId'.
  102. cmTlObj_t* cmTimeLineNextTypeObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId, unsigned typeId );
  103. cmTlMidiFile_t* cmTlNextMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  104. cmTlAudioFile_t* cmTlNextAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  105. cmTlMidiEvt_t* cmTlNextMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  106. cmTlAudioEvt_t* cmTlNextAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  107. cmTlMarker_t* cmTlNextMarkerObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  108. cmTlObj_t* cmTlIdToObjPtr( cmTlH_t h, unsigned uid );
  109. // Cast a genereic cmTlObj_t pointer to a specificy type.
  110. cmTlMidiFile_t* cmTimeLineMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  111. cmTlAudioFile_t* cmTimeLineAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  112. cmTlMidiEvt_t* cmTimeLineMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  113. cmTlAudioEvt_t* cmTimeLineAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  114. cmTlMarker_t* cmTimeLineMarkerObjPtr( cmTlH_t h, cmTlObj_t* op );
  115. // Same as cmTimeLineXXXObjPtr() but does not generate an error when
  116. // 'op' does not point to the correct type. These function quietly
  117. // return NULL if the requested type does not match.
  118. cmTlMidiFile_t* cmTlMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  119. cmTlAudioFile_t* cmTlAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  120. cmTlMidiEvt_t* cmTlMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  121. cmTlAudioEvt_t* cmTlAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  122. cmTlMarker_t* cmTlMarkerObjPtr( cmTlH_t h, cmTlObj_t* op );
  123. cmTlAudioFile_t* cmTimeLineFindAudioFile( cmTlH_t h, const cmChar_t* fn );
  124. cmTlMidiFile_t* cmTimeLineFindMidiFile( cmTlH_t h, const cmChar_t* fn );
  125. cmTlAudioFile_t* cmTimeLineAudioFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx );
  126. cmTlMidiFile_t* cmTimeLineMidiFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx );
  127. cmTlMidiEvt_t* cmTimeLineMidiEvtAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx );
  128. cmTlMarker_t* cmTimeLineMarkerAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx );
  129. // 'typeId' = kAudioFileTlId, kMidiFileTId, kMarkerTlId.
  130. // 'nameStr' and 'refObjNameStr' may be NULL.
  131. cmTlRC_t cmTimeLineInsert(
  132. cmTlH_t h,
  133. const cmChar_t* nameStr,
  134. unsigned typeId,
  135. const cmChar_t* fn_or_markerStr,
  136. int begSmpIdx,
  137. unsigned durSmpCnt,
  138. const cmChar_t* refObjNameStr,
  139. unsigned seqId );
  140. // See src/data/tl0.json for an example JSON file.
  141. cmTlRC_t cmTimeLineReadJson( cmTlH_t h, const cmChar_t* ifn );
  142. // Return a count of sequences contained within this timeline.
  143. unsigned cmTimeLineSeqCount( cmTlH_t h );
  144. // Make notifications for all records belonging to the sequence.
  145. cmTlRC_t cmTimeLineSeqNotify( cmTlH_t h, unsigned seqId );
  146. cmTlRC_t cmTimeLineWrite( cmTlH_t h, const cmChar_t* fn );
  147. cmTlRC_t cmTimeLinePrint( cmTlH_t h, cmRpt_t* rpt );
  148. cmTlRC_t cmTimeLinePrintFn( cmCtx_t* ctx, const cmChar_t* fn, cmRpt_t* rpt );
  149. cmTlRC_t cmTimeLineTest( cmCtx_t* ctx, const cmChar_t* jsFn );
  150. // The time-line notifies listeners of initialization and finalization
  151. // events via calling a cmTlCbFunc_t function. The argument to this
  152. // function is a serialized cmTlUiMsg_t. The recipient of the callback
  153. // can extract information from this message using cmTimeLineDecode()
  154. // to form a cmTlUiMsg_t record. Note that all pointers internal to the
  155. // cmTlUiMsg_t point into the message buffer itself.
  156. // id's used to indicate the type of a serialized object
  157. typedef enum
  158. {
  159. kInvalidMsgTlId,
  160. kInitMsgTlId, // A a set of time-line objects is about to be transmitted
  161. kFinalMsgTlId, // A time-line object is being finalized.
  162. kDoneMsgTlId, // All the objects assoc'd with a time line seq-notify have been sent.
  163. kInsertMsgTlId, // A time-line object was inserted.
  164. } cmTlUiMsgTypeId_t;
  165. typedef struct
  166. {
  167. cmTlUiMsgTypeId_t msgId; // See cmTlUiMsgTypeId_t.
  168. unsigned objId; // Set to cmTlObj_t.uid
  169. unsigned seqId; // Sequence id
  170. double srate; // Only valid with kInitMsgTlId.
  171. unsigned seqCnt; // Only valid with kInitMsgTlId.
  172. } cmTlUiMsg_t;
  173. // Decode a serialized cmTlObj_t as passed to the cmTlCb_t listener
  174. // callback function.
  175. cmTlRC_t cmTimeLineDecode( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* uiMsg );
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif