-
Notifications
You must be signed in to change notification settings - Fork 1
/
midisynth.cpp
executable file
·1518 lines (1493 loc) · 54.6 KB
/
midisynth.cpp
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ソフトウェアMIDIシンセサイザ。
// Copyright(c)2003-2005 yuno
#include "midisynth.hpp"
#include <cassert>
#include <cmath>
#include <cstring>
#include <utility>
#ifdef __BORLANDC__
#include <fastmath.h>
namespace std{
using ::_fm_sin;
using ::_fm_cos;
using ::_fm_log10;
}
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace midisynth{
// チャンネルコンストラクタ。
channel::channel(note_factory* factory_, int bank):
factory(factory_), default_bank(bank)
{
notes.reserve(16);
reset_all_parameters();
}
// チャンネルデストラクタ。
channel::~channel()
{
all_sound_off_immediately();
}
// 発音中の音を合成する。
int channel::synthesize(int_least32_t* out, std::size_t samples, float rate, int_least32_t master_volume, int master_balance)
{
double volume = mute ? 0.0 : std::pow(static_cast<double>(master_volume) * this->volume * expression / (16383.0 * 16383.0 * 16383.0), 2) * 16383.0;
int num_notes = 0;
std::vector<NOTE>::iterator i = notes.begin();
while(i != notes.end()){
class note* note = i->note;
uint_least32_t panpot = note->get_panpot();
if(this->panpot <= 8192){
panpot = panpot * this->panpot / 8192;
}else{
panpot = panpot * (16384 - this->panpot) / 8192 + (this->panpot - 8192) * 2;
}
if(master_balance <= 8192){
panpot = panpot * master_balance / 8192;
}else{
panpot = panpot * (16384 - master_balance) / 8192 + (master_balance - 8192) * 2;
}
int_least32_t left = static_cast<int_least32_t>(volume * std::cos(std::max(0u, panpot - 1) * (M_PI / 2 / 16382)));
int_least32_t right = static_cast<int_least32_t>(volume * std::sin(std::max(0u, panpot - 1) * (M_PI / 2 / 16382)));
bool ret = note->synthesize(out, samples, rate, left, right);
if(ret){
++i;
}else{
i = notes.erase(i);
delete note;
}
++num_notes;
}
return num_notes;
}
// すべてのパラメータを初期状態に戻す。
void channel::reset_all_parameters()
{
program = default_bank * 128;
bank = default_bank;
panpot = 8192;
volume = 12800;
fine_tuning = 8192;
coarse_tuning = 8192;
tremolo_frequency = 3;
vibrato_frequency = 3;
master_frequency_multiplier = 1;
mono = false;
mute = false;
system_mode = system_mode_default;
reset_all_controller();
}
// パラメータを初期状態に戻す。
void channel::reset_all_controller()
{
expression = 16383;
channel_pressure(0);
pitch_bend = 8192;
pitch_bend_sensitivity = 256;
update_frequency_multiplier();
modulation_depth = 0;
modulation_depth_range = 64;
update_modulation();
set_damper(0);
set_sostenute(0);
set_freeze(0);
RPN = 0x3FFF;
NRPN = 0x3FFF;
}
// すべての音をノートオフする。
void channel::all_note_off()
{
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->status == NOTE::NOTEON){
i->status = NOTE::NOTEOFF;
i->note->note_off(64);
}
}
}
// すべての音をサウンドオフする。
void channel::all_sound_off()
{
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->status != NOTE::SOUNDOFF){
i->status = NOTE::SOUNDOFF;
i->note->sound_off();
}
}
}
// 即時消音。
void channel::all_sound_off_immediately()
{
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
delete i->note;
}
notes.clear();
}
// ノートオン。音を出す。
void channel::note_on(int note, int velocity)
{
assert(note >= 0 && note < NUM_NOTES);
assert(velocity >= 0 && velocity <= 127);
note_off(note, 64);
if(velocity){
if(mono){
all_sound_off();
}
class note* p = factory->note_on(program, note, velocity, frequency_multiplier);
if(p){
int assign = p->get_assign();
if(assign){
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->note->get_assign() == assign){
i->note->sound_off();
}
}
}
if(freeze){
p->set_freeze(freeze);
}
if(damper){
p->set_damper(damper);
}
if(modulation_depth){
float depth = static_cast<double>(modulation_depth) * modulation_depth_range / (16383.0 * 128.0);
p->set_vibrato(depth, vibrato_frequency);
}
if(pressure){
p->set_tremolo(pressure, tremolo_frequency);
}
notes.push_back(NOTE(p, note));
}
}
}
// ノートオフ。音がリリースタイムに入る。
void channel::note_off(int note, int velocity)
{
assert(note >= 0 && note < NUM_NOTES);
assert(velocity >= 0 && velocity <= 127);
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->key == note && i->status == NOTE::NOTEON){
i->status = NOTE::NOTEOFF;
i->note->note_off(velocity);
}
}
}
// ポリフォニックキープレッシャ。
void channel::polyphonic_key_pressure(int note, int value)
{
assert(note >= 0 && note < NUM_NOTES);
assert(value >= 0 && value <= 127);
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->key == note && i->status == NOTE::NOTEON){
i->note->set_tremolo(value, tremolo_frequency);
}
}
}
// チャンネルプレッシャ。
void channel::channel_pressure(int value)
{
assert(value >= 0 && value <= 127);
if(pressure != value){
pressure = value;
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
if(i->status == NOTE::NOTEON){
i->note->set_tremolo(value, tremolo_frequency);
}
}
}
}
// コントロールチェンジ。
void channel::control_change(int control, int value)
{
assert(value >= 0 && value <= 0x7F);
switch(control){
case 0x00:
bank_select((bank & 0x7F) | (value << 7));
break;
case 0x01:
set_modulation_depth((modulation_depth & 0x7F) | (value << 7));
break;
case 0x06:
set_registered_parameter((get_registered_parameter() & 0x7F) | (value << 7));
break;
case 0x07:
volume = (volume & 0x7F) | (value << 7);
break;
case 0x0A:
panpot = (panpot & 0x7F) | (value << 7);
break;
case 0x0B:
expression = (expression & 0x7F) | (value << 7);
break;
case 0x20:
bank_select((bank & 0x7F) | (value << 7));
break;
case 0x21:
set_modulation_depth((modulation_depth & ~0x7F) | value);
break;
case 0x26:
set_registered_parameter((get_registered_parameter() & ~0x7F) | value);
break;
case 0x27:
volume = (volume & ~0x7F) | value;
break;
case 0x2A:
panpot = (panpot & ~0x7F) | value;
break;
case 0x2B:
expression = (expression & ~0x7F) | value;
break;
case 0x40:
set_damper(value);
break;
case 0x42:
set_sostenute(value);
break;
case 0x45:
set_freeze(value);
break;
case 0x60:
set_registered_parameter(std::max(0x3FFF, get_registered_parameter() + 1));
break;
case 0x61:
set_registered_parameter(std::min(0, get_registered_parameter() - 1));
break;
case 0x62:
set_NRPN((NRPN & ~0x7F) | value);
break;
case 0x63:
set_NRPN((NRPN & 0x7F) | (value << 7));
break;
case 0x64:
set_RPN((RPN & ~0x7F) | value);
break;
case 0x65:
set_RPN((RPN & 0x7F) | (value << 7));
break;
case 0x78:
all_sound_off();
break;
case 0x79:
reset_all_controller();
break;
case 0x7B:
case 0x7C:
case 0x7D:
all_note_off();
break;
case 0x7E:
mono_mode_on();
break;
case 0x7F:
poly_mode_on();
break;
}
}
// バンクセレクト
void channel::bank_select(int value)
{
switch(system_mode){
case system_mode_gm:
break;
case system_mode_gs:
if(((bank & 0x3F80) == 0x3C00) == ((value & 0x3F80) == 0x3C00)){
set_bank(value);
}
break;
case system_mode_xg:
if(default_bank == 0x3C00){
set_bank(0x3C00 | (value & 0x7F));
}else if((value & 0x3F80) == 0x3F80){
set_bank(0x3C00 | (value & 0x7F));
}else{
set_bank(value);
}
break;
default:
if(default_bank == 0x3C00){
set_bank(0x3C00 | (value & 0x7F));
}else{
set_bank(value);
}
break;
}
}
// ダンパー効果。
void channel::set_damper(int value)
{
if(damper != value){
damper = value;
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
i->note->set_damper(value);
}
}
}
// ソステヌート効果。
void channel::set_sostenute(int value)
{
sostenute = value;
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
i->note->set_sostenute(value);
}
}
// フリーズ効果。
void channel::set_freeze(int value)
{
if(freeze != value){
freeze = value;
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
i->note->set_freeze(value);
}
}
}
// RPN取得。
int channel::get_registered_parameter()
{
switch(RPN){
case 0x0000:
return pitch_bend_sensitivity;
case 0x0001:
return fine_tuning;
case 0x0002:
return coarse_tuning;
case 0x0005:
return modulation_depth_range;
default:
return 0;
}
}
// RPN設定。
void channel::set_registered_parameter(int value)
{
switch(RPN){
case 0x0000:
set_pitch_bend_sensitivity(value);
break;
case 0x0001:
set_fine_tuning(value);
break;
case 0x0002:
set_coarse_tuning(value);
break;
case 0x0005:
set_modulation_depth_range(value);
break;
default:
break;
}
}
// 周波数倍率を再計算し更新する。
void channel::update_frequency_multiplier()
{
float value = master_frequency_multiplier
* std::pow(2, (coarse_tuning - 8192) / (128.0 * 100.0 * 12.0)
+ (fine_tuning - 8192) / (8192.0 * 100.0 * 12.0)
+ static_cast<double>(pitch_bend - 8192) * pitch_bend_sensitivity / (8192.0 * 128.0 * 12.0));
if(frequency_multiplier != value){
frequency_multiplier = value;
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
i->note->set_frequency_multiplier(value);
}
}
}
// モジュレーションデプス効果の更新。
void channel::update_modulation()
{
float depth = static_cast<double>(modulation_depth) * modulation_depth_range / (16383.0 * 128.0);
for(std::vector<NOTE>::iterator i = notes.begin(); i != notes.end(); ++i){
i->note->set_vibrato(depth, vibrato_frequency);
}
}
// シンセサイザコンストラクタ。
synthesizer::synthesizer(note_factory* factory)
{
for(int i = 0; i < 16; ++i){
channels[i].reset(new channel(factory, i == 9 ? 0x3C00 : 0x3C80));
}
reset_all_parameters();
}
// チャンネル取得。
channel* synthesizer::get_channel(int ch)
{
assert(ch >= 0 && ch < NUM_CHANNELS);
return channels[ch].get();
}
// 音を合成する。発音数を返す。
int synthesizer::synthesize(int_least16_t* output, std::size_t samples, float rate)
{
std::size_t n = samples * 2;
std::vector<int_least32_t> buf(n);
int num_notes = synthesize_mixing(&buf[0], samples, rate);
if(num_notes){
for(std::size_t i = 0; i < n; ++i){
int_least32_t x = buf[i];
if(x < -32767){
output[i] = -32767;
}else if(x > 32767){
output[i] = 32767;
}else{
output[i] = static_cast<int_least16_t>(x);
}
}
}else{
std::memset(output, 0, sizeof(int_least16_t) * n);
}
return num_notes;
}
int synthesizer::synthesize_mixing(int_least32_t* output, std::size_t samples, float rate)
{
if(active_sensing == 0){
all_sound_off();
active_sensing = -1;
}else if(active_sensing > 0){
active_sensing = std::max(0.0f, active_sensing - samples / rate);
}
int_least32_t volume = static_cast<int_least32_t>(main_volume) * master_volume / 16384;
int num_notes = 0;
for(int i = 0; i < NUM_CHANNELS; ++i){
num_notes += channels[i]->synthesize(output, samples, rate, volume, master_balance);
}
return num_notes;
}
// シンセサイザを完全にリセットする。
void synthesizer::reset()
{
all_sound_off_immediately();
reset_all_parameters();
}
// すべてのパラメータを初期状態に戻す。
void synthesizer::reset_all_parameters()
{
active_sensing = -1;
main_volume = 8192;
master_volume = 16383;
master_balance = 8192;
master_fine_tuning = 8192;
master_coarse_tuning = 8192;
master_frequency_multiplier = 1;
system_mode = system_mode_default;
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->reset_all_parameters();
}
}
// パラメータを初期状態に戻す。
void synthesizer::reset_all_controller()
{
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->reset_all_controller();
}
}
// オールノートオフ。すべての音をノートオフする。
void synthesizer::all_note_off()
{
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->all_note_off();
}
}
// オールサウンドオフ。すべての音をサウンドオフする。
void synthesizer::all_sound_off()
{
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->all_sound_off();
}
}
// 即時消音。
void synthesizer::all_sound_off_immediately()
{
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->all_sound_off_immediately();
}
}
// システムエクスクルーシブメッセージの解釈実行。
void synthesizer::sysex_message(const void* pvdata, std::size_t size)
{
const unsigned char* data = reinterpret_cast<const unsigned char*>(pvdata);
if(size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x01\xF7", 6) == 0){
/* GM system on */
set_system_mode(system_mode_gm);
}else if(size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x02\xF7", 6) == 0){
/* GM system off */
set_system_mode(system_mode_gm2);
}else if(size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x03\xF7", 6) == 0){
/* GM2 system on */
set_system_mode(system_mode_gm2);
}else if(size == 11 && std::memcmp(data, "\xF0\x41", 2) == 0 && std::memcmp(data + 3, "\x42\x12\x40\x00\x7F\x00\x41\xF7", 8) == 0){
/* GS reset */
set_system_mode(system_mode_gs);
}else if(size == 9 && std::memcmp(data, "\xF0\x43", 2) == 0 && (data[2] & 0xF0) == 0x10 && std::memcmp(data + 3, "\x4C\x00\x00\x7E\x00\xF7", 6) == 0){
/* XG system on */
set_system_mode(system_mode_xg);
}else if(size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x01", 5) == 0 && data[7] == 0xF7){
/* master volume */
set_master_volume((data[5] & 0x7F) | ((data[6] & 0x7F) << 7));
}else if(size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x02", 5) == 0 && data[7] == 0xF7){
/* master balance */
set_master_balance((data[5] & 0x7F) | ((data[6] & 0x7F) << 7));
}else if(size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x03", 5) == 0 && data[7] == 0xF7){
/* master fine tuning */
set_master_fine_tuning((data[5] & 0x7F) | ((data[6] & 0x7F) << 7));
}else if(size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x04", 5) == 0 && data[7] == 0xF7){
/* master coarse tuning */
set_master_coarse_tuning((data[5] & 0x7F) | ((data[6] & 0x7F) << 7));
}else if(size == 11 && std::memcmp(data, "\xF0\x41", 2) == 0 && (data[2] & 0xF0) == 0x10 && std::memcmp(data + 3, "\x42\x12\x40", 3) == 0 && (data[6] & 0xF0) == 0x10 && data[7] == 0x15 && data[10] == 0xF7){
/* use for rhythm part */
int channel = data[6] & 0x0F;
int map = data[8];
if(map == 0){
channels[channel]->set_bank(0x3C80);
}else{
channels[channel]->set_bank(0x3C00);
}
channels[channel]->program_change(0);
}
}
// MIDIイベントの解釈実行。
void synthesizer::midi_event(int event, int param1, int param2)
{
switch(event & 0xF0){
case 0x80:
note_off(event & 0x0F, param1 & 0x7F, param2 & 0x7F);
break;
case 0x90:
note_on(event & 0x0F, param1 & 0x7F, param2 & 0x7F);
break;
case 0xA0:
polyphonic_key_pressure(event & 0x0F, param1 & 0x7F, param2 & 0x7F);
break;
case 0xB0:
control_change(event & 0x0F, param1 & 0x7F, param2 & 0x7F);
break;
case 0xC0:
program_change(event & 0x0F, param1 & 0x7F);
break;
case 0xD0:
channel_pressure(event & 0x0F, param1 & 0x7F);
break;
case 0xE0:
pitch_bend_change(event & 0x0F, ((param2 & 0x7F) << 7) | (param1 & 0x7F));
break;
case 0xFE:
active_sensing = 0.33f;
break;
case 0xFF:
all_sound_off();
reset_all_parameters();
break;
default:
break;
}
}
// システムモードを変更する。
void synthesizer::set_system_mode(system_mode_t mode)
{
all_sound_off();
reset_all_parameters();
system_mode = mode;
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->set_system_mode(mode);
}
}
// マスターチューニングを再計算し更新する。
void synthesizer::update_master_frequency_multiplier()
{
float value = std::pow(2, (master_coarse_tuning - 8192) / (128.0 * 100.0 * 12.0)
+ (master_fine_tuning - 8192) / (8192.0 * 100.0 * 12.0));
if(master_frequency_multiplier != value){
master_frequency_multiplier = value;
for(int i = 0; i < NUM_CHANNELS; ++i){
channels[i]->set_master_frequency_multiplier(value);
}
}
}
// 正弦テーブル。正弦波ジェネレータ用。
namespace{
class sine_table{
public:
enum{ DIVISION = 4096 };
sine_table();
int_least16_t get(int n)const{ return data[n]; }
private:
int_least16_t data[DIVISION];
}sine_table;
sine_table::sine_table()
{
for(int i = 0; i < DIVISION; ++i){
data[i] = static_cast<int_least16_t>(32767 * std::sin(i * 2 * M_PI / DIVISION));
}
}
}
// 正弦波生成器コンストラクタ。
inline sine_wave_generator::sine_wave_generator():
position(0), step(0)
{
}
inline sine_wave_generator::sine_wave_generator(float cycle):
position(0)
{
set_cycle(cycle);
}
// 正弦波の周期を変更する。
void sine_wave_generator::set_cycle(float cycle)
{
if(cycle){
step = static_cast<uint_least32_t>(sine_table::DIVISION * 32768.0 / cycle);
}else{
step = 0;
}
}
// モジュレーションを加える。
void sine_wave_generator::add_modulation(int_least32_t x)
{
position += static_cast<int_least32_t>(static_cast<int_least64_t>(step) * x >> 16);
}
// 次のサンプルを取り出す。
inline int sine_wave_generator::get_next()
{
return sine_table.get((position += step) / 32768 % sine_table::DIVISION);
}
// 次のサンプルを取り出す(周波数変調付き)。
inline int sine_wave_generator::get_next(int_least32_t modulation)
{
uint_least32_t m = modulation * sine_table::DIVISION / 65536;
uint_least32_t p = ((position += step) / 32768 + m) % sine_table::DIVISION;
return sine_table.get(p);
}
// 対数変換テーブル。エンベロープジェネレータのディケイ以降で使う。
namespace{
#define LOG10_32767 4.5154366811416989472479934140484
#define LOGTABLE_SIZE 4096
#define LOGTABLE_FACTOR (LOGTABLE_SIZE / LOG10_32767)
class log_table{
public:
log_table();
uint_least16_t get(int x)const{ return data[x]; }
private:
uint_least16_t data[LOGTABLE_SIZE];
}log_table;
log_table::log_table()
{
for(int i = 0; i < LOGTABLE_SIZE; ++i){
data[i] = static_cast<uint_least16_t>(std::pow(10, static_cast<double>(i) / LOGTABLE_FACTOR));
}
}
}
// レートテーブル。AR、DR、SR、RRの計算処理の高速化。
namespace{
struct envelope_table{
envelope_table();
uint_least32_t TL[128];
uint_least32_t SL[16][128];
double AR[64][128];
double RR[64][128];
}const envelope_table;
envelope_table::envelope_table()
{
for(int t = 0; t < 128; ++t){
double fTL = 32767 * std::pow(10, t * -0.75 / 10);
TL[t] = static_cast<uint_least32_t>(fTL);
if(TL[t] == 0){
TL[t] = 1;
}
for(int s = 0; s < 16; ++s){
double x = fTL * std::pow(10, s * -3.0 / 10);
if(x <= 1){
SL[s][t] = 0;
}else{
SL[s][t] = static_cast<uint_least32_t>(65536 * LOGTABLE_FACTOR * std::log10(x));
}
}
}
for(int x = 0; x < 64; ++x){
double attack_time = 15.3262 * std::pow(10, x * -0.75 / 10);
double release_time = 211.84 * std::pow(10, x * -0.75 / 10);
for(int t = 0; t < 128; ++t){
AR[x][t] = TL[t] / attack_time;
RR[x][t] = 65536 * LOGTABLE_FACTOR * 48.0 / 10 * TL[t] / 32767 / release_time;
}
}
}
}
// エンベロープ生成器コンストラクタ。
envelope_generator::envelope_generator(int AR_, int DR_, int SR_, int RR_, int SL, int TL_):
state(ATTACK), AR(AR_), DR(DR_), SR(SR_), RR(RR_), TL(TL_),
current(0), rate(1), hold(0), freeze(0)
{
if(AR >= 63) AR = 63;
if(DR >= 63) DR = 63;
if(SR >= 63) SR = 63;
if(RR >= 63) RR = 63;
assert(AR >= 0);
assert(DR >= 0);
assert(SR >= 0);
assert(RR >= 0);
assert(SL >= 0 && SL <= 15);
assert(TL >= 0 && TL <= 127);
fTL = envelope_table.TL[TL];
fSS = fSL = envelope_table.SL[SL][TL];
fAR = 0;
fDR = 0;
fSR = 0;
fRR = 0;
fOR = 0;
fDRR = 0;
fDSS = 0;
}
// 再生レートを設定する。
inline void envelope_generator::set_rate(float rate)
{
this->rate = rate ? rate : 1;
update_parameters();
}
// ホールド(ダンパー&ソステヌート)を設定する。
void envelope_generator::set_hold(float hold)
{
if(this->hold > hold || state <= SASTAIN || current >= fSL){
this->hold = hold;
update_parameters();
}
}
// フリーズを設定する。
void envelope_generator::set_freeze(float freeze)
{
if(this->freeze > freeze || state <= SASTAIN || current >= fSL){
this->freeze = freeze;
update_parameters();
}
}
// 各パラメータの更新。
void envelope_generator::update_parameters()
{
double fAR = envelope_table.AR[AR][TL] / rate;
double fDR = envelope_table.RR[DR][TL] / rate;
double fSR = envelope_table.RR[SR][TL] / rate;
double fRR = envelope_table.RR[RR][TL] / rate;
if(fRR < 1){
fRR = 1;
}
if(hold > 0){
fRR = fSR * hold + fRR * (1 - hold);
}
if(freeze > 0){
fDR *= (1 - freeze);
fSR *= (1 - freeze);
fRR *= (1 - freeze);
}
if(fAR < 1){
fAR = 1;
}
this->fAR = static_cast<uint_least32_t>(fAR);
this->fDR = static_cast<uint_least32_t>(fDR);
this->fSR = static_cast<uint_least32_t>(fSR);
this->fRR = static_cast<uint_least32_t>(fRR);
this->fOR = static_cast<uint_least32_t>(envelope_table.RR[63][0] / rate);
this->fSS = std::max(this->fDR, fSL);
this->fDRR = std::max(this->fDR, this->fRR);
this->fDSS = std::max(this->fDRR, this->fSS);
}
// キーオフ。リリースに入る。
void envelope_generator::key_off()
{
switch(state){
case ATTACK:
state = ATTACK_RELEASE;
break;
case DECAY:
state = DECAY_RELEASE;
break;
case SASTAIN:
state = RELEASE;
break;
default:
break;
}
}
// サウンドオフ。急速消音モードに入る。
void envelope_generator::sound_off()
{
switch(state){
case ATTACK:
case ATTACK_RELEASE:
if(current){
current = static_cast<uint_least32_t>(65536 * LOGTABLE_FACTOR * std::log10(static_cast<double>(current)));
}
break;
default:
break;
}
state = SOUNDOFF;
}
// リリースからサウンドオフに移るレベル。リリースの長い音をいつまでも発音
// してるとCPUパワーの無駄なので適当なところで切るため。減衰は対数なので
// 音量が真にゼロになるには無限の時間を要する。実際には整数に切り捨てて
// 処理しているので1未満になったら無音になるとはいえ…。
// 不自然でない程度になるべく高い値の方がパフォーマンスが向上する。
#define SOUNDOFF_LEVEL 1024
// 次のサンプルを得る。
int envelope_generator::get_next()
{
uint_least32_t current = this->current;
switch(state){
case ATTACK:
if(current < fTL){
return this->current = current + fAR;
}
this->current = static_cast<uint_least32_t>(65536 * LOGTABLE_FACTOR * std::log10(static_cast<double>(fTL)));
state = DECAY;
return fTL;
case DECAY:
if(current > fSS){
this->current = current -= fDR;
return log_table.get(current / 65536);
}
this->current = current = fSL;
state = SASTAIN;
return log_table.get(current / 65536);
case SASTAIN:
if(current > fSR){
this->current = current -= fSR;
int n = log_table.get(current / 65536);
if(n > 1){
return n;
}
}
state = FINISHED;
return 0;
case ATTACK_RELEASE:
if(current < fTL){
return this->current = current + fAR;
}
this->current = static_cast<uint_least32_t>(65536 * LOGTABLE_FACTOR * std::log10(static_cast<double>(fTL)));
state = DECAY_RELEASE;
return fTL;
case DECAY_RELEASE:
if(current > fDSS){
this->current = current -= fDRR;
return log_table.get(current / 65536);
}
this->current = current = fSL;
state = RELEASE;
return log_table.get(current / 65536);
case RELEASE:
if(current > fRR){
this->current = current -= fRR;
int n = log_table.get(current / 65536);
if(n > SOUNDOFF_LEVEL){
return n;
}
state = SOUNDOFF;
return n;
}
state = FINISHED;
return 0;
case SOUNDOFF:
if(current > fOR){
this->current = current -= fOR;
int n = log_table.get(current / 65536);
if(n > 1){
return n;
}
}
state = FINISHED;
return 0;
default:
return 0;
}
}
namespace{
// キースケーリングテーブル
const int keyscale_table[4][128] = {
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
}, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
}, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 8, 9, 9, 9,10,10,10,10,10,10,10,10,
10,11,11,11,12,12,12,12,12,12,12,12,12,13,13,13,
14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
}, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4, 4, 5,
5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9,10,10,11,
12,12,12,12,12,12,12,13,13,14,14,15,16,16,16,16,
16,16,16,17,17,18,18,19,20,20,20,20,20,20,20,21,
21,22,22,23,24,24,24,24,24,24,24,25,25,26,26,27,
28,28,28,28,28,28,28,29,29,30,30,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31
}
};
// ディチューンテーブル
const float detune_table[4][128] = {
{ 0 },
{
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053,
0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053,
0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.159, 0.159, 0.159, 0.159, 0.159,
0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212,
0.212, 0.212, 0.212, 0.264, 0.264, 0.264, 0.264, 0.264,
0.264, 0.264, 0.264, 0.317, 0.317, 0.317, 0.317, 0.370,
0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423,
0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423,
0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423,
0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423
}, {
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.000, 0.000, 0.000, 0.000, 0.000, 0.053, 0.053, 0.053,
0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.106 ,0.106, 0.159, 0.159, 0.159,
0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212 ,0.212,
0.212, 0.212, 0.212, 0.264, 0.264, 0.264, 0.264, 0.264,
0.264, 0.264, 0.264, 0.317, 0.317, 0.317, 0.317, 0.370,
0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423,
0.423, 0.476, 0.476, 0.529, 0.582, 0.582, 0.582, 0.582,
0.582, 0.582 ,0.582, 0.635, 0.635, 0.688, 0.688, 0.741,
0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846 ,0.846,
0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846,
0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846
}, {
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.000, 0.000, 0.000, 0.000, 0.000, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106,
0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.159,
0.159, 0.159, 0.159, 0.159, 0.212, 0.212, 0.212, 0.212,
0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.264,
0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.317,
0.317, 0.317, 0.317, 0.370, 0.423, 0.423, 0.423, 0.423,
0.423, 0.423, 0.423, 0.423, 0.423, 0.476, 0.476, 0.529,
0.582, 0.582, 0.582, 0.582, 0.582, 0.582, 0.582, 0.635,
0.635, 0.688, 0.688, 0.741, 0.846, 0.846, 0.846, 0.846,
0.846, 0.846, 0.846, 0.899, 0.899, 1.005, 1.005, 1.058,