|
@@ -0,0 +1,276 @@
|
|
1
|
+#include "cmPrefix.h"
|
|
2
|
+#include "cmGlobal.h"
|
|
3
|
+#include "cmFloatTypes.h"
|
|
4
|
+#include "cmRpt.h"
|
|
5
|
+#include "cmErr.h"
|
|
6
|
+#include "cmCtx.h"
|
|
7
|
+#include "cmMem.h"
|
|
8
|
+#include "cmMallocDebug.h"
|
|
9
|
+#include "cmLinkedHeap.h"
|
|
10
|
+#include "cmXml.h"
|
|
11
|
+#include "cmText.h"
|
|
12
|
+#include "cmXScore.h"
|
|
13
|
+
|
|
14
|
+cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
|
|
15
|
+
|
|
16
|
+typedef struct cmXsNote_str
|
|
17
|
+{
|
|
18
|
+} cmXsNote_t;
|
|
19
|
+
|
|
20
|
+typedef struct cmXsVoice_str
|
|
21
|
+{
|
|
22
|
+ unsigned id; // Voice id
|
|
23
|
+ cmXsNote_t* noteL; // List of notes in this voice
|
|
24
|
+ struct cmXsVoice_str* link; // Link to other voices in this measure
|
|
25
|
+} cmXsVoice_t;
|
|
26
|
+
|
|
27
|
+typedef struct cmXsMeas_str
|
|
28
|
+{
|
|
29
|
+ unsigned number; // Measure number
|
|
30
|
+
|
|
31
|
+ unsigned divisions;
|
|
32
|
+ unsigned beats;
|
|
33
|
+ unsigned beat_type;
|
|
34
|
+
|
|
35
|
+ cmXsVoice_t* voiceL; // List of voices in this measure
|
|
36
|
+
|
|
37
|
+ struct cmXsMeas_str* link; // Link to other measures in this part.
|
|
38
|
+} cmXsMeas_t;
|
|
39
|
+
|
|
40
|
+typedef struct cmXsPart_str
|
|
41
|
+{
|
|
42
|
+ const cmChar_t* idStr; // Id of this part
|
|
43
|
+ cmXsMeas_t* measL; // List of measures in this part.
|
|
44
|
+ struct cmXsPart_str* link; // Link to other parts in this score
|
|
45
|
+} cmXsPart_t;
|
|
46
|
+
|
|
47
|
+typedef struct
|
|
48
|
+{
|
|
49
|
+ cmErr_t err;
|
|
50
|
+ cmXmlH_t xmlH;
|
|
51
|
+ cmLHeapH_t lhH;
|
|
52
|
+ cmXsPart_t* partL;
|
|
53
|
+} cmXScore_t;
|
|
54
|
+
|
|
55
|
+cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
|
|
56
|
+{
|
|
57
|
+ cmXScore_t* p = (cmXScore_t*)h.h;
|
|
58
|
+ assert( p != NULL );
|
|
59
|
+ return p;
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
|
|
63
|
+{
|
|
64
|
+ cmXsRC_t rc = kOkXsRC;
|
|
65
|
+
|
|
66
|
+ // release the XML file
|
|
67
|
+ if( cmXmlIsValid(p->xmlH) )
|
|
68
|
+ cmXmlFree( &p->xmlH );
|
|
69
|
+
|
|
70
|
+ // release the local linked heap memory
|
|
71
|
+ if( cmLHeapIsValid(p->lhH) )
|
|
72
|
+ cmLHeapDestroy(&p->lhH);
|
|
73
|
+
|
|
74
|
+ cmMemFree(p);
|
|
75
|
+
|
|
76
|
+ return rc;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmChar_t* label, const cmXmlAttr_t* attr )
|
|
80
|
+{
|
|
81
|
+ if( attr == NULL )
|
|
82
|
+ return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'.",label);
|
|
83
|
+
|
|
84
|
+ return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s' - Attribute:%s=%s",label,attr->label,attr->value);
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
|
|
88
|
+{
|
|
89
|
+ return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
|
|
93
|
+{
|
|
94
|
+ cmXsRC_t rc = kOkXsRC;
|
|
95
|
+ cmXsPart_t* lastPartPtr = NULL;
|
|
96
|
+ const cmXmlNode_t* xnp;
|
|
97
|
+
|
|
98
|
+ // find the 'part-list'
|
|
99
|
+ if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
|
|
100
|
+ return _cmXScoreMissingNode(p,"part-list",NULL);
|
|
101
|
+
|
|
102
|
+ const cmXmlNode_t* cnp = xnp->children;
|
|
103
|
+
|
|
104
|
+ // for each child of the 'part-list'
|
|
105
|
+ for(; cnp!=NULL; cnp=cnp->sibling)
|
|
106
|
+ if( cmTextCmp( cnp->label, "score-part" ) == 0 )
|
|
107
|
+ {
|
|
108
|
+ const cmXmlAttr_t* a;
|
|
109
|
+
|
|
110
|
+ // find the 'score-part' id
|
|
111
|
+ if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
|
|
112
|
+ return _cmXScoreMissingAttribute(p,cnp,"id");
|
|
113
|
+
|
|
114
|
+ // allocate a new part record
|
|
115
|
+ cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
|
|
116
|
+
|
|
117
|
+ pp->idStr = a->value; // set the part id
|
|
118
|
+
|
|
119
|
+ // link the new part record to the end of the part list
|
|
120
|
+ if(lastPartPtr == NULL)
|
|
121
|
+ p->partL = pp;
|
|
122
|
+ else
|
|
123
|
+ lastPartPtr->link = pp;
|
|
124
|
+
|
|
125
|
+ lastPartPtr = pp;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ return rc;
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp )
|
|
133
|
+{
|
|
134
|
+ cmXsRC_t rc = kOkXsRC;
|
|
135
|
+
|
|
136
|
+ cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
|
|
137
|
+ const cmXmlNode_t* np;
|
|
138
|
+
|
|
139
|
+ // get measure number
|
|
140
|
+ if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
|
|
141
|
+ return _cmXScoreMissingAttribute(p,mnp,"number");
|
|
142
|
+
|
|
143
|
+ if( pp->measL == NULL )
|
|
144
|
+ pp->measL = meas;
|
|
145
|
+ else
|
|
146
|
+ {
|
|
147
|
+ cmXsMeas_t* m = pp->measL;
|
|
148
|
+ while( m->link != NULL )
|
|
149
|
+ m = m->link;
|
|
150
|
+ m->link = meas;
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ // get measure attributes node
|
|
154
|
+ if((np = cmXmlSearch(mnp,"attributes",NULL,0)) == NULL)
|
|
155
|
+ return rc; // (this measure does not have any attributes)
|
|
156
|
+
|
|
157
|
+ cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
|
|
158
|
+ cmXmlNodeUInt(np,&meas->beats,"time","beats",NULL);
|
|
159
|
+ cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
|
|
160
|
+
|
|
161
|
+ return rc;
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
|
|
165
|
+{
|
|
166
|
+ cmXsRC_t rc = kOkXsRC;
|
|
167
|
+ const cmXmlNode_t* xnp;
|
|
168
|
+ cmXmlAttr_t partAttr = { "id", pp->idStr };
|
|
169
|
+
|
|
170
|
+ // find the 'part'
|
|
171
|
+ if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
|
|
172
|
+ return _cmXScoreMissingNode(p,"part",&partAttr);
|
|
173
|
+
|
|
174
|
+ // for each child of this part - find each measure
|
|
175
|
+ const cmXmlNode_t* cnp = xnp->children;
|
|
176
|
+ for(; cnp!=NULL; cnp=cnp->sibling)
|
|
177
|
+ if( cmTextCmp(cnp->label,"measure") == 0 )
|
|
178
|
+ if((rc = _cmXScoreParseMeasure(p,pp,cnp)) != kOkXsRC )
|
|
179
|
+ return rc;
|
|
180
|
+
|
|
181
|
+ return rc;
|
|
182
|
+}
|
|
183
|
+
|
|
184
|
+cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn )
|
|
185
|
+{
|
|
186
|
+ cmXsRC_t rc = kOkXsRC;
|
|
187
|
+
|
|
188
|
+ if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
|
|
189
|
+ return rc;
|
|
190
|
+
|
|
191
|
+ cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
|
|
192
|
+
|
|
193
|
+ cmErrSetup(&p->err,&ctx->rpt,"XScore");
|
|
194
|
+
|
|
195
|
+ // create a local linked heap
|
|
196
|
+ if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
|
|
197
|
+ return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
|
|
198
|
+
|
|
199
|
+ // open the music xml file
|
|
200
|
+ if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
|
|
201
|
+ {
|
|
202
|
+ rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
|
|
203
|
+ goto errLabel;
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ // parse the part-list
|
|
207
|
+ if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
|
|
208
|
+ goto errLabel;
|
|
209
|
+
|
|
210
|
+ cmXsPart_t* pp = p->partL;
|
|
211
|
+ for(; pp!=NULL; pp=pp->link)
|
|
212
|
+ if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
|
|
213
|
+ goto errLabel;
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+ errLabel:
|
|
217
|
+ if( rc != kOkXsRC )
|
|
218
|
+ _cmXScoreFinalize(p);
|
|
219
|
+ else
|
|
220
|
+ hp->h = p;
|
|
221
|
+
|
|
222
|
+ return rc;
|
|
223
|
+}
|
|
224
|
+
|
|
225
|
+cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
|
|
226
|
+{
|
|
227
|
+ cmXsRC_t rc = kOkXsRC;
|
|
228
|
+
|
|
229
|
+ if( hp == NULL || cmXScoreIsValid(*hp)==false )
|
|
230
|
+ return kOkXsRC;
|
|
231
|
+
|
|
232
|
+ cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
|
|
233
|
+
|
|
234
|
+ if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
|
|
235
|
+ return rc;
|
|
236
|
+
|
|
237
|
+ hp->h = NULL;
|
|
238
|
+
|
|
239
|
+ return rc;
|
|
240
|
+
|
|
241
|
+}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+bool cmXScoreIsValid( cmXsH_t h )
|
|
245
|
+{ return h.h != NULL; }
|
|
246
|
+
|
|
247
|
+void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt )
|
|
248
|
+{
|
|
249
|
+ cmXScore_t* p = _cmXScoreHandleToPtr(h);
|
|
250
|
+
|
|
251
|
+ cmXsPart_t* pp = p->partL;
|
|
252
|
+ for(; pp!=NULL; pp=pp->link)
|
|
253
|
+ {
|
|
254
|
+ cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
|
|
255
|
+
|
|
256
|
+ const cmXsMeas_t* meas = pp->measL;
|
|
257
|
+ for(; meas!=NULL; meas=meas->link)
|
|
258
|
+ cmRptPrintf(rpt," %i : div:%i beat:%i beat-type:%i\n",meas->number,meas->divisions,meas->beats,meas->beat_type);
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
|
|
265
|
+{
|
|
266
|
+ cmXsRC_t rc;
|
|
267
|
+ cmXsH_t h = cmXsNullHandle;
|
|
268
|
+
|
|
269
|
+ if((rc = cmXScoreInitialize( ctx, &h, fn)) != kOkXsRC )
|
|
270
|
+ return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
|
|
271
|
+
|
|
272
|
+ cmXScoreReport(h,&ctx->rpt);
|
|
273
|
+
|
|
274
|
+ return cmXScoreFinalize(&h);
|
|
275
|
+
|
|
276
|
+}
|