forked from a1k0n/jsxm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmeffects.js
346 lines (314 loc) · 8.38 KB
/
xmeffects.js
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
(function (window) {
if (!window.XMPlayer) {
window.XMPlayer = {};
}
var player = window.XMPlayer;
function eff_t1_0(ch) { // arpeggio
if (ch.effectdata !== 0 && ch.inst !== undefined) {
var arpeggio = [0, ch.effectdata>>4, ch.effectdata&15];
var note = ch.note + arpeggio[player.cur_tick % 3];
ch.period = player.periodForNote(ch, note);
}
}
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) {
// is this limited? it appears not
ch.period -= ch.slideupspeed;
}
}
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) {
// 1728 is the period for C-1
ch.period = Math.min(1728, 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);
}
}
}
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;
if (isNaN(ch.periodoffset)) {
console.log("vibrato periodoffset NaN?",
ch.vibratopos, ch.vibratospeed, ch.vibratodepth);
ch.periodoffset = 0;
}
// only updates on non-first ticks
if (player.cur_tick > 0) {
ch.vibratopos += ch.vibratospeed;
ch.vibratopos &= 63;
}
}
function getVibratoDelta(type, x) {
var delta = 0;
switch (type & 0x03) {
case 1: // sawtooth (ramp-down)
delta = ((1 + x * 2 / 64) % 2) - 1;
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 = Math.sin(x * Math.PI / 32);
break;
}
return delta;
}
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
ch.off = data * 256;
}
function eff_t0_a(ch, data) { // volume slide
if (data) {
ch.volumeslide = -(data & 0x0f) + (data >> 4);
}
}
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 (untested)
if (data < player.xm.songpats.length) {
player.cur_songpos = data;
player.cur_pat = player.xm.songpats[player.cur_songpos];
player.cur_row = -1;
}
}
function eff_t0_c(ch, data) { // set volume
ch.vol = Math.min(64, data);
}
function eff_t0_d(ch, data) { // pattern jump
player.cur_songpos++;
if (player.cur_songpos >= player.xm.songpats.length)
player.cur_songpos = player.xm.song_looppos;
player.cur_pat = player.xm.songpats[player.cur_songpos];
player.cur_row = (data >> 4) * 10 + (data & 0x0f) - 1;
}
function eff_t0_e(ch, data) { // extended effects!
var eff = data >> 4;
data = data & 0x0f;
switch (eff) {
case 1: // fine porta up
ch.period -= data;
break;
case 2: // fine porta down
ch.period += data;
break;
case 4: // set vibrato waveform
ch.vibratotype = data & 0x07;
break;
case 5: // finetune
ch.fine = (data<<4) + data - 128;
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 0x0c: // note cut handled in eff_t1_e
break;
default:
console.log("unimplemented extended effect E", ch.effectdata.toString(16));
break;
}
}
function eff_t1_e(ch) { // note cut
switch (ch.effectdata >> 4) {
case 0x0c:
if (player.cur_tick == (ch.effectdata & 0x0f)) {
ch.vol = 0;
}
break;
}
}
function eff_t0_f(ch, data) { // set tempo
if (data === 0) {
console.log("tempo 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) {
// same as Axy but multiplied by 2
player.xm.global_volumeslide = (-(data & 0x0f) + (data >> 4)) * 2;
}
}
function eff_t1_h(ch) { // global volume slide
if (player.xm.global_volumeslide !== undefined) {
player.xm.global_volume = Math.max(0, Math.min(player.max_global_volume,
player.xm.global_volume + player.xm.global_volumeslide));
}
}
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);
// retrigger volume table
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 *= 2; ch.vol /= 3; break;
case 7: ch.vol /= 2; 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 *= 3; ch.vol /= 2; break;
case 0x0f: ch.vol *= 2; break;
}
ch.vol = Math.min(64, Math.max(0, ch.vol));
}
function eff_t1_r(ch) {
if (player.cur_tick % (ch.retrig & 0x0f) === 0) {
ch.off = 0;
}
}
function eff_unimplemented() {}
function eff_unimplemented_t0(ch, data) {
console.log("unimplemented effect", player.prettify_effect(ch.effect, data));
}
player.effects_t0 = [ // effect functions on tick 0
eff_t1_0, // 1, arpeggio is processed on all ticks
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_unimplemented_t0, // 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
eff_unimplemented_t0, // i
eff_unimplemented_t0, // j
eff_unimplemented_t0, // k
eff_unimplemented_t0, // l
eff_unimplemented_t0, // m
eff_unimplemented_t0, // n
eff_unimplemented_t0, // o
eff_unimplemented_t0, // p
eff_unimplemented_t0, // q
eff_t0_r, // r
eff_unimplemented_t0, // s
eff_unimplemented_t0, // t
eff_unimplemented_t0, // u
eff_unimplemented_t0, // v
eff_unimplemented_t0, // w
eff_unimplemented_t0, // x
eff_unimplemented_t0, // y
eff_unimplemented_t0, // 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_unimplemented, // 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
eff_unimplemented, // i
eff_unimplemented, // j
eff_unimplemented, // k
eff_unimplemented, // l
eff_unimplemented, // m
eff_unimplemented, // n
eff_unimplemented, // o
eff_unimplemented, // p
eff_unimplemented, // q
eff_t1_r, // r
eff_unimplemented, // s
eff_unimplemented, // t
eff_unimplemented, // u
eff_unimplemented, // v
eff_unimplemented, // w
eff_unimplemented, // x
eff_unimplemented, // y
eff_unimplemented // z
];
})(window);