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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #ifndef cmScore_h
  2. #define cmScore_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //( { file_desc:"Object for managing musical score data." kw:[score]}
  7. enum
  8. {
  9. kOkScRC = cmOkRC,
  10. kCsvFailScRC,
  11. kSyntaxErrScRC,
  12. kInvalidIdxScRC,
  13. kTimeLineFailScRC,
  14. kInvalidDynRefCntScRC,
  15. kMidiFileFailScRC,
  16. kPedalInvalidScRC,
  17. kFileFailScRC
  18. };
  19. enum
  20. {
  21. kInvalidEvtScId = 0,
  22. kTimeSigEvtScId,
  23. kKeySigEvtScId,
  24. kTempoEvtScId,
  25. kTrackEvtScId,
  26. kTextEvtScId,
  27. kNameEvtScId,
  28. kEOTrackEvtScId,
  29. kCopyEvtScId,
  30. kBlankEvtScId,
  31. kBarEvtScId,
  32. kPgmEvtScId,
  33. kCtlEvtScId,
  34. kNonEvtScId,
  35. kPedalEvtScId
  36. };
  37. // Flags used by cmScoreEvt_t.flags
  38. enum
  39. {
  40. kEvenScFl = 0x001, // This note is marked for evenness measurement
  41. kDynScFl = 0x002, // This note is marked for dynamics measurement
  42. kTempoScFl = 0x004, // This note is marked for tempo measurement
  43. kSkipScFl = 0x008, // This isn't a real event (e.g. tied note) skip over it
  44. kGraceScFl = 0x010, // This is a grace note
  45. kInvalidScFl = 0x020, // This note has a calculated time
  46. kPedalDnScFl = 0x040, // This is a pedal down event (pitch holds the pedal id and durSecs holds the time the pedal will remain down.)
  47. kPedalUpScFl = 0x080 // This is a pedal up event (pitch holds the pedal id)
  48. };
  49. // Id's used by cmScoreSet_t.varId and as indexes into
  50. // cmScoreSection_t.vars[].
  51. enum
  52. {
  53. kInvalidVarScId, // 0
  54. kEvenVarScId, // 1
  55. kDynVarScId, // 2
  56. kTempoVarScId, // 3
  57. kScVarCnt
  58. };
  59. struct cmScoreLoc_str;
  60. struct cmScoreSet_str;
  61. // The score can be divided into arbitrary non-overlapping sections.
  62. typedef struct
  63. {
  64. const cmChar_t* label; // section label
  65. unsigned index; // index of this record in the internal section array
  66. struct cmScoreLoc_str* locPtr; // location where this section starts
  67. unsigned begEvtIndex; // score element index where this section starts
  68. unsigned setCnt; // Count of elements in setArray[]
  69. struct cmScoreSet_str** setArray; // Ptrs to sets which are applied to this section.
  70. double vars[ kScVarCnt ]; // Set to DBL_MAX by default.
  71. } cmScoreSection_t;
  72. typedef struct
  73. {
  74. unsigned type; // Event type
  75. double secs; // Time location in seconds
  76. double durSecs; // Duration in seconds
  77. unsigned index; // Index of this event in the event array.
  78. unsigned locIdx; // Index of the location containing this event
  79. cmMidiByte_t pitch; // MIDI pitch of this note or the MIDI pedal id of pedal down/up msg (64=sustain 65=sostenuto 66=soft)
  80. cmMidiByte_t vel; // MIDI velocity of this note
  81. unsigned flags; // Attribute flags for this event
  82. unsigned dynVal; // Dynamcis value pppp to ffff (1 to 11) for this note.
  83. double frac; // Note's time value for tempo and non-grace evenness notes.
  84. unsigned barNumb; // Bar id of the measure containing this event.
  85. unsigned barNoteIdx; // Index of this note in this bar
  86. unsigned csvRowNumb; // File row number (not index) from which this record originated
  87. unsigned perfSmpIdx; // Time this event was performed or cmInvalidIdx if the event was not performed.
  88. unsigned perfVel; // Velocity of the performed note or 0 if the note was not performed.
  89. unsigned perfDynLvl; // Index into dynamic level ref. array assoc'd with perfVel
  90. unsigned line; // Line number of this event in the score file.
  91. unsigned csvEventId; // EventId from CSV 'evt' column.
  92. } cmScoreEvt_t;
  93. // A 'set' is a collection of events that are grouped in time and all marked with a given attribute.
  94. // (e.g. eveness, tempo, dynamcs ... )
  95. typedef struct cmScoreSet_str
  96. {
  97. unsigned varId; // See kXXXVarScId flags above
  98. cmScoreEvt_t** eleArray; // Events that make up this set in time order
  99. unsigned eleCnt; //
  100. cmScoreSection_t** sectArray; // Sections this set will be applied to
  101. unsigned sectCnt; //
  102. unsigned* symArray; // symArray[sectCnt] - symbol name of all variables represented by this set (e.g '1a-e', '1b-e', '2-t', etc)
  103. unsigned* costSymArray; // costSymArray[sectCnt] - same as symbols in symArray[] with 'c' prepended to front
  104. bool doneFl;
  105. double value;
  106. struct cmScoreSet_str* llink; // cmScoreLoc_t setList link
  107. } cmScoreSet_t;
  108. typedef enum
  109. {
  110. kInvalidScMId,
  111. kRecdBegScMId,
  112. kRecdEndScMId,
  113. kFadeScMId,
  114. kPlayBegScMId,
  115. kPlayEndScMId
  116. } cmMarkScMId_t;
  117. // score markers
  118. typedef struct cmScoreMarker_str
  119. {
  120. cmMarkScMId_t markTypeId; // marker type
  121. unsigned labelSymId; // marker label
  122. struct cmScoreLoc_str* scoreLocPtr; // score location of the marker
  123. unsigned csvRowIdx; // score CSV file line assoc'd w/ this marker
  124. struct cmScoreMarker_str* link; // cmScoreLoc_t.markList links
  125. } cmScoreMarker_t;
  126. // All events which are simultaneous are collected into a single
  127. // cmScoreLoc_t record.
  128. typedef struct cmScoreLoc_str
  129. {
  130. unsigned index; // index of this location record
  131. double secs; // Time of this location
  132. unsigned evtCnt; // Count of events in evtArray[].
  133. cmScoreEvt_t** evtArray; // Events which occur at this time.
  134. unsigned barNumb; // Bar number this event is contained by.
  135. cmScoreSet_t* setList; // Set's which end on this time location (linked through cmScoreSet_t.llink)
  136. cmScoreSection_t* begSectPtr; // NULL if this location does not start a section
  137. cmScoreMarker_t* markList; // List of markers assigned to this location
  138. } cmScoreLoc_t;
  139. typedef void (*cmScCb_t)( void* arg, const void* data, unsigned byteCnt );
  140. typedef cmRC_t cmScRC_t;
  141. typedef cmHandle_t cmScH_t;
  142. extern cmScH_t cmScNullHandle;
  143. const cmChar_t* cmScEvtTypeIdToLabel( unsigned id );
  144. const cmChar_t* cmScDynIdToLabel( unsigned id );
  145. const cmChar_t* cmScStatusToOpString( unsigned id );
  146. // Initialize a score object from a CSV File generated from a score spreadsheet.
  147. // The dynRefArray[dynRefCnt] and cbFunc(cbArg) are optional if these
  148. // features are not used.
  149. // If provided the dynRefArray[] is copied into an internal array.
  150. // The physical array passed here therefore does not need to remain valid.
  151. // Set 'srate' to zero if the score will not be used to perform measurement calculations.
  152. // The symbol table is only necessary if valid symbols are to be assigned to the cmScoreSet_t.symArray[].
  153. cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn, double srate, const unsigned* dynRefArray, unsigned dynRefCnt, cmScCb_t cbFunc, void* cbArg, cmSymTblH_t stH );
  154. cmScRC_t cmScoreFinalize( cmScH_t* hp );
  155. // Filename of last successfuly loaded score file.
  156. const cmChar_t* cmScoreFileName( cmScH_t h );
  157. // Sample rate as set in cmScoreInitialize()
  158. double cmScoreSampleRate( cmScH_t h );
  159. // Validate the score handle
  160. bool cmScoreIsValid( cmScH_t h );
  161. // Access the score data.
  162. unsigned cmScoreEvtCount( cmScH_t h );
  163. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx );
  164. // Given a bar number return the associated 'bar' event record.
  165. const cmScoreEvt_t* cmScoreBarEvt( cmScH_t h, unsigned barNumb );
  166. // Given a csvEventId return the associated event
  167. const cmScoreEvt_t* cmScoreIdToEvt( cmScH_t h, unsigned csvEventId );
  168. // Access section records
  169. unsigned cmScoreSectionCount( cmScH_t h );
  170. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx );
  171. // Access the score location data
  172. unsigned cmScoreLocCount( cmScH_t h );
  173. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx );
  174. void cmScorePrintLoc( cmScH_t h );
  175. // Return the location associated with a given score event.
  176. cmScoreLoc_t* cmScoreEvtLoc( cmScH_t h, const cmScoreEvt_t* evt );
  177. // Return the count of sets.
  178. unsigned cmScoreSetCount( cmScH_t h );
  179. unsigned cmScoreMarkerLabelCount( cmScH_t h );
  180. unsigned cmScoreMarkerLabelSymbolId( cmScH_t h, unsigned idx );
  181. const cmScoreMarker_t* cmScoreMarker( cmScH_t h, cmMarkScMId_t markMId, unsigned labelSymId );
  182. // Make callbacks for all events in the score. The callbacks
  183. // contain cmScMsg_t records serialized as a byte stream.
  184. // Use cmScoreDecode() to convert the byte string to a
  185. // cmScMsg_t record.
  186. cmScRC_t cmScoreSeqNotify( cmScH_t h );
  187. cmScRC_t cmScoreSeqNotifyCb( cmScH_t h, cmScCb_t cbFunc, void* cbArg );
  188. void cmScoreClearPerfInfo( cmScH_t h );
  189. // Assign 'smpIdx' and 'vel' to the event matching 'pitch' at 'locIdx'
  190. // but do not trigger any variable calculations. Return true if as a
  191. // result of this call all events assigned to 'locIdx' have been received
  192. // otherwise return false.
  193. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel );
  194. // Assign 'smpIdx' and 'vel' to the event matching 'pitch' at 'locIdx'
  195. // but and trigger any variable calculations which may happen on, or before, 'locIdx'.
  196. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel );
  197. // Assign 'value' to the section at, or before, 'locIdx'.
  198. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value );
  199. // Set the performed dynamic level of a score event.
  200. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl );
  201. typedef enum
  202. {
  203. kInvalidMsgScId,
  204. kBeginMsgScId,
  205. kEventMsgScId,
  206. kSectionMsgScId,
  207. kEndMsgScId,
  208. kVarMsgScId,
  209. kDynMsgScId
  210. } cmScMsgTypeId_t;
  211. typedef struct
  212. {
  213. unsigned varId; // see kXXXVarScId from cmScoreSet_t.varId
  214. double value; // value of a variable
  215. } cmScMeas_t;
  216. typedef struct
  217. {
  218. unsigned evtIdx;
  219. unsigned dynLvl;
  220. } cmScDyn_t;
  221. typedef struct
  222. {
  223. cmScMsgTypeId_t typeId;
  224. union
  225. {
  226. cmScoreEvt_t evt; // only used when typeId == kEventMsgScId
  227. cmScMeas_t meas; // only used when typeId == kVarMsgScId
  228. cmScoreSection_t sect; // only used when typeId == kSectionMsgScId
  229. cmScDyn_t dyn; // only used when typeId == kDynLvlMsgScId
  230. } u;
  231. } cmScMsg_t;
  232. // Decode a serialized cmScMsg_t from a byte stream as passed to the
  233. // cmScCb_t function.
  234. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* );
  235. void cmScorePrint( cmScH_t h, cmRpt_t* rpt );
  236. void cmScorePrintSets( cmScH_t h, cmRpt_t* rpt );
  237. // Generate a new score file from a MIDI file.
  238. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn );
  239. // Print open the score file 'fn' and report the contents. This function
  240. // simply wraps calls to cmScoreInitialize() and cmScorePrint().
  241. void cmScoreReport( cmCtx_t* ctx, const cmChar_t* fn, const cmChar_t* outFn );
  242. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn );
  243. //)
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif