exmod 0.3.3

Bindings to libxm, a small XM player library.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
/* Author: Romain "Artefact2" Dalmaso <artefact2@gmail.com> */
/* Contributor: Daniel Oaks <daniel@danieloaks.net> */

/* This program is free software. It comes without any warranty, to the
 * extent permitted by applicable law. You can redistribute it and/or
 * modify it under the terms of the Do What The Fuck You Want To Public
 * License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */

#include "xm_internal.h"
#include <inttypes.h>

/* ----- Static functions ----- */

static float xm_waveform(xm_waveform_type_t, uint8_t);
static void xm_autovibrato(xm_context_t*, xm_channel_context_t*);
static void xm_vibrato(xm_context_t*, xm_channel_context_t*, uint8_t);
static void xm_tremolo(xm_context_t*, xm_channel_context_t*, uint8_t, uint16_t);
static void xm_arpeggio(xm_context_t*, xm_channel_context_t*, uint8_t, uint16_t);
static void xm_tone_portamento(xm_context_t*, xm_channel_context_t*);
static void xm_pitch_slide(xm_context_t*, xm_channel_context_t*, float);
static void xm_panning_slide(xm_channel_context_t*, uint8_t);
static void xm_volume_slide(xm_channel_context_t*, uint8_t);

static float xm_envelope_lerp(xm_envelope_point_t*, xm_envelope_point_t*, uint16_t);
static void xm_envelope_tick(xm_channel_context_t*, xm_envelope_t*, uint16_t*, float*);
static void xm_envelopes(xm_channel_context_t*);

static float xm_linear_period(float);
static float xm_linear_frequency(float);
static float xm_amiga_period(float);
static float xm_amiga_frequency(float);
static float xm_period(xm_context_t*, float);
static float xm_frequency(xm_context_t*, float, float, float);
static void xm_update_frequency(xm_context_t*, xm_channel_context_t*);

static void xm_handle_note_and_instrument(xm_context_t*, xm_channel_context_t*, xm_pattern_slot_t*);
static void xm_trigger_note(xm_context_t*, xm_channel_context_t*, unsigned int flags);
static void xm_cut_note(xm_channel_context_t*);
static void xm_key_off(xm_channel_context_t*);

static void xm_post_pattern_change(xm_context_t*);
static void xm_row(xm_context_t*);
static void xm_tick(xm_context_t*);

static float xm_sample_at(xm_sample_t*, size_t);
static float xm_next_of_sample(xm_channel_context_t*);
static void xm_sample(xm_context_t*, float*, float*);

/* ----- Other oddities ----- */

#define XM_TRIGGER_KEEP_VOLUME (1 << 0)
#define XM_TRIGGER_KEEP_PERIOD (1 << 1)
#define XM_TRIGGER_KEEP_SAMPLE_POSITION (1 << 2)
#define XM_TRIGGER_KEEP_ENVELOPE (1 << 3)

#define AMIGA_FREQ_SCALE   1024

static const uint32_t amiga_frequencies[] = {
	1712*AMIGA_FREQ_SCALE, 1616*AMIGA_FREQ_SCALE, 1525*AMIGA_FREQ_SCALE, 1440*AMIGA_FREQ_SCALE, /* C-2, C#2, D-2, D#2 */
	1357*AMIGA_FREQ_SCALE, 1281*AMIGA_FREQ_SCALE, 1209*AMIGA_FREQ_SCALE, 1141*AMIGA_FREQ_SCALE, /* E-2, F-2, F#2, G-2 */
	1077*AMIGA_FREQ_SCALE, 1017*AMIGA_FREQ_SCALE,  961*AMIGA_FREQ_SCALE,  907*AMIGA_FREQ_SCALE, /* G#2, A-2, A#2, B-2 */
	856*AMIGA_FREQ_SCALE,                                                                       /* C-3 */
};

static const float multi_retrig_add[] = {
	 0.f,  -1.f,  -2.f,  -4.f,  /* 0, 1, 2, 3 */
	-8.f, -16.f,   0.f,   0.f,  /* 4, 5, 6, 7 */
	 0.f,   1.f,   2.f,   4.f,  /* 8, 9, A, B */
	 8.f,  16.f,   0.f,   0.f   /* C, D, E, F */
};

