-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathit-module.cpp
More file actions
1182 lines (1061 loc) · 37.3 KB
/
it-module.cpp
File metadata and controls
1182 lines (1061 loc) · 37.3 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
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
#include <cmath>
#include "it-module.h"
#include "utils.h"
IT_Module::IT_Module(
const std::vector<Wave> &waves,
const std::vector<Drumkit> &drumkits,
const std::vector<std::vector<uint8_t>> &drums,
int32_t drumkit
) {
generate_it_module({}, {}, {}, {}, waves, drumkits, drums, drumkit);
_mod = new openmpt::module_ext(_data);
_mod->set_repeat_count(-1);
_is_interleaved = false;
if (!try_open()) {
_is_interleaved = true;
try_open();
}
}
IT_Module::IT_Module(
const std::vector<Note_View> &channel_1_notes,
const std::vector<Note_View> &channel_2_notes,
const std::vector<Note_View> &channel_3_notes,
const std::vector<Note_View> &channel_4_notes,
const std::vector<Wave> &waves,
const std::vector<Drumkit> &drumkits,
const std::vector<std::vector<uint8_t>> &drums,
int32_t loop_tick,
bool stereo
) {
generate_it_module(channel_1_notes, channel_2_notes, channel_3_notes, channel_4_notes, waves, drumkits, drums, -1, loop_tick, stereo);
_mod = new openmpt::module_ext(_data);
if (loop_tick != -1) {
_mod->set_repeat_count(-1);
}
_is_interleaved = false;
if (!try_open()) {
_is_interleaved = true;
try_open();
}
}
IT_Module::~IT_Module() noexcept {
if (_mod) {
delete _mod;
_mod = nullptr;
}
}
void IT_Module::regenerate_it_module(
const std::vector<Wave> &waves,
const std::vector<Drumkit> &drumkits,
const std::vector<std::vector<uint8_t>> &drums,
int32_t drumkit
) {
if (_mod) {
delete _mod;
_mod = nullptr;
}
_data.clear();
_tempo_change_wrong_channel = -1;
_tempo_change_mid_note = -1;
_too_many_drums = false;
_current_pattern = 0;
_current_row = 0;
generate_it_module({}, {}, {}, {}, waves, drumkits, drums, drumkit);
_mod = new openmpt::module_ext(_data);
_mod->set_repeat_count(-1);
}
bool IT_Module::export_file(const char *f) {
std::ofstream ofs;
open_ofstream(ofs, f);
if (!ofs.good()) return false;
ofs.write((char *)&_data[0], _data.size());
ofs.close();
return true;
}
bool IT_Module::play() {
if (!ready() || !playing()) return true;
std::size_t count = _is_interleaved ?
_mod->read_interleaved_stereo(SAMPLE_RATE, BUFFER_SIZE, _buffer.data()) :
_mod->read(SAMPLE_RATE, BUFFER_SIZE, _buffer.data(), _buffer.data() + BUFFER_SIZE);
_current_pattern = _mod->get_current_pattern();
_current_row = _mod->get_current_row();
if (count == 0) {
stop();
return true;
}
try {
if (_is_interleaved) {
_stream.write(_buffer.data(), static_cast<unsigned long>(count));
}
else {
const float * const buffers[2] = { _buffer.data(), _buffer.data() + BUFFER_SIZE };
_stream.write(buffers, static_cast<unsigned long>(count));
}
return true;
}
catch (...) {}
return false;
}
void IT_Module::mute_channel(int32_t channel, bool mute) {
if (channel - 1 < _mod->get_num_channels()) {
openmpt::ext::interactive *interactive = static_cast<openmpt::ext::interactive *>(_mod->get_interface(openmpt::ext::interactive_id));
interactive->set_channel_mute_status(channel - 1, mute);
}
}
int32_t IT_Module::play_note(Pitch pitch, int32_t octave, int channel, int32_t duty_wave) {
int32_t instrument = 2; // 50% square
if (channel == 1) {
instrument = duty_wave;
}
else if (channel == 2) {
instrument = duty_wave;
}
else if (channel == 3) {
instrument = 4 + duty_wave;
}
else if (channel == 4) {
instrument = 4 + 16 + (int32_t)pitch;
}
int32_t note = channel != 4 ? octave * NUM_PITCHES + (int32_t)pitch - 1 : 60;
openmpt::ext::interactive *interactive = static_cast<openmpt::ext::interactive *>(_mod->get_interface(openmpt::ext::interactive_id));
return interactive->play_note(instrument, note, 1.0, 0.0);
}
void IT_Module::stop_note(int32_t mod_channel) {
openmpt::ext::interactive *interactive = static_cast<openmpt::ext::interactive *>(_mod->get_interface(openmpt::ext::interactive_id));
interactive->stop_note(mod_channel);
}
void IT_Module::set_tick(int32_t tick) {
_mod->set_position_order_row(tick / ROWS_PER_PATTERN, tick % ROWS_PER_PATTERN);
}
double IT_Module::get_position_seconds() {
return _mod->get_position_seconds();
}
double IT_Module::get_duration_seconds() {
return _mod->get_duration_seconds();
}
bool IT_Module::try_open() {
try {
portaudio::System &portaudio = portaudio::System::instance();
portaudio::DirectionSpecificStreamParameters outputstream_parameters(
portaudio.defaultOutputDevice(),
2,
portaudio::FLOAT32,
_is_interleaved,
portaudio.defaultOutputDevice().defaultHighOutputLatency(),
0
);
portaudio::StreamParameters stream_parameters(
portaudio::DirectionSpecificStreamParameters::null(),
outputstream_parameters,
SAMPLE_RATE,
paFramesPerBufferUnspecified,
paNoFlag
);
_stream.open(stream_parameters);
return true;
}
catch (...) {}
return false;
}
static inline void put_int(std::vector<uint8_t> &data, const uint32_t v) {
data.push_back(v >> 0);
data.push_back(v >> 8);
data.push_back(v >> 16);
data.push_back(v >> 24);
}
static inline void put_short(std::vector<uint8_t> &data, const uint32_t v) {
data.push_back(v >> 0);
data.push_back(v >> 8);
}
static inline void patch_int(std::vector<uint8_t> &data, const uint32_t i, const uint32_t v) {
data[i + 0] = (v >> 0);
data[i + 1] = (v >> 8);
data[i + 2] = (v >> 16);
data[i + 3] = (v >> 24);
}
std::vector<std::vector<uint8_t>> IT_Module::get_instruments() {
std::vector<std::vector<uint8_t>> instruments;
return instruments;
}
std::vector<std::vector<uint8_t>> IT_Module::get_samples(const std::vector<Wave> &waves, const std::vector<const std::vector<uint8_t> *> &drums) {
const uint32_t sample_filename_length = 12;
const uint32_t sample_global_volume = 64;
const uint32_t sample_wave_flags = 0b00010001;
const uint32_t sample_noise_flags = 0b00000001;
const uint32_t sample_default_volume = 64;
const uint32_t sample_name_length = 26;
const uint32_t sample_default_panning = 32;
const uint32_t sample_length = 64;
const uint32_t sample_loop_begin = 0;
const uint32_t sample_loop_end = 64;
const uint32_t sample_speed = 33520;
const uint32_t sample_sustain_loop_begin = 0;
const uint32_t sample_sustain_loop_end = 0;
const uint32_t sample_vibrato_speed = 0;
const uint32_t sample_vibrato_depth = 0;
const uint32_t sample_vibrato_rate = 0;
const uint32_t sample_vibrato_waveform = 0;
std::vector<std::vector<uint8_t>> samples;
// sample header, 80 bytes
auto sample_header = [&](std::vector<uint8_t> &sample, uint32_t sample_size, bool noise = false) {
sample.push_back('I');
sample.push_back('M');
sample.push_back('P');
sample.push_back('S');
for (uint32_t i = 0; i < sample_filename_length; ++i) {
sample.push_back('\0');
}
sample.push_back(0); // unused
sample.push_back(sample_global_volume);
sample.push_back(noise ? sample_noise_flags : sample_wave_flags);
sample.push_back(sample_default_volume);
for (uint32_t i = 0; i < sample_name_length; ++i) {
sample.push_back('\0');
}
sample.push_back(1); // ???
sample.push_back(sample_default_panning);
put_int(sample, sample_size);
put_int(sample, sample_loop_begin);
put_int(sample, noise ? 0 : sample_loop_end);
put_int(sample, noise ? sample_speed * NOISE_SAMPLE_SPEED_FACTOR : sample_speed);
put_int(sample, sample_sustain_loop_begin);
put_int(sample, sample_sustain_loop_end);
put_int(sample, 0); // sample offset (index 72)
sample.push_back(sample_vibrato_speed);
sample.push_back(sample_vibrato_depth);
sample.push_back(sample_vibrato_rate);
sample.push_back(sample_vibrato_waveform);
};
// four hard-coded square samples (duty cycles)
{
std::vector<uint8_t> sample;
sample_header(sample, sample_length);
// sample (12.5% square)
for (uint32_t i = 0; i < sample_length / 2 * 1/8; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 7/8; ++i) {
sample.push_back(127);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/8; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 7/8; ++i) {
sample.push_back(127);
}
samples.push_back(std::move(sample));
}
{
std::vector<uint8_t> sample;
sample_header(sample, sample_length);
// sample (25% square)
for (uint32_t i = 0; i < sample_length / 2 * 1/4; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 3/4; ++i) {
sample.push_back(127);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/4; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 3/4; ++i) {
sample.push_back(127);
}
samples.push_back(std::move(sample));
}
{
std::vector<uint8_t> sample;
sample_header(sample, sample_length);
// sample (50% square)
for (uint32_t i = 0; i < sample_length / 2 * 1/2; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/2; ++i) {
sample.push_back(127);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/2; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/2; ++i) {
sample.push_back(127);
}
samples.push_back(std::move(sample));
}
{
std::vector<uint8_t> sample;
sample_header(sample, sample_length);
// sample (75% square)
for (uint32_t i = 0; i < sample_length / 2 * 3/4; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/4; ++i) {
sample.push_back(127);
}
for (uint32_t i = 0; i < sample_length / 2 * 3/4; ++i) {
sample.push_back(0);
}
for (uint32_t i = 0; i < sample_length / 2 * 1/4; ++i) {
sample.push_back(127);
}
samples.push_back(std::move(sample));
}
// dynamic wave samples
for (const Wave &wave : waves) {
std::vector<uint8_t> sample;
sample_header(sample, sample_length);
for (uint32_t i = 0; i < NUM_WAVE_SAMPLES; ++i) {
sample.push_back(wave[i] * 255 / 15 / 2);
sample.push_back(wave[i] * 255 / 15 / 2);
}
samples.push_back(std::move(sample));
}
for (const std::vector<uint8_t> *drum : drums) {
std::vector<uint8_t> sample;
if (drum) {
sample_header(sample, (uint32_t)drum->size(), true);
for (uint32_t i = 0; i < drum->size(); ++i) {
sample.push_back(drum->at(i));
}
}
else {
sample_header(sample, 0, true);
}
samples.push_back(std::move(sample));
}
return samples;
}
std::vector<std::vector<uint8_t>> IT_Module::get_patterns(
const std::vector<Note_View> &channel_1_notes,
const std::vector<Note_View> &channel_2_notes,
const std::vector<Note_View> &channel_3_notes,
const std::vector<Note_View> &channel_4_notes,
const std::vector<Drumkit> &drumkits,
int32_t loop_tick,
bool stereo,
int32_t num_inline_waves
) {
const uint8_t CHANNEL = 0x80;
const uint8_t CH1 = 1;
const uint8_t CH2 = 2;
const uint8_t CH3 = 3;
const uint8_t CH4 = 4;
const uint8_t CH5 = 5;
const uint8_t CH6 = 6;
const uint8_t CH7 = 7;
const uint8_t CH8 = 8;
const uint8_t CH9 = 9;
const uint8_t CH10 = 10;
const uint8_t NOTE = 1;
const uint8_t SAMPLE = 2;
const uint8_t VOLUME = 4;
const uint8_t COMMAND = 8;
const uint8_t PATTERN_JUMP = 0x02;
const uint8_t ROW_JUMP = 0x03;
const uint8_t FADE = 0x04;
const uint8_t PITCH_SLIDE = 0x07;
const uint8_t VIBRATO = 0x08;
const uint8_t FADE_VIBRATO = 0x0b;
const uint8_t FADE_PITCH_SLIDE = 0x0c;
const uint8_t TEMPO = 0x14;
const uint8_t STEREO_PANNING = 0x18;
const uint8_t EXTENSION = 0x1b;
const uint8_t CUT = 0xfe;
const uint8_t FINELY = 0xf;
std::vector<std::vector<uint8_t>> patterns;
auto channel_1_itr = channel_1_notes.begin();
auto channel_2_itr = channel_2_notes.begin();
auto channel_3_itr = channel_3_notes.begin();
auto channel_4_itr = channel_4_notes.begin();
int32_t channel_1_note_length = 0;
int32_t channel_2_note_length = 0;
int32_t channel_3_note_length = 0;
int32_t channel_4_note_length = 0;
int32_t channel_1_note_duration = 0;
int32_t channel_2_note_duration = 0;
int32_t channel_3_note_duration = 0;
Note_View channel_1_prev_note;
Note_View channel_2_prev_note;
Note_View channel_3_prev_note;
Note_View channel_4_prev_note;
int32_t global_tempo = 256;
int32_t channel_1_tempo = 0;
int32_t channel_2_tempo = 0;
int32_t channel_3_tempo = 0;
int32_t channel_4_tempo = 0;
int32_t wave = 0;
int32_t first_channel =
channel_1_notes.size() > 0 ? 1 :
channel_2_notes.size() > 0 ? 2 :
channel_3_notes.size() > 0 ? 3 :
4;
auto song_finished = [&]() {
return
channel_1_itr == channel_1_notes.end() &&
channel_2_itr == channel_2_notes.end() &&
channel_3_itr == channel_3_notes.end() &&
channel_4_itr == channel_4_notes.end() &&
channel_1_note_length == 0 &&
channel_2_note_length == 0 &&
channel_3_note_length == 0 &&
channel_4_note_length == 0;
};
auto note = [](const Note_View &view) {
return (uint8_t)(
((std::max(view.octave - view.transpose_octaves, 1) + view.transpose_pitches / 12) * 12 +
((int32_t)view.pitch - 1 + view.transpose_pitches % 12)) % (10 * 12)
);
};
auto channel_3_volume = [](int32_t volume) {
return (uint8_t)(
volume == 1 ? 64 :
volume == 2 ? 32 :
volume == 3 ? 16 :
0
);
};
auto convert_tempo = [](int32_t tempo) {
int32_t bpm = (int32_t)(2.0f * UNITS_PER_MINUTE / 48.0f / tempo + 0.5f);
return std::max(bpm, 32);
};
auto convert_fade_period = [](int32_t tempo, int32_t fade) {
return std::abs(fade) + std::max(192 - tempo, 0) / 32;
};
auto convert_vibrato_delay = [](int32_t tempo, int32_t speed, int32_t delay) {
return (int32_t)(delay * speed / std::pow(tempo, 0.35));
};
auto convert_vibrato_rate = [](int32_t tempo, int32_t rate) {
const int32_t v = 6 + tempo / 60;
return std::min(v - std::min(rate, v - 1), 15);
};
auto get_stereo_panning = [](bool left, bool right) {
if (left && right) return 0x80;
if (left && !right) return 0x00;
if (!left && right) return 0xFF;
return 0x80;
};
do {
std::vector<uint8_t> pattern;
std::vector<uint8_t> pattern_data;
uint32_t row = 0;
do {
if (channel_1_tempo != 0) {
pattern_data.push_back(CHANNEL + CH5);
pattern_data.push_back(COMMAND);
pattern_data.push_back(EXTENSION);
pattern_data.push_back(channel_1_tempo % 256);
channel_1_tempo = 0;
}
if (channel_2_tempo != 0) {
pattern_data.push_back(CHANNEL + CH6);
pattern_data.push_back(COMMAND);
pattern_data.push_back(EXTENSION);
pattern_data.push_back(channel_2_tempo % 256);
channel_2_tempo = 0;
}
if (channel_3_tempo != 0) {
pattern_data.push_back(CHANNEL + CH7);
pattern_data.push_back(COMMAND);
pattern_data.push_back(EXTENSION);
pattern_data.push_back(channel_3_tempo % 256);
channel_3_tempo = 0;
}
if (channel_4_tempo != 0) {
pattern_data.push_back(CHANNEL + CH8);
pattern_data.push_back(COMMAND);
pattern_data.push_back(EXTENSION);
pattern_data.push_back(channel_4_tempo % 256);
channel_4_tempo = 0;
}
if (channel_1_note_length == 0 && channel_1_itr != channel_1_notes.end()) {
channel_1_note_length = channel_1_itr->length * channel_1_itr->speed - 1;
channel_1_note_duration = 0;
if (channel_1_itr->tempo != channel_1_prev_note.tempo) {
global_tempo = channel_1_itr->tempo;
channel_1_tempo = convert_tempo(channel_1_itr->tempo);
pattern_data.push_back(CHANNEL + CH5);
pattern_data.push_back(COMMAND);
pattern_data.push_back(TEMPO);
pattern_data.push_back(channel_1_tempo / 256);
if (_tempo_change_mid_note == -1) {
if (channel_2_note_length > 0) {
_tempo_change_mid_note = 2;
}
else if (channel_3_note_length > 0) {
_tempo_change_mid_note = 3;
}
else if (channel_4_note_length > 0) {
_tempo_change_mid_note = 4;
}
}
}
if (channel_1_itr->pitch != Pitch::REST) {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(NOTE + SAMPLE + VOLUME);
pattern_data.push_back(note(*channel_1_itr)); // note
pattern_data.push_back(channel_1_itr->duty + 1); // sample
pattern_data.push_back((channel_1_itr->volume + 1) * 4); // volume
}
else {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (
stereo &&
((channel_1_itr->panning_left != channel_1_prev_note.panning_left) ||
(channel_1_itr->panning_right != channel_1_prev_note.panning_right))
) {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(COMMAND);
pattern_data.push_back(STEREO_PANNING);
pattern_data.push_back(get_stereo_panning(channel_1_itr->panning_left, channel_1_itr->panning_right));
}
channel_1_prev_note = *channel_1_itr;
++channel_1_itr;
}
else if (channel_1_note_length > 0) {
uint32_t volume_fade_period = convert_fade_period(global_tempo, channel_1_prev_note.fade);
if (channel_1_prev_note.fade && row % volume_fade_period == 0) {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(COMMAND);
if (channel_1_prev_note.slide_pitch != Pitch::REST) {
pattern_data.push_back(FADE_PITCH_SLIDE);
}
else if (
channel_1_prev_note.vibrato_extent &&
channel_1_note_duration > convert_vibrato_delay(global_tempo, channel_1_prev_note.speed, channel_1_prev_note.vibrato_delay)
) {
pattern_data.push_back(FADE_VIBRATO);
}
else {
pattern_data.push_back(FADE);
}
if (channel_1_prev_note.fade > 0) {
pattern_data.push_back((FINELY << 4) | 4);
}
else {
pattern_data.push_back((4 << 4) | FINELY);
}
}
else if (channel_1_prev_note.slide_pitch != Pitch::REST) {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(NOTE + COMMAND);
pattern_data.push_back(channel_1_prev_note.slide_octave * 12 + (uint32_t)channel_1_prev_note.slide_pitch - 1);
pattern_data.push_back(PITCH_SLIDE);
pattern_data.push_back(channel_1_prev_note.slide_duration * 6);
}
else if (
channel_1_prev_note.vibrato_extent &&
channel_1_note_duration >= convert_vibrato_delay(global_tempo, channel_1_prev_note.speed, channel_1_prev_note.vibrato_delay)
) {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(COMMAND);
pattern_data.push_back(VIBRATO);
pattern_data.push_back(convert_vibrato_rate(global_tempo, channel_1_prev_note.vibrato_rate) << 4 | (channel_1_prev_note.vibrato_extent));
}
channel_1_note_length -= 1;
channel_1_note_duration += 1;
}
else {
pattern_data.push_back(CHANNEL + CH1);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (channel_2_note_length == 0 && channel_2_itr != channel_2_notes.end()) {
channel_2_note_length = channel_2_itr->length * channel_2_itr->speed - 1;
channel_2_note_duration = 0;
if (channel_2_itr->tempo != channel_2_prev_note.tempo) {
global_tempo = channel_2_itr->tempo;
channel_2_tempo = convert_tempo(channel_2_itr->tempo);
pattern_data.push_back(CHANNEL + CH6);
pattern_data.push_back(COMMAND);
pattern_data.push_back(TEMPO);
pattern_data.push_back(channel_2_tempo / 256);
if (first_channel != 2) {
_tempo_change_wrong_channel = 2;
}
if (_tempo_change_mid_note == -1) {
if (channel_1_note_length > 0) {
_tempo_change_mid_note = 1;
}
else if (channel_3_note_length > 0) {
_tempo_change_mid_note = 3;
}
else if (channel_4_note_length > 0) {
_tempo_change_mid_note = 4;
}
}
}
if (channel_2_itr->pitch != Pitch::REST) {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(NOTE + SAMPLE + VOLUME);
pattern_data.push_back(note(*channel_2_itr)); // note
pattern_data.push_back(channel_2_itr->duty + 1); // sample
pattern_data.push_back((channel_2_itr->volume + 1) * 4); // volume
}
else {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (
stereo &&
((channel_2_itr->panning_left != channel_2_prev_note.panning_left) ||
(channel_2_itr->panning_right != channel_2_prev_note.panning_right))
) {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(COMMAND);
pattern_data.push_back(STEREO_PANNING);
pattern_data.push_back(get_stereo_panning(channel_2_itr->panning_left, channel_2_itr->panning_right));
}
channel_2_prev_note = *channel_2_itr;
++channel_2_itr;
}
else if (channel_2_note_length > 0) {
uint32_t volume_fade_period = convert_fade_period(global_tempo, channel_2_prev_note.fade);
if (channel_2_prev_note.fade && row % volume_fade_period == 0) {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(COMMAND);
if (channel_2_prev_note.slide_pitch != Pitch::REST) {
pattern_data.push_back(FADE_PITCH_SLIDE);
}
else if (
channel_2_prev_note.vibrato_extent &&
channel_2_note_duration > convert_vibrato_delay(global_tempo, channel_2_prev_note.speed, channel_2_prev_note.vibrato_delay)
) {
pattern_data.push_back(FADE_VIBRATO);
}
else {
pattern_data.push_back(FADE);
}
if (channel_2_prev_note.fade > 0) {
pattern_data.push_back((FINELY << 4) | 4);
}
else {
pattern_data.push_back((4 << 4) | FINELY);
}
}
else if (channel_2_prev_note.slide_pitch != Pitch::REST) {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(NOTE + COMMAND);
pattern_data.push_back(channel_2_prev_note.slide_octave * 12 + (uint32_t)channel_2_prev_note.slide_pitch - 1);
pattern_data.push_back(PITCH_SLIDE);
pattern_data.push_back(channel_2_prev_note.slide_duration * 6);
}
else if (
channel_2_prev_note.vibrato_extent &&
channel_2_note_duration >= convert_vibrato_delay(global_tempo, channel_2_prev_note.speed, channel_2_prev_note.vibrato_delay)
) {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(COMMAND);
pattern_data.push_back(VIBRATO);
pattern_data.push_back(convert_vibrato_rate(global_tempo, channel_2_prev_note.vibrato_rate) << 4 | (channel_2_prev_note.vibrato_extent));
}
channel_2_note_length -= 1;
channel_2_note_duration += 1;
}
else {
pattern_data.push_back(CHANNEL + CH2);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (channel_3_note_length == 0 && channel_3_itr != channel_3_notes.end()) {
channel_3_note_length = channel_3_itr->length * channel_3_itr->speed - 1;
channel_3_note_duration = 0;
if (channel_3_itr->wave != 0x0f || !num_inline_waves) {
wave = channel_3_itr->wave;
}
if (channel_3_itr->tempo != channel_3_prev_note.tempo) {
global_tempo = channel_3_itr->tempo;
channel_3_tempo = convert_tempo(channel_3_itr->tempo);
pattern_data.push_back(CHANNEL + CH7);
pattern_data.push_back(COMMAND);
pattern_data.push_back(TEMPO);
pattern_data.push_back(channel_3_tempo / 256);
if (first_channel != 3) {
_tempo_change_wrong_channel = 3;
}
if (_tempo_change_mid_note == -1) {
if (channel_1_note_length > 0) {
_tempo_change_mid_note = 1;
}
else if (channel_2_note_length > 0) {
_tempo_change_mid_note = 2;
}
else if (channel_4_note_length > 0) {
_tempo_change_mid_note = 4;
}
}
}
if (channel_3_itr->pitch != Pitch::REST && wave < 16 + 15) {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(NOTE + SAMPLE + VOLUME);
pattern_data.push_back(note(*channel_3_itr)); // note
pattern_data.push_back(wave + 1 + 4); // sample
pattern_data.push_back(channel_3_volume(channel_3_itr->volume)); // volume
}
else {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (
stereo &&
((channel_3_itr->panning_left != channel_3_prev_note.panning_left) ||
(channel_3_itr->panning_right != channel_3_prev_note.panning_right))
) {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(COMMAND);
pattern_data.push_back(STEREO_PANNING);
pattern_data.push_back(get_stereo_panning(channel_3_itr->panning_left, channel_3_itr->panning_right));
}
channel_3_prev_note = *channel_3_itr;
++channel_3_itr;
}
else if (channel_3_note_length > 0) {
if (channel_3_prev_note.slide_pitch != Pitch::REST) {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(NOTE + COMMAND);
pattern_data.push_back(channel_3_prev_note.slide_octave * 12 + (uint32_t)channel_3_prev_note.slide_pitch - 1);
pattern_data.push_back(PITCH_SLIDE);
pattern_data.push_back(channel_3_prev_note.slide_duration * 6);
}
else if (
channel_3_prev_note.vibrato_extent &&
channel_3_note_duration >= convert_vibrato_delay(global_tempo, channel_3_prev_note.speed, channel_3_prev_note.vibrato_delay)
) {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(COMMAND);
pattern_data.push_back(VIBRATO);
pattern_data.push_back(convert_vibrato_rate(global_tempo, channel_3_prev_note.vibrato_rate) << 4 | (channel_3_prev_note.vibrato_extent));
}
channel_3_note_length -= 1;
channel_3_note_duration += 1;
}
else {
pattern_data.push_back(CHANNEL + CH3);
pattern_data.push_back(NOTE);
pattern_data.push_back(CUT);
}
if (channel_4_note_length == 0 && channel_4_itr != channel_4_notes.end()) {
channel_4_note_length = channel_4_itr->length * channel_4_itr->speed - 1;
if (channel_4_itr->tempo != channel_4_prev_note.tempo) {
global_tempo = channel_4_itr->tempo;
channel_4_tempo = convert_tempo(channel_4_itr->tempo);
pattern_data.push_back(CHANNEL + CH8);
pattern_data.push_back(COMMAND);
pattern_data.push_back(TEMPO);
pattern_data.push_back(channel_4_tempo / 256);
if (first_channel != 4) {
_tempo_change_wrong_channel = 4;
}
if (_tempo_change_mid_note == -1) {
if (channel_1_note_length > 0) {
_tempo_change_mid_note = 1;
}
else if (channel_2_note_length > 0) {
_tempo_change_mid_note = 2;
}
else if (channel_3_note_length > 0) {
_tempo_change_mid_note = 3;
}
}
}
if (
channel_4_itr->pitch != Pitch::REST &&
channel_4_itr->drumkit != -1 &&
channel_4_itr->drumkit < (int32_t)drumkits.size() &&
drumkits[channel_4_itr->drumkit].drums[(int32_t)channel_4_itr->pitch] != -1 &&
drumkits[channel_4_itr->drumkit].drums[(int32_t)channel_4_itr->pitch] < 64
) {
pattern_data.push_back(CHANNEL + CH4);
pattern_data.push_back(NOTE + SAMPLE + VOLUME);
pattern_data.push_back(60); // note
pattern_data.push_back(drumkits[channel_4_itr->drumkit].drums[(int32_t)channel_4_itr->pitch] + 1 + 4 + 16 + num_inline_waves); // sample
pattern_data.push_back(64); // volume
}
if (
stereo &&
((channel_4_itr->panning_left != channel_4_prev_note.panning_left) ||
(channel_4_itr->panning_right != channel_4_prev_note.panning_right))
) {
pattern_data.push_back(CHANNEL + CH4);
pattern_data.push_back(COMMAND);
pattern_data.push_back(STEREO_PANNING);
pattern_data.push_back(get_stereo_panning(channel_4_itr->panning_left, channel_4_itr->panning_right));
}
channel_4_prev_note = *channel_4_itr;
++channel_4_itr;
}
else if (channel_4_note_length > 0) {
channel_4_note_length -= 1;
}
if (song_finished() && loop_tick != -1) {
uint32_t pattern_number = (uint32_t)(loop_tick) / ROWS_PER_PATTERN;
uint32_t row_number = (uint32_t)(loop_tick) % ROWS_PER_PATTERN;
pattern_data.push_back(CHANNEL + CH9);
pattern_data.push_back(COMMAND);
pattern_data.push_back(PATTERN_JUMP);
pattern_data.push_back(pattern_number);
pattern_data.push_back(CHANNEL + CH10);
pattern_data.push_back(COMMAND);
pattern_data.push_back(ROW_JUMP);
pattern_data.push_back(row_number);
}
pattern_data.push_back(0);
row += 1;
} while (row < ROWS_PER_PATTERN && !song_finished());
put_short(pattern, (uint32_t)pattern_data.size());
put_short(pattern, row);
put_short(pattern, 0); // unused
put_short(pattern, 0); // unused
pattern.insert(pattern.end(), pattern_data.begin(), pattern_data.end());
patterns.push_back(std::move(pattern));
} while (!song_finished());
return patterns;
}
static uint32_t get_total_size(const std::vector<std::vector<uint8_t>> &data) {
std::size_t size = 0;
for (const auto &v : data) {
size += v.size();
}
return (uint32_t)size;
}
void IT_Module::generate_it_module(
const std::vector<Note_View> &channel_1_notes,
const std::vector<Note_View> &channel_2_notes,
const std::vector<Note_View> &channel_3_notes,
const std::vector<Note_View> &channel_4_notes,
const std::vector<Wave> &waves,
const std::vector<Drumkit> &drumkits,
const std::vector<std::vector<uint8_t>> &drums,
int32_t preserve_drumkit,
int32_t loop_tick,
bool stereo
) {
const uint32_t song_name_length = 26;
const uint32_t pattern_row_highlight = 0x1004; // ???
const uint32_t tracker_version = 0x5130;
const uint32_t compatible_version = 0x0214;
const uint32_t flags = 0b01001001;
const uint32_t special = 0b00000110;
const uint32_t global_volume = 128;
const uint32_t mix_volume = 48;
const uint32_t initial_speed = 1;
const uint32_t initial_tempo = 150;
const uint32_t panning_separation = 128;
const uint32_t pitch_wheel_depth = 0;
const uint32_t default_channel_panning = 32;
const uint32_t channel_disabled = 0b10000000;
const uint32_t max_num_channels = 64;
const uint32_t default_channel_volume = 64;
const uint32_t sample_header_size = 80;
_too_many_drums = false;
std::vector<Drumkit> optimized_drumkits = std::vector<Drumkit>(drumkits.size());
for (Drumkit &drumkit : optimized_drumkits) {
for (uint32_t i = 0; i < NUM_DRUMS_PER_DRUMKIT; ++i) {
drumkit.drums[i] = -1;
}
}
std::vector<const std::vector<uint8_t> *> optimized_drums;
if (preserve_drumkit == -1) {
for (const Note_View ¬e : channel_4_notes) {
if (
note.pitch != Pitch::REST &&
note.drumkit != -1 &&
note.drumkit < (int32_t)optimized_drumkits.size() &&
optimized_drumkits[note.drumkit].drums[(int32_t)note.pitch] == -1
) {
if (optimized_drums.size() >= 64) {
_too_many_drums = true;
break;
}
int32_t drum_index = drumkits[note.drumkit].drums[(int32_t)note.pitch];
for (uint32_t j = 0; j < drumkits.size(); ++j) {
for (uint32_t i = 0; i < NUM_DRUMS_PER_DRUMKIT; ++i) {
if (drumkits[j].drums[i] == drum_index) {
optimized_drumkits[j].drums[i] = (int32_t)optimized_drums.size();
}
}
}
optimized_drums.push_back(&drums[drum_index]);
}
}
}
else {
if (preserve_drumkit < (int32_t)drumkits.size()) {
for (uint32_t i = 0; i < NUM_DRUMS_PER_DRUMKIT; ++i) {
int32_t drum_index = drumkits[preserve_drumkit].drums[i];
optimized_drums.push_back(&drums[drum_index]);
}
}
optimized_drums.resize(NUM_DRUMS_PER_DRUMKIT);
}
std::vector<std::vector<uint8_t>> instruments = get_instruments();
std::vector<std::vector<uint8_t>> samples = get_samples(waves, optimized_drums);
std::vector<std::vector<uint8_t>> patterns = get_patterns(channel_1_notes, channel_2_notes, channel_3_notes, channel_4_notes, optimized_drumkits, loop_tick, stereo, (int32_t)waves.size() - 0x10);
const uint32_t number_of_orders = (uint32_t)patterns.size() + 1;
const uint32_t number_of_instruments = (uint32_t)instruments.size();
const uint32_t number_of_samples = (uint32_t)samples.size();
const uint32_t number_of_patterns = (uint32_t)patterns.size();