xmrs 0.10.1

A library to edit SoundTracker data with pleasure
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
/*
InitCommandTable and CommandTable (see `IT_MUSIC.ASM`):

T | Effect (letter and params)  | Code inside IT file     | InitCommandTable | CommandTable | Comment                                           |
- |-----------------------------|-------------------------|------------------|--------------|---------------------------------------------------|
V | Axx                         | 0x01                    | true             | false        | Set song speed (xx = speed)                       |
V | Bxx                         | 0x02                    | true             | false        | Jump to order xx                                  |
V | Cxx                         | 0x03                    | true             | false        | Break to row xx                                   |
V | Dxy                         | 0x04                    | true             | true         | Volume slide                                      |
V | Exy                         | 0x05                    | true             | true         | Pitch slide down                                  |
V | Fxy                         | 0x06                    | true             | true         | Pitch slide up                                    |
V | Gxx                         | 0x07                    | true             | true         | Slide to note                                     |
V | Hxy                         | 0x08                    | true             | true         | Vibrato                                           |
V | Ixy                         | 0x09                    | true             | true         | Tremor                                            |
V | Jxy                         | 0x0A                    | true             | true         | Arpeggio                                          |
V | Kxy                         | 0x0B                    | true             | true         | H00 Vibrato + Dxy Volume Slide                    |
V | Lxy                         | 0x0C                    | true             | true         | G00 Portamento + Dxy Volume slide                 |
V | Mxx                         | 0x0D                    | true             | false        | Set channel volume                                |
V | Nxy                         | 0x0E                    | true             | true         | Channel volume slide                              |
V | Oxx                         | 0x0F                    | true             | false        | Set sample offset                                 |
V | Pxx                         | 0x10                    | true             | true         | Panning slide                                     |
V | Qxy                         | 0x11                    | true             | true         | Retriggers a note after y ticks w/ vol modifier x |
V | Rxy                         | 0x12                    | true             | true         | Tremolo                                           |
V | Sxx                         | 0x13                    | true             | true         | Special commands (voir S-effects ci-dessous)      |
V | Txy                         | 0x14                    | true             | true         | Tempo slide                                       |
V | Uxy                         | 0x15                    | true             | true (Hxx!)  | Fine vibrato with speed x, depth y                |
V | Vxx                         | 0x16                    | true             | false        | Set global volume                                 |
V | Wxx                         | 0x17                    | true             | true         | Global volume slide                               |
V | Xxx                         | 0x18                    | true             | false        | Set panning position                              |
V | Yxy                         | 0x19                    | true             | true         | Panbrello                                         |
V | Zxx                         | 0x1A                    | true             | false        | Midi macro                                        |

CommandSTable (see `IT_M_EFF.INC`):

| Sub-Effect Sxx             | Code inside IT file     | Comment                                   |
|---------------------------|-------------------------|-------------------------------------------|
| S3x                       | 0x03                    | Set vibrato waveform                      |
| S4x                       | 0x04                    | Set tremolo waveform                      |
| S5x                       | 0x05                    | Set panbrello waveform                    |
| S6x                       | 0x06                    | Extra delay of x frames                   |
| S7x                       | 0x07                    | Instrument functions (see CommandS7Table) |
| S8x                       | 0x08                    | Set pan                                   |
| S9x                       | 0x09                    | Set surround                              |
| SAx                       | 0x0A                    | Set high order offset                     |
| SBx                       | 0x0B                    | Loop control                              |
| SCx                       | 0x0C                    | Note cut                                  |
| SDx                       | 0x0D                    | Note delay                                |
| SEx                       | 0x0E                    | Pattern delay                             |
| SFx                       | 0x0F                    | MIDI macro select                         |


CommandS7Table (see `IT_M_EFF.INC`):

| Sub-Effect S7x              | Code inside IT file     | Comment                                         |
|----------------------------|-------------------------|-------------------------------------------------|
| S70                        | 0x70                    | Past note cut (coupe la note précédente)        |
| S71                        | 0x71                    | Past note off (arrête la note précédente)       |
| S72                        | 0x72                    | Past note fade (fait fondre la note précédente) |
| S73                        | 0x73                    | Set NNA to cut (note suivante est coupée)       |
| S74                        | 0x74                    | Set NNA to continue (note suivante continue)    |
| S75                        | 0x75                    | Set NNA to off (désactive la note suivante)     |
| S76                        | 0x76                    | Set NNA to fade (fond la note suivante)         |
| S77                        | 0x77                    | Enable volume envelope                          |
| S78                        | 0x78                    | Disable volume envelope                         |
| S79                        | 0x79                    | Enable panning envelope                         |
| S7A                        | 0x7A                    | Disable panning envelope                        |
| S7B                        | 0x7B                    | Enable pitch envelope                           |
| S7C                        | 0x7C                    | Disable pitch envelope                          |


| Code inside IT file | Nearest in XM file  |
|---------------------|---------------------|
| 0x01                | 0x0F                |
| 0x02                | 0x0B                |
| 0x03                | 0x0D                |
| 0x04                | 0x0A                |
| 0x05                | 0x02,0x0E2y,0x212y  |
| 0x06                | 0x01,0x0E1y,0x211y  |
| 0x07                | 0x03                |
| 0x08                | 0x04                |
| 0x09                | 0x1D                |
| 0x0A                | 0x00                |
| 0x0B                | 0x06                |
| 0x0C                | 0x05                |
| 0x0D                | 0x0C                |
| 0x0E                | None                |
| 0x0F                | 0x09                |
| 0x10                | 0x19                |
| 0x11                | 0x1B                |
| 0x12                | 0x07                |
| 0x13                | 0x14                |
| 0x14                | 0x0F                |
| 0x15                | None                |
| 0x16                | 0x10                |
| 0x17                | None                |
| 0x18                | 0x15                |
| 0x19                | 0x17                |
| 0x1A                | 0x18                |

*/