static const float multi_retrig_multiply[] = {
	1.f,   1.f,  1.f,        1.f,  /* 0, 1, 2, 3 */
	1.f,   1.f,   .6666667f,  .5f, /* 4, 5, 6, 7 */
	1.f,   1.f,  1.f,        1.f,  /* 8, 9, A, B */
	1.f,   1.f,  1.5f,       2.f   /* C, D, E, 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_type == 6 \
						 || ((s)->volume_column >> 4) == 0xB)
#define NOTE_IS_VALID(n) ((n) > 0 && (n) < 97)

/* ----- Function definitions ----- */

static float xm_waveform(xm_waveform_type_t waveform, uint8_t step) {
	static unsigned int next_rand = 24492;
	step %= 0x40;

	switch(waveform) {

	case XM_SINE_WAVEFORM:
		/* Why not use a table? For saving space, and because there's
		 * very very little actual performance gain. */
		return -sinf(2.f * 3.141592f * (float)step / (float)0x40);

	case XM_RAMP_DOWN_WAVEFORM:
		/* Ramp down: 1.0f when step = 0; -1.0f when step = 0x40 */
		return (float)(0x20 - step) / 0x20;

	case XM_SQUARE_WAVEFORM:
		/* Square with a 50% duty */
		return (step >= 0x20) ? 1.f : -1.f;

	case XM_RANDOM_WAVEFORM:
		/* Use the POSIX.1-2001 example, just to be deterministic
		 * across different machines */
		next_rand = next_rand * 1103515245 + 12345;
		return (float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1.f;

	case XM_RAMP_UP_WAVEFORM:
		/* Ramp up: -1.f when step = 0; 1.f when step = 0x40 */
		return (float)(step - 0x20) / 0x20;

	default:
		break;

	}

	return .0f;
}

static void xm_autovibrato(xm_context_t* ctx, xm_channel_context_t* ch) {
	if(ch->instrument == NULL || ch->instrument->vibrato_depth == 0){
		if (ch->autovibrato_note_offset){
			ch->autovibrato_note_offset = 0.f;
			xm_update_frequency(ctx, ch);
		}
		return;
	}
	xm_instrument_t* instr = ch->instrument;
	float sweep = 1.f;

	if(ch->autovibrato_ticks < instr->vibrato_sweep) {
		/* No idea if this is correct, but it sounds close enough… */
		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);
}

static void xm_vibrato(xm_context_t* ctx, xm_channel_context_t* ch, uint8_t param) {
	ch->vibrato_ticks += (param >> 4);
	ch->vibrato_note_offset =
		-2.f
		* xm_waveform(ch->vibrato_waveform, ch->vibrato_ticks)
		* (float)(param & 0x0F) / (float)0xF;
	xm_update_frequency(ctx, ch);
}

static void xm_tremolo(xm_context_t* ctx, xm_channel_context_t* ch, uint8_t param, uint16_t pos) {
	unsigned int step = pos * (param >> 4);
	/* Not so sure about this, it sounds correct by ear compared with
	 * MilkyTracker, but it could come from other bugs */
	ch->tremolo_volume = -1.f * xm_waveform(ch->tremolo_waveform, step)
		* (float)(param & 0x0F) / (float)0xF;
}

static 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);
}

static void xm_tone_portamento(xm_context_t* ctx, xm_channel_context_t* ch) {
	/* 3xx called without a note, wait until we get an actual
	 * target note. */
	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.f : 1.f) * ch->tone_portamento_param
		);
		xm_update_frequency(ctx, ch);
	}
}

static void xm_pitch_slide(xm_context_t* ctx, xm_channel_context_t* ch, float period_offset) {
	/* Don't ask about the 4.f coefficient. I found mention of it
	 * nowhere. Found by earâ„¢. */
	if(ctx->module.frequency_type == XM_LINEAR_FREQUENCIES) {
		period_offset *= 4.f;
	}

	ch->period += period_offset;
	XM_CLAMP_DOWN(ch->period);
	/* XXX: upper bound of period ? */

	xm_update_frequency(ctx, ch);
}

static void xm_panning_slide(xm_channel_context_t* ch, uint8_t rawval) {
	float f;

	if((rawval & 0xF0) && (rawval & 0x0F)) {
		/* Illegal state */
		return;
	}

	if(rawval & 0xF0) {
		/* Slide right */
		f = (float)(rawval >> 4) / (float)0xFF;
		ch->panning += f;
		XM_CLAMP_UP(ch->panning);
	} else {
		/* Slide left */
		f = (float)(rawval & 0x0F) / (float)0xFF;
		ch->panning -= f;
		XM_CLAMP_DOWN(ch->panning);
	}
}

static void xm_volume_slide(xm_channel_context_t* ch, uint8_t rawval) {
	float f;

	if((rawval & 0xF0) && (rawval & 0x0F)) {
		/* Illegal state */
		return;
	}

	if(rawval & 0xF0) {
		/* Slide up */
		f = (float)(rawval >> 4) / (float)0x40;
		ch->volume += f;
		XM_CLAMP_UP(ch->volume);
	} else {
		/* Slide down */
		f = (float)(rawval & 0x0F) / (float)0x40;
		ch->volume -= f;
		XM_CLAMP_DOWN(ch->volume);
	}
}

static float xm_envelope_lerp(xm_envelope_point_t* restrict a, xm_envelope_point_t* restrict b, uint16_t pos) {
	/* Linear interpolation between two envelope points */
	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;
	}
}

static void xm_post_pattern_change(xm_context_t* ctx) {
	/* Loop if necessary */
	if(ctx->current_table_index >= ctx->module.length) {
		ctx->current_table_index = ctx->module.restart_position;
	}
}

static float xm_linear_period(float note) {
	return 7680.f - note * 64.f;
}

static float xm_linear_frequency(float period) {
	return 8363.f * powf(2.f, (4608.f - period) / 768.f);
}

static float xm_amiga_period(float note) {
	unsigned int intnote = note;
	uint8_t a = intnote % 12;
	int8_t octave = note / 12.f - 2;
	int32_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) / AMIGA_FREQ_SCALE;
}

static float xm_amiga_frequency(float period) {
	if(period == .0f) return .0f;

	/* This is the PAL value. No reason to choose this one over the
	 * NTSC value. */
	return 7093789.2f / (period * 2.f);
}

static 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;
}

static float xm_frequency(xm_context_t* ctx, float period, float note_offset, float period_offset) {
	uint8_t a;
	int8_t octave;
	float note;
	int32_t p1, p2;

	switch(ctx->module.frequency_type) {

	case XM_LINEAR_FREQUENCIES:
		return xm_linear_frequency(period - 64.f * note_offset - 16.f * period_offset);

	case XM_AMIGA_FREQUENCIES:
		if(note_offset == 0) {
			/* A chance to escape from insanity */
			return xm_amiga_frequency(period + 16.f * period_offset);
		}

		/* FIXME: this is very crappy at best */
		a = octave = 0;

		/* Find the octave of the current period */
		period *= AMIGA_FREQ_SCALE;
		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;
		}

		/* Find the smallest note closest to the current period */
		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;
			}
		}

		if(XM_DEBUG && (p1 < period || p2 > period)) {
			DEBUG("%" PRId32 " <= %f <= %" PRId32 " should hold but doesn't, this is a bug", p2, period, p1);
		}

		note = 12.f * (octave + 2) + a + XM_INVERSE_LERP(p1, p2, period);

		return xm_amiga_frequency(xm_amiga_period(note + note_offset) + 16.f * period_offset);

	}

	return .0f;
}

