-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscorefile.cpp
More file actions
492 lines (441 loc) · 12.7 KB
/
scorefile.cpp
File metadata and controls
492 lines (441 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
Scoreview (R)
Copyright (C) 2015 Patrick Areny
All Rights Reserved.
Scoreview is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <assert.h>
#include <iterator>
#include <list>
#include <tinyxml.h>
#include "score.h"
#include "scorefile.h"
using namespace std;
CScoreFile::CScoreFile()
{
}
bool CScoreFile::add_measures_toxml_document(TiXmlElement *pdoc, std::list<CMesure*> *pmesure_list)
{
std::list<CMesure*>::iterator iter;
TiXmlElement *pxml_measure;
iter = pmesure_list->begin();
while (iter != pmesure_list->end())
{
pxml_measure = new TiXmlElement("measure");
pxml_measure->SetDoubleAttribute("start_timecode", (*iter)->m_start);
pxml_measure->SetDoubleAttribute("duration", (*iter)->duration());
pxml_measure->SetAttribute("times", (*iter)->m_times);
pdoc->LinkEndChild(pxml_measure);
iter++;
}
return true;
}
bool CScoreFile::add_notes_toxml_document(TiXmlElement *pinstrument_element, CInstrument *pinstr)
{
CNote *pnote;
std::list<t_notefreq>::iterator iter;
TiXmlElement *pnote_element;
TiXmlElement *pfreq_element;
pnote = pinstr->get_first_note(0.);
while (pnote != NULL)
{
pnote_element = new TiXmlElement("note");
pnote_element->SetDoubleAttribute("time", pnote->m_time);
pnote_element->SetDoubleAttribute("duration", pnote->m_duration);
pnote_element->SetAttribute("beam", pnote->m_lconnected);
iter = pnote->m_freq_list.begin();
while (iter != pnote->m_freq_list.end())
{
pfreq_element = new TiXmlElement("frequency");
pfreq_element->SetDoubleAttribute("value", (*iter).f);
pfreq_element->SetAttribute("string_sel", (*iter).string);
pnote_element->LinkEndChild(pfreq_element);
iter++;
}
pinstrument_element->LinkEndChild(pnote_element);
pnote = pinstr->get_next_note();
}
return true;
}
bool CScoreFile::add_instruments_toxml_document(TiXmlElement *pdoc, CScore *pscore)
{
CInstrument *pins;
TiXmlElement *pinstrument_element;
pins = pscore->get_first_instrument();
while (pins != NULL)
{
pinstrument_element = new TiXmlElement("instrument");
pinstrument_element->SetAttribute("name", pins->m_name);
//pinstrument_element->SetAttribute("voice", pins->identifier());
add_notes_toxml_document(pinstrument_element, pins);
pdoc->LinkEndChild(pinstrument_element);
pins = pscore->get_next_instrument();
}
return true;
}
bool CScoreFile::create_xml_file_from_score(CScore *pscore, string filename)
{
TiXmlDocument doc;
TiXmlDeclaration *pdecl;
TiXmlElement *pelement;
TiXmlElement *pscore_element;
TiXmlElement *pmeasures_element;
TiXmlElement *pnotations_element;
pdecl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(pdecl);
pelement = new TiXmlElement("scoreview_file");
pelement->SetAttribute("version", 1.0);
doc.LinkEndChild(pelement);
pscore_element = new TiXmlElement("score");
doc.LinkEndChild(pscore_element);
// Measures
pmeasures_element = new TiXmlElement("measures");
pscore_element->LinkEndChild(pmeasures_element);
add_measures_toxml_document(pmeasures_element, &pscore->m_mesure_list);
// Instruments
add_instruments_toxml_document(pscore_element, pscore);
// Notations, symbols like vibrato, ties...
pnotations_element = new TiXmlElement("notations");
pscore_element->LinkEndChild(pnotations_element);
return doc.SaveFile(filename);
}
//------------------------------------------------------------------------------------
//
// Reading from file
//
//------------------------------------------------------------------------------------
// ----------------------------------------------------------------------
// STDOUT dump and indenting utility functions
// From the tutorial http://www.grinninglizard.com/tinyxmldocs/tutorial0.html
// ----------------------------------------------------------------------
const unsigned int NUM_INDENTS_PER_SPACE=2;
const char* getIndent(unsigned int numIndents)
{
static const char *pINDENT = " + ";
static const unsigned int LENGTH = strlen( pINDENT );
unsigned int n = numIndents*NUM_INDENTS_PER_SPACE;
if (n > LENGTH)
n = LENGTH;
return &pINDENT[LENGTH - n];
}
// same as getIndent but no "+" at the end
const char* getIndentAlt( unsigned int numIndents )
{
static const char *pINDENT = " ";
static const unsigned int LENGTH = strlen( pINDENT );
unsigned int n = numIndents*NUM_INDENTS_PER_SPACE;
if (n > LENGTH)
n = LENGTH;
return &pINDENT[LENGTH - n];
}
int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
if (!pElement)
return 0;
TiXmlAttribute* pAttrib = pElement->FirstAttribute();
int i = 0;
int ival;
double dval;
const char* pIndent = getIndent(indent);
printf("\n");
while (pAttrib)
{
printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival) == TIXML_SUCCESS)
printf( " int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval) == TIXML_SUCCESS)
printf( " d=%1.1f", dval);
printf( "\n" );
i++;
pAttrib = pAttrib->Next();
}
return i;
}
void dump_to_stdout(TiXmlNode* pParent, unsigned int indent = 0)
{
if (!pParent)
return;
TiXmlNode* pChild;
TiXmlText* pText;
int t = pParent->Type();
printf("%s", getIndent(indent));
int num;
switch (t)
{
case TiXmlNode::TINYXML_DOCUMENT:
printf("Document");
break;
case TiXmlNode::TINYXML_ELEMENT:
printf("Element [%s]", pParent->Value());
num = dump_attribs_to_stdout(pParent->ToElement(), indent + 1);
switch (num)
{
case 0:
printf(" (No attributes)");
break;
case 1:
printf( "%s1 attribute", getIndentAlt(indent));
break;
default:
printf( "%s%d attributes", getIndentAlt(indent), num);
break;
}
break;
case TiXmlNode::TINYXML_COMMENT:
printf( "Comment: [%s]", pParent->Value());
break;
case TiXmlNode::TINYXML_UNKNOWN:
printf( "Unknown" );
break;
case TiXmlNode::TINYXML_TEXT:
pText = pParent->ToText();
printf("Text: [%s]", pText->Value());
break;
case TiXmlNode::TINYXML_DECLARATION:
printf("Declaration");
break;
default:
break;
}
printf( "\n" );
for (pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
dump_to_stdout(pChild, indent + 1);
}
}
bool CScoreFile::dump_xml_file(string name)
{
TiXmlDocument doc(name);
bool loadOkay = doc.LoadFile();
if (loadOkay)
{
printf("\n%s:\n", name.c_str());
dump_to_stdout( &doc ); // defined later in the tutorial
return true;
}
else
{
printf("Failed to load file \"%s\"\n", name.c_str());
}
return false;
}
//#define VERBOSE_XML
bool CScoreFile::get_measure(TiXmlElement *pElt, CScore *pscore)
{
string ValueStr;
double timecode;
double duration;
int times;
CMesure *pmeasure;
duration = timecode = 0;
pElt->QueryDoubleAttribute("start_timecode", &timecode);
pElt->QueryDoubleAttribute("duration", &duration);
pElt->QueryIntAttribute("times", ×);
pmeasure = new CMesure(timecode, timecode + duration);
pmeasure->m_times = times;
#ifdef VERBOSE_XML
printf("Addin a measure starting at=%f duration=%f times=%d\n", timecode, duration, times);
#endif
pscore->m_mesure_list.push_back(pmeasure);
return true;
}
bool CScoreFile::get_note_frequencies(TiXmlElement *pParent, CNote *pnote)
{
TiXmlNode *pChild;
TiXmlElement *pElt;
string ValueStr;
double f;
int string_selection;
t_notefreq nf;
pnote->m_freq_list.clear();
for (pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
ValueStr = pChild->ValueStr();
if (ValueStr == string("frequency"))
{
f = 0;
pElt = pChild->ToElement();
pElt->QueryDoubleAttribute("value", &f);
pElt->QueryIntAttribute("string_sel", &string_selection);
nf.f = f;
nf.string = string_selection;
pnote->m_freq_list.push_back(nf);
#ifdef VERBOSE_XML
printf("Addinf frequency=%f and string=%d to note\n", f, nf.string);
#endif
}
else
printf("Warning: unknown field in the frequency list\n");
}
return true;
}
bool CScoreFile::get_notes(TiXmlElement *pParent, CInstrument *pinstr)
{
TiXmlNode *pChild;
TiXmlElement *pElt;
string ValueStr;
double timecode;
double duration;
int beam;
CNote *pnote;
for (pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
ValueStr = pChild->ValueStr();
if (ValueStr == string("note"))
{
duration = timecode = 0;
beam = 0;
pElt = pChild->ToElement();
pElt->QueryDoubleAttribute("time", &timecode);
pElt->QueryDoubleAttribute("duration", &duration);
pElt->QueryIntAttribute("beam", &beam);
pnote = new CNote(timecode, duration);
pnote->m_lconnected = (beam != 0);
get_note_frequencies(pElt, pnote);
#ifdef VERBOSE_XML
printf("Adding a note tcode=%f duration=%f\n", timecode, duration);
#endif
pinstr->add_note(pnote);
}
else
printf("Warning: unknown field in the note list\n");
}
return true;
}
bool CScoreFile::extract_score_data_from_xml(TiXmlNode* pParent, CScore *pscore)
{
TiXmlNode *pChild;
#ifdef VERBOSE_XML
TiXmlText *pText;
#endif
int type;
string ValueStr;
string InstrumentName;
//string InstrumentVoiceName;
CInstrument *pinstr;
TiXmlAttribute *pAttrib;
if (!pParent)
return false;
type = pParent->Type();
switch (type)
{
case TiXmlNode::TINYXML_ELEMENT:
#ifdef VERBOSE_XML
printf("Element [%s]", pParent->Value());
#endif
ValueStr = pParent->ValueStr();
if (ValueStr == string("measures"))
{
}
if (ValueStr == string("measure"))
{
get_measure(pParent->ToElement(), pscore);
}
if (ValueStr == string("instrument"))
{
pAttrib = pParent->ToElement()->FirstAttribute();
while (pAttrib != NULL)
{
if (string(pAttrib->Name()) == string("name"))
{
InstrumentName = string(pAttrib->Value());
break;
}
/*
if (string(pAttrib->Name()) == string("voice"))
{
InstrumentVoiceName = string(pAttrib->Value());
break;
}
*/
pAttrib = pAttrib->Next();
}
#ifdef VERBOSE_XML
printf("creating an istrument of name=%s\n", InstrumentName.c_str());
#endif
pinstr = new CInstrument(InstrumentName);
get_notes(pParent->ToElement(), pinstr);
pscore->add_instrument(pinstr);
// Branch out of the notes section
return true;
}
break;
#ifdef VERBOSE_XML
case TiXmlNode::TINYXML_DOCUMENT:
printf("Document");
break;
case TiXmlNode::TINYXML_COMMENT:
printf( "Comment: [%s]", pParent->Value());
break;
case TiXmlNode::TINYXML_UNKNOWN:
printf( "Unknown" );
break;
case TiXmlNode::TINYXML_TEXT:
pText = pParent->ToText();
printf("Text: [%s]", pText->Value());
break;
case TiXmlNode::TINYXML_DECLARATION: // like xml=1.0
printf("Declaration");
break;
#endif //VERBOSE_XML
default:
break;
}
#ifdef VERBOSE_XML
printf( "\n" );
#endif
for (pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
extract_score_data_from_xml(pChild->ToElement(), pscore);
}
return true;
}
CScore* CScoreFile::read_score_from_xml_file(string filename)
{
CScore *pretscore;
string instrument_name;
TiXmlDocument *pdoc;
bool bload;
try
{
printf("loading the score xml file.\n");
pdoc = new TiXmlDocument(filename);
bload = pdoc->LoadFile();
if (!bload)
{
delete pdoc;
throw 1;
}
pretscore = new CScore(); // Empty score
if (!extract_score_data_from_xml(pdoc, pretscore))
{
delete pretscore;
throw 2;
}
delete pdoc;
}
catch (int e)
{
switch (e)
{
case 2:
break;
case 1:
break;
default:
break;
}
return NULL;
}
return pretscore;
}