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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Return the object following 'p' assigned to 'seqId'.
  94. // If 'p' is NULL then return the first object assigned to seqId.
  95. // If 'seqId' is set to cmInvalidId then return the next object on any seq.
  96. // If no objects follow 'p' on the specified sequence then return NULL.
  97. cmTlObj_t* cmTimeLineNextObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId );
  98. // Same as cmTimeLineNextObj() but returns next object whose type matches 'typeId'.
  99. cmTlObj_t* cmTimeLineNextTypeObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId, unsigned typeId );
  100. cmTlMidiFile_t* cmTlNextMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  101. cmTlAudioFile_t* cmTlNextAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  102. cmTlMidiEvt_t* cmTlNextMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  103. cmTlAudioEvt_t* cmTlNextAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  104. cmTlMarker_t* cmTlNextMarkerObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId );
  105. // Cast a genereic cmTlObj_t pointer to a specificy type.
  106. cmTlMidiFile_t* cmTimeLineMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  107. cmTlAudioFile_t* cmTimeLineAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  108. cmTlMidiEvt_t* cmTimeLineMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  109. cmTlAudioEvt_t* cmTimeLineAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  110. cmTlMarker_t* cmTimeLineMarkerObjPtr( cmTlH_t h, cmTlObj_t* op );
  111. // Same as cmTimeLineXXXObjPtr() but does not generate an error when
  112. // 'op' does not point to the correct type. These function quietly
  113. // return NULL if the requested type does not match.
  114. cmTlMidiFile_t* cmTlMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  115. cmTlAudioFile_t* cmTlAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op );
  116. cmTlMidiEvt_t* cmTlMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  117. cmTlAudioEvt_t* cmTlAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op );
  118. cmTlMarker_t* cmTlMarkerObjPtr( cmTlH_t h, cmTlObj_t* op );
  119. cmTlAudioFile_t* cmTimeLineFindAudioFile( cmTlH_t h, const cmChar_t* fn );
  120. cmTlMidiFile_t* cmTimeLineFindMidiFile( cmTlH_t h, const cmChar_t* fn );
  121. // 'typeId' = kAudioFileTlId, kMidiFileTId, kMarkerTlId.
  122. // 'nameStr' and 'refObjNameStr' may be NULL.
  123. cmTlRC_t cmTimeLineInsert(
  124. cmTlH_t h,
  125. const cmChar_t* nameStr,
  126. unsigned typeId,
  127. const cmChar_t* fn_or_markerStr,
  128. int begSmpIdx,
  129. unsigned durSmpCnt,
  130. const cmChar_t* refObjNameStr,
  131. unsigned seqId );
  132. // See src/data/tl0.json for an example JSON file.
  133. cmTlRC_t cmTimeLineReadJson( cmTlH_t h, const cmChar_t* ifn );
  134. // Return a count of sequences contained within this timeline.
  135. unsigned cmTimeLineSeqCount( cmTlH_t h );
  136. // Make notifications for all records belonging to the sequence.
  137. cmTlRC_t cmTimeLineSeqNotify( cmTlH_t h, unsigned seqId );
  138. cmTlRC_t cmTimeLineWrite( cmTlH_t h, const cmChar_t* fn );
  139. cmTlRC_t cmTimeLinePrint( cmTlH_t h, cmRpt_t* rpt );
  140. cmTlRC_t cmTimeLinePrintFn( cmCtx_t* ctx, const cmChar_t* fn, cmRpt_t* rpt );
  141. cmTlRC_t cmTimeLineTest( cmCtx_t* ctx, const cmChar_t* jsFn );
  142. // The time-line notifies listeners of initialization and finalization
  143. // events via calling a cmTlCbFunc_t function. The argument to this
  144. // function is a serialized cmTlUiMsg_t. The recipient of the callback
  145. // can extract information from this message using cmTimeLineDecode()
  146. // to form a cmTlUiMsg_t record. Note that all pointers internal to the
  147. // cmTlUiMsg_t point into the message buffer itself.
  148. // id's used to indicate the type of a serialized object
  149. typedef enum
  150. {
  151. kInvalidMsgTlId,
  152. kInitMsgTlId, // A new time-line object is begin intialized.
  153. kFinalMsgTlId, // A time-line object is being finalized.
  154. kDoneMsgTlId, // All the objects assoc'd with a time line seq-notify have been sent.
  155. kInsertMsgTlId, // A time-line object was inserted.
  156. } cmTlUiMsgTypeId_t;
  157. typedef struct
  158. {
  159. cmTlUiMsgTypeId_t msgId; // See cmTlUiMsgTypeId_t.
  160. unsigned objId; // Set to cmTlObj_t.uid
  161. unsigned parentObjId; // cmTlObj_t.uid of the object this object's begSmpIdx is set relative to.
  162. unsigned seqId; //
  163. cmTlObjTypeId_t typeId; //
  164. int begSmpIdx; // Time relative to parent.
  165. unsigned durSmpCnt; // Duration of the object.
  166. const char* label; // Object label (points to memory inside the serialized msg.)
  167. double srate; // Only valid with kInitMsgTlId.
  168. unsigned seqCnt; // Only valid with kInitMsgTlId.
  169. const cmMidiTrackMsg_t* midiTrkMsg; // Only valid for typeId == kMidiEvtTlId. Internal pointers refer to memory inside the serialzed msg. buffer.
  170. unsigned midiFileObjId; // Only valid for typeId == kMidiEvtTlId
  171. const char* textStr; // filename for kAudioFileTlId and kMidiFileTlId, marker text for kMarkerTlId
  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