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
use crate::{prelude::NewNoteAction, waveform::Waveform};
use serde::{Deserialize, Serialize};
// Float math backend (only needed when `std` is disabled).
// Priority: std > libm > micromath.
#[cfg(all(not(feature = "std"), not(feature = "libm"), feature = "micromath"))]
#[allow(unused_imports)]
use micromath::F32Ext;
#[cfg(all(not(feature = "std"), feature = "libm"))]
#[allow(unused_imports)]
use num_traits::float::Float;
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum NoteRetrigOperator {
#[default]
None,
Sum(f32),
Mul(f32),
}
#[repr(u8)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TrackEffect {
/// Pitch effect
Arpeggio {
/// 1st halftone
half1: usize,
/// 2nd halftone
half2: usize,
},
/// `value`, set the Channel Volume
/// Channel Volume effect
ChannelVolume(f32),
// `(speed, tick)`, slides the current channel volume up or down
/// Channel Volume effect
ChannelVolumeSlide {
speed: f32,
/// if true, only at tick0, otherwise from tick1
fine: bool,
},
/// `bool`, round to the nearest halftone when using effects
/// Pitch effect
Glissando(bool),
/// `tune`, this effet should be used together with a note.
/// It will cause another fine-tune value to be used
/// Pitch effect
InstrumentFineTune(f32),
/// `nna` Change Instrument New Note Action
/// Misc effect
InstrumentNewNoteAction(NewNoteAction),
/// `position`, change the panning envelope position
/// Panning effect
InstrumentPanningEnvelopePosition(usize),
/// `activate`, set the panning envelope on
/// Panning effect
InstrumentPanningEnvelope(bool),
/// `activate`, set the pitch envelope on
/// Pitch effect
InstrumentPitchEnvelope(bool),
/// `offset`, this effect should be used together with a note
/// The sample will be played from `offset` instead of zero
/// Misc effect
InstrumentSampleOffset(usize),
/// `surround`
/// Misc effect
InstrumentSurround(bool),
/// `position`, change the volume envelope position
/// Volume effect
InstrumentVolumeEnvelopePosition(usize),
/// `activate`, set the volume envelope on
/// Volume effect
InstrumentVolumeEnvelope(bool),
/// `(tick, past)`, cut the note at the specified tick.
/// if past is true, do it for past note too
/// Note that it will only set the volume to zero, and the sample will still be played.
/// Volume effect
NoteCut {
/// cut at tick
tick: usize,
/// do it for past note too
past: bool,
},
/// `ticks`, this effect will delay the note the selected number of ticks
/// Misc effect
NoteDelay(usize),
/// `(tick, past)`, fadeout the note at the specified tick
/// if past is true, do it for past note too
/// Volume effect
NoteFadeOut {
/// fadeout the note at the specified tick
tick: usize,
/// do it for past note too
past: bool,
},
/// `tick`, this effect will trigger a "Note Off" at the specified tick
/// if past is true, do it for past note too
/// Misc effect
NoteOff {
/// off the note at the specified tick
tick: usize,
/// do it for past note too
past: bool,
},
/// `(speed, volume modifier)`
/// Misc effect
NoteRetrig {
speed: usize,
volume_modifier: NoteRetrigOperator,
},
/// `(speed, depth)`, set Panbrello
/// Panning effect
Panbrello { speed: f32, depth: f32 },
/// `(waveform, retrig)`, change Panbrello waveform.
/// Panning effect
PanbrelloWaveform {
waveform: Waveform,
/// retrig when a new instrument is played.
retrig: bool,
},
/// `position` [0.0..1.0], sets the panning position
/// 0.0 is the leftmost position and 1.0 the rightmost
/// Panning effect
Panning(f32),
/// `(speed, tick)`, this effect slide the panning position
/// Panning effect
PanningSlide {
speed: f32,
/// if true, only at tick0, otherwise from tick1
fine: bool,
},
/// `speed` (period units added per application), `fine` flag.
///
/// Pitch effect, covering every FT2 pitch-slide variant that
/// manipulates the raw period directly (as opposed to
/// [`Self::TonePortamento`] which slides toward a target note):
///
/// * `fine = false` — *regular* slide (FT2 1xx, 2xx; S3M Exx, Fxx
/// with hi-nibble < 0xE; IT Exx, Fxx with hi-nibble < 0xE): the
/// replayer applies `speed` to the period on every non-zero
/// tick of the row.
/// * `fine = true` — *fine* or *extra-fine* slide (FT2 E1x, E2x,
/// X1y, X2y; S3M EFx/FFx, EEx/FEx; IT EFx/FFx, EEx/FEx): the
/// replayer applies `speed` exactly once, at tick 0 of the row.
///
/// Sign encodes direction: a negative speed shortens the period
/// (pitch up); a positive speed lengthens it (pitch down). The
/// importer pre-scales by the format's unit factor so `speed` is
/// always in xmrs period units ready to be added to `period`.
Portamento { speed: f32, fine: bool },
/// `speed`, portamento to note at speed.
/// see `::Glissando` to round to the nearest halftone
/// Pitch efect
TonePortamento(f32),
/// `(speed, depth)`
/// Volume effect
Tremolo { speed: f32, depth: f32 },
/// `(waveform, retrig)`, change Tremolo waveform.
/// Volume effect
TremoloWaveform {
waveform: Waveform,
/// retrig when a new instrument is played.
retrig: bool,
},
/// `(On time, Off time)`
/// This weird effect will set the volume to zero during `Off time` number of ticks
/// Volume effect
Tremor {
on_time: usize,
/// set the volume to zero during off time numbre of ticks
off_time: usize,
},
/// `(speed, depth)`, set Vibrato
/// Pitch effect
Vibrato { speed: f32, depth: f32 },
/// `speed`, set Vibrato speed
/// Pitch effect
VibratoSpeed(f32),
/// `depth`, set Vibrato depth
/// Pitch effect
VibratoDepth(f32),
/// `(waveform, retrig)`, change Vibrato waveform.
/// Pitch effect
VibratoWaveform {
waveform: Waveform,
/// retrig when a new instrument is played.
retrig: bool,
},
/// `(value, tick)`, set the current volume at the current tick
/// Volume effect
Volume {
value: f32,
/// set at the current tick
tick: usize,
},
/// `(speed, tick)`, slides the current volume up or down
/// Volume effect
VolumeSlide {
speed: f32,
/// if true, only at tick0, otherwise from tick1
fine: bool,
},
}
/// MIDI-macro effect variant. IT's macro system has two user-
/// visible dispatch axes and a per-channel selector register; the
/// three variants below capture them distinctly.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MidiMacroType {
/// `SFx` — set the channel's active parametric-macro slot to
/// `idx` (0..15). Does not invoke the macro; the next `Zxx`
/// with `xx < 0x80` on this channel will use it.
SelectParametric(usize),
/// `Zxx` with `xx < 0x80` — invoke the channel's currently
/// selected parametric macro, substituting the byte `z` (0x7A)
/// in the macro string with the `xx` parameter.
Parametric(u8),
/// `Zxx` with `xx >= 0x80` — invoke fixed macro at `idx =
/// xx - 0x80`. The `z` byte (if present in the macro string)
/// is substituted with the literal `xx` parameter, matching
/// OpenMPT's interpretation.
Fixed { idx: usize, z: u8 },
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum GlobalEffect {
/// `bpm` set the BPM of the song
Bpm(usize),
/// `value` slide the BPM on every tick of the row
BpmSlide(isize),
/// Midi macro
MidiMacro(MidiMacroType),
/// `pattern-position`, jump to the next pattern and play from the specified position.
PatternBreak(usize),
/// `(quantity, unit)`, this effect will delay the row the selected number of units.
/// if `unit` is true, `unit` is on tempo, `else` unit is on tick
/// If multiple commands are on the same row, the sum of their parameters is used.
///
/// All effects in the row will be repeated for each row delayed.
///
/// if `unit` is true (tempo unit)
/// does not delay for the sum of all commands in in the row
/// else
/// delays by the sum of that row
PatternDelay {
quantity: usize,
/// if true, `unit` is on tempo, `else` unit is on tick
tempo: bool,
},
/// `value`, if value is zero, set the loopbackpoint
/// is value is a non-zero, loop from previous loopback point value times
PatternLoop(usize),
/// `position`, jump to the selected song position and play the pattern from the beginning
PositionJump(usize),
/// `speed` set the speed of the song
Speed(usize),
/// `value`, set the global Volume
Volume(f32),
/// `(speed, tick)`, slides the global volume up or down
/// if tick is true, only at tick0, otherwise from tick1
/// Volume
VolumeSlide { speed: f32, fine: bool },
}
impl TrackEffect {
pub fn merge(&self, other: &Self) -> Option<Self> {
match (self, other) {
(
TrackEffect::Arpeggio {
half1: h1,
half2: h2,
},
TrackEffect::Arpeggio {
half1: o1,
half2: o2,
},
) => Some(TrackEffect::Arpeggio {
half1: h1 + o1,
half2: h2 + o2,
}),
(TrackEffect::ChannelVolume(value1), TrackEffect::ChannelVolume(value2)) => Some(
TrackEffect::ChannelVolume((value1 + value2).clamp(0.0, 1.0)),
),
(
TrackEffect::ChannelVolumeSlide {
speed: h1,
fine: h_fine,
},
TrackEffect::ChannelVolumeSlide {
speed: o1,
fine: o_fine,
},
) if h_fine == o_fine => Some(TrackEffect::ChannelVolumeSlide {
speed: h1 + o1,
fine: *o_fine,
}),
(TrackEffect::Glissando(_h1), TrackEffect::Glissando(o1)) => {
Some(TrackEffect::Glissando(*o1)) // overwrite glissando value.
}
(TrackEffect::InstrumentFineTune(h1), TrackEffect::InstrumentFineTune(o1)) => {
Some(TrackEffect::InstrumentFineTune(h1 + o1))
}
(
TrackEffect::InstrumentPanningEnvelopePosition(h1),
TrackEffect::InstrumentPanningEnvelopePosition(o1),
) => Some(TrackEffect::InstrumentPanningEnvelopePosition(h1 + o1)),
(TrackEffect::InstrumentSampleOffset(h1), TrackEffect::InstrumentSampleOffset(o1)) => {
Some(TrackEffect::InstrumentSampleOffset(h1 + o1))
}
(
TrackEffect::InstrumentVolumeEnvelopePosition(h1),
TrackEffect::InstrumentVolumeEnvelopePosition(o1),
) => Some(TrackEffect::InstrumentVolumeEnvelopePosition(h1 + o1)),
(
TrackEffect::NoteCut {
tick: h1,
past: _h2,
},
TrackEffect::NoteCut { tick: o1, past: o2 },
) => Some(TrackEffect::NoteCut {
tick: h1 + o1,
past: *o2,
}),
(TrackEffect::NoteDelay(h1), TrackEffect::NoteDelay(o1)) => {
Some(TrackEffect::NoteDelay(h1 + o1))
}
(
TrackEffect::NoteFadeOut {
tick: h1,
past: _h2,
},
TrackEffect::NoteFadeOut { tick: o1, past: o2 },
) => Some(TrackEffect::NoteFadeOut {
tick: h1 + o1,
past: *o2,
}),
(
TrackEffect::NoteOff {
tick: h1,
past: _h2,
},
TrackEffect::NoteOff { tick: o1, past: o2 },
) => Some(TrackEffect::NoteOff {
tick: h1 + o1,
past: *o2,
}),
(
TrackEffect::NoteRetrig {
speed: h1,
volume_modifier: h2,
},
TrackEffect::NoteRetrig {
speed: o1,
volume_modifier: o2,
},
) => Some(TrackEffect::NoteRetrig {
speed: h1 + o1,
volume_modifier: match o2 {
NoteRetrigOperator::None => h2.clone(),
_ => o2.clone(),
},
}),
(
TrackEffect::Panbrello {
speed: h1,
depth: h2,
},
TrackEffect::Panbrello {
speed: o1,
depth: o2,
},
) => Some(TrackEffect::Panbrello {
speed: h1 + o1,
depth: h2 + o2,
}),
(
TrackEffect::PanbrelloWaveform {
waveform: _h1,
retrig: _h2,
},
TrackEffect::PanbrelloWaveform {
waveform: o1,
retrig: o2,
},
) => {
Some(TrackEffect::PanbrelloWaveform {
waveform: *o1,
retrig: *o2,
}) // overwrite values
}
(TrackEffect::Panning(h1), TrackEffect::Panning(o1)) => {
Some(TrackEffect::Panning(h1 + o1))
}
(
TrackEffect::PanningSlide {
speed: h1,
fine: h_fine,
},
TrackEffect::PanningSlide {
speed: o1,
fine: o_fine,
},
) if h_fine == o_fine => Some(TrackEffect::PanningSlide {
speed: h1 + o1,
fine: *o_fine,
}),
(
TrackEffect::Portamento {
speed: h1,
fine: h_fine,
},
TrackEffect::Portamento {
speed: o1,
fine: o_fine,
},
) if h_fine == o_fine => Some(TrackEffect::Portamento {
speed: h1 + o1,
fine: *o_fine,
}),
(TrackEffect::TonePortamento(h1), TrackEffect::TonePortamento(o1)) => {
Some(TrackEffect::TonePortamento(h1 + o1))
}
(
TrackEffect::Tremolo {
speed: h1,
depth: h2,
},
TrackEffect::Tremolo {
speed: o1,
depth: o2,
},
) => Some(TrackEffect::Tremolo {
speed: h1 + o1,
depth: h2 + o2,
}),
(
TrackEffect::TremoloWaveform {
waveform: _h1,
retrig: _h2,
},
TrackEffect::TremoloWaveform {
waveform: o1,
retrig: o2,
},
) => {
Some(TrackEffect::TremoloWaveform {
waveform: *o1,
retrig: *o2,
}) // overwrite values
}
(
TrackEffect::Tremor {
on_time: h1,
off_time: h2,
},
TrackEffect::Tremor {
on_time: o1,
off_time: o2,
},
) => Some(TrackEffect::Tremor {
on_time: h1 + o1,
off_time: h2 + o2,
}),
(
TrackEffect::Vibrato {
speed: h1,
depth: h2,
},
TrackEffect::Vibrato {
speed: o1,
depth: o2,
},
) => Some(TrackEffect::Vibrato {
speed: h1 + o1,
depth: h2 + o2,
}),
(TrackEffect::VibratoSpeed(h1), TrackEffect::VibratoSpeed(o1)) => {
Some(TrackEffect::VibratoSpeed(h1 + o1))
}
(TrackEffect::VibratoDepth(h1), TrackEffect::VibratoDepth(o1)) => {
Some(TrackEffect::VibratoDepth(h1 + o1))
}
(
TrackEffect::VibratoWaveform {
waveform: _h1,
retrig: _h2,
},
TrackEffect::VibratoWaveform {
waveform: o1,
retrig: o2,
},
) => {
Some(TrackEffect::VibratoWaveform {
waveform: *o1,
retrig: *o2,
}) // overwrite values
}
(
TrackEffect::Volume {
value: value1,
tick: tick1,
},
TrackEffect::Volume {
value: value2,
tick: tick2,
},
) => Some(TrackEffect::Volume {
value: (value1 + value2).clamp(0.0, 1.0),
tick: tick1 + tick2,
}),
(
TrackEffect::VolumeSlide {
speed: speed1,
fine: h_fine,
},
TrackEffect::VolumeSlide {
speed: speed2,
fine: o_fine,
},
) if h_fine == o_fine => {
// Only merge slides that share the same tick scope
// (both fine OR both regular). A row may legitimately
// carry one of each (e.g. XM main-effect Axy + vol-col
// D/U), and `remove_duplicates_and_merge` keeps them as
// parallel entries when `merge` returns `None`.
Some(TrackEffect::VolumeSlide {
speed: speed1 + speed2,
fine: *o_fine,
})
}
_ => None,
}
}
}