static void xm_update_frequency(xm_context_t* ctx, xm_channel_context_t* ch) {
	ch->frequency = xm_frequency(
		ctx, ch->period,
		ch->arp_note_offset,
		ch->vibrato_note_offset + ch->autovibrato_note_offset
	);
	ch->step = ch->frequency / ctx->rate;
}

static 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) {
			/* Tone portamento in effect, unclear stuff happens */
			xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_PERIOD | XM_TRIGGER_KEEP_SAMPLE_POSITION);
		} else if(s->note == 0 && ch->sample != NULL) {
			/* Ghost instrument, trigger note */
			/* Sample position is kept, but envelopes are reset */
			xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_SAMPLE_POSITION);
		} else if(s->instrument > ctx->module.num_instruments) {
			/* Invalid instrument, Cut current note */
			xm_cut_note(ch);
			ch->instrument = NULL;
			ch->sample = NULL;
		} else {
			ch->instrument = ctx->module.instruments + (s->instrument - 1);
		}
	}

	if(NOTE_IS_VALID(s->note)) {
		/* Yes, the real note number is s->note -1. Try finding
		 * THAT in any of the specs! :-) */

		xm_instrument_t* instr = ch->instrument;

		if(HAS_TONE_PORTAMENTO(ch->current) && instr != NULL && ch->sample != NULL) {
			/* Tone portamento in effect */
			ch->note = s->note + ch->sample->relative_note + ch->sample->finetune / 128.f - 1.f;
			ch->tone_portamento_target_period = xm_period(ctx, ch->note);
		} else if(instr == NULL || ch->instrument->num_samples == 0) {
			/* Bad instrument */
			xm_cut_note(ch);
		} else {
			if(instr->sample_of_notes[s->note - 1] < instr->num_samples) {
#if XM_RAMPING
				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;
#endif
				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.f - 1.f;
				if(s->instrument > 0) {
					xm_trigger_note(ctx, ch, 0);
				} else {
					/* Ghost note: keep old volume */
					xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_VOLUME);
				}
			} else {
				/* Bad sample */
				xm_cut_note(ch);
			}
		}
	} else if(s->note == 97) {
		/* Key Off */
		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:
		/* Set volume */
		ch->volume = (float)(s->volume_column - 0x10) / (float)0x40;
		break;

	case 0x8: /* Fine volume slide down */
		xm_volume_slide(ch, s->volume_column & 0x0F);
		break;

	case 0x9: /* Fine volume slide up */
		xm_volume_slide(ch, s->volume_column << 4);
		break;

	case 0xA: /* Set vibrato speed */
		ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((s->volume_column & 0x0F) << 4);
		break;

	case 0xC: /* Set panning */
		ch->panning = (float)(
			((s->volume_column & 0x0F) << 4) | (s->volume_column & 0x0F)
			) / (float)0xFF;
		break;

	case 0xF: /* Tone portamento */
		if(s->volume_column & 0x0F) {
			ch->tone_portamento_param = ((s->volume_column & 0x0F) << 4)
				| (s->volume_column & 0x0F);
		}
		break;

	default:
		break;

	}

	switch(s->effect_type) {

	case 1: /* 1xx: Portamento up */
		if(s->effect_param > 0) {
			ch->portamento_up_param = s->effect_param;
		}
		break;

	case 2: /* 2xx: Portamento down */
		if(s->effect_param > 0) {
			ch->portamento_down_param = s->effect_param;
		}
		break;

	case 3: /* 3xx: Tone portamento */
		if(s->effect_param > 0) {
			ch->tone_portamento_param = s->effect_param;
		}
		break;

	case 4: /* 4xy: Vibrato */
		if(s->effect_param & 0x0F) {
			/* Set vibrato depth */
			ch->vibrato_param = (ch->vibrato_param & 0xF0) | (s->effect_param & 0x0F);
		}
		if(s->effect_param >> 4) {
			/* Set vibrato speed */
			ch->vibrato_param = (s->effect_param & 0xF0) | (ch->vibrato_param & 0x0F);
		}
		break;

	case 5: /* 5xy: Tone portamento + Volume slide */
		if(s->effect_param > 0) {
			ch->volume_slide_param = s->effect_param;
		}
		break;

	case 6: /* 6xy: Vibrato + Volume slide */
		if(s->effect_param > 0) {
			ch->volume_slide_param = s->effect_param;
		}
		break;

	case 7: /* 7xy: Tremolo */
		if(s->effect_param & 0x0F) {
			/* Set tremolo depth */
			ch->tremolo_param = (ch->tremolo_param & 0xF0) | (s->effect_param & 0x0F);
		}
		if(s->effect_param >> 4) {
			/* Set tremolo speed */
			ch->tremolo_param = (s->effect_param & 0xF0) | (ch->tremolo_param & 0x0F);
		}
		break;

	case 8: /* 8xx: Set panning */
		ch->panning = (float)s->effect_param / (float)0xFF;
		break;

	case 9: /* 9xx: Sample offset */
		if(ch->sample != NULL && NOTE_IS_VALID(s->note)) {
			uint32_t final_offset = s->effect_param << (ch->sample->bits == 16 ? 7 : 8);
			if(final_offset >= ch->sample->length) {
				/* Pretend the sample dosen't loop and is done playing */
				ch->sample_position = -1;
				break;
			}
			ch->sample_position = final_offset;
		}
		break;

	case 0xA: /* Axy: Volume slide */
		if(s->effect_param > 0) {
			ch->volume_slide_param = s->effect_param;
		}
		break;

	case 0xB: /* Bxx: Position jump */
		if(s->effect_param < ctx->module.length) {
			ctx->position_jump = true;
			ctx->jump_dest = s->effect_param;
			ctx->jump_row = 0;
		}
		break;

	case 0xC: /* Cxx: Set volume */
		ch->volume = (float)((s->effect_param > 0x40)
							 ? 0x40 : s->effect_param) / (float)0x40;
		break;

	case 0xD: /* Dxx: Pattern break */
		/* Jump after playing this line */
		ctx->pattern_break = true;
		ctx->jump_row = (s->effect_param >> 4) * 10 + (s->effect_param & 0x0F);
		break;

	case 0xE: /* EXy: Extended command */
		switch(s->effect_param >> 4) {

		case 1: /* E1y: Fine portamento up */
			if(s->effect_param & 0x0F) {
				ch->fine_portamento_up_param = s->effect_param & 0x0F;
			}
			xm_pitch_slide(ctx, ch, -ch->fine_portamento_up_param);
			break;

		case 2: /* E2y: Fine portamento down */
			if(s->effect_param & 0x0F) {
				ch->fine_portamento_down_param = s->effect_param & 0x0F;
			}
			xm_pitch_slide(ctx, ch, ch->fine_portamento_down_param);
			break;

		case 4: /* E4y: Set vibrato control */
			ch->vibrato_waveform = s->effect_param & 3;
			ch->vibrato_waveform_retrigger = !((s->effect_param >> 2) & 1);
			break;

		case 5: /* E5y: Set finetune */
			if(NOTE_IS_VALID(ch->current->note) && ch->sample != NULL) {
				ch->note = ch->current->note + ch->sample->relative_note +
					(float)(((s->effect_param & 0x0F) - 8) << 4) / 128.f - 1.f;
				ch->period = xm_period(ctx, ch->note);
				xm_update_frequency(ctx, ch);
			}
			break;

		case 6: /* E6y: Pattern loop */
			if(s->effect_param & 0x0F) {
				if((s->effect_param & 0x0F) == ch->pattern_loop_count) {
					/* Loop is over */
					ch->pattern_loop_count = 0;
					break;
				}

				/* Jump to the beginning of the loop */
				ch->pattern_loop_count++;
				ctx->position_jump = true;
				ctx->jump_row = ch->pattern_loop_origin;
				ctx->jump_dest = ctx->current_table_index;
			} else {
				/* Set loop start point */
				ch->pattern_loop_origin = ctx->current_row;
				/* Replicate FT2 E60 bug */
				ctx->jump_row = ch->pattern_loop_origin;
			}
			break;

		case 7: /* E7y: Set tremolo control */
			ch->tremolo_waveform = s->effect_param & 3;
			ch->tremolo_waveform_retrigger = !((s->effect_param >> 2) & 1);
			break;

		case 0xA: /* EAy: Fine volume slide up */
			if(s->effect_param & 0x0F) {
				ch->fine_volume_slide_param = s->effect_param & 0x0F;
			}
			xm_volume_slide(ch, ch->fine_volume_slide_param << 4);
			break;

		case 0xB: /* EBy: Fine volume slide down */
			if(s->effect_param & 0x0F) {
				ch->fine_volume_slide_param = s->effect_param & 0x0F;
			}
			xm_volume_slide(ch, ch->fine_volume_slide_param);
			break;

		case 0xD: /* EDy: Note delay */
			/* XXX: figure this out better. EDx triggers
			 * the note even when there no note and no
			 * instrument. But ED0 acts like like a ghost
			 * note, EDx (x ≠ 0) does not. */
			if(s->note == 0 && s->instrument == 0) {
				unsigned int flags = XM_TRIGGER_KEEP_VOLUME;

				if(ch->current->effect_param & 0x0F) {
					ch->note = ch->orig_note;
					xm_trigger_note(ctx, ch, flags);
				} else {
					xm_trigger_note(
						ctx, ch,
						flags
						| XM_TRIGGER_KEEP_PERIOD
						| XM_TRIGGER_KEEP_SAMPLE_POSITION
						);
				}
			}
			break;

		case 0xE: /* EEy: Pattern delay */
			ctx->extra_ticks = (ch->current->effect_param & 0x0F) * ctx->tempo;
			break;

		default:
			break;

		}
		break;

	case 0xF: /* Fxx: Set tempo/BPM */
		if(s->effect_param > 0) {
			if(s->effect_param <= 0x1F) {
				ctx->tempo = s->effect_param;
			} else {
				ctx->bpm = s->effect_param;
			}
		}
		break;

	case 16: /* Gxx: Set global volume */
		ctx->global_volume = (float)((s->effect_param > 0x40)
									 ? 0x40 : s->effect_param) / (float)0x40;
		break;

	case 17: /* Hxy: Global volume slide */
		if(s->effect_param > 0) {
			ch->global_volume_slide_param = s->effect_param;
		}
		break;

	case 21: /* Lxx: Set envelope position */
		ch->volume_envelope_frame_count = s->effect_param;
		ch->panning_envelope_frame_count = s->effect_param;
		break;

	case 25: /* Pxy: Panning slide */
		if(s->effect_param > 0) {
			ch->panning_slide_param = s->effect_param;
		}
		break;

	case 27: /* Rxy: Multi retrig note */
		if(s->effect_param > 0) {
			if((s->effect_param >> 4) == 0) {
				/* Keep previous x value */
				ch->multi_retrig_param = (ch->multi_retrig_param & 0xF0) | (s->effect_param & 0x0F);
			} else {
				ch->multi_retrig_param = s->effect_param;
			}
		}
		break;

	case 29: /* Txy: Tremor */
		if(s->effect_param > 0) {
			/* Tremor x and y params do not appear to be separately
			 * kept in memory, unlike Rxy */
			ch->tremor_param = s->effect_param;
		}
		break;

	case 33: /* Xxy: Extra stuff */
		switch(s->effect_param >> 4) {

		case 1: /* X1y: Extra fine portamento up */
			if(s->effect_param & 0x0F) {
				ch->extra_fine_portamento_up_param = s->effect_param & 0x0F;
			}
			xm_pitch_slide(ctx, ch, -1.0f * ch->extra_fine_portamento_up_param);
			break;

		case 2: /* X2y: Extra fine portamento down */
			if(s->effect_param & 0x0F) {
				ch->extra_fine_portamento_down_param = s->effect_param & 0x0F;
			}
			xm_pitch_slide(ctx, ch, ch->extra_fine_portamento_down_param);
			break;

		default:
			break;

		}
		break;

	default:
		break;

	}
}