use crate::effect::MidiMacroType;
use crate::import::patternslot::PatternSlot;
use crate::import::track_import_effect::TrackImportEffect;
use crate::import::track_import_unit::TrackImportUnit;
use crate::prelude::*;
use alloc::vec;
use alloc::vec::Vec;

pub struct ItEffect;

/// The idea here is to achieve a first level of abstraction
/// to make the data accessible via rust typing.
/// The data remains raw and is not interpreted.
impl ItEffect {
    // Volume slide helper
    // D0x Volume slide down
    // Dx0 Volume slide up
    // DFx Fine volume slide down
    // DxF Fine volume slide up
    //
    // ITTECH.TXT defines the test order as "Dx0, D0x, DxF, DFx"
    // (IT_MUSIC.ASM enforces the same order). For ambiguous values
    // where both nibbles are `F` (DFF / NFF / WFF), this ordering
    // resolves to the fine-slide-UP reading — so (f, 0xF) must be
    // tested before (0xF, f) in the Rust match, not after, or DFF
    // silently flips to a fine-slide-DOWN.
    fn it_volume_slide(fx: u8) -> Option<TrackImportEffect> {
        let nibble_high = (fx & 0xF0) >> 4;
        let nibble_low = fx & 0x0F;

        match (nibble_high, nibble_low) {
            (f, 0) => Some(TrackImportEffect::VolumeSlideN(f as f32 / 64.0)),
            (0, f) => Some(TrackImportEffect::VolumeSlideN(-(f as f32) / 64.0)),
            (f, 0xF) => Some(TrackImportEffect::VolumeSlide0(f as f32 / 64.0)),
            (0xF, f) => Some(TrackImportEffect::VolumeSlide0(-(f as f32) / 64.0)),
            _ => None,
        }
    }

