forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicXmlWriter.hpp
More file actions
398 lines (374 loc) · 19 KB
/
MusicXmlWriter.hpp
File metadata and controls
398 lines (374 loc) · 19 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
/**
* MusicXmlWriter.hpp
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq 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.
*/
#ifndef __MusicXmlWriter_hpp__
#define __MusicXmlWriter_hpp__
#include "vsqglobal.hpp"
#include "Sequence.hpp"
#include "OutputStream.hpp"
#include "NoteNumberUtil.hpp"
VSQ_BEGIN_NAMESPACE
class MusicXmlWriter{
public:
/**
* @brief シーケンスを MusicXML として、指定されたストリームに出力する
* @param sequence 出力するシーケンス
* @param stream 出力先のストリーム
* @todo change_tempo を changeTempo に名前を変える
* @todo sw を stream に名前変える
* @todo vsq を sequence に名前変える
* @todo tempo と change_tempo を渡せるようにする
*/
// private void printAsMusicXmlCore( String file, String encoding, String software, int tempo, boolean change_tempo )
void write( const Sequence *sequence, TextOutputStream *sw, const std::string &software ){
bool change_tempo = false;
int tempo = 120;
Sequence vsq = *sequence;
vsq.updateTotalClocks();
int intTempo = tempo;
if( !change_tempo ){
intTempo = (int)(60e6 / vsq.tempoList.getTempoAt( 0 ));
}
if( change_tempo ){
//TODO: vsq.adjustClockToMatchWith( tempo );
}
// ヘッダ
sw->writeLine( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
sw->writeLine( "<!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 2.0 Partwise//EN\"" );
sw->writeLine( " \"http://www.musicxml.org/dtds/partwise.dtd\">" );
sw->writeLine( "<score-partwise version=\"2.0\">" );
sw->writeLine( " <identification>" );
sw->writeLine( " <encoding>" );
if( software.size() > 0 ){
sw->writeLine( " <software>" + software + "</software>" );
}
sw->writeLine( " <software>libvsq</software>" );
sw->writeLine( " </encoding>" );
sw->writeLine( " </identification>" );
sw->writeLine( " <part-list>" );
int track_count = vsq.tracks()->size();
Timesig timesigStart = vsq.timesigList.getTimesigAt( 0 );
int clockPerMeasure = timesigStart.numerator * 480 * 4 / timesigStart.denominator;
for( int i = 0; i < track_count; i++ ){
const Track *vsq_track = vsq.track(i);
sw->writeLine( " <score-part id=\"P" + StringUtil::toString( i + 1 ) + "\">" );
sw->writeLine( " <part-name>" + vsq_track->getName() + "</part-name>" );
sw->writeLine( " </score-part>" );
}
sw->writeLine( " </part-list>" );
int measureStart = 0; // 出力開始する小節
for( int i = 0; i < track_count; i++ ){
const Track *vsq_track = vsq.track(i);
int numEvents = vsq_track->events()->size();
sw->writeLine( " <part id=\"P" + StringUtil::toString( i + 1 ) + "\">" );
// 拍子変更毎に出力していく
int countTimesig = vsq.timesigList.size();
int totalMeasure = measureStart; // 出力してきた小節の数
int clockLastBase = measureStart * clockPerMeasure; // 前回の拍子ブロックで出力し終わったクロック
int startIndex = 0;
for( int n = 0; n < countTimesig; n++ ){
Timesig timesigEntryThis = vsq.timesigList.get( n );
clockPerMeasure = timesigEntryThis.numerator * 480 * 4 / timesigEntryThis.denominator;
// この拍子が曲の終まで続くとしたら,あと何小節出力する必要があるのか?
int remainingMeasures = 0;
if( n + 1 < countTimesig ){
Timesig timesigEntryNext = vsq.timesigList.get( n + 1 );
remainingMeasures = timesigEntryNext.barCount - timesigEntryThis.barCount;
}else{
int remainingClocks = vsq.getTotalClocks() - clockLastBase;
remainingMeasures = remainingClocks / clockPerMeasure;
if( remainingClocks % clockPerMeasure != 0 ){
remainingMeasures++;
}
}
// remainingMeasures小節を順次出力
for( int j = totalMeasure; j < totalMeasure + remainingMeasures; j++ ){
int clockStart = clockLastBase + (j - totalMeasure) * clockPerMeasure;
int clockEnd = clockStart + clockPerMeasure;
// 今出力している小節内に、テンポ変更が挿入されているかどうか
int numTempo = vsq.tempoList.size();
std::vector<Tempo> tempoInsert; // 挿入するテンポ情報のリスト
for( int k = 0; k < numTempo; k++ ){
Tempo itemk = vsq.tempoList.get( k );
if( clockStart <= itemk.clock && itemk.clock < clockEnd ){
tempoInsert.push_back( itemk );
}
}
sw->writeLine( " <measure number=\"" + StringUtil::toString( j + 1 - measureStart ) + "\">" );
if( j == totalMeasure ){
sw->writeLine( " <attributes>" );
sw->writeLine( " <divisions>480</divisions>" );
sw->writeLine( " <time symbol=\"common\">" );
sw->writeLine( " <beats>" + StringUtil::toString( timesigEntryThis.numerator ) + "</beats>" );
sw->writeLine( " <beat-type>" + StringUtil::toString( timesigEntryThis.denominator ) + "</beat-type>" );
sw->writeLine( " </time>" );
sw->writeLine( " </attributes>" );
if( j == 0 ){
sw->writeLine( " <direction placement=\"above\">" );
sw->writeLine( " <direction-type>" );
sw->writeLine( " <metronome>" );
sw->writeLine( " <beat-unit>quarter</beat-unit>" );
sw->writeLine( " <per-minute>" + StringUtil::toString( intTempo ) + "</per-minute>" );
sw->writeLine( " </metronome>" );
sw->writeLine( " <words>Tempo " + StringUtil::toString( intTempo ) + "</words>" );
sw->writeLine( " </direction-type>" );
sw->writeLine( " <sound tempo=\"" + StringUtil::toString( intTempo ) + "\"/>" );
sw->writeLine( " </direction>" );
}
}
// 臨時記号のON/OFFを制御するために
std::map<std::string, bool> altered;
altered.insert( make_pair( "C", false ) );
altered.insert( make_pair( "D", false ) );
altered.insert( make_pair( "E", false ) );
altered.insert( make_pair( "F", false ) );
altered.insert( make_pair( "G", false ) );
altered.insert( make_pair( "A", false ) );
altered.insert( make_pair( "B", false ) );
int clockLast = clockStart; // 出力済みのクロック
for( int k = startIndex; k < numEvents; k++ ){
const Event *itemk = vsq_track->events()->get( k );
if( itemk->type != EventType::NOTE ){
if( clockEnd <= itemk->clock ){
startIndex = k;
break;
}
continue;
}
// 第 k 番目の音符が、第 j 小節の範囲に入っているかどうか。
// 入っていれば、出力する。
if( (clockStart <= itemk->clock && itemk->clock < clockEnd) ||
(clockStart <= itemk->clock + itemk->getLength() && itemk->clock + itemk->getLength() < clockEnd) ||
(itemk->clock <= clockStart && clockEnd <= itemk->clock + itemk->getLength()) )
{
if( clockLast < itemk->clock ){
// 第 j 小節の開始位置から、第 k 番目の音符の間に、休符があるので、出力する
// [clockLast, itemk.Clock) の間でテンポ変更イベントがあれば、これも出力する
std::vector<Tempo> insert;
if( !change_tempo ){
for( int m = 0; m < tempoInsert.size(); m++ ){
Tempo itemm = tempoInsert[m];
if( clockLast <= itemm.clock && itemm.clock < itemk->clock ){
insert.push_back( itemm );
}
}
}
printStyledNote( sw, clockLast, itemk->clock - clockLast, -1, insert, "", altered, false, false );
clockLast = itemk->clock;
}
bool tieStopRequired = false;
int start = itemk->clock;
if( start < clockStart ){
// 前の小節からタイで接続されている場合
start = clockStart;
tieStopRequired = true;
}
int end = itemk->clock + itemk->getLength();
bool tieStartRequired = false;
if( clockEnd < end ){
// 次の小節にタイで接続しなければならない場合
end = clockEnd;
tieStartRequired = true;
}
int actualLength = end - start;
std::vector<Tempo> insert2;
if( !change_tempo ){
for( int m = 0; m < tempoInsert.size(); m++ ){
Tempo itemm = tempoInsert[m];
if( start <= itemm.clock && itemm.clock < end ){
insert2.push_back( itemm );
}
}
}
printStyledNote( sw, start, actualLength, itemk->note, insert2, itemk->lyricHandle.getLyricAt( 0 ).phrase, altered, tieStartRequired, tieStopRequired );
clockLast = end;
if( tieStartRequired ){
startIndex = k;
}else{
startIndex = k + 1;
}
}
}
if( clockLast < clockEnd ){
// 小節の最後に休符を入れる必要がある
std::vector<Tempo> insert3;
if( !change_tempo ){
for( int m = 0; m < tempoInsert.size(); m++ ){
Tempo itemm = tempoInsert[m];
if( clockEnd <= itemm.clock && itemm.clock < clockLast ){
insert3.push_back( itemm );
}
}
}
printStyledNote( sw, clockLast, (clockEnd - clockLast), -1, insert3, "", altered, false, false );
clockLast = clockEnd;
}
sw->writeLine( " </measure>" );
}
clockLastBase += remainingMeasures * clockPerMeasure;
totalMeasure += remainingMeasures;
}
sw->writeLine( " </part>" );
}
sw->writeLine( "</score-partwise>" );
}
protected:
void printStyledNote(
TextOutputStream *writer,
int clock_start,
int clock_length,
int note,
std::vector<Tempo> &tempoInsert,
std::string lyric,
std::map<std::string, bool> &altered_context,
bool tie_start_required,
bool tie_stop_required )
{
int numInsert = tempoInsert.size();
for( int i = 0; i < numInsert; i++ ){
Tempo itemi = tempoInsert[i];
int tempo = (int)(60e6 / (double)itemi.tempo);
writer->writeLine( " <direction placement=\"above\">" );
writer->writeLine( " <direction-type>" );
writer->writeLine( " <metronome>" );
writer->writeLine( " <beat-unit>quarter</beat-unit>" );
writer->writeLine( " <per-minute>" + StringUtil::toString( tempo ) + "</per-minute>" );
writer->writeLine( " </metronome>" );
writer->writeLine( " <words>Tempo " + StringUtil::toString( tempo ) + "</words>" );
writer->writeLine( " </direction-type>" );
writer->writeLine( " <sound tempo=\"" + StringUtil::toString( tempo ) + "\"/>" );
writer->writeLine( " <offset>" + StringUtil::toString( itemi.clock - clock_start ) + "</offset>" );
writer->writeLine( " </direction>" );
}
int ret[9];
int len[8] = { 1920, 960, 480, 240, 120, 60, 30, 15 };
std::vector<std::string> name;
name.push_back( "whole" );
name.push_back( "half" );
name.push_back( "quarter" );
name.push_back( "eighth" );
name.push_back( "16th" );
name.push_back( "32nd" );
name.push_back( "64th" );
name.push_back( "128th" );
name.push_back( "" );
int remain = clock_length;
for( int i = 0; i < 8; i++ ){
ret[i] = remain / len[i];
if ( ret[i] > 0 ) {
remain -= len[i] * ret[i];
}
}
ret[8] = remain;
int firstContainedIndex = -1;
int lastContainedIndex = -1;
int numSeparated = 0;
for( int i = 0; i < 8; i++ ){
if( ret[i] > 0 ){
if( firstContainedIndex < 0 ){
firstContainedIndex = i;
}
lastContainedIndex = i;
numSeparated += ret[i];
}
}
for( int i = 0; i < 8; i++ ){
int count = ret[i];
if( count <= 0 ){
continue;
}
for( int j = 0; j < count; j++ ){
bool local_tie_start_required = numSeparated > 0 ? true : false;
bool local_tie_stop_required = numSeparated > 0 ? true : false;
if( i == firstContainedIndex && j == 0 ){
local_tie_stop_required = tie_stop_required;
}
if( i == lastContainedIndex && j == count - 1 ){
local_tie_start_required = tie_start_required;
}
printStyledNoteCor( writer, len[i], note, lyric, altered_context, local_tie_start_required, local_tie_stop_required, name[i] );
}
}
}
void printStyledNoteCor(
TextOutputStream *writer,
int clock_length,
int note,
std::string lyric,
std::map<std::string, bool> &altered_context,
bool tie_start_required,
bool tie_stop_required,
std::string type )
{
writer->writeLine( " <note>" );
if( note < 0 ){
writer->writeLine( " <rest/>" );
}else{
std::string noteStringBase = NoteNumberUtil::getNoteStringBase( note ); // "C"など
int octave = NoteNumberUtil::getNoteOctave( note );
writer->writeLine( " <pitch>" );
writer->writeLine( " <step>" + noteStringBase + "</step>" );
int alter = NoteNumberUtil::getNoteAlter( note );
if( alter != 0 ){
writer->writeLine( " <alter>" + StringUtil::toString( alter ) + "</alter>" );
}
writer->writeLine( " <octave>" + StringUtil::toString( octave + 1 ) + "</octave>" );
writer->writeLine( " </pitch>" );
std::string stem = note >= 70 ? "down" : "up";
writer->writeLine( " <stem>" + stem + "</stem>" );
std::string accidental = "";
std::string checkAltered = noteStringBase;
if( !tie_stop_required && altered_context.find( checkAltered ) != altered_context.end() ){
if( alter == 0 ){
if( altered_context[checkAltered] ){
accidental = "natural";
altered_context[checkAltered] = false;
}
}else{
if( !altered_context[checkAltered] ){
accidental = alter == 1 ? "sharp" : "flat";
altered_context[checkAltered] = true;
}
}
}
if( accidental.size() > 0 ) {
writer->writeLine( " <accidental>" + accidental + "</accidental>" );
}
if( tie_start_required ){
writer->writeLine( " <tie type=\"start\"/>" );
writer->writeLine( " <notations>" );
writer->writeLine( " <tied type=\"start\"/>" );
writer->writeLine( " </notations>" );
}
if( tie_stop_required ){
writer->writeLine( " <tie type=\"stop\"/>" );
writer->writeLine( " <notations>" );
writer->writeLine( " <tied type=\"stop\"/>" );
writer->writeLine( " </notations>" );
}
if( !tie_stop_required ){
writer->writeLine( " <lyric>" );
writer->writeLine( " <text>" + lyric + "</text>" );
writer->writeLine( " </lyric>" );
}
}
writer->writeLine( " <voice>1</voice>" );
writer->writeLine( " <duration>" + StringUtil::toString( clock_length ) + "</duration>" );
if( type.size() > 0 ){
writer->writeLine( " <type>" + type + "</type>" );
}
writer->writeLine( " </note>" );
}
};
VSQ_END_NAMESPACE
#endif