static void xm_trigger_note(xm_context_t* ctx, xm_channel_context_t* ch, unsigned int flags) {
	if(!(flags & XM_TRIGGER_KEEP_SAMPLE_POSITION)) {
		ch->sample_position = 0.f;
		ch->ping = true;
	}

	if(ch->sample != NULL) {
		if(!(flags & XM_TRIGGER_KEEP_VOLUME)) {
			ch->volume = ch->sample->volume;
		}

		ch->panning = ch->sample->panning;
	}

	if(!(flags & XM_TRIGGER_KEEP_ENVELOPE)) {
		ch->sustained = true;
		ch->fadeout_volume = ch->volume_envelope_volume = 1.0f;
		ch->panning_envelope_panning = .5f;
		ch->volume_envelope_frame_count = ch->panning_envelope_frame_count = 0;
	}
	ch->vibrato_note_offset = 0.f;
	ch->tremolo_volume = 0.f;
	ch->tremor_on = false;

	ch->autovibrato_ticks = 0;

	if(ch->vibrato_waveform_retrigger) {
		ch->vibrato_ticks = 0; /* XXX: should the waveform itself also
								* be reset to sine? */
	}
	if(ch->tremolo_waveform_retrigger) {
		ch->tremolo_ticks = 0;
	}

	if(!(flags & XM_TRIGGER_KEEP_PERIOD)) {
		ch->period = xm_period(ctx, ch->note);
		xm_update_frequency(ctx, ch);
	}

	ch->latest_trigger = ctx->generated_samples;
	if(ch->instrument != NULL) {
		ch->instrument->latest_trigger = ctx->generated_samples;
	}
	if(ch->sample != NULL) {
		ch->sample->latest_trigger = ctx->generated_samples;
	}
}

