forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVocaloidMidiEventListFactory.hpp
More file actions
726 lines (664 loc) · 32.6 KB
/
VocaloidMidiEventListFactory.hpp
File metadata and controls
726 lines (664 loc) · 32.6 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/**
* VocaloidMidiEventListFactory.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 __VocaloidMidiEventListFactory_hpp__
#define __VocaloidMidiEventListFactory_hpp__
#include "vsqglobal.hpp"
#include "NrpnEvent.hpp"
#include "Track.hpp"
#include "TempoList.hpp"
#include "MidiParameterType.hpp"
VSQ_BEGIN_NAMESPACE
using namespace std;
/**
* @brief A class to generate NRPN list from an instance of Track. The NRPN list is used for VOCALOID.
*/
class VocaloidMidiEventListFactory{
private:
/**
* @brief A base class to provide Delay NrpnEvent and Value NrpnEvent from an instance of BPList.
*/
class NrpnEventProvider{
protected:
MidiParameterType::MidiParameterTypeEnum delayNrpn;
MidiParameterType::MidiParameterTypeEnum nrpn;
public:
explicit NrpnEventProvider(
MidiParameterType::MidiParameterTypeEnum delayNrpnType,
MidiParameterType::MidiParameterTypeEnum nrpnType )
{
this->delayNrpn = delayNrpnType;
this->nrpn = nrpnType;
}
virtual NrpnEvent getDelayNrpnEvent( tick_t actualClock, int delay ){
int delayMsb, delayLsb;
VocaloidMidiEventListFactory::_getMsbAndLsb( delay, &delayMsb, &delayLsb );
return NrpnEvent( actualClock, delayNrpn, delayMsb, delayLsb );
}
virtual NrpnEvent getNrpnEvent( tick_t actualClock, int value ){
return NrpnEvent( actualClock, nrpn, value );
}
};
/**
* @brief An implementation of NrpnEventProvider for "Pitch Bend" BPList.
*/
class PitchBendNrpnEventProvider : public NrpnEventProvider{
public:
explicit PitchBendNrpnEventProvider()
: NrpnEventProvider( MidiParameterType::PB_DELAY, MidiParameterType::PB_PITCH_BEND )
{
}
NrpnEvent getNrpnEvent( tick_t actualClock, int value ){
int actualValue = value + 0x2000;
int msb, lsb;
VocaloidMidiEventListFactory::_getMsbAndLsb( actualValue, &msb, &lsb );
return NrpnEvent( actualClock, nrpn, msb, lsb );
}
};
/**
* @brief An implementation of NrpnEventProvider for "Pitch Bend Sensitivity" BPList.
*/
class PitchBendSensitivityNrpnEventProvider : public NrpnEventProvider{
public:
explicit PitchBendSensitivityNrpnEventProvider()
: NrpnEventProvider( MidiParameterType::CC_PBS_DELAY, MidiParameterType::CC_PBS_PITCH_BEND_SENSITIVITY )
{
}
NrpnEvent getNrpnEvent( tick_t actualClock, int value ){
return NrpnEvent( actualClock, nrpn, value, 0x00 );
}
};
public:
/**
* @brief Generate a list of VOCALOID MIDI event from a specified track.
* @param target An instance of Track.
* @param tempoList Tempo information.
* @param totalClocks Length of the sequence (in tick unit).
* @param preMeasureClock Length of pre-measure (in tick unit).
* @param msPreSend Length of pre-send time in milli seconds.
* @return A list of VOCALOID MIDI event.
*/
static vector<MidiEvent> generateMidiEventList(
Track *target, TempoList *tempoList, tick_t totalClocks, tick_t preMeasureClock, int msPreSend )
{
vector<NrpnEvent> nrpnEventList = generateNRPN( target, tempoList, totalClocks, preMeasureClock, msPreSend );
return NrpnEvent::convert( nrpnEventList );
}
protected:
/**
* @brief Generate a list of NrpnEvent from a specified track.
* @param track An instance of Track.
* @param tempoList Tempo information.
* @param totalClocks Length of the sequence (in tick unit).
* @param preMeasureClock Length of pre-measure (in tick unit).
* @param msPreSend Length of pre-send time in milli seconds.
* @return A list of NrpnEvent.
*/
static vector<NrpnEvent> generateNRPN(
Track *target, TempoList *tempoList, tick_t totalClocks, tick_t preMeasureClock, int msPreSend )
{
vector<NrpnEvent> list;
string version = target->common()->version;
Event::List *events = target->events();
int count = events->size();
int note_start = 0;
int note_end = count - 1;
for( int i = 0; i < count; i++ ){
if( 0 <= events->get( i )->clock ){
note_start = i;
break;
}
note_start = i;
}
for( int i = count - 1; i >= 0; i-- ){
if( events->get( i )->clock <= totalClocks ){
note_end = i;
break;
}
}
// determine first singer
int singer_event = -1;
for( int i = note_start; i >= 0; i-- ){
if( events->get( i )->type == EventType::SINGER ){
singer_event = i;
break;
}
}
if( singer_event >= 0 ){
// first singer was found
vector<NrpnEvent> singerNrpnList = generateSingerNRPN( tempoList, events->get( singer_event ), 0 );
list.insert( list.end(), singerNrpnList.begin(), singerNrpnList.end() );
}else{
// first singer was not found. may be rate-case
list.push_back( NrpnEvent( 0, MidiParameterType::CC_BS_LANGUAGE_TYPE, 0x0 ) );
list.push_back( NrpnEvent( 0, MidiParameterType::PC_VOICE_TYPE, 0x0 ) );
}
vector<NrpnEvent> voiceChangeParameterNrpn = generateVoiceChangeParameterNRPN( target, tempoList, msPreSend, preMeasureClock );
list.insert( list.end(), voiceChangeParameterNrpn.begin(), voiceChangeParameterNrpn.end() );
if( version.substr( 0, 4 ) == "DSB2" ){
vector<NrpnEvent> fx2DepthNrpn = generateFx2DepthNRPN( target, tempoList, msPreSend );
list.insert( list.end(), fx2DepthNrpn.begin(), fx2DepthNrpn.end() );
}
int ms_presend = msPreSend;
if (target->curve("dyn")->size() > 0) {
vector<NrpnEvent> listdyn = generateExpressionNRPN( target, tempoList, ms_presend );
if( ! listdyn.empty() ){
list.insert( list.end(), listdyn.begin(), listdyn.end() );
}
}
if (target->curve("pbs")->size() > 0) {
vector<NrpnEvent> listpbs = generatePitchBendSensitivityNRPN( target, tempoList, ms_presend );
if( ! listpbs.empty() ){
list.insert( list.end(), listpbs.begin(), listpbs.end() );
}
}
if (target->curve("pit")->size() > 0) {
vector<NrpnEvent> listpit = generatePitchBendNRPN( target, tempoList, ms_presend );
if( ! listpit.empty() ){
list.insert( list.end(), listpit.begin(), listpit.end() );
}
}
int lastDelay = 0;
int last_note_end = 0;
for( int i = note_start; i <= note_end; i++ ){
const Event *item = events->get( i );
if( item->type == EventType::NOTE ){
int note_loc = 0x03;
if( item->clock == last_note_end ){
note_loc = note_loc - 0x02;
}
// find next note event
tick_t nextclock = item->clock + item->getLength() + 1;
int event_count = events->size();
for( int j = i + 1; j < event_count; j++ ){
const Event *itemj = events->get( j );
if( itemj->type == EventType::NOTE ){
nextclock = itemj->clock;
break;
}
}
if( item->clock + item->getLength() == nextclock ){
note_loc = note_loc - 0x01;
}
int delay;
NrpnEvent noteNrpn =
generateNoteNRPN( target, tempoList, item, msPreSend, note_loc, &lastDelay, &delay );
lastDelay = delay;
list.push_back( noteNrpn );
vector<NrpnEvent> vibratoNrpn = generateVibratoNRPN( tempoList, item, msPreSend );
list.insert( list.end(), vibratoNrpn.begin(), vibratoNrpn.end() );
last_note_end = item->clock + item->getLength();
}else if( item->type == EventType::SINGER ){
if( i > note_start && i != singer_event ){
vector<NrpnEvent> singerNrpn = generateSingerNRPN( tempoList, item, msPreSend );
list.insert( list.end(), singerNrpn.begin(), singerNrpn.end() );
}
}
}
std::sort( list.begin(), list.end(), NrpnEvent::compare );
vector<NrpnEvent> merged;
for( int i = 0; i < list.size(); i++ ){
vector<NrpnEvent> expanded = list[i].expand();
merged.insert( merged.end(), expanded.begin(), expanded.end() );
}
return merged;
}
/**
* @brief Generate a list of Expression(DYN) NrpnEvent from a specified track.
* @param track An instance of Track.
* @param tempoList Tempo information.
* @param msPreSend Length of pre-send time in milli seconds.
* @return A list of NrpnEvent.
*/
static vector<NrpnEvent> generateExpressionNRPN( Track *track, TempoList *tempoList, int preSendMilliseconds ){
vector<NrpnEvent> ret;
BPList *dyn = track->curve("DYN");
NrpnEventProvider *provider = new NrpnEventProvider( MidiParameterType::CC_E_DELAY, MidiParameterType::CC_E_EXPRESSION );
generateNRPNByBPList( ret, tempoList, preSendMilliseconds, dyn, provider );
delete provider;
return ret;
}
/**
* @brief Generate prefix NrpnEvent for track.
* @return A NrpnEvent.
*/
static NrpnEvent generateHeaderNRPN(){
NrpnEvent ret( 0, MidiParameterType::CC_BS_VERSION_AND_DEVICE, 0x00, 0x00 );
ret.append( MidiParameterType::CC_BS_DELAY, 0x00, 0x00 );
ret.append( MidiParameterType::CC_BS_LANGUAGE_TYPE, 0x00 );
return ret;
}
/**
* @brief Generate a list of singer change NrpnEvent.
* This method is not intended to use for first singer event on a track.
* That is generated in generateNRPN method.
* @param tempoList Tempo information.
* @param singerEvent A singer event.
* @param preSendMilliseconds Length of pre-send time in milli seconds.
* @return A list of NrpnEvent.
*/
static vector<NrpnEvent> generateSingerNRPN( TempoList *tempoList, const Event *singerEvent, int preSendMilliseconds ){
tick_t clock = singerEvent->clock;
Handle singer_handle;
double clock_msec = tempoList->getSecFromClock( clock ) * 1000.0;
double msEnd = tempoList->getSecFromClock( singerEvent->clock + singerEvent->getLength() ) * 1000.0;
int duration = (int)::floor( ::ceil( msEnd - clock_msec ) );
int duration0, duration1;
_getMsbAndLsb( duration, &duration0, &duration1 );
tick_t actualClock;
int delay;
_getActualClockAndDelay( tempoList, clock, preSendMilliseconds, &actualClock, &delay );
int delayMsb, delayLsb;
_getMsbAndLsb( delay, &delayMsb, &delayLsb );
NrpnEvent add( actualClock, MidiParameterType::CC_BS_VERSION_AND_DEVICE, 0x00, 0x00 );
add.append( MidiParameterType::CC_BS_DELAY, delayMsb, delayLsb, true );
add.append( MidiParameterType::CC_BS_LANGUAGE_TYPE, singer_handle.language, true );
add.append( MidiParameterType::PC_VOICE_TYPE, singer_handle.program );
vector<NrpnEvent> ret;
ret.push_back( add );
return ret;
}
/**
* @brief Generate NRPN from note events in the track.
* @param track A track, source of note events.
* @param noteEvent A note event, to be output.
* @param msPreSend Pre-send time in milli-seconds
* @param noteLocation Specifies note position relationship around the note event.<ul>
* <li>00: the note item is bonded with both forward and backward note items.
* <li>01: the note item is bonded with only backward note item.
* <li>02: the note item is bonded with only forward note item.
* <li>03: the note item is not boned with any note item.
* </ul>
* @param lastDelay The delay time set to forward note item. Set NULL if the note item is the first note item in the track.
* @param [out] delay Delay time of the note item.
* @return Generated NRPN.
*/
static NrpnEvent generateNoteNRPN( Track *track, TempoList *tempoList, const Event *noteEvent, int msPreSend, int noteLocation, int *lastDelay, int *delay ){
tick_t clock = noteEvent->clock;
NrpnEvent add( 0, MidiParameterType::CC_BS_DELAY, 0, 0 );
tick_t actualClock;
_getActualClockAndDelay( tempoList, clock, msPreSend, &actualClock, delay );
// take care, lastDelay and delay are both pointer
bool addInitialized = false;
int lastDelayValue;
if( 0 == lastDelay ){
add = NrpnEvent(
actualClock,
MidiParameterType::CVM_NM_VERSION_AND_DEVICE,
0x00, 0x00
);
lastDelayValue = 0;
addInitialized = true;
}else{
lastDelayValue = *lastDelay;
}
if( lastDelayValue != *delay ){
int delayMsb, delayLsb;
_getMsbAndLsb( *delay, &delayMsb, &delayLsb );
if( false == addInitialized ){
//TODO: In this case, CVM_NM_VERSION_AND_DEVICE is omitted. Farther verification is required.
add = NrpnEvent( actualClock, MidiParameterType::CVM_NM_DELAY, delayMsb, delayLsb );
addInitialized = true;
}else{
add.append( MidiParameterType::CVM_NM_DELAY, delayMsb, delayLsb, true );
}
}
if( false == addInitialized ){
add = NrpnEvent( actualClock, MidiParameterType::CVM_NM_NOTE_NUMBER, noteEvent->note );
}else{
add.append( MidiParameterType::CVM_NM_NOTE_NUMBER, noteEvent->note, true );
}
// Velocity
add.append( MidiParameterType::CVM_NM_VELOCITY, noteEvent->dynamics, true );
// Note Duration
double msEnd = tempoList->getSecFromClock( clock + noteEvent->getLength() ) * 1000.0;
double clock_msec = tempoList->getSecFromClock( clock ) * 1000.0;
int duration = (int)::floor( msEnd - clock_msec );
int duration0, duration1;
_getMsbAndLsb( duration, &duration0, &duration1 );
add.append( MidiParameterType::CVM_NM_NOTE_DURATION, duration0, duration1, true );
// Note Location
add.append( MidiParameterType::CVM_NM_NOTE_LOCATION, noteLocation, true );
if( noteEvent->vibratoHandle.getHandleType() != HandleType::UNKNOWN ){
add.append( MidiParameterType::CVM_NM_INDEX_OF_VIBRATO_DB, 0x00, 0x00, true );
string icon_id = noteEvent->vibratoHandle.iconId;
string num = icon_id.substr( icon_id.length() - 3 );
int vibrato_type = StringUtil::parseInt<int>( num, 16 );
int note_length = noteEvent->getLength();
int vibrato_delay = noteEvent->vibratoDelay;
int bVibratoDuration = (int)::floor( (note_length - vibrato_delay) / (double)note_length * 127.0 );
int bVibratoDelay = 0x7f - bVibratoDuration;
add.append( MidiParameterType::CVM_NM_VIBRATO_CONFIG, vibrato_type, bVibratoDuration, true );
add.append( MidiParameterType::CVM_NM_VIBRATO_DELAY, bVibratoDelay, true );
}
vector<string> spl = noteEvent->lyricHandle.getLyricAt( 0 ).getPhoneticSymbolList();
ostringstream os;
for( int j = 0; j < spl.size(); j++ ){
os << spl[j];
}
string s = os.str();
vector<string> symbols;
for( int i = 0; i < s.length(); i++ ){
symbols.push_back( s.substr( i, 1 ) );
}
string renderer = track->common()->version;
if( renderer.substr( 0, 4 ) == string( "DSB2" ) ){
add.append( (MidiParameterType::MidiParameterTypeEnum)0x5011, 0x01, true );//TODO: Meaning of (byte)0x5011 is unknown.
}
add.append( MidiParameterType::CVM_NM_PHONETIC_SYMBOL_BYTES, symbols.size(), true );// (byte)0x12(Number of phonetic symbols in bytes)
int count = -1;
vector<int> consonantAdjustment = noteEvent->lyricHandle.getLyricAt( 0 ).getConsonantAdjustmentList();
for( int j = 0; j < spl.size(); j++ ){
string chars = spl[j];
for( int k = 0; k < chars.length(); k++ ){
count = count + 1;
if( k == 0 ){
add.append( (MidiParameterType::MidiParameterTypeEnum)((0x50 << 8) | (0x13 + count)), chars[k], consonantAdjustment[j], true ); // Phonetic symbol j
}else{
add.append( (MidiParameterType::MidiParameterTypeEnum)((0x50 << 8) | (0x13 + count)), chars[k], true ); // Phonetic symbol j
}
}
}
if( renderer.substr( 0, 4 ) != string( "DSB2" ) ){
add.append( MidiParameterType::CVM_NM_PHONETIC_SYMBOL_CONTINUATION, 0x7f, true ); // End of phonetic symbols
}
if( renderer.substr( 0, 4 ) == string( "DSB3" ) ){
int v1mean = (int)::floor( noteEvent->pmBendDepth * 60 / 100 );
if( v1mean < 0 ){
v1mean = 0;
}
if( 60 < v1mean ){
v1mean = 60;
}
int d1mean = (int)::floor( 0.3196 * noteEvent->pmBendLength + 8.0 );
int d2mean = (int)::floor( 0.92 * noteEvent->pmBendLength + 28.0 );
add.append( MidiParameterType::CVM_NM_V1MEAN, v1mean, true );// (byte)0x50(v1mean)
add.append( MidiParameterType::CVM_NM_D1MEAN, d1mean, true );// (byte)0x51(d1mean)
add.append( MidiParameterType::CVM_NM_D1MEAN_FIRST_NOTE, 0x14, true );// (byte)0x52(d1meanFirstNote)
add.append( MidiParameterType::CVM_NM_D2MEAN, d2mean, true );// (byte)0x53(d2mean)
add.append( MidiParameterType::CVM_NM_D4MEAN, noteEvent->d4mean, true );// (byte)0x54(d4mean)
add.append( MidiParameterType::CVM_NM_PMEAN_ONSET_FIRST_NOTE, noteEvent->pMeanOnsetFirstNote, true ); // 055(pMeanOnsetFirstNote)
add.append( MidiParameterType::CVM_NM_VMEAN_NOTE_TRNSITION, noteEvent->vMeanNoteTransition, true ); // (byte)0x56(vMeanNoteTransition)
add.append( MidiParameterType::CVM_NM_PMEAN_ENDING_NOTE, noteEvent->pMeanEndingNote, true );// (byte)0x57(pMeanEndingNote)
add.append( MidiParameterType::CVM_NM_ADD_PORTAMENTO, noteEvent->pmbPortamentoUse, true );// (byte)0x58(AddScoopToUpInternals&AddPortamentoToDownIntervals)
int decay = (int)::floor( noteEvent->demDecGainRate / 100.0 * 0x64 );
add.append( MidiParameterType::CVM_NM_CHANGE_AFTER_PEAK, decay, true );// (byte)0x59(changeAfterPeak)
int accent = (int)::floor( 0x64 * noteEvent->demAccent / 100.0 );
add.append( MidiParameterType::CVM_NM_ACCENT, accent, true );// (byte)0x5a(Accent)
}
add.append( MidiParameterType::CVM_NM_NOTE_MESSAGE_CONTINUATION, 0x7f, true );// (byte)0x7f(Note message continuation)
return add;
}
/**
* @brief 指定したシーケンスの指定したトラックから、PitchBend の NRPN リストを作成する
* @param sequence (Sequence) 出力元のシーケンス
* @param track (int) 出力するトラックの番号
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @return (table<NrpnEvent>) NrpnEvent の配列
*/
static vector<NrpnEvent> generatePitchBendNRPN( Track *track, TempoList *tempoList, int msPreSend ){
vector<NrpnEvent> ret;
BPList *pit = track->curve("PIT");
PitchBendNrpnEventProvider *provider = new PitchBendNrpnEventProvider();
generateNRPNByBPList( ret, tempoList, msPreSend, pit, provider );
delete provider;
return ret;
}
/**
* @brief 指定したシーケンスの指定したトラックから、PitchBendSensitivity の NRPN リストを作成する
* @param sequence (Sequence) 出力元のシーケンス
* @param track (int) 出力するトラックの番号
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @return (table<NrpnEvent>) NrpnEvent の配列
*/
static vector<NrpnEvent> generatePitchBendSensitivityNRPN( Track *track, TempoList *tempoList, int msPreSend ){
vector<NrpnEvent> ret;
BPList *pbs = track->curve("PBS");
PitchBendSensitivityNrpnEventProvider *provider = new PitchBendSensitivityNrpnEventProvider();
generateNRPNByBPList( ret, tempoList, msPreSend, pbs, provider );
delete provider;
return ret;
}
/**
* @brief トラックの音符イベントから、ビブラート出力用の NRPN のリストを作成する
* @param sequence (Sequence) 出力元のシーケンス
* @param noteEvent (Event) 出力する音符イベント
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @return (table<NrpnEvent>) NrpnEvent の配列
*/
static vector<NrpnEvent> generateVibratoNRPN( TempoList *tempoList, const Event *noteEvent, int msPreSend ){
vector<NrpnEvent> ret;
if( noteEvent->vibratoHandle.getHandleType() != HandleType::UNKNOWN ){
tick_t vclock = noteEvent->clock + noteEvent->vibratoDelay;
tick_t actualClock;
int delay;
_getActualClockAndDelay( tempoList, vclock, msPreSend, &actualClock, &delay );
int delayMsb, delayLsb;
_getMsbAndLsb( delay, &delayMsb, &delayLsb );
NrpnEvent add2( actualClock, MidiParameterType::CC_VD_VERSION_AND_DEVICE, 0x00, 0x00 );
add2.append( MidiParameterType::CC_VR_VERSION_AND_DEVICE, 0x00, 0x00 );
add2.append( MidiParameterType::CC_VD_DELAY, delayMsb, delayLsb );
add2.append( MidiParameterType::CC_VR_DELAY, delayMsb, delayLsb );
// CC_VD_VIBRATO_DEPTH, CC_VR_VIBRATO_RATE では、NRPN の MSB を省略してはいけない
add2.append( MidiParameterType::CC_VD_VIBRATO_DEPTH, noteEvent->vibratoHandle.startDepth );
add2.append( MidiParameterType::CC_VR_VIBRATO_RATE, noteEvent->vibratoHandle.startRate );
ret.push_back( add2 );
tick_t vlength = noteEvent->getLength() - noteEvent->vibratoDelay;
VibratoBPList depthBP = noteEvent->vibratoHandle.depthBP;
int count = depthBP.size();
if( count > 0 ){
int lastDelay = 0;
for( int i = 0; i < count; i++ ){
VibratoBP itemi = depthBP.get( i );
double percent = itemi.x;
tick_t cl = vclock + (tick_t) ::floor( percent * vlength );
_getActualClockAndDelay( tempoList, cl, msPreSend, &actualClock, &delay );
NrpnEvent nrpnEvent( 0, MidiParameterType::CC_BS_DELAY, 0 );
if( lastDelay != delay ){
_getMsbAndLsb( delay, &delayMsb, &delayLsb );
nrpnEvent = NrpnEvent( actualClock, MidiParameterType::CC_VD_DELAY, delayMsb, delayLsb );
nrpnEvent.append( MidiParameterType::CC_VD_VIBRATO_DEPTH, itemi.y );
}else{
nrpnEvent = NrpnEvent( actualClock, MidiParameterType::CC_VD_VIBRATO_DEPTH, itemi.y );
}
lastDelay = delay;
ret.push_back( nrpnEvent );
}
}
VibratoBPList rateBP = noteEvent->vibratoHandle.rateBP;
count = rateBP.size();
if( count > 0 ){
int lastDelay = 0;
for( int i = 0; i < count; i++ ){
VibratoBP itemi = rateBP.get( i );
double percent = itemi.x;
tick_t cl = vclock + (tick_t)::floor( percent * vlength );
_getActualClockAndDelay( tempoList, cl, msPreSend, &actualClock, &delay );
NrpnEvent nrpnEvent( 0, MidiParameterType::CC_BS_DELAY, 0 );
if( lastDelay != delay ){
_getMsbAndLsb( delay, &delayMsb, &delayLsb );
nrpnEvent = NrpnEvent( actualClock, MidiParameterType::CC_VR_DELAY, delayMsb, delayLsb );
nrpnEvent.append( MidiParameterType::CC_VR_VIBRATO_RATE, itemi.y );
}else{
nrpnEvent = NrpnEvent( actualClock, MidiParameterType::CC_VR_VIBRATO_RATE, itemi.y );
}
lastDelay = delay;
ret.push_back( nrpnEvent );
}
}
}
std::sort( ret.begin(), ret.end(), NrpnEvent::compare );
return ret;
}
/**
* @brief 指定したシーケンスの指定したトラックから、VoiceChangeParameter の NRPN リストを作成する
* @param sequence (Sequence) 出力元のシーケンス
* @param track (int) 出力するトラックの番号
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @return (table<NrpnEvent>) NrpnEvent の配列
*/
static vector<NrpnEvent> generateVoiceChangeParameterNRPN( Track *track, TempoList *tempoList, int msPreSend, tick_t premeasure_clock ){
string renderer = track->common()->version;
vector<NrpnEvent> res;
vector<string> curves;
if( renderer.substr( 0, 4 ) == "DSB3" ){
curves.push_back( "BRE" ); curves.push_back( "BRI" ); curves.push_back( "CLE" );
curves.push_back( "POR" ); curves.push_back( "OPE" ); curves.push_back( "GEN" );
}else if( renderer.substr( 0, 4 ) == "DSB2" ){
curves.push_back( "BRE" ); curves.push_back( "BRI" ); curves.push_back( "CLE" );
curves.push_back( "POR" ); curves.push_back( "GEN" ); curves.push_back( "harmonics" );
curves.push_back( "reso1amp" ); curves.push_back( "reso1bw" ); curves.push_back( "reso1freq" );
curves.push_back( "reso2amp" ); curves.push_back( "reso2bw" ); curves.push_back( "reso2freq" );
curves.push_back( "reso3amp" ); curves.push_back( "reso3bw" ); curves.push_back( "reso3freq" );
curves.push_back( "reso4amp" ); curves.push_back( "reso4bw" ); curves.push_back( "reso4freq" );
}else{
curves.push_back( "BRE" ); curves.push_back( "BRI" ); curves.push_back( "CLE" );
curves.push_back( "POR" ); curves.push_back( "GEN" );
}
int lastDelay = 0;
for( int i = 0; i < curves.size(); i++ ){
BPList *list = track->curve(curves[i]);
if( list->size() > 0 ){
lastDelay = addVoiceChangeParameters( res, list, tempoList, msPreSend, lastDelay );
}
}
std::sort( res.begin(), res.end(), NrpnEvent::compare );
return res;
}
/**
* @param vsq [VsqFile]
* @param track [int]
* @param msPreSend [int]
* @return [VsqNrpn[] ]
*/
static vector<NrpnEvent> generateFx2DepthNRPN( Track *track, TempoList *tempoList, int preSendMilliseconds ){
vector<NrpnEvent> ret;
BPList *fx2depth = track->curve("fx2depth");
NrpnEventProvider *provider = new NrpnEventProvider( MidiParameterType::CC_FX2_DELAY, MidiParameterType::CC_FX2_EFFECT2_DEPTH );
generateNRPNByBPList( ret, tempoList, preSendMilliseconds, fx2depth, provider );
delete provider;
return ret;
}
/**
* @brief Voice Change Parameter の NRPN を追加する
* @param dest (table) 追加先のテーブル
* @param list (BPList) Voice Change Parameter のデータ点が格納された BPList
* @param sequence (Sequence) シーケンス
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @param lastDelay (int) 直前の delay 値(ミリ秒単位)
* @return (int) delay 値(ミリ秒単位)
*/
static int addVoiceChangeParameters( vector<NrpnEvent> &dest, BPList *list, TempoList *tempoList, int msPreSend, int lastDelay ){
int id = MidiParameterType::getVoiceChangeParameterId( list->getName() );
for( int j = 0; j < list->size(); j++ ){
tick_t clock = list->getKeyClock( j );
int value = list->getValue( j );
tick_t actualClock;
int delay;
_getActualClockAndDelay( tempoList, clock, msPreSend, &actualClock, &delay );
if( actualClock >= 0 ){
NrpnEvent add( 0, MidiParameterType::CC_BS_DELAY, 0 );
bool initialized = false;
if( lastDelay != delay ){
int delayMsb, delayLsb;
_getMsbAndLsb( delay, &delayMsb, &delayLsb );
add = NrpnEvent( actualClock, MidiParameterType::VCP_DELAY, delayMsb, delayLsb );
initialized = true;
}
lastDelay = delay;
if( false == initialized ){
add = NrpnEvent( actualClock, MidiParameterType::VCP_VOICE_CHANGE_PARAMETER_ID, id );
}else{
add.append( MidiParameterType::VCP_VOICE_CHANGE_PARAMETER_ID, id );
}
add.append( MidiParameterType::VCP_VOICE_CHANGE_PARAMETER, value, true );
dest.push_back( add );
}
}
return lastDelay;
}
/**
* @brief 指定した時刻における、プリセンド込の時刻と、ディレイを取得する
* @param tempoList テンポ情報
* @param clock (int) Tick 単位の時刻
* @param msPreSend (int) ミリ秒単位のプリセンド時間
* @return (int) プリセンド分のクロックを引いた Tick 単位の時刻
* @return (int) ミリ秒単位のプリセンド時間
*/
static void _getActualClockAndDelay( TempoList *tempoList, tick_t clock, int msPreSend, tick_t *actualClock, int *delay ){
double clock_msec = tempoList->getSecFromClock( clock ) * 1000.0;
if( clock_msec - msPreSend <= 0 ){
*actualClock = 0;
}else{
double draft_clock_sec = (clock_msec - msPreSend) / 1000.0;
*actualClock = (tick_t)::floor( tempoList->getClockFromSec( draft_clock_sec ) );
}
*delay = (int)::floor( clock_msec - tempoList->getSecFromClock( (double)*actualClock ) * 1000.0 );
}
/**
* @brief DATA の値を MSB と LSB に分解する
* @param value (int) 分解する値
* @param (int) MSB の値
* @param (int) LSB の値
*/
static void _getMsbAndLsb( int value, int *msb, int *lsb ){
if( 0x3fff < value ){
*msb = 0x7f;
*lsb = 0x7f;
}else{
*msb = 0xff & (value >> 7);
*lsb = value - (*msb << 7);
}
}
private:
/**
* @brief データ点のリストから、NRPN のリストを作成する
* @param result 作成した NRPN のリストの格納先
* @param tempoList テンポ情報
* @param preSendMilliseconds ミリ秒単位のプリセンド秒
* @param list NRPN リストの元になるデータのリスト
* @param delayNrpnType delay を指定する際の NRPN のタイプ
* @param nrpnType データ点の値を指定する際の NRPN のタイプ
*/
static void generateNRPNByBPList(
vector<NrpnEvent> &result,
TempoList *tempoList, int preSendMilliseconds,
BPList *list, NrpnEventProvider *provider
){
size_t count = list->size();
int lastDelay = 0;
for( int i = 0; i < count; i++ ){
tick_t clock = list->getKeyClock( i );
tick_t actualClock;
int delay;
_getActualClockAndDelay( tempoList, clock, preSendMilliseconds, &actualClock, &delay );
if( actualClock >= 0 ){
NrpnEvent add = provider->getNrpnEvent( actualClock, list->getValue( i ) );
if( lastDelay != delay ){
NrpnEvent delayNrpn = provider->getDelayNrpnEvent( actualClock, delay );
if( add.hasLSB ){
delayNrpn.append( add.nrpn, add.dataLSB, add.dataLSB, add.isMSBOmittingRequired );
}else{
delayNrpn.append( add.nrpn, add.dataMSB, add.isMSBOmittingRequired );
}
result.push_back( delayNrpn );
}else{
result.push_back( add );
}
lastDelay = delay;
}
}
}
};
VSQ_END_NAMESPACE
#endif