-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmplayer.c
More file actions
1823 lines (1493 loc) · 56.9 KB
/
Copy pathxmplayer.c
File metadata and controls
1823 lines (1493 loc) · 56.9 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
// THIS PLAYER IS ESSENTIALLY https://github.com/Artefact2/libxm/
// PLEASE FORK THE ORIGINAL INSTEAD OF MINE, IT'S CLEANER AND LIKELY MORE UP TO DATE.
//
// HUGE THANKS TO ARTEFACT2 FOR CREATING SUCH A GREAT PLAYER
#ifdef DEBUG
#include "includes.c"
#endif
void memcpy_pad(void* dst, size_t dst_len, const void* src, size_t src_len, size_t offset) {
uint8_t* dst_c = dst;
const uint8_t* src_c = src;
size_t copy_bytes = (src_len >= offset) ? (src_len - offset) : 0;
copy_bytes = copy_bytes > dst_len ? dst_len : copy_bytes;
memcpy(dst_c, src_c + offset, copy_bytes);
memset(dst_c + copy_bytes, 0, dst_len - copy_bytes);
}
// ----- XM constants -----
#define SAMPLE_NAME_LENGTH 22
#define INSTRUMENT_NAME_LENGTH 22
#define MODULE_NAME_LENGTH 20
#define TRACKER_NAME_LENGTH 20
#define PATTERN_ORDER_TABLE_LENGTH 256
#define NUM_NOTES 96
#define NUM_ENVELOPE_POINTS 12
#define MAX_NUM_ROWS 256
#define XM_SAMPLE_RAMPING_POINTS 0x20
#define READ_U8(offset) (((offset) < moddata_length) ? (*(uint8_t*)(moddata + (offset))) : 0)
#define READ_U16(offset) ((uint16_t)READ_U8(offset) | ((uint16_t)READ_U8((offset) + 1) << 8))
#define READ_U32(offset) ((uint32_t)READ_U16(offset) | ((uint32_t)READ_U16((offset) + 2) << 16))
#define READ_MEMCPY(ptr, offset, length) memcpy_pad(ptr, length, moddata, moddata_length, offset)
int XM_LINEAR_INTERPOLATION=1;
// ----- Data types -----
enum xm_waveform_type_e {
XM_SINE_WAVEFORM = 0,
XM_RAMP_DOWN_WAVEFORM = 1,
XM_SQUARE_WAVEFORM = 2,
XM_RANDOM_WAVEFORM = 3,
XM_RAMP_UP_WAVEFORM = 4,
};
typedef enum xm_waveform_type_e xm_waveform_type_t;
enum xm_loop_type_e {
XM_NO_LOOP,
XM_FORWARD_LOOP,
XM_PING_PONG_LOOP,
};
typedef enum xm_loop_type_e xm_loop_type_t;
enum xm_frequency_type_e {
XM_LINEAR_FREQUENCIES,
XM_AMIGA_FREQUENCIES,
};
typedef enum xm_frequency_type_e xm_frequency_type_t;
struct xm_envelope_point_s {
uint16_t frame;
uint16_t value;
};
typedef struct xm_envelope_point_s xm_envelope_point_t;
struct xm_envelope_s {
xm_envelope_point_t points[NUM_ENVELOPE_POINTS];
uint8_t num_points;
uint8_t sustain_point;
uint8_t loop_start_point;
uint8_t loop_end_point;
bool enabled;
bool sustain_enabled;
bool loop_enabled;
};
typedef struct xm_envelope_s xm_envelope_t;
struct xm_sample_s {
uint8_t bits;
uint32_t length;
uint32_t loop_start;
uint32_t loop_length;
uint32_t loop_end;
float volume;
int8_t finetune;
xm_loop_type_t loop_type;
float panning;
int8_t relative_note;
uint64_t latest_trigger;
union {
int8_t* data8;
int16_t* data16;
};
};
typedef struct xm_sample_s xm_sample_t;
struct xm_instrument_s {
uint16_t num_samples;
uint8_t sample_of_notes[NUM_NOTES];
xm_envelope_t volume_envelope;
xm_envelope_t panning_envelope;
xm_waveform_type_t vibrato_type;
uint8_t vibrato_sweep;
uint8_t vibrato_depth;
uint8_t vibrato_rate;
uint16_t volume_fadeout;
uint64_t latest_trigger;
bool muted;
xm_sample_t* samples;
};
typedef struct xm_instrument_s xm_instrument_t;
struct xm_pattern_slot_s {
uint8_t note;
uint8_t instrument;
uint8_t volume_column;
uint8_t effect_type;
uint8_t effect_param;
};
typedef struct xm_pattern_slot_s xm_pattern_slot_t;
struct xm_pattern_s {
uint16_t num_rows;
xm_pattern_slot_t* slots;
};
typedef struct xm_pattern_s xm_pattern_t;
struct xm_module_s {
uint16_t length;
uint16_t restart_position;
uint16_t num_channels;
uint16_t num_patterns;
uint16_t num_instruments;
xm_frequency_type_t frequency_type;
uint8_t pattern_table[PATTERN_ORDER_TABLE_LENGTH];
xm_pattern_t* patterns;
xm_instrument_t* instruments;
};
typedef struct xm_module_s xm_module_t;
struct xm_channel_context_s {
float note;
float orig_note;
xm_instrument_t* instrument;
xm_sample_t* sample;
xm_pattern_slot_t* current;
float sample_position;
float period;
float frequency;
float step;
bool ping;
float volume;
float panning;
uint16_t autovibrato_ticks;
bool sustained;
float fadeout_volume;
float volume_envelope_volume;
float panning_envelope_panning;
uint16_t volume_envelope_frame_count;
uint16_t panning_envelope_frame_count;
float autovibrato_note_offset;
bool arp_in_progress;
uint8_t arp_note_offset;
uint8_t volume_slide_param;
uint8_t fine_volume_slide_param;
uint8_t global_volume_slide_param;
uint8_t panning_slide_param;
uint8_t portamento_up_param;
uint8_t portamento_down_param;
uint8_t fine_portamento_up_param;
uint8_t fine_portamento_down_param;
uint8_t extra_fine_portamento_up_param;
uint8_t extra_fine_portamento_down_param;
uint8_t tone_portamento_param;
float tone_portamento_target_period;
uint8_t multi_retrig_param;
uint8_t note_delay_param;
uint8_t pattern_loop_origin;
uint8_t pattern_loop_count;
bool vibrato_in_progress;
xm_waveform_type_t vibrato_waveform;
bool vibrato_waveform_retrigger;
uint8_t vibrato_param;
uint16_t vibrato_ticks;
float vibrato_note_offset;
xm_waveform_type_t tremolo_waveform;
bool tremolo_waveform_retrigger;
uint8_t tremolo_param;
uint8_t tremolo_ticks;
float tremolo_volume;
uint8_t tremor_param;
bool tremor_on;
uint64_t latest_trigger;
bool muted;
float target_panning;
float target_volume;
unsigned long frame_count;
float end_of_previous_sample[XM_SAMPLE_RAMPING_POINTS];
float actual_panning;
float actual_volume;
};
typedef struct xm_channel_context_s xm_channel_context_t;
struct xm_context_s {
size_t ctx_size;
xm_module_t module;
uint32_t rate;
uint16_t tempo;
uint16_t bpm;
float global_volume;
float amplification;
float volume_ramp;
float panning_ramp;
uint8_t current_table_index;
uint8_t current_row;
uint16_t current_tick;
float remaining_samples_in_tick;
uint64_t generated_samples;
bool position_jump;
bool pattern_break;
uint8_t jump_dest;
uint8_t jump_row;
uint16_t extra_ticks;
uint8_t* row_loop_count;
uint8_t loop_count;
uint8_t max_loop_count;
xm_channel_context_t* channels;
};
typedef struct xm_context_s xm_context_t;
size_t xm_get_memory_needed_for_context(const char* moddata, size_t moddata_length) {
size_t memory_needed = 0;
size_t offset = 60;
uint16_t num_channels;
uint16_t num_patterns;
uint16_t num_instruments;
num_channels = READ_U16(offset + 8);
num_channels = READ_U16(offset + 8);
num_patterns = READ_U16(offset + 10);
memory_needed += num_patterns * sizeof(xm_pattern_t);
num_instruments = READ_U16(offset + 12);
memory_needed += num_instruments * sizeof(xm_instrument_t);
memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) * sizeof(uint8_t);
offset += READ_U32(offset);
for(uint16_t i = 0; i < num_patterns; ++i) {
uint16_t num_rows;
num_rows = READ_U16(offset + 5);
memory_needed += num_rows * num_channels * sizeof(xm_pattern_slot_t);
offset += READ_U32(offset) + READ_U16(offset + 7);
}
for(uint16_t i = 0; i < num_instruments; ++i) {
uint16_t num_samples;
uint32_t sample_header_size = 0;
uint32_t sample_size_aggregate = 0;
num_samples = READ_U16(offset + 27);
memory_needed += num_samples * sizeof(xm_sample_t);
if(num_samples > 0) {
sample_header_size = READ_U32(offset + 29);
}
offset += READ_U32(offset);
for(uint16_t j = 0; j < num_samples; ++j) {
uint32_t sample_size;
sample_size = READ_U32(offset);
sample_size_aggregate += sample_size;
memory_needed += sample_size;
offset += sample_header_size;
}
offset += sample_size_aggregate;
}
memory_needed += num_channels * sizeof(xm_channel_context_t);
memory_needed += sizeof(xm_context_t);
return memory_needed;
}
char* xm_load_module(xm_context_t* ctx, const char* moddata, size_t moddata_length, char* mempool) {
size_t offset = 0;
xm_module_t* mod = &(ctx->module);
offset += 60;
uint32_t header_size = READ_U32(offset);
mod->length = READ_U16(offset + 4);
mod->restart_position = READ_U16(offset + 6);
mod->num_channels = READ_U16(offset + 8);
mod->num_patterns = READ_U16(offset + 10);
mod->num_instruments = READ_U16(offset + 12);
mod->patterns = (xm_pattern_t*)mempool;
mempool += mod->num_patterns * sizeof(xm_pattern_t);
mod->instruments = (xm_instrument_t*)mempool;
mempool += mod->num_instruments * sizeof(xm_instrument_t);
uint16_t flags = READ_U32(offset + 14);
mod->frequency_type = (flags & (1 << 0)) ? XM_LINEAR_FREQUENCIES : XM_AMIGA_FREQUENCIES;
ctx->tempo = READ_U16(offset + 16);
ctx->bpm = READ_U16(offset + 18);
READ_MEMCPY(mod->pattern_table, offset + 20, PATTERN_ORDER_TABLE_LENGTH);
offset += header_size;
for(uint16_t i = 0; i < mod->num_patterns; ++i) {
uint16_t packed_patterndata_size = READ_U16(offset + 7);
xm_pattern_t* pat = mod->patterns + i;
pat->num_rows = READ_U16(offset + 5);
pat->slots = (xm_pattern_slot_t*)mempool;
mempool += mod->num_channels * pat->num_rows * sizeof(xm_pattern_slot_t);
offset += READ_U32(offset);
if(packed_patterndata_size == 0) {
memset(pat->slots, 0, sizeof(xm_pattern_slot_t) * pat->num_rows * mod->num_channels);
} else {
for(uint16_t j = 0, k = 0; j < packed_patterndata_size; ++k) {
uint8_t note = READ_U8(offset + j);
xm_pattern_slot_t* slot = pat->slots + k;
if(note & (1 << 7)) {
++j;
if(note & (1 << 0)) {
slot->note = READ_U8(offset + j);
++j;
} else {
slot->note = 0;
}
if(note & (1 << 1)) {
slot->instrument = READ_U8(offset + j);
++j;
} else {
slot->instrument = 0;
}
if(note & (1 << 2)) {
slot->volume_column = READ_U8(offset + j);
++j;
} else {
slot->volume_column = 0;
}
if(note & (1 << 3)) {
slot->effect_type = READ_U8(offset + j);
++j;
} else {
slot->effect_type = 0;
}
if(note & (1 << 4)) {
slot->effect_param = READ_U8(offset + j);
++j;
} else {
slot->effect_param = 0;
}
} else {
slot->note = note;
slot->instrument = READ_U8(offset + j + 1);
slot->volume_column = READ_U8(offset + j + 2);
slot->effect_type = READ_U8(offset + j + 3);
slot->effect_param = READ_U8(offset + j + 4);
j += 5;
}
}
}
offset += packed_patterndata_size;
}
for(uint16_t i = 0; i < ctx->module.num_instruments; ++i) {
uint32_t sample_header_size = 0;
xm_instrument_t* instr = mod->instruments + i;
instr->num_samples = READ_U16(offset + 27);
if(instr->num_samples > 0) {
sample_header_size = READ_U32(offset + 29);
READ_MEMCPY(instr->sample_of_notes, offset + 33, NUM_NOTES);
instr->volume_envelope.num_points = READ_U8(offset + 225);
instr->panning_envelope.num_points = READ_U8(offset + 226);
for(uint8_t j = 0; j < instr->volume_envelope.num_points; ++j) {
instr->volume_envelope.points[j].frame = READ_U16(offset + 129 + 4 * j);
instr->volume_envelope.points[j].value = READ_U16(offset + 129 + 4 * j + 2);
}
for(uint8_t j = 0; j < instr->panning_envelope.num_points; ++j) {
instr->panning_envelope.points[j].frame = READ_U16(offset + 177 + 4 * j);
instr->panning_envelope.points[j].value = READ_U16(offset + 177 + 4 * j + 2);
}
instr->volume_envelope.sustain_point = READ_U8(offset + 227);
instr->volume_envelope.loop_start_point = READ_U8(offset + 228);
instr->volume_envelope.loop_end_point = READ_U8(offset + 229);
instr->panning_envelope.sustain_point = READ_U8(offset + 230);
instr->panning_envelope.loop_start_point = READ_U8(offset + 231);
instr->panning_envelope.loop_end_point = READ_U8(offset + 232);
uint8_t flags = READ_U8(offset + 233);
instr->volume_envelope.enabled = flags & (1 << 0);
instr->volume_envelope.sustain_enabled = flags & (1 << 1);
instr->volume_envelope.loop_enabled = flags & (1 << 2);
flags = READ_U8(offset + 234);
instr->panning_envelope.enabled = flags & (1 << 0);
instr->panning_envelope.sustain_enabled = flags & (1 << 1);
instr->panning_envelope.loop_enabled = flags & (1 << 2);
instr->vibrato_type = READ_U8(offset + 235);
if(instr->vibrato_type == 2) {
instr->vibrato_type = 1;
} else if(instr->vibrato_type == 1) {
instr->vibrato_type = 2;
}
instr->vibrato_sweep = READ_U8(offset + 236);
instr->vibrato_depth = READ_U8(offset + 237);
instr->vibrato_rate = READ_U8(offset + 238);
instr->volume_fadeout = READ_U16(offset + 239);
instr->samples = (xm_sample_t*)mempool;
mempool += instr->num_samples * sizeof(xm_sample_t);
} else {
instr->samples = NULL;
}
offset += READ_U32(offset);
for(uint16_t j = 0; j < instr->num_samples; ++j) {
xm_sample_t* sample = instr->samples + j;
sample->length = READ_U32(offset);
sample->loop_start = READ_U32(offset + 4);
sample->loop_length = READ_U32(offset + 8);
sample->loop_end = sample->loop_start + sample->loop_length;
sample->volume = (float)READ_U8(offset + 12) / (float)0x40;
sample->finetune = (int8_t)READ_U8(offset + 13);
uint8_t flags = READ_U8(offset + 14);
if((flags & 3) == 0) {
sample->loop_type = XM_NO_LOOP;
} else if((flags & 3) == 1) {
sample->loop_type = XM_FORWARD_LOOP;
} else {
sample->loop_type = XM_PING_PONG_LOOP;
}
sample->bits = (flags & (1 << 4)) ? 16 : 8;
sample->panning = (float)READ_U8(offset + 15) / (float)0xFF;
sample->relative_note = (int8_t)READ_U8(offset + 16);
sample->data8 = (int8_t*)mempool;
mempool += sample->length;
if(sample->bits == 16) {
sample->loop_start >>= 1;
sample->loop_length >>= 1;
sample->loop_end >>= 1;
sample->length >>= 1;
}
offset += sample_header_size;
}
for(uint16_t j = 0; j < instr->num_samples; ++j) {
xm_sample_t* sample = instr->samples + j;
uint32_t length = sample->length;
if(sample->bits == 16) {
int16_t v = 0;
for(uint32_t k = 0; k < length; ++k) {
v = v + (int16_t)READ_U16(offset + (k << 1));
sample->data16[k] = v;
}
offset += sample->length << 1;
} else {
int8_t v = 0;
for(uint32_t k = 0; k < length; ++k) {
v = v + (int8_t)READ_U8(offset + k);
sample->data8[k] = v;
}
offset += sample->length;
}
}
}
return mempool;
}
int xm_create_context_safe(xm_context_t** ctxp, const char* moddata, size_t moddata_length, uint32_t rate) {
size_t bytes_needed;
char* mempool;
xm_context_t* ctx;
bytes_needed = xm_get_memory_needed_for_context(moddata, moddata_length);
mempool = malloc(bytes_needed);
if(mempool == NULL && bytes_needed > 0) {
return 2;
}
memset(mempool, 0, bytes_needed);
ctx = (*ctxp = (xm_context_t*)mempool);
ctx->ctx_size = bytes_needed;
mempool += sizeof(xm_context_t);
ctx->rate = rate;
mempool = xm_load_module(ctx, moddata, moddata_length, mempool);
ctx->channels = (xm_channel_context_t*)mempool;
mempool += ctx->module.num_channels * sizeof(xm_channel_context_t);
ctx->global_volume = 1;
ctx->amplification = .25f;
ctx->volume_ramp = (1/ 128.f);
ctx->panning_ramp = (1/ 128.f);
for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
xm_channel_context_t* ch = ctx->channels + i;
ch->ping = true;
ch->vibrato_waveform = XM_SINE_WAVEFORM;
ch->vibrato_waveform_retrigger = true;
ch->tremolo_waveform = XM_SINE_WAVEFORM;
ch->tremolo_waveform_retrigger = true;
ch->volume = ch->volume_envelope_volume = ch->fadeout_volume = 1.0f;
ch->panning = ch->panning_envelope_panning = .5f;
ch->actual_volume = .0f;
ch->actual_panning = .5f;
}
ctx->row_loop_count = (uint8_t*)mempool;
mempool += ctx->module.length * MAX_NUM_ROWS * sizeof(uint8_t);
return 0;
}
// ----- Playback -----
float xm_waveform(xm_waveform_type_t, uint8_t);
void xm_autovibrato(xm_context_t*, xm_channel_context_t*);
void xm_vibrato(xm_context_t*, xm_channel_context_t*, uint8_t, uint16_t);
void xm_tremolo(xm_context_t*, xm_channel_context_t*, uint8_t, uint16_t);
void xm_arpeggio(xm_context_t*, xm_channel_context_t*, uint8_t, uint16_t);
void xm_tone_portamento(xm_context_t*, xm_channel_context_t*);
void xm_pitch_slide(xm_context_t*, xm_channel_context_t*, float);
void xm_panning_slide(xm_channel_context_t*, uint8_t);
void xm_volume_slide(xm_channel_context_t*, uint8_t);
float xm_envelope_lerp(xm_envelope_point_t*, xm_envelope_point_t*, uint16_t);
void xm_envelope_tick(xm_channel_context_t*, xm_envelope_t*, uint16_t*, float*);
void xm_envelopes(xm_channel_context_t*);
float xm_linear_period(float);
float xm_linear_frequency(float);
float xm_amiga_period(float);
float xm_amiga_frequency(float);
float xm_period(xm_context_t*, float);
float xm_frequency(xm_context_t*, float, float);
void xm_update_frequency(xm_context_t*, xm_channel_context_t*);
void xm_handle_note_and_instrument(xm_context_t*, xm_channel_context_t*, xm_pattern_slot_t*);
void xm_trigger_note(xm_context_t*, xm_channel_context_t*, unsigned int flags);
void xm_cut_note(xm_channel_context_t*);
void xm_key_off(xm_channel_context_t*);
void xm_post_pattern_change(xm_context_t*);
void xm_row(xm_context_t*);
void xm_tick(xm_context_t*);
float xm_sample_at(xm_sample_t*, size_t);
float xm_next_of_sample(xm_channel_context_t*);
void xm_sample(xm_context_t*, float*, float*);
#define XM_TRIGGER_KEEP_VOLUME (1 << 0)
#define XM_TRIGGER_KEEP_PERIOD (1 << 1)
#define XM_TRIGGER_KEEP_SAMPLE_POSITION (1 << 2)
const uint16_t amiga_frequencies[] = {
1712, 1616, 1525, 1440,
1357, 1281, 1209, 1141,
1077, 1017, 961, 907,
856
};
const float multi_retrig_add[] = {
0.f, -1.f, -2.f, -4.f,
-8.f, -16.f, 0.f, 0.f,
0.f, 1.f, 2.f, 4.f,
8.f, 16.f, 0.f, 0.f
};
const float multi_retrig_multiply[] = {
1.f, 1.f, 1.f, 1.f,
1.f, 1.f, .6666667f, .5f,
1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.5f, 2.f
};
#define XM_CLAMP_UP1F(vol, limit) do{if((vol) > (limit)) (vol) = (limit);}while(0)
#define XM_CLAMP_UP(vol) XM_CLAMP_UP1F((vol), 1.f)
#define XM_CLAMP_DOWN1F(vol, limit) do{if((vol) < (limit)) (vol) = (limit);}while(0)
#define XM_CLAMP_DOWN(vol) XM_CLAMP_DOWN1F((vol), .0f)
#define XM_CLAMP2F(vol, up, down) do{if((vol) > (up)) (vol) = (up);else if((vol) < (down)) (vol) = (down);}while(0)
#define XM_CLAMP(vol) XM_CLAMP2F((vol), 1.f, .0f)
#define XM_SLIDE_TOWARDS(val, goal, incr) do{if((val) > (goal)) {(val) -= (incr);XM_CLAMP_DOWN1F((val), (goal));} else if((val) < (goal)) {(val) += (incr);XM_CLAMP_UP1F((val), (goal));}}while(0)
#define XM_LERP(u, v, t) ((u) + (t) * ((v) - (u)))
#define XM_INVERSE_LERP(u, v, lerp) (((lerp) - (u)) / ((v) - (u)))
#define HAS_TONE_PORTAMENTO(s) ((s)->effect_type == 3 || (s)->effect_type == 5 || ((s)->volume_column >> 4) == 0xF)
#define HAS_ARPEGGIO(s) ((s)->effect_type == 0 && (s)->effect_param != 0)
#define HAS_VIBRATO(s) ((s)->effect_type == 4 || (s)->effect_param == 6 || ((s)->volume_column >> 4) == 0xB)
#define NOTE_IS_VALID(n) ((n) > 0 && (n) < 97)
float xm_waveform(xm_waveform_type_t waveform, uint8_t step) {
unsigned int next_rand = 24492;
step %= 0x40;
switch(waveform) {
case XM_SINE_WAVEFORM:
return -sinf(2* 3.141592f * (float)step / (float)0x40);
case XM_RAMP_DOWN_WAVEFORM:
return (float)(0x20 - step) / 0x20;
case XM_SQUARE_WAVEFORM:
return (step >= 0x20) ? 1: -1;
case XM_RANDOM_WAVEFORM:
next_rand = next_rand * 1103515245 + 12345;
return (float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1;
case XM_RAMP_UP_WAVEFORM:
return (float)(step - 0x20) / 0x20;
default:
break;
}
return .0f;
}
void xm_autovibrato(xm_context_t* ctx, xm_channel_context_t* ch) {
if(ch->instrument == NULL || ch->instrument->vibrato_depth == 0) return;
xm_instrument_t* instr = ch->instrument;
float sweep = 1;
if(ch->autovibrato_ticks < instr->vibrato_sweep) {
sweep = XM_LERP(0.f, 1.f, (float)ch->autovibrato_ticks / (float)instr->vibrato_sweep);
}
unsigned int step = ((ch->autovibrato_ticks++) * instr->vibrato_rate) >> 2;
ch->autovibrato_note_offset = .25f * xm_waveform(instr->vibrato_type, step)
* (float)instr->vibrato_depth / (float)0xF * sweep;
xm_update_frequency(ctx, ch);
}
void xm_vibrato(xm_context_t* ctx, xm_channel_context_t* ch, uint8_t param, uint16_t pos) {
unsigned int step = pos * (param >> 4);
ch->vibrato_note_offset =
2.f
* xm_waveform(ch->vibrato_waveform, step)
* (float)(param & 0x0F) / (float)0xF;
xm_update_frequency(ctx, ch);
}
void xm_tremolo(xm_context_t* ctx, xm_channel_context_t* ch, uint8_t param, uint16_t pos) {
unsigned int step = pos * (param >> 4);
ch->tremolo_volume = -1* xm_waveform(ch->tremolo_waveform, step)
* (float)(param & 0x0F) / (float)0xF;
}
void xm_arpeggio(xm_context_t* ctx, xm_channel_context_t* ch, uint8_t param, uint16_t tick) {
switch(tick % 3) {
case 0:
ch->arp_in_progress = false;
ch->arp_note_offset = 0;
break;
case 2:
ch->arp_in_progress = true;
ch->arp_note_offset = param >> 4;
break;
case 1:
ch->arp_in_progress = true;
ch->arp_note_offset = param & 0x0F;
break;
}
xm_update_frequency(ctx, ch);
}
void xm_tone_portamento(xm_context_t* ctx, xm_channel_context_t* ch) {
if(ch->tone_portamento_target_period == 0.f) return;
if(ch->period != ch->tone_portamento_target_period) {
XM_SLIDE_TOWARDS(ch->period,
ch->tone_portamento_target_period,
(ctx->module.frequency_type == XM_LINEAR_FREQUENCIES ?
4: 1.f) * ch->tone_portamento_param
);
xm_update_frequency(ctx, ch);
}
}
void xm_pitch_slide(xm_context_t* ctx, xm_channel_context_t* ch, float period_offset) {
if(ctx->module.frequency_type == XM_LINEAR_FREQUENCIES) {
period_offset *= 4;
}
ch->period += period_offset;
XM_CLAMP_DOWN(ch->period);
xm_update_frequency(ctx, ch);
}
void xm_panning_slide(xm_channel_context_t* ch, uint8_t rawval) {
float f;
if((rawval & 0xF0) && (rawval & 0x0F)) {
return;
}
if(rawval & 0xF0) {
f = (float)(rawval >> 4) / (float)0xFF;
ch->panning += f;
XM_CLAMP_UP(ch->panning);
} else {
f = (float)(rawval & 0x0F) / (float)0xFF;
ch->panning -= f;
XM_CLAMP_DOWN(ch->panning);
}
}
void xm_volume_slide(xm_channel_context_t* ch, uint8_t rawval) {
float f;
if((rawval & 0xF0) && (rawval & 0x0F)) {
return;
}
if(rawval & 0xF0) {
f = (float)(rawval >> 4) / (float)0x40;
ch->volume += f;
XM_CLAMP_UP(ch->volume);
} else {
f = (float)(rawval & 0x0F) / (float)0x40;
ch->volume -= f;
XM_CLAMP_DOWN(ch->volume);
}
}
float xm_envelope_lerp(xm_envelope_point_t* restrict a, xm_envelope_point_t* restrict b, uint16_t pos) {
if(pos <= a->frame) return a->value;
else if(pos >= b->frame) return b->value;
else {
float p = (float)(pos - a->frame) / (float)(b->frame - a->frame);
return a->value * (1 - p) + b->value * p;
}
}
void xm_post_pattern_change(xm_context_t* ctx) {
if(ctx->current_table_index >= ctx->module.length) {
ctx->current_table_index = ctx->module.restart_position;
}
}
float xm_linear_period(float note) {
return 7680- note * 64;
}
float xm_linear_frequency(float period) {
return 8363* powf(2.f, (4608- period) / 768.f);
}
float xm_amiga_period(float note) {
unsigned int intnote = note;
uint8_t a = intnote % 12;
int8_t octave = note / 12- 2;
uint16_t p1 = amiga_frequencies[a], p2 = amiga_frequencies[a + 1];
if(octave > 0) {
p1 >>= octave;
p2 >>= octave;
} else if(octave < 0) {
p1 <<= (-octave);
p2 <<= (-octave);
}
return XM_LERP(p1, p2, note - intnote);
}
float xm_amiga_frequency(float period) {
if(period == .0f) return .0f;
return 7093789.2f / (period * 2.f);
}
float xm_period(xm_context_t* ctx, float note) {
switch(ctx->module.frequency_type) {
case XM_LINEAR_FREQUENCIES:
return xm_linear_period(note);
case XM_AMIGA_FREQUENCIES:
return xm_amiga_period(note);
}
return .0f;
}
float xm_frequency(xm_context_t* ctx, float period, float note_offset) {
uint8_t a;
int8_t octave;
float note;
uint16_t p1, p2;
switch(ctx->module.frequency_type) {
case XM_LINEAR_FREQUENCIES:
return xm_linear_frequency(period - 64* note_offset);
case XM_AMIGA_FREQUENCIES:
if(note_offset == 0) {
return xm_amiga_frequency(period);
}
a = octave = 0;
if(period > amiga_frequencies[0]) {
--octave;
while(period > (amiga_frequencies[0] << (-octave))) --octave;
} else if(period < amiga_frequencies[12]) {
++octave;
while(period < (amiga_frequencies[12] >> octave)) ++octave;
}
for(uint8_t i = 0; i < 12; ++i) {
p1 = amiga_frequencies[i], p2 = amiga_frequencies[i + 1];
if(octave > 0) {
p1 >>= octave;
p2 >>= octave;
} else if(octave < 0) {
p1 <<= (-octave);
p2 <<= (-octave);
}
if(p2 <= period && period <= p1) {
a = i;
break;
}
}
note = 12* (octave + 2) + a + XM_INVERSE_LERP(p1, p2, period);
return xm_amiga_frequency(xm_amiga_period(note + note_offset));
}
return .0f;
}
void xm_update_frequency(xm_context_t* ctx, xm_channel_context_t* ch) {
ch->frequency = xm_frequency(ctx, ch->period, (ch->arp_note_offset > 0 ? ch->arp_note_offset : (ch->vibrato_note_offset + ch->autovibrato_note_offset)));
ch->step = ch->frequency / ctx->rate;
}
void xm_handle_note_and_instrument(xm_context_t* ctx, xm_channel_context_t* ch,
xm_pattern_slot_t* s) {
if(s->instrument > 0) {
if(HAS_TONE_PORTAMENTO(ch->current) && ch->instrument != NULL && ch->sample != NULL) {
xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_PERIOD | XM_TRIGGER_KEEP_SAMPLE_POSITION);
} else if(s->instrument > ctx->module.num_instruments) {
xm_cut_note(ch);
ch->instrument = NULL;
ch->sample = NULL;
} else {
ch->instrument = ctx->module.instruments + (s->instrument - 1);
if(s->note == 0 && ch->sample != NULL) {
xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_SAMPLE_POSITION);
}
}
}
if(NOTE_IS_VALID(s->note)) {
xm_instrument_t* instr = ch->instrument;
if(HAS_TONE_PORTAMENTO(ch->current) && instr != NULL && ch->sample != NULL) {
ch->note = s->note + ch->sample->relative_note + ch->sample->finetune / 128- 1;
ch->tone_portamento_target_period = xm_period(ctx, ch->note);
} else if(instr == NULL || ch->instrument->num_samples == 0) {
xm_cut_note(ch);
} else {
if(instr->sample_of_notes[s->note - 1] < instr->num_samples) {
for(unsigned int z = 0; z < XM_SAMPLE_RAMPING_POINTS; ++z) {
ch->end_of_previous_sample[z] = xm_next_of_sample(ch);
}
ch->frame_count = 0;
ch->sample = instr->samples + instr->sample_of_notes[s->note - 1];
ch->orig_note = ch->note = s->note + ch->sample->relative_note
+ ch->sample->finetune / 128- 1;
if(s->instrument > 0) {
xm_trigger_note(ctx, ch, 0);
} else {
xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_VOLUME);
}
} else {
xm_cut_note(ch);
}
}
} else if(s->note == 97) {
xm_key_off(ch);
}
switch(s->volume_column >> 4) {
case 0x5:
if(s->volume_column > 0x50) break;
case 0x1:
case 0x2:
case 0x3:
case 0x4:
ch->volume = (float)(s->volume_column - 0x10) / (float)0x40;
break;
case 0x8:
xm_volume_slide(ch, s->volume_column & 0x0F);
break;
case 0x9:
xm_volume_slide(ch, s->volume_column << 4);
break;
case 0xA:
ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((s->volume_column & 0x0F) << 4);
break;
case 0xC:
ch->panning = (float)(
((s->volume_column & 0x0F) << 4) | (s->volume_column & 0x0F)
) / (float)0xFF;
break;
case 0xF:
if(s->volume_column & 0x0F) {
ch->tone_portamento_param = ((s->volume_column & 0x0F) << 4)
| (s->volume_column & 0x0F);
}
break;
default:
break;
}