    pub fn parse_it_effect(
        freq_type: FrequencyType,
        current: &PatternSlot,
    ) -> Option<Vec<TrackImportEffect>> {
        match current.effect_type {
            // Effect Volume Slide (Dxy)
            0x04 => Self::it_volume_slide(current.effect_parameter).map(|tie| vec![tie]),
            // Pitch slide down (Exy)
            // Exx Pitch slide down
            // EFx Fine pitch slide down
            // EEx Extra fine pitch slide down
            0x05 => {
                let fx = current.effect_parameter;
                let nibble_high = (fx & 0xF0) >> 4;
                let nibble_low = fx & 0x0F;

                match (nibble_high, nibble_low) {
                    (0xE, low) => {
                        Some(vec![TrackImportEffect::PortamentoExtraFineDown(low as f32)])
                    }
                    (0xF, low) => Some(vec![TrackImportEffect::PortamentoFineDown(
                        4.0 * low as f32,
                    )]),
                    (_high, _low) => Some(vec![TrackImportEffect::PortamentoDown(4.0 * fx as f32)]),
                }
            }

            // Pitch slide up (Fxy)
            // Fxx Pitch Slide up
            // FFx Fine Pitch slide up
            // FEx Extra fine pitch slide up
            0x06 => {
                let fx = current.effect_parameter;
                let nibble_high = (fx & 0xF0) >> 4;
                let nibble_low = fx & 0x0F;

                match (nibble_high, nibble_low) {
                    (0xE, low) => Some(vec![TrackImportEffect::PortamentoExtraFineUp(
                        -(low as f32),
                    )]),
                    (0xF, low) => {
                        Some(vec![TrackImportEffect::PortamentoFineUp(-4.0 * low as f32)])
                    }
                    (_high, _low) => Some(vec![TrackImportEffect::PortamentoUp(-4.0 * fx as f32)]),
                }
            }

            // Slide to note (Gxx)
            0x07 => {
                let param = current.effect_parameter as f32;
                let speed = match freq_type {
                    FrequencyType::LinearFrequencies => 4.0 * param,
                    FrequencyType::AmigaFrequencies => param,
                };
                Some(vec![TrackImportEffect::TonePortamento(speed)])
            }

            // Effect Vibrato (Hxy)
            0x08 => {
                let param = current.effect_parameter;
                let speed = ((param & 0xF0) >> 4) as f32 / 64.0;
                let depth = (param & 0x0F) as f32 * 4.0 / 16.0;
                Some(vec![TrackImportEffect::Vibrato(speed, depth)])
            }

            // Effect Tremor (Ixy)
            0x09 => {
                let param = current.effect_parameter;
                let on_time = param >> 4;
                let off_time = param & 0x0F;
                Some(vec![TrackImportEffect::Tremor(
                    on_time as usize,
                    off_time as usize,
                )])
            }

            // Effect Arpeggio (Jxy)
            0x0A => {
                let param = current.effect_parameter;
                if param > 0 {
                    let v1 = (param >> 4) as usize;
                    let v2 = (param & 0x0F) as usize;
                    Some(vec![TrackImportEffect::Arpeggio(v1, v2)])
                } else {
                    None
                }
            }

            // Kxy = H00 Vibrato + Dxy Volume Slide
            0x0B => {
                if let Some(vste) = Self::it_volume_slide(current.effect_parameter) {
                    Some(vec![TrackImportEffect::Vibrato(0.0, 0.0), vste])
                } else {
                    Some(vec![
                        TrackImportEffect::Vibrato(0.0, 0.0),
                        TrackImportEffect::VolumeSlideN(0.0),
                    ])
                }
            }

            // Lxy = G00 Portamento + Dxy Volume slide
            0x0C => {
                if let Some(vste) = Self::it_volume_slide(current.effect_parameter) {
                    Some(vec![TrackImportEffect::TonePortamento(0.0), vste])
                } else {
                    Some(vec![
                        TrackImportEffect::TonePortamento(0.0),
                        TrackImportEffect::VolumeSlideN(0.0),
                    ])
                }
            }

            // Set Channel Volume (Mxx)
            0x0D => {
                let volume = current.effect_parameter.min(64) as f32 / 64.0;
                Some(vec![TrackImportEffect::ChannelVolume(volume)])
            }

            // Effect Channel Volume Slide (Nxy)
            // N0x Channel volume slide down
            // Nx0 Channel volume slide up
            // NFx Fine channel volume slide down
            // NxF Fine channel volume slide up
            //
            // Same test-order as Dxy (see `it_volume_slide`).
            0x0E => {
                let fx = current.effect_parameter;
                let nibble_high = (fx & 0xF0) >> 4;
                let nibble_low = fx & 0x0F;

                match (nibble_high, nibble_low) {
                    (f, 0) => Some(vec![TrackImportEffect::ChannelVolumeSlideN(
                        f as f32 / 64.0,
                    )]),
                    (0, low) => Some(vec![TrackImportEffect::ChannelVolumeSlideN(
                        -(low as f32) / 64.0,
                    )]),
                    (f, 0xF) => Some(vec![TrackImportEffect::ChannelVolumeSlide0(
                        f as f32 / 64.0,
                    )]),
                    (0xF, low) => Some(vec![TrackImportEffect::ChannelVolumeSlide0(
                        -(low as f32) / 64.0,
                    )]),
                    _ => None,
                }
            }

            // Effect Set Sample Offset (Oxx)
            0x0F => Some(vec![TrackImportEffect::InstrumentSampleOffset(
                current.effect_parameter as usize * 256,
            )]),

            // Effect Panning Slide (Pxx)
            // P0x Pan slide left (subtract from panning)
            // Px0 Pan slide right (add to panning)
            // PFx Fine pan slide left
            // PxF Fine pan slide right
            //
            // Same test-order constraint as Dxy (see `it_volume_slide`):
            // (f, 0xF) is placed before (0xF, f) so PFF resolves to
            // fine-right, not fine-left. Also the upper nibble is
            // shifted down before matching, otherwise a literal `0xF`
            // would never equal the unshifted upper half (0xF0).
            0x10 => {
                let fx = current.effect_parameter;
                let nibble_high = (fx & 0xF0) >> 4;
                let nibble_low = fx & 0x0F;

                match (nibble_high, nibble_low) {
                    (f, 0) => Some(vec![TrackImportEffect::PanningSlideN(f as f32 / 16.0)]),
                    (0, f) => Some(vec![TrackImportEffect::PanningSlideN(-(f as f32) / 16.0)]),
                    (f, 0xF) => Some(vec![TrackImportEffect::PanningSlide0(f as f32 / 16.0)]),
                    (0xF, f) => Some(vec![TrackImportEffect::PanningSlide0(-(f as f32) / 16.0)]),
                    _ => None,
                }
            }

            // Effect Retrigger Note (Qxy) -> Retriggers a note after
            // y ticks with volume modifier x. IT inherits Qxy from
            // S3M (not FT2/XM Rxy), so the nibble-6 multiplier is
            // `vol * 10/16` (= 5/8), not FT2's `11/16`. Route
            // through the `NoteRetrigExtendedS3m` variant so the
            // volume-modifier table gets the S3M constants.
            0x11 => {
                let param = current.effect_parameter;
                let vol = param >> 4;
                let speed = param & 0x0F;
                Some(vec![TrackImportEffect::NoteRetrigExtendedS3m(
                    speed as usize,
                    vol as usize,
                )])
            }

            // Effect Tremolo (Rxy)
            0x12 => {
                let param = current.effect_parameter;
                let speed = ((param & 0xF0) >> 4) as f32 / 64.0;
                let depth = (param & 0x0F) as f32 / 16.0;
                Some(vec![TrackImportEffect::Tremolo(speed, depth)])
            }

            // Effect Special Commands (Sxx)
            0x13 => {
                let fx = current.effect_parameter >> 4;
                let param = current.effect_parameter & 0xF;
                match fx {
                    // Set vibrato waveform (S3x)
                    0x3 => {
                        let waveform = match param & 0b0000_0011 {
                            1 => Waveform::RampDown,
                            2 => Waveform::Square,
                            3 => Waveform::Random,
                            _ => Waveform::Sine,
                        };
                        Some(vec![TrackImportEffect::VibratoWaveform(waveform, true)])
                    }
                    // Set tremolo waveform (S4x)
                    0x4 => {
                        let waveform = match param & 0b0000_0011 {
                            1 => Waveform::RampDown,
                            2 => Waveform::Square,
                            3 => Waveform::Random,
                            _ => Waveform::Sine,
                        };
                        Some(vec![TrackImportEffect::TremoloWaveform(waveform, true)])
                    }
                    // Set panbrello waveform (S5x)
                    0x5 => {
                        let waveform = match param & 0b0000_0011 {
                            1 => Waveform::RampDown,
                            2 => Waveform::Square,
                            3 => Waveform::Random,
                            _ => Waveform::Sine,
                        };
                        Some(vec![TrackImportEffect::PanbrelloWaveform(waveform, true)])
                    }
                    0x7 => {
                        match param {
                            // S70 Past note cut
                            // **Cuts** all notes playing as a result of New Note Actions on the current channel
                            0x0 => Some(vec![TrackImportEffect::NoteCut(0, true)]),
                            // S71 Past note off
                            // **Sends a Note Off** to all notes playing as a result of New Note Actions on the current channel
                            0x1 => Some(vec![TrackImportEffect::NoteOff(0, true)]),
                            // S72 Past note fade
                            // **Fades out** all notes playing as a result of New Note Actions on the current channel
                            0x2 => Some(vec![TrackImportEffect::NoteFadeOut(0, true)]),
                            // S73 Set NNA to cut
                            0x3 => Some(vec![TrackImportEffect::InstrumentNewNoteAction(
                                NewNoteAction::NoteCut,
                            )]),
                            // S74 Set NNA to continue
                            0x4 => Some(vec![TrackImportEffect::InstrumentNewNoteAction(
                                NewNoteAction::Continue,
                            )]),
                            // S75 Set NNA to off
                            0x5 => Some(vec![TrackImportEffect::InstrumentNewNoteAction(
                                NewNoteAction::NoteOff,
                            )]),
                            // S76 Set NNA to fade
                            0x6 => Some(vec![TrackImportEffect::InstrumentNewNoteAction(
                                NewNoteAction::NoteFadeOut,
                            )]),
                            // S77 Set volume envelope on
                            0x7 => Some(vec![TrackImportEffect::InstrumentVolumeEnvelope(true)]),
                            // S78 Set volume envelope off
                            0x8 => Some(vec![TrackImportEffect::InstrumentVolumeEnvelope(false)]),
                            // S79 Set panning envelope on
                            0x9 => Some(vec![TrackImportEffect::InstrumentPanningEnvelope(true)]),
                            // S7A Set panning envelope off
                            0xA => Some(vec![TrackImportEffect::InstrumentPanningEnvelope(false)]),
                            // S7B Set pitch envelope on
                            0xB => Some(vec![TrackImportEffect::InstrumentPitchEnvelope(true)]),
                            // S7C Set pitch envelope off
                            0xC => Some(vec![TrackImportEffect::InstrumentPitchEnvelope(false)]),
                            _ => None,
                        }
                    }
                    0x8 => Some(vec![TrackImportEffect::Panning((param as f32) / 15.0)]),
                    0x9 => Some(vec![TrackImportEffect::InstrumentSurround(param == 1)]),
                    0xA => Some(vec![TrackImportEffect::InstrumentSampleOffsetAddHigh(
                        param as usize * 65536,
                    )]),
                    0xC => Some(vec![TrackImportEffect::NoteCut(param as usize, false)]),
                    0xD => Some(vec![TrackImportEffect::NoteDelay(param as usize)]),
                    _ => None,
                }
            }

            // Effect Fine Vibrato (Uxy)
            //
            // ITTECH: Uxy speed = 4*x (same as Hxy), depth = y (4x
            // finer than Hxy's depth = y*4). Same speed-unit reasoning
            // as Hxy — 4x/256-entry-table = x/64 per tick, not x/256.
            0x15 => {
                let param = current.effect_parameter;
                let speed = ((param & 0xF0) >> 4) as f32 / 64.0;
                let depth = (param & 0x0F) as f32 / 16.0;
                Some(vec![TrackImportEffect::Vibrato(speed, depth)])
            }

            // Effect Set panning position (Xxx)
            // Full 8-bit range 0..=255 maps to [0.0, 1.0]; divide by
            // 255, not 256, otherwise 0xFF (full right) renders as
            // 0.9961 and true full-right becomes unreachable.
            0x18 => Some(vec![TrackImportEffect::Panning(
                (current.effect_parameter as f32) / 255.0,
            )]),

            // Effect Panbrello (Yxy)
            0x19 => {
                let param = current.effect_parameter;
                let speed = ((param & 0xF0) >> 4) as f32 / 256.0;
                let depth = (param & 0x0F) as f32 * 4.0 / 16.0;
                Some(vec![TrackImportEffect::Panbrello(speed, depth)])
            }

            _ => None,
        }
    }