static void xm_cut_note(xm_channel_context_t* ch) {
	/* NB: this is not the same as Key Off */
	ch->volume = .0f;
}

static void xm_key_off(xm_channel_context_t* ch) {
	/* Key Off */
	ch->sustained = false;

	/* If no volume envelope is used, also cut the note */
	if(ch->instrument == NULL || !ch->instrument->volume_envelope.enabled) {
		xm_cut_note(ch);
	}
}

static void xm_row(xm_context_t* ctx) {
	static xm_pattern_slot_t empty_slot = {0};

	if(ctx->position_jump) {
		ctx->current_table_index = ctx->jump_dest;
		ctx->current_row = ctx->jump_row;
		ctx->position_jump = false;
		ctx->pattern_break = false;
		ctx->jump_row = 0;
		xm_post_pattern_change(ctx);
	} else if(ctx->pattern_break) {
		ctx->current_table_index++;
		ctx->current_row = ctx->jump_row;
		ctx->pattern_break = false;
		ctx->jump_row = 0;
		xm_post_pattern_change(ctx);
	}

	uint8_t pat_idx = ctx->module.pattern_table[ctx->current_table_index];
	xm_pattern_t* cur = (pat_idx < ctx->module.num_patterns ? ctx->module.patterns + pat_idx : NULL);
	bool in_a_loop = false;

	/* Read notes… */
	for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
		xm_pattern_slot_t* s = (cur ? cur->slots + ctx->current_row * ctx->module.num_channels + i : &empty_slot);
		xm_channel_context_t* ch = ctx->channels + i;

		ch->current = s;

		if(s->effect_type != 0xE || s->effect_param >> 4 != 0xD) {
			xm_handle_note_and_instrument(ctx, ch, s);
		} else {
			ch->note_delay_param = s->effect_param & 0x0F;
		}

		if(!in_a_loop && ch->pattern_loop_count > 0) {
			in_a_loop = true;
		}
	}

	if(!in_a_loop) {
		/* No E6y loop is in effect (or we are in the first pass) */
		ctx->loop_count = (ctx->row_loop_count[MAX_NUM_ROWS * ctx->current_table_index + ctx->current_row]++);
	}

	ctx->current_row++; /* Since this is an uint8, this line can
						 * increment from 255 to 0, in which case it
						 * is still necessary to go the next
						 * pattern. */
	if(!ctx->position_jump && !ctx->pattern_break &&
	   (ctx->current_row >= (cur ? cur->num_rows : DEFAULT_PATTERN_LENGTH) || ctx->current_row == 0)) {
		ctx->current_table_index++;
		ctx->current_row = ctx->jump_row; /* This will be 0 most of
										   * the time, except when E60
										   * is used */
		ctx->jump_row = 0;
		xm_post_pattern_change(ctx);
	}
}

