forked from a1k0n/jsxm
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxmeffects.js
More file actions
564 lines (519 loc) · 15.2 KB
/
Copy pathxmeffects.js
File metadata and controls
564 lines (519 loc) · 15.2 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
(function (window) {
if (!window.XMPlayer) {
window.XMPlayer = {};
}
var player = window.XMPlayer;
function eff_t1_0(ch) { // arpeggio
if (ch.effectdata !== 0 && ch.inst !== undefined) {
// FT2 indexes arpeggioTab with countdown tick; equivalent to (tempo - tick) % 3
var tick3 = (player.xm.tempo - player.cur_tick) % 3;
var ofs = tick3 === 1 ? ch.effectdata >> 4 : tick3 === 2 ? ch.effectdata & 15 : 0;
// TODO: FT2 uses period2NotePeriod(ch.realPeriod, noteOffset, ch) which resolves
// from the CURRENT period (may have been shifted by portamento), not from the
// original note. This matters when arpeggio and portamento interact.
// Also: at tempo > 16, FT2's arpeggioTab overflows into unrelated binary data
// producing undefined behavior; our %3 always gives clean results (minor difference).
ch.periodoffset = player.periodForNote(ch, ch.note + ofs) - ch.period;
}
}
function eff_t0_1(ch, data) { // pitch slide up
if (data !== 0) {
ch.slideupspeed = data;
}
}
function eff_t1_1(ch) { // pitch slide up
if (ch.slideupspeed !== undefined) {
ch.period -= ch.slideupspeed;
if (ch.period < 1) ch.period = 1;
}
}
function eff_t0_2(ch, data) { // pitch slide down
if (data !== 0) {
ch.slidedownspeed = data;
}
}
function eff_t1_2(ch) { // pitch slide down
if (ch.slidedownspeed !== undefined) {
// FT2 clamps at period 31999 (= 7999 in JS 1/4-scale periods)
ch.period = Math.min(7999, ch.period + ch.slidedownspeed);
}
}
function eff_t0_3(ch, data) { // portamento
if (data !== 0) {
ch.portaspeed = data;
}
}
function eff_t1_3(ch) { // portamento
if (ch.periodtarget !== undefined && ch.portaspeed !== undefined) {
if (ch.period > ch.periodtarget) {
ch.period = Math.max(ch.periodtarget, ch.period - ch.portaspeed);
} else {
ch.period = Math.min(ch.periodtarget, ch.period + ch.portaspeed);
}
if (ch.glissando) {
// TODO: Broken in Amiga mode. Rounding to 16 period units assumes linear
// periods where each semitone = 16 units. In Amiga mode, semitones are not
// equally spaced. FT2 uses period2NotePeriod(realPeriod, 0, ch) to snap to
// the nearest note period via binary search in amigaPeriodLUT.
// round to nearest semitone (16 period units per semitone)
ch.periodoffset = Math.round(ch.period / 16) * 16 - ch.period;
}
}
}
function eff_t0_4(ch, data) { // vibrato
if (data & 0x0f) {
ch.vibratodepth = (data & 0x0f) * 2;
}
if (data >> 4) {
ch.vibratospeed = data >> 4;
}
eff_t1_4(ch);
}
function eff_t1_4(ch) { // vibrato
ch.periodoffset = getVibratoDelta(ch.vibratotype, ch.vibratopos) * ch.vibratodepth;
// FT2 updates vibratoPos on every tick including tick 0
ch.vibratopos += ch.vibratospeed;
ch.vibratopos &= 63;
}
// Vibrato sine LUT defined in xm.js; reference it from player
function getVibratoDelta(type, x) {
var delta = 0;
switch (type & 0x03) {
case 1: // ramp (FT2: index<<3, then bitwise NOT for negative half)
var idx = x & 31;
delta = x < 32 ? (idx << 3) / 256 : -((255 - (idx << 3)) & 0xFF) / 256;
break;
case 2: // square
case 3: // random (in FT2 these two are the same)
delta = x < 32 ? 1 : -1;
break;
case 0:
default: // sine
delta = player.vibratoSineLUT[x];
break;
}
return delta;
}
function eff_t0_7(ch, data) { // tremolo
if (data & 0x0f) {
ch.tremolodepth = data & 0x0f;
}
if (data >> 4) {
ch.tremolospeed = data >> 4;
}
eff_t1_7(ch);
}
function eff_t1_7(ch) { // tremolo
// FT2 bug: uses vibratoPos for sign instead of tremoloPos (in both
// the ramp waveform shape and the overall sign determination)
var idx = ch.tremolopos & 31;
var amp;
switch (ch.tremolotype & 3) {
case 1: // ramp — FT2 bug: ramp shape flipped by vibratoPos
amp = (idx << 3) / 256;
if (ch.vibratopos >= 32) amp = ((255 - (idx << 3)) & 0xFF) / 256;
break;
case 2:
case 3: // square
amp = 1;
break;
case 0:
default: // sine (half-wave lookup, always positive)
amp = player.vibratoSineLUT[idx];
break;
}
// Overall sign from tremoloPos (correct in FT2; the vibratoPos bug only affects ramp shape)
ch.voloffset = (ch.tremolopos >= 32 ? -amp : amp) * ch.tremolodepth * 4;
ch.tremolopos += ch.tremolospeed;
ch.tremolopos &= 63;
}
function eff_t1_5(ch) { // portamento + volume slide
eff_t1_a(ch);
eff_t1_3(ch);
}
function eff_t1_6(ch) { // vibrato + volume slide
eff_t1_a(ch);
eff_t1_4(ch);
}
function eff_t0_8(ch, data) { // set panning
ch.pan = data;
}
function eff_t0_9(ch, data) { // sample offset
// FT2: only stores the offset memory here; actual offset is applied in triggerNote()
if (data !== 0) ch.offsetmemory = data;
}
function eff_t0_a(ch, data) { // volume slide
if (data) {
// FT2: high nibble takes priority over low nibble
if (data & 0xf0) {
ch.volumeslide = data >> 4;
} else {
ch.volumeslide = -(data & 0x0f);
}
}
}
function eff_t1_a(ch) { // volume slide
if (ch.volumeslide !== undefined) {
ch.vol = Math.max(0, Math.min(64, ch.vol + ch.volumeslide));
}
}
function eff_t0_b(ch, data) { // song jump (deferred)
player.posJumpFlag = true;
player.posJumpPos = data;
player.pBreakPos = 0; // FT2: Bxx resets pBreakPos
}
function eff_t0_c(ch, data) { // set volume
ch.vol = Math.min(64, data);
}
function eff_t0_d(ch, data) { // pattern break (deferred)
player.posJumpFlag = true;
player.pBreakPos = (data >> 4) * 10 + (data & 0x0f);
if (player.pBreakPos > 63) player.pBreakPos = 0;
}
function eff_t0_e(ch, data) { // extended effects!
var eff = data >> 4;
data = data & 0x0f;
switch (eff) {
case 1: // fine porta up
if (data !== 0) ch.fineportaup = data;
else data = ch.fineportaup || 0;
ch.period -= data;
if (ch.period < 1) ch.period = 1;
break;
case 2: // fine porta down
if (data !== 0) ch.fineportadown = data;
else data = ch.fineportadown || 0;
ch.period = Math.min(7999, ch.period + data);
break;
case 3: // glissando control
ch.glissando = data;
break;
case 4: // set vibrato waveform
ch.vibratotype = data & 0x07;
break;
case 5: // finetune
ch.fine = (data << 4) - 128;
break;
case 6: // pattern loop
if (data == 0) {
ch.loopstart = player.cur_row;
} else if (ch.loopremaining === undefined || ch.loopremaining === 0) {
ch.loopremaining = data;
player.pBreakPos = ch.loopstart || 0;
player.pBreakFlag = true;
} else if (--ch.loopremaining > 0) {
player.pBreakPos = ch.loopstart || 0;
player.pBreakFlag = true;
}
break;
case 7: // set tremolo waveform
ch.tremolotype = data & 0x07;
break;
case 8: // panning
ch.pan = data * 0x11;
break;
case 0x0a: // fine vol slide up (with memory)
if (data === 0 && ch.finevolup !== undefined)
data = ch.finevolup;
ch.vol = Math.min(64, ch.vol + data);
ch.finevolup = data;
break;
case 0x0b: // fine vol slide down
if (data === 0 && ch.finevoldown !== undefined)
data = ch.finevoldown;
ch.vol = Math.max(0, ch.vol - data);
ch.finevoldown = data;
break;
case 9: // retrig note
if (data !== 0) {
ch.retrig_interval = data;
}
break;
case 0x0c: // note cut at tick 0
if (data === 0) ch.vol = 0;
break;
case 0x0d: // note delay - handled in eff_t1_e and nextRow
break;
case 0x0e: // pattern delay
if (player.patterndelay === undefined) {
player.patterndelay = data;
}
break;
default:
break;
}
}
function eff_t1_e(ch) { // extended effects tick 1+
switch (ch.effectdata >> 4) {
case 9: // retrig note
if (ch.retrig_interval &&
player.cur_tick % ch.retrig_interval === 0) {
var inst = ch.inst;
if (!inst || !inst.samplemap) break;
// FT2: triggerNote(0,0,0,ch) — crossfade + restart voice
player.snapshotFadeVoice(ch);
if (ch.samp) {
ch.fine = ch.samp.fine;
}
if (ch.note !== undefined) {
ch.period = player.periodForNote(ch, ch.note);
}
ch.off = 0;
player.startVoiceQuickRamp(ch);
// FT2: triggerInstrument(ch) — reset envelopes, fadeout, vibrato, etc.
player.triggerInstrument(ch, inst);
}
break;
case 0x0c: // note cut
if (player.cur_tick === (ch.effectdata & 0x0f)) {
ch.vol = 0;
}
break;
case 0x0d: // note delay
if (player.cur_tick === (ch.effectdata & 0x0f)) {
player.triggerNote(ch);
}
break;
}
}
function eff_t0_f(ch, data) { // set tempo
if (data === 0) return;
else if (data < 0x20) {
player.xm.tempo = data;
} else {
player.xm.bpm = data;
}
}
function eff_t0_g(ch, data) { // set global volume
if (data <= 0x40) {
// volume gets multiplied by 2 to match
// the initial max global volume of 128
player.xm.global_volume = Math.max(0, data * 2);
} else {
player.xm.global_volume = player.max_global_volume;
}
}
function eff_t0_h(ch, data) { // global volume slide
if (data) {
// FT2: high nibble takes priority (same rule as Axy), multiplied by 2
// FT2: memory is per-channel (ch->globVolSlideSpeed)
if (data & 0xf0) {
ch.globalvolumeslide = (data >> 4) * 2;
} else {
ch.globalvolumeslide = -(data & 0x0f) * 2;
}
}
}
function eff_t1_h(ch) { // global volume slide
if (ch.globalvolumeslide !== undefined) {
player.xm.global_volume = Math.max(0, Math.min(player.max_global_volume,
player.xm.global_volume + ch.globalvolumeslide));
}
}
function retriggerVolume(ch) {
switch (ch.retrig >> 4) {
case 1: ch.vol -= 1; break;
case 2: ch.vol -= 2; break;
case 3: ch.vol -= 4; break;
case 4: ch.vol -= 8; break;
case 5: ch.vol -= 16; break;
case 6: ch.vol = (ch.vol >> 1) + (ch.vol >> 3) + (ch.vol >> 4); break;
case 7: ch.vol = ch.vol >> 1; break;
case 9: ch.vol += 1; break;
case 0x0a: ch.vol += 2; break;
case 0x0b: ch.vol += 4; break;
case 0x0c: ch.vol += 8; break;
case 0x0d: ch.vol += 16; break;
case 0x0e: ch.vol = (ch.vol * 3 / 2) | 0; break;
case 0x0f: ch.vol *= 2; break;
}
ch.vol = Math.min(64, Math.max(0, ch.vol));
}
// FT2: Rxy retrig calls triggerNote (voice restart + quick vol ramp),
// NOT triggerInstrument. Envelopes, fadeout, and key-off are NOT reset.
function doMultiNoteRetrig(ch) {
ch.retrigcounter = (ch.retrigcounter || 0) + 1;
if (ch.retrigcounter >= (ch.retrig & 0x0f)) {
ch.retrigcounter = 0;
retriggerVolume(ch);
// FT2: after volume modification, volume column set-vol/set-pan overrides
if (ch.volColumnVol >= 0x10 && ch.volColumnVol <= 0x50) {
ch.vol = ch.volColumnVol - 0x10;
} else if (ch.volColumnVol >= 0xc0 && ch.volColumnVol <= 0xcf) {
ch.pan = (ch.volColumnVol & 0x0f) << 4;
}
// FT2: triggerNote(0,0,0,ch) — reset finetune and recalculate period
if (ch.samp) {
ch.fine = ch.samp.fine;
}
if (ch.note !== undefined) {
ch.period = player.periodForNote(ch, ch.note);
}
// FT2: triggerNote restarts voice with quick volume ramp (crossfade)
player.snapshotFadeVoice(ch);
ch.off = 0;
player.startVoiceQuickRamp(ch);
}
}
function eff_t0_r(ch, data) { // retrigger
if (data & 0x0f) ch.retrig = (ch.retrig & 0xf0) + (data & 0x0f);
if (data & 0xf0) ch.retrig = (ch.retrig & 0x0f) + (data & 0xf0);
// FT2 quirk: skip tick-0 retrigger when volume column has data
if (!ch.hasVolColumn) {
doMultiNoteRetrig(ch);
}
}
function eff_t1_r(ch) {
doMultiNoteRetrig(ch);
}
function eff_t0_p(ch, data) { // panning slide
if (data) {
// FT2: high nibble takes priority
if (data & 0xf0) {
ch.panslide = data >> 4;
} else {
ch.panslide = -(data & 0x0f);
}
}
}
function eff_t1_p(ch) { // panning slide
if (ch.panslide !== undefined) {
ch.pan = Math.max(0, Math.min(255, ch.pan + ch.panslide));
}
}
function eff_t0_x(ch, data) { // extra fine portamento
var val = data & 0x0f;
if ((data >> 4) === 1) { // X1x - extra fine porta up
if (val !== 0) ch.extrafineportaup = val;
else val = ch.extrafineportaup || 0;
// FT2: extra fine porta subtracts raw param (no *4), so in 1/4-scale periods: /4
ch.period -= val / 4;
} else if ((data >> 4) === 2) { // X2x - extra fine porta down
if (val !== 0) ch.extrafineportadown = val;
else val = ch.extrafineportadown || 0;
ch.period += val / 4;
}
}
function eff_t0_t(ch, data) { // tremor
if (data) {
ch.tremorParam = data;
}
}
function eff_t1_t(ch) { // tremor
// FT2 tremor: tremorPos is a packed byte with bit 7 = sign (0x80=on, 0x00=off),
// bits 0-6 = countdown. When countdown underflows, toggle sign and reload.
var param = ch.tremorParam;
if (param === undefined) return;
var tremorSign = (ch.tremorPos || 0) & 0x80;
var tremorData = (ch.tremorPos || 0) & 0x7f;
tremorData--;
if (tremorData < 0) {
if (tremorSign === 0x80) {
tremorSign = 0x00;
tremorData = param & 0x0f;
} else {
tremorSign = 0x80;
tremorData = param >> 4;
}
}
ch.tremorPos = tremorSign | tremorData;
if (tremorSign !== 0x80) {
ch.voloffset = -ch.vol; // mute during off phase
}
}
function eff_t0_l(ch, data) { // set envelope position
// FT2: only set volume envelope position if volume envelope is enabled
var flags = ch.inst && ch.inst.env_vol_flags;
if ((flags & 1) && ch.env_vol) {
ch.env_vol.tick = data;
}
// FT2 bug: checks volEnvFlags & ENV_SUSTAIN, not panEnvFlags & ENV_ENABLED
if ((flags & 2) && ch.env_pan) {
ch.env_pan.tick = data;
}
}
function eff_t0_k(ch, data) { // key off at tick 0
if (data === 0) player.keyOff(ch);
}
function eff_t1_k(ch) { // key off at tick
if (player.cur_tick === (ch.effectdata & 31)) {
player.keyOff(ch);
}
}
player.effects_t0 = [ // effect functions on tick 0
null, // 0, arpeggio does nothing on tick 0 (FT2: dummy)
eff_t0_1,
eff_t0_2,
eff_t0_3,
eff_t0_4, // 4
eff_t0_a, // 5, same as A on first tick
eff_t0_a, // 6, same as A on first tick
eff_t0_7, // 7
eff_t0_8, // 8
eff_t0_9, // 9
eff_t0_a, // a
eff_t0_b, // b
eff_t0_c, // c
eff_t0_d, // d
eff_t0_e, // e
eff_t0_f, // f
eff_t0_g, // g
eff_t0_h, // h
null, // i
null, // j
eff_t0_k, // k
eff_t0_l, // l
null, // m
null, // n
null, // o
eff_t0_p, // p
null, // q
eff_t0_r, // r
null, // s
eff_t0_t, // t
null, // u
null, // v
null, // w
eff_t0_x, // x
null, // y
null, // z
];
player.effects_t1 = [ // effect functions on tick 1+
eff_t1_0,
eff_t1_1,
eff_t1_2,
eff_t1_3,
eff_t1_4,
eff_t1_5, // 5
eff_t1_6, // 6
eff_t1_7, // 7
null, // 8
null, // 9
eff_t1_a, // a
null, // b
null, // c
null, // d
eff_t1_e, // e
null, // f
null, // g
eff_t1_h, // h
null, // i
null, // j
eff_t1_k, // k
null, // l
null, // m
null, // n
null, // o
eff_t1_p, // p
null, // q
eff_t1_r, // r
null, // s
eff_t1_t, // t
null, // u
null, // v
null, // w
null, // x
null, // y
null // z
];
})(window);