    pub fn parse_global_it_effect(current: &PatternSlot) -> Option<GlobalEffect> {
        match current.effect_type {
            // Effect Set Song Speed (Axx).
            // Per ITTECH, A00 is ignored (no change). Return None
            // so the effect column stays empty — otherwise the
            // sequencer would assign tempo = 0, which either stalls
            // the song or causes an infinite tick loop depending on
            // the tick-advance path.
            0x01 => {
                let speed = current.effect_parameter as usize;
                if speed == 0 {
                    None
                } else {
                    Some(GlobalEffect::Speed(speed))
                }
            }
            // Effect Jump to Order (Bxx)
            0x02 => Some(GlobalEffect::PositionJump(
                current.effect_parameter as usize,
            )),
            // Effect Break to Row (Cxx)
            0x03 => Some(GlobalEffect::PatternBreak(
                current.effect_parameter as usize,
            )),

            // Effect Special Commands (Sxx)
            0x13 => {
                let fx = current.effect_parameter >> 4;
                let param = current.effect_parameter & 0xF;
                match fx {
                    // Extra delay of x frames
                    // Extends the current row by x ticks.
                    // Note: if multiple S6x commands are on the same row, the player should sum their parameters.
                    0x6 => Some(GlobalEffect::PatternDelay {
                        quantity: param as usize,
                        tempo: false,
                    }),
                    // SB0 Set loopback point
                    // SBx Loop from previous SB0 x times
                    0xB => Some(GlobalEffect::PatternLoop(param as usize)),
                    // SEx Pattern delay for x rows.
                    // All effects in the row will be repeated for each row delayed.
                    0xE => Some(GlobalEffect::PatternDelay {
                        quantity: param as usize,
                        tempo: true,
                    }),
                    // SFx Sets the current channel's active parametered macro.
                    0xF => Some(GlobalEffect::MidiMacro(MidiMacroType::SelectParametric(
                        param as usize,
                    ))),
                    _ => None,
                }
            }

            // Effect Set Tempo (Txx)
            // T0x = tempo slide down by x
            // T1x = tempo slide up by x
            // Txx (xx >= 0x20) = set tempo to xx
            0x14 => {
                let param = current.effect_parameter;
                // Use `>> 4` so `upper_nibble` is the actual nibble
                // value 0..F, not the byte with its low nibble
                // cleared. With the previous `& 0xF0`, the match
                // arm `(1, f)` for T1x was dead — `upper_nibble` was
                // `0x10`, never `1`.
                let upper_nibble = param >> 4;
                let lower_nibble = param & 0x0F;

                match (upper_nibble, lower_nibble) {
                    (0, f) => Some(GlobalEffect::BpmSlide(-(f as isize))),
                    (1, f) => Some(GlobalEffect::BpmSlide(f as isize)),
                    _ => Some(GlobalEffect::Bpm(param as usize)),
                }
            }

            // Effect Set Global Volume (Vxx)
            // Per ITTECH GV ranges 0–128 (not 0–64 like the MOD/XM/S3M
            // families). Clamping to 64 would cap every IT module at
            // half global volume.
            0x16 => Some(GlobalEffect::Volume(
                (current.effect_parameter.min(128) as f32) / 128.0,
            )),

            // Effect Global Volume Slide (Wxy)
            // W0x Global volume slide down
            // Wx0 Global volume slide up
            // WFx Fine global volume slide down
            // WxF Fine global volume slide up
            //
            // Same test-order as Dxy (see `it_volume_slide`): test
            // (f, 0xF) before (0xF, f) so WFF resolves to fine-up.
            //
            // Scale is /128 (not /64 as in Dxy) because IT global
            // volume is 0–128, per ITTECH.
            0x17 => {
                let fx = current.effect_parameter;
                let nibble_high = (fx & 0xF0) >> 4;
                let nibble_low = fx & 0x0F;

                match (nibble_high, nibble_low) {
                    (f, 0) => Some(GlobalEffect::VolumeSlide {
                        speed: f as f32 / 128.0,
                        fine: false,
                    }),
                    (0, f) => Some(GlobalEffect::VolumeSlide {
                        speed: -(f as f32) / 128.0,
                        fine: false,
                    }),
                    (f, 0xF) => Some(GlobalEffect::VolumeSlide {
                        speed: f as f32 / 128.0,
                        fine: true,
                    }),
                    (0xF, f) => Some(GlobalEffect::VolumeSlide {
                        speed: -(f as f32) / 128.0,
                        fine: true,
                    }),
                    _ => None,
                }
            }

            // Effect Midi macro (Zxx)
            // Per ITTECH: `xx < 0x80` invokes the channel's
            // currently-selected parametric macro with `z` = xx;
            // `xx >= 0x80` invokes fixed macro `xx - 0x80`.
            0x1A => {
                let param = current.effect_parameter;
                if param & 0x80 == 0 {
                    Some(GlobalEffect::MidiMacro(MidiMacroType::Parametric(param)))
                } else {
                    Some(GlobalEffect::MidiMacro(MidiMacroType::Fixed {
                        idx: (param - 0x80) as usize,
                        z: param,
                    }))
                }
            }

            _ => None,
        }
    }