static void xm_envelope_tick(xm_channel_context_t* ch,
							 xm_envelope_t* env,
							 uint16_t* counter,
							 float* outval) {
	if(env->num_points < 2) {
		/* Don't really know what to do… */
		if(env->num_points == 1) {
			/* XXX I am pulling this out of my ass */
			*outval = (float)env->points[0].value / (float)0x40;
			if(*outval > 1) {
				*outval = 1;
			}
		}

		return;
	} else {
		uint8_t j;

		if(env->loop_enabled) {
			uint16_t loop_start = env->points[env->loop_start_point].frame;
			uint16_t loop_end = env->points[env->loop_end_point].frame;
			uint16_t loop_length = loop_end - loop_start;

			if(*counter >= loop_end) {
				*counter -= loop_length;
			}
		}

		for(j = 0; j < (env->num_points - 2); ++j) {
			if(env->points[j].frame <= *counter &&
			   env->points[j+1].frame >= *counter) {
				break;
			}
		}

		*outval = xm_envelope_lerp(env->points + j, env->points + j + 1, *counter) / (float)0x40;

		/* Make sure it is safe to increment frame count */
		if(!ch->sustained || !env->sustain_enabled ||
		   *counter != env->points[env->sustain_point].frame) {
			(*counter)++;
		}
	}
}

static void xm_envelopes(xm_channel_context_t* ch) {
	if(ch->instrument != NULL) {
		if(ch->instrument->volume_envelope.enabled) {
			if(!ch->sustained) {
				ch->fadeout_volume -= (float)ch->instrument->volume_fadeout / 32768.f;
				XM_CLAMP_DOWN(ch->fadeout_volume);
			}

			xm_envelope_tick(ch,
							 &(ch->instrument->volume_envelope),
							 &(ch->volume_envelope_frame_count),
							 &(ch->volume_envelope_volume));
		}

		if(ch->instrument->panning_envelope.enabled) {
			xm_envelope_tick(ch,
							 &(ch->instrument->panning_envelope),
							 &(ch->panning_envelope_frame_count),
							 &(ch->panning_envelope_panning));
		}
	}
}

static void xm_tick(xm_context_t* ctx) {
	if(ctx->current_tick == 0) {
		xm_row(ctx);
	}

	for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
		xm_channel_context_t* ch = ctx->channels + i;

		xm_envelopes(ch);
		xm_autovibrato(ctx, ch);

		if(ch->arp_in_progress && !HAS_ARPEGGIO(ch->current)) {
			ch->arp_in_progress = false;
			ch->arp_note_offset = 0;
			xm_update_frequency(ctx, ch);
		}
		if(ch->vibrato_in_progress && !HAS_VIBRATO(ch->current)) {
			ch->vibrato_in_progress = false;
			ch->vibrato_note_offset = 0.f;
			xm_update_frequency(ctx, ch);
		}

		switch(ch->current->volume_column >> 4) {

		case 0x6: /* Volume slide down */
			if(ctx->current_tick == 0) break;
			xm_volume_slide(ch, ch->current->volume_column & 0x0F);
			break;

		case 0x7: /* Volume slide up */
			if(ctx->current_tick == 0) break;
			xm_volume_slide(ch, ch->current->volume_column << 4);
			break;

		case 0xB: /* Vibrato */
			if(ctx->current_tick == 0) break;
			ch->vibrato_in_progress = false;
			xm_vibrato(ctx, ch, ch->vibrato_param);
			break;

		case 0xD: /* Panning slide left */
			if(ctx->current_tick == 0) break;
			xm_panning_slide(ch, ch->current->volume_column & 0x0F);
			break;

		case 0xE: /* Panning slide right */
			if(ctx->current_tick == 0) break;
			xm_panning_slide(ch, ch->current->volume_column << 4);
			break;

		case 0xF: /* Tone portamento */
			if(ctx->current_tick == 0) break;
			xm_tone_portamento(ctx, ch);
			break;

		default:
			break;

		}

		switch(ch->current->effect_type) {

		case 0: /* 0xy: Arpeggio */
			if(ch->current->effect_param > 0) {
				char arp_offset = ctx->tempo % 3;
				switch(arp_offset) {
				case 2: /* 0 -> x -> 0 -> y -> x -> … */
					if(ctx->current_tick == 1) {
						ch->arp_in_progress = true;
						ch->arp_note_offset = ch->current->effect_param >> 4;
						xm_update_frequency(ctx, ch);
						break;
					}
					/* No break here, this is intended */
				case 1: /* 0 -> 0 -> y -> x -> … */
					if(ctx->current_tick == 0) {
						ch->arp_in_progress = false;
						ch->arp_note_offset = 0;
						xm_update_frequency(ctx, ch);
						break;
					}
					/* No break here, this is intended */
				case 0: /* 0 -> y -> x -> … */
					xm_arpeggio(ctx, ch, ch->current->effect_param, ctx->current_tick - arp_offset);
				default:
					break;
				}
			}
			break;

		case 1: /* 1xx: Portamento up */
			if(ctx->current_tick == 0) break;
			xm_pitch_slide(ctx, ch, -ch->portamento_up_param);
			break;

		case 2: /* 2xx: Portamento down */
			if(ctx->current_tick == 0) break;
			xm_pitch_slide(ctx, ch, ch->portamento_down_param);
			break;

		case 3: /* 3xx: Tone portamento */
			if(ctx->current_tick == 0) break;
			xm_tone_portamento(ctx, ch);
			break;

		case 4: /* 4xy: Vibrato */
			if(ctx->current_tick == 0) break;
			ch->vibrato_in_progress = true;
			xm_vibrato(ctx, ch, ch->vibrato_param);
			break;

		case 5: /* 5xy: Tone portamento + Volume slide */
			if(ctx->current_tick == 0) break;
			xm_tone_portamento(ctx, ch);
			xm_volume_slide(ch, ch->volume_slide_param);
			break;

		case 6: /* 6xy: Vibrato + Volume slide */
			if(ctx->current_tick == 0) break;
			ch->vibrato_in_progress = true;
			xm_vibrato(ctx, ch, ch->vibrato_param);
			xm_volume_slide(ch, ch->volume_slide_param);
			break;

		case 7: /* 7xy: Tremolo */
			if(ctx->current_tick == 0) break;
			xm_tremolo(ctx, ch, ch->tremolo_param, ch->tremolo_ticks++);
			break;

		case 0xA: /* Axy: Volume slide */
			if(ctx->current_tick == 0) break;
			xm_volume_slide(ch, ch->volume_slide_param);
			break;

		case 0xE: /* EXy: Extended command */
			switch(ch->current->effect_param >> 4) {

			case 0x9: /* E9y: Retrigger note */
				if(ctx->current_tick != 0 && ch->current->effect_param & 0x0F) {
					if(!(ctx->current_tick % (ch->current->effect_param & 0x0F))) {
						xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_VOLUME);
						xm_envelopes(ch);
					}
				}
				break;

			case 0xC: /* ECy: Note cut */
				if((ch->current->effect_param & 0x0F) == ctx->current_tick) {
				    xm_cut_note(ch);
				}
				break;

			case 0xD: /* EDy: Note delay */
				if(ch->note_delay_param == ctx->current_tick) {
					xm_handle_note_and_instrument(ctx, ch, ch->current);
					xm_envelopes(ch);
				}
				break;

			default:
				break;

			}
			break;

		case 17: /* Hxy: Global volume slide */
			if(ctx->current_tick == 0) break;
			if((ch->global_volume_slide_param & 0xF0) &&
			   (ch->global_volume_slide_param & 0x0F)) {
				/* Illegal state */
				break;
			}
			if(ch->global_volume_slide_param & 0xF0) {
				/* Global slide up */
				float f = (float)(ch->global_volume_slide_param >> 4) / (float)0x40;
				ctx->global_volume += f;
				XM_CLAMP_UP(ctx->global_volume);
			} else {
				/* Global slide down */
				float f = (float)(ch->global_volume_slide_param & 0x0F) / (float)0x40;
				ctx->global_volume -= f;
				XM_CLAMP_DOWN(ctx->global_volume);
			}
			break;

		case 20: /* Kxx: Key off */
			/* Most documentations will tell you the parameter has no
			 * use. Don't be fooled. */
			if(ctx->current_tick == ch->current->effect_param) {
				xm_key_off(ch);
			}
			break;

		case 25: /* Pxy: Panning slide */
			if(ctx->current_tick == 0) break;
			xm_panning_slide(ch, ch->panning_slide_param);
			break;

		case 27: /* Rxy: Multi retrig note */
			if(ctx->current_tick == 0) break;
			if(((ch->multi_retrig_param) & 0x0F) == 0) break;
			if((ctx->current_tick % (ch->multi_retrig_param & 0x0F)) == 0) {
				xm_trigger_note(ctx, ch, XM_TRIGGER_KEEP_VOLUME | XM_TRIGGER_KEEP_ENVELOPE);

				/* Rxy doesn't affect volume if there's a command in the volume
				   column, or if the instrument has a volume envelope. */
				if (!ch->current->volume_column && !ch->instrument->volume_envelope.enabled){
					float v = ch->volume * multi_retrig_multiply[ch->multi_retrig_param >> 4]
						+ multi_retrig_add[ch->multi_retrig_param >> 4] / (float)0x40;
					XM_CLAMP(v);
					ch->volume = v;
				}
			}
			break;

		case 29: /* Txy: Tremor */
			if(ctx->current_tick == 0) break;
			ch->tremor_on = (
				(ctx->current_tick - 1) % ((ch->tremor_param >> 4) + (ch->tremor_param & 0x0F) + 2)
				>
				(ch->tremor_param >> 4)
			);
			break;

		default:
			break;

		}

		float panning, volume;

		panning = ch->panning +
			(ch->panning_envelope_panning - .5f) * (.5f - fabsf(ch->panning - .5f)) * 2.0f;

		if(ch->tremor_on) {
		        volume = .0f;
		} else {
			volume = ch->volume + ch->tremolo_volume;
			XM_CLAMP(volume);
			volume *= ch->fadeout_volume * ch->volume_envelope_volume;
		}

#if XM_RAMPING
		/* See https://modarchive.org/forums/index.php?topic=3517.0
		 * and https://github.com/Artefact2/libxm/pull/16 */
		ch->target_volume[0] = volume * sqrtf(1.f - panning);
		ch->target_volume[1] = volume * sqrtf(panning);
#else
		ch->actual_volume[0] = volume * sqrtf(1.f - panning);
		ch->actual_volume[1] = volume * sqrtf(panning);
#endif
	}

	ctx->current_tick++;
	if(ctx->current_tick >= ctx->tempo + ctx->extra_ticks) {
		ctx->current_tick = 0;
		ctx->extra_ticks = 0;
	}

	/* FT2 manual says number of ticks / second = BPM * 0.4 */
	ctx->remaining_samples_in_tick += (float)ctx->rate / ((float)ctx->bpm * 0.4f);
}