    fn parse_it_volume(freq_type: FrequencyType, slot: &PatternSlot) -> Option<TrackImportEffect> {
        // No vol-column command on this row — bail out before any
        // emit. The IT pattern unpacker writes NO_VOL_COL (0xFF) on
        // every cell that lacked vol-col bits in the channel mask;
        // 0xFF is also outside every legal IT vol-col byte (max
        // valid is 212), so this guard doubles as malformed-input
        // protection. Without it, every empty cell would emit a
        // vol-col event and clobber the per-note volume register.
        if slot.volume > 212 {
            return None;
        }

        // Set per-note Volume (vxx) — IT VOLFX_VOLUME, raw 0..64.
        // Targets the same register a fresh trigger writes when it
        // loads `sample.default_note_volume`. NOT the channel-level
        // M-volume; that one is set exclusively by Mxx (effect 0x0D).
        // The previous mapping to ChannelVolume turned every vNN
        // into a sticky channel-volume override — and combined with
        // the missing NO_VOL_COL sentinel, it silenced every cell
        // without an explicit vol-col.
        if slot.volume <= 64 {
            return Some(TrackImportEffect::Volume(slot.volume as f32 / 64.0, 0));
        }

        // Set Panning (pxx)
        if (slot.volume & 0x7F) <= 64 {
            let p = (slot.volume & 0x7F) as f32 / 64.0;
            return Some(TrackImportEffect::Panning(p));
        }

        let temp = if slot.volume & 0x80 == 0 {
            // ['A' 'B' 'C' 'D' 'E' 'F'] -> [0 1 2 3 4 5] (A B C D E F effects)
            slot.volume - b'A'
        } else {
            // [11+k*128 12+k*128]=[6 7] (G H effects)
            (slot.volume & 0x7F) - 5
        };

        let fx = temp / 10;
        let param = temp % 10;

        match fx {
            // Fine Volume Slide Up (a0x)
            //
            // Schism applies `chan->volume += param * 4` on a 0..256
            // volume scale — i.e. `param/64` in the 0..1 normalised
            // scale this importer uses. The vol-column nibble (Ax)
            // shares this scale with the effect-column DxF; both
            // must produce identical per-tick deltas. The previous
            // `/16` was 4× too aggressive on every Ax/Bx/Cx/Dx cell.
            0x0 => return Some(TrackImportEffect::VolumeSlide0(param as f32 / 64.0)),
            // Fine Volume Slide Down (b0x)
            0x1 => return Some(TrackImportEffect::VolumeSlide0(-(param as f32) / 64.0)),
            // Volume Slide Up (c0x)
            0x2 => return Some(TrackImportEffect::VolumeSlideN(param as f32 / 64.0)),
            // Volume Slide Down (d0x)
            0x3 => return Some(TrackImportEffect::VolumeSlideN(-(param as f32) / 64.0)),
            // Portamento Down (e0x)
            //
            // Schism's vol-col porta sets `mem_pitchslide = 4 * vol`
            // and then `fx_reg_portamento_down` slides by
            // `mem_pitchslide * 4` per tick — net 16 * vol per tick
            // in linear-period units. The same scale convention as
            // the effect-column Exx/Fxx arms further up. Previous
            // `/16` was 256× too weak.
            //
            // The rest of the crate's sign convention is:
            //   PortamentoDown receives positive speed (period grows).
            //   PortamentoUp   receives negative speed (period shrinks).
            0x4 => return Some(TrackImportEffect::PortamentoDown(16.0 * param as f32)),
            // Portamento Up (f0x)
            0x5 => return Some(TrackImportEffect::PortamentoUp(-16.0 * param as f32)),
            // Tone Portamento (g0x)
            0x6 => {
                let param = match param {
                    1 => 1,
                    2 => 4,
                    3 => 8,
                    4 => 16,
                    5 => 32,
                    6 => 64,
                    7 => 96,
                    8 => 128,
                    9 => 255,
                    _ => 0,
                } as f32;
                let speed = match freq_type {
                    FrequencyType::LinearFrequencies => 4.0 * param,
                    FrequencyType::AmigaFrequencies => param,
                };
                return Some(TrackImportEffect::TonePortamento(speed));
            }
            // Vibrato Depth (h0x)
            0x7 => {
                let depth = (param & 0x0F) as f32 * 4.0 / 16.0;
                return Some(TrackImportEffect::Vibrato(0.0, depth));
            }
            _ => {}
        }
        None
    }

    pub fn it_unpack_pattern(
        freq_type: FrequencyType,
        pattern: &[Vec<PatternSlot>],
    ) -> Vec<Vec<TrackImportUnit>> {
        pattern
            .iter()
            .map(|channel| {
                channel
                    .iter()
                    .map(|slot| {
                        let te = Self::parse_it_effect(freq_type, slot);
                        let ve = Self::parse_it_volume(freq_type, slot);
                        let cc = Self::parse_global_it_effect(slot);

                        let mut tiu = TrackImportUnit::default();
                        tiu.note = slot.note;
                        tiu.instrument = slot.instrument;
                        if let Some(e) = ve {
                            tiu.effects.push(e);
                        }
                        if let Some(e) = te {
                            tiu.effects.extend(e)
                        }
                        if let Some(c) = cc {
                            tiu.global_effects.push(c);
                        }
                        tiu
                    })
                    .collect::<Vec<TrackImportUnit>>() // Convertir les slots d'un canal
            })
            .collect::<Vec<Vec<TrackImportUnit>>>() // Convertir tous les canaux
    }
}