static float xm_sample_at(xm_sample_t* sample, size_t k) {
	return sample->bits == 8 ? (sample->data8[k] / 128.f) : (sample->data16[k] / 32768.f);
}

static float xm_next_of_sample(xm_channel_context_t* ch) {
	if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) {
#if XM_RAMPING
		if(ch->frame_count < XM_SAMPLE_RAMPING_POINTS) {
			return XM_LERP(ch->end_of_previous_sample[ch->frame_count], .0f,
			               (float)ch->frame_count / (float)XM_SAMPLE_RAMPING_POINTS);
		}
#endif
		return .0f;
	}
	if(ch->sample->length == 0) {
		return .0f;
	}

	float u, v, t;
	uint32_t a, b;
	a = (uint32_t)ch->sample_position; /* This cast is fine,
										* sample_position will not
										* go above integer
										* ranges */
	if(XM_LINEAR_INTERPOLATION) {
		b = a + 1;
		t = ch->sample_position - a; /* Cheaper than fmodf(., 1.f) */
	}
	u = xm_sample_at(ch->sample, a);

	switch(ch->sample->loop_type) {

	case XM_NO_LOOP:
		if(XM_LINEAR_INTERPOLATION) {
			v = (b < ch->sample->length) ? xm_sample_at(ch->sample, b) : .0f;
		}
		ch->sample_position += ch->step;
		if(ch->sample_position >= ch->sample->length) {
			ch->sample_position = -1;
		}
		break;

	case XM_FORWARD_LOOP:
		if(XM_LINEAR_INTERPOLATION) {
			v = xm_sample_at(
				ch->sample,
				(b == ch->sample->loop_end) ? ch->sample->loop_start : b
				);
		}
		ch->sample_position += ch->step;
		while(ch->sample_position >= ch->sample->loop_end) {
			ch->sample_position -= ch->sample->loop_length;
		}
		break;

	case XM_PING_PONG_LOOP:
		if(ch->ping) {
			ch->sample_position += ch->step;
		} else {
			ch->sample_position -= ch->step;
		}
		/* XXX: this may not work for very tight ping-pong loops
		 * (ie switches direction more than once per sample */
		if(ch->ping) {
			if(XM_LINEAR_INTERPOLATION) {
				v = xm_sample_at(ch->sample, (b >= ch->sample->loop_end) ? a : b);
			}
			if(ch->sample_position >= ch->sample->loop_end) {
				ch->ping = false;
				ch->sample_position = (ch->sample->loop_end << 1) - ch->sample_position;
			}
			/* sanity checking */
			if(ch->sample_position >= ch->sample->length) {
				ch->ping = false;
				ch->sample_position -= ch->sample->length - 1;
			}
		} else {
			if(XM_LINEAR_INTERPOLATION) {
				v = u;
				u = xm_sample_at(
					ch->sample,
					(b == 1 || b - 2 <= ch->sample->loop_start) ? a : (b - 2)
					);
			}
			if(ch->sample_position <= ch->sample->loop_start) {
				ch->ping = true;
				ch->sample_position = (ch->sample->loop_start << 1) - ch->sample_position;
			}
			/* sanity checking */
			if(ch->sample_position <= .0f) {
				ch->ping = true;
				ch->sample_position = .0f;
			}
		}
		break;

	default:
		v = .0f;
		break;
	}

	float endval = (XM_LINEAR_INTERPOLATION ? XM_LERP(u, v, t) : u);

#if XM_RAMPING
	if(ch->frame_count < XM_SAMPLE_RAMPING_POINTS) {
		/* Smoothly transition between old and new sample. */
		return XM_LERP(ch->end_of_previous_sample[ch->frame_count], endval,
		               (float)ch->frame_count / (float)XM_SAMPLE_RAMPING_POINTS);
	}
#endif

	return endval;
}

static void xm_sample(xm_context_t* ctx, float* left, float* right) {
	if(ctx->remaining_samples_in_tick <= 0) {
		xm_tick(ctx);
	}
	ctx->remaining_samples_in_tick--;

	*left = 0.f;
	*right = 0.f;

	if(ctx->max_loop_count > 0 && ctx->loop_count >= ctx->max_loop_count) {
		return;
	}

	for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
		xm_channel_context_t* ch = ctx->channels + i;

		if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) {
			continue;
		}

		const float fval = xm_next_of_sample(ch);

		if(!ch->muted && !ch->instrument->muted) {
			*left += fval * ch->actual_volume[0];
			*right += fval * ch->actual_volume[1];
		}

#if XM_RAMPING
		ch->frame_count++;
		XM_SLIDE_TOWARDS(ch->actual_volume[0], ch->target_volume[0], ctx->volume_ramp);
		XM_SLIDE_TOWARDS(ch->actual_volume[1], ch->target_volume[1], ctx->volume_ramp);
#endif
	}

	const float fgvol = ctx->global_volume * ctx->amplification;
	*left *= fgvol;
	*right *= fgvol;

	if(XM_DEBUG) {
		if(fabs(*left) > 1 || fabs(*right) > 1) {
			DEBUG("clipping frame: %f %f, this is a bad module or a libxm bug", *left, *right);
		}
	}
}

void xm_generate_samples(xm_context_t* ctx, float* output, size_t numsamples) {
	ctx->generated_samples += numsamples;

	for(size_t i = 0; i < numsamples; i++) {
		xm_sample(ctx, output + (2 * i), output + (2 * i + 1));
	}
}