xmrs 0.9.10

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
/*
Managing Dynamic Memory Effects

Here we fill the shortcuts to 0 with the correct values
​​so as not to have to manage it in the player.

It is impossible to do this update perfectly.
Indeed, patterns can be with zero effects and called
with different initialization pre-patterns.
So the last call to the pattern will be the winner.


| **MEMORY 00**                | **MOD** | **XM** | **S3M** | **IT** |
| ---------------------------- | ------- | ------ | ------- | ------ |
| **Arpeggio**                 |         |        | GLOBAL  | TRUE   |
| **Panbrello**                |         | TRUE   | TRUE    | TRUE   |
| **PanningSlide**             |         | TRUE   | TRUE    | TRUE   |
| **PortamentoUp**             |         | TRUE   | GLOBAL  | TRUE   |
| **PortamentoDown**           |         | TRUE   | GLOBAL  | TRUE   |
| **PortamentoFineUp**         |         | TRUE   | GLOBAL  | TRUE   |
| **PortamentoFineDown**       |         | TRUE   | GLOBAL  | TRUE   |
| **PortamentoFineExtraUp**    |         | TRUE   | GLOBAL  | TRUE   |
| **PortamentoFineExtraDown**  |         | TRUE   | GLOBAL  | TRUE   |
| **NoteRetrigExtended**       |         | TRUE   | GLOBAL  | TRUE   |
| **TonePortamento**           | TRUE    | TRUE   | TRUE    | TRUE   |
| **Tremolo**                  | TRUE    | TRUE   | GLOBAL  | TRUE   |
| **Tremor**                   |         | TRUE   | GLOBAL  | TRUE   |
| **SampleOffset**             | TRUE    | TRUE   | TRUE    | TRUE   |
| **Vibrato**                  | TRUE    | TRUE   | TRUE    | TRUE   |
| **VibratoFine**              |         |        |         | TRUE   |
| **VolumeSlide0**             |         | TRUE   | GLOBAL  | TRUE   | 0 EA EB v6 v7
| **VolumeSlideN**             |         | TRUE   | GLOBAL  | TRUE   | N 5 6 A v8 v9
| **FxVolume Tone Portamento** |         | TRUE   |         | FX MEM |
| **FxVolume Vibrato Depth**   |         | TRUE   |         | FX MEM |
|                              |         |        |         |        |
| **Channel Volume Slide**     |         | TRUE   | TRUE    | TRUE   |
| **global Volume Slide**      |         | TRUE   | TRUE    | TRUE   |
| **TempoUp**                  |         |        | TRUE    | TRUE   |
| **TempoDown**                |         |        | TRUE    | TRUE   |

*/

use alloc::vec::Vec;

use crate::import::it::it_effect::ItEffect;
use crate::import::s3m::s3m_effect::S3mEffect;
use crate::import::xm::mod_xm_effect::ModXmEffect;

use super::patternslot::PatternSlot;
use super::track_import_effect::TrackImportEffect;
use super::track_import_unit::TrackImportUnit;
use crate::prelude::*;

// Memory type
pub enum MemoryType {
    Mod,
    Xm,
    S3m,
    It,
}

// Memory Array index
const ARPEGGIO: usize = 0;
const PANBRELLO: usize = 1;
const PANNING_SLIDE: usize = 2;
const PORTAMENTO_UP: usize = 3;
const PORTAMENTO_DOWN: usize = 4;
const PORTAMENTO_FINE_UP: usize = 5;
const PORTAMENTO_FINE_DOWN: usize = 6;
const PORTAMENTO_FINE_EXTRA_UP: usize = 7;
const PORTAMENTO_FINE_EXTRA_DOWN: usize = 8;
const NOTE_RETRIG: usize = 9;
const TONE_PORTAMENTO: usize = 10;
const TREMOLO: usize = 11;
const TREMOR: usize = 12;
const SAMPLE_OFFSET: usize = 13;
const VIBRATO: usize = 14;
const VIBRATO_FINE: usize = 15;
const PITCH_VOLUME_SLIDE_0: usize = 16;
const PITCH_VOLUME_SLIDE_N: usize = 17;
const FX_VOLUME_TONE_PORTAMENTO: usize = 18;
const FX_VOLUME_VIBRATO_DEPTH: usize = 19;
const CHANNEL_VOLUME_SLIDE: usize = 20;
const GLOBAL_VOLUME_SLIDE: usize = 21;
const TEMPO_UP: usize = 22;
const TEMPO_DOWN: usize = 23;
const SAMPLE_OFFSET_ADD_HIGH: usize = 24;
const ARRAY_SIZE: usize = 25;

/// For compatibility between formats and to compensate for special case difficulties with memory management, a best effort analysis is performed during import to associate the exact value with each effect.
// Horrible hack to store values...use from left to right. First usable type wins.
pub struct ImportMemory {
    global: [(f32, f32, usize, usize); ARRAY_SIZE], // s3m way: shared across all channels
    channel: [[(f32, f32, usize, usize); ARRAY_SIZE]; 64], // mod, xm, it way
}

impl Default for ImportMemory {
    fn default() -> Self {
        Self {
            global: [(0.0, 0.0, 0, 0); ARRAY_SIZE],
            channel: [[(0.0, 0.0, 0, 0); ARRAY_SIZE]; 64],
        }
    }
}

impl ImportMemory {
    fn update_memory(&mut self, mem: &MemoryType, index: usize, tiu: &mut TrackImportUnit) {
        tiu.effects.iter_mut().for_each(|e| {
            match e {
                TrackImportEffect::Arpeggio(a, b) => {
                    match mem {
                        MemoryType::Mod | MemoryType::Xm => {
                            // No memory use
                        }
                        MemoryType::It => {
                            if *a == 0 {
                                *a = self.channel[index][ARPEGGIO].2
                            } else {
                                self.channel[index][ARPEGGIO].2 = *a;
                            }
                            // second arg
                            if *b == 0 {
                                *b = self.channel[index][ARPEGGIO].3
                            } else {
                                self.channel[index][ARPEGGIO].3 = *b;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0 {
                                *a = self.global[ARPEGGIO].2
                            } else {
                                self.global[ARPEGGIO].2 = *a;
                            }
                            // second arg
                            if *b == 0 {
                                *b = self.global[ARPEGGIO].3
                            } else {
                                self.global[ARPEGGIO].3 = *b;
                            }
                        }
                    }
                }

                TrackImportEffect::Panbrello(a, b) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.channel[index][PANBRELLO].0
                            } else {
                                self.channel[index][PANBRELLO].0 = *a;
                            }
                            // second arg
                            if *b == 0.0 {
                                *b = self.channel[index][PANBRELLO].1
                            } else {
                                self.channel[index][PANBRELLO].1 = *b;
                            }
                        }
                    }
                }

                TrackImportEffect::PanningSlideN(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.channel[index][PANNING_SLIDE].0
                            } else {
                                self.channel[index][PANNING_SLIDE].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoUp(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_UP].0
                            } else {
                                self.channel[index][PORTAMENTO_UP].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_UP].0
                            } else {
                                self.global[PORTAMENTO_UP].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoDown(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_DOWN].0
                            } else {
                                self.channel[index][PORTAMENTO_DOWN].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_DOWN].0
                            } else {
                                self.global[PORTAMENTO_DOWN].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoFineUp(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_FINE_UP].0
                            } else {
                                self.channel[index][PORTAMENTO_FINE_UP].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_FINE_UP].0
                            } else {
                                self.global[PORTAMENTO_FINE_UP].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoFineDown(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_FINE_DOWN].0
                            } else {
                                self.channel[index][PORTAMENTO_FINE_DOWN].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_FINE_DOWN].0
                            } else {
                                self.global[PORTAMENTO_FINE_DOWN].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoExtraFineUp(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_FINE_EXTRA_UP].0
                            } else {
                                self.channel[index][PORTAMENTO_FINE_EXTRA_UP].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_FINE_EXTRA_UP].0
                            } else {
                                self.global[PORTAMENTO_FINE_EXTRA_UP].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::PortamentoExtraFineDown(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PORTAMENTO_FINE_EXTRA_DOWN].0
                            } else {
                                self.channel[index][PORTAMENTO_FINE_EXTRA_DOWN].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PORTAMENTO_FINE_EXTRA_DOWN].0
                            } else {
                                self.global[PORTAMENTO_FINE_EXTRA_DOWN].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::NoteRetrigExtended(speed, vol) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory use
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *speed == 0 {
                                *speed = self.channel[index][NOTE_RETRIG].2
                            } else {
                                self.channel[index][NOTE_RETRIG].2 = *speed;
                            }
                            if *vol == 0 {
                                *vol = self.channel[index][NOTE_RETRIG].3
                            } else {
                                self.channel[index][NOTE_RETRIG].3 = *vol;
                            }
                        }
                        MemoryType::S3m => {
                            if *speed == 0 {
                                *speed = self.global[NOTE_RETRIG].2
                            } else {
                                self.global[NOTE_RETRIG].2 = *speed;
                            }
                            if *vol == 0 {
                                *vol = self.channel[index][NOTE_RETRIG].3
                            } else {
                                self.channel[index][NOTE_RETRIG].3 = *vol;
                            }
                        }
                    }
                }

                TrackImportEffect::TonePortamento(a) => match mem {
                    MemoryType::Mod | MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                        if *a == 0.0 {
                            *a = self.channel[index][TONE_PORTAMENTO].0
                        } else {
                            self.channel[index][TONE_PORTAMENTO].0 = *a;
                        }
                    }
                },

                TrackImportEffect::Tremolo(a, b) => match mem {
                    MemoryType::Mod | MemoryType::Xm | MemoryType::It => {
                        if *a == 0.0 {
                            *a = self.channel[index][TREMOLO].0
                        } else {
                            self.channel[index][TREMOLO].0 = *a;
                        }
                        if *b == 0.0 {
                            *b = self.channel[index][TREMOLO].1
                        } else {
                            self.channel[index][TREMOLO].1 = *b;
                        }
                    }
                    MemoryType::S3m => {
                        if *a == 0.0 {
                            *a = self.global[TREMOLO].0
                        } else {
                            self.global[TREMOLO].0 = *a;
                        }
                        if *b == 0.0 {
                            *b = self.global[TREMOLO].1
                        } else {
                            self.global[TREMOLO].1 = *b;
                        }
                    }
                },

                TrackImportEffect::Tremor(a, b) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0 {
                                *a = self.channel[index][TREMOR].2
                            } else {
                                self.channel[index][TREMOR].2 = *a;
                            }
                            if *b == 0 {
                                *b = self.channel[index][TREMOR].3
                            } else {
                                self.channel[index][TREMOR].3 = *b;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0 {
                                *a = self.global[TREMOR].2
                            } else {
                                self.global[TREMOR].2 = *a;
                            }
                            if *b == 0 {
                                *b = self.global[TREMOR].3
                            } else {
                                self.global[TREMOR].3 = *b;
                            }
                        }
                    }
                }

                TrackImportEffect::InstrumentSampleOffset(a) => match mem {
                    MemoryType::Mod | MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                        if *a == 0 {
                            *a = self.channel[index][SAMPLE_OFFSET].2
                                + self.channel[index][SAMPLE_OFFSET_ADD_HIGH].2;
                        } else {
                            self.channel[index][SAMPLE_OFFSET].2 = *a;
                        }
                    }
                },

                TrackImportEffect::InstrumentSampleOffsetAddHigh(a) => match mem {
                    MemoryType::Mod | MemoryType::Xm | MemoryType::S3m => {
                        // No memory
                    }
                    MemoryType::It => {
                        self.channel[index][SAMPLE_OFFSET_ADD_HIGH].2 = *a;
                    }
                },

                TrackImportEffect::Vibrato(a, b) => match mem {
                    MemoryType::Mod | MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                        if *a == 0.0 {
                            *a = self.channel[index][VIBRATO].0
                        } else {
                            self.channel[index][VIBRATO].0 = *a;
                        }
                        if *b == 0.0 {
                            *b = self.channel[index][VIBRATO].1
                        } else {
                            self.channel[index][VIBRATO].1 = *b;
                        }
                    }
                },

                TrackImportEffect::VibratoFine(a, b) => {
                    match mem {
                        MemoryType::Mod | MemoryType::Xm | MemoryType::It => {
                            // No memory
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.channel[index][VIBRATO_FINE].0
                            } else {
                                self.channel[index][VIBRATO_FINE].0 = *a;
                            }
                            if *b == 0.0 {
                                *b = self.channel[index][VIBRATO_FINE].1
                            } else {
                                self.channel[index][VIBRATO_FINE].1 = *b;
                            }
                        }
                    }
                }

                TrackImportEffect::VolumeSlide0(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PITCH_VOLUME_SLIDE_0].0
                            } else {
                                self.channel[index][PITCH_VOLUME_SLIDE_0].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PITCH_VOLUME_SLIDE_0].0
                            } else {
                                self.global[PITCH_VOLUME_SLIDE_0].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::VolumeSlideN(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It => {
                            if *a == 0.0 {
                                *a = self.channel[index][PITCH_VOLUME_SLIDE_N].0
                            } else {
                                self.channel[index][PITCH_VOLUME_SLIDE_N].0 = *a;
                            }
                        }
                        MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.global[PITCH_VOLUME_SLIDE_N].0
                            } else {
                                self.global[PITCH_VOLUME_SLIDE_N].0 = *a;
                            }
                        }
                    }
                }

                TrackImportEffect::TonePortamentoFxVol(a) => {
                    match mem {
                        MemoryType::Mod | MemoryType::S3m => {
                            // No memory
                        }
                        MemoryType::Xm => {
                            if *a == 0.0 {
                                *a = self.channel[index][FX_VOLUME_TONE_PORTAMENTO].0
                            } else {
                                self.channel[index][FX_VOLUME_TONE_PORTAMENTO].0 = *a;
                            }
                        }
                        MemoryType::It => {
                            // never called because no use
                        }
                    }
                }

                TrackImportEffect::VibratoDepthFxVol(a) => {
                    match mem {
                        MemoryType::Mod | MemoryType::S3m => {
                            // No memory
                        }
                        MemoryType::Xm => {
                            if *a == 0.0 {
                                *a = self.channel[index][FX_VOLUME_VIBRATO_DEPTH].0
                            } else {
                                self.channel[index][FX_VOLUME_VIBRATO_DEPTH].0 = *a;
                            }
                        }
                        MemoryType::It => {
                            // never called because no use
                        }
                    }
                }

                TrackImportEffect::ChannelVolumeSlide0(a)
                | TrackImportEffect::ChannelVolumeSlideN(a) => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.channel[index][CHANNEL_VOLUME_SLIDE].0
                            } else {
                                self.channel[index][CHANNEL_VOLUME_SLIDE].0 = *a;
                            }
                        }
                    }
                }

                _ => {}
            }
        });

        tiu.global_effects.iter_mut().for_each(|e| {
            match e {
                GlobalEffect::BpmSlide(a) => {
                    let fx_index = if *a < 0 { TEMPO_DOWN } else { TEMPO_UP };
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                            if *a == 0 {
                                *a = self.channel[index][fx_index].2 as isize;
                            } else {
                                self.channel[index][fx_index].2 = *a as usize;
                            }
                        }
                    }
                }
                GlobalEffect::VolumeSlide { speed: a, fine: _b } => {
                    match mem {
                        MemoryType::Mod => {
                            // No memory
                        }
                        MemoryType::Xm | MemoryType::It | MemoryType::S3m => {
                            if *a == 0.0 {
                                *a = self.channel[index][CHANNEL_VOLUME_SLIDE].0
                            } else {
                                self.channel[index][CHANNEL_VOLUME_SLIDE].0 = *a;
                            }
                        }
                    }
                }
                _ => {}
            }
        });
    }

    fn apply_memory(
        &mut self,
        freq_type: FrequencyType,
        mem: MemoryType,
        order: &[Vec<usize>],
        patterns: &[Vec<Vec<PatternSlot>>],
    ) -> Vec<Vec<Vec<TrackImportUnit>>> {
        let mut source: Vec<Vec<Vec<TrackImportUnit>>> = patterns
            .iter()
            .map(|pattern| match mem {
                MemoryType::Mod | MemoryType::Xm => {
                    ModXmEffect::mod_xm_unpack_pattern(freq_type, pattern)
                }
                MemoryType::S3m => S3mEffect::s3m_unpack_pattern(freq_type, pattern),
                MemoryType::It => ItEffect::it_unpack_pattern(freq_type, pattern),
                // _ => todo!(),
            })
            .collect();

        // It is impossible to guarantee that a pattern will not be designed
        // to have different pre-memory configurations.
        // We "play" the music in order and it will remain a best effort.
        order.iter().flat_map(|inner| inner.iter()).for_each(|o| {
            if *o >= source.len() {
                return;
            }
            let pattern = &mut source[*o];
            pattern.iter_mut().for_each(|row| {
                row.iter_mut()
                    .enumerate()
                    .for_each(|(index, tiu)| self.update_memory(&mem, index, tiu));
            });
        });

        source
    }

    pub fn unpack_patterns(
        &mut self,
        freq_type: FrequencyType,
        mem: MemoryType,
        order: &[Vec<usize>],
        patterns: &[Vec<Vec<PatternSlot>>],
    ) -> Vec<Vec<Vec<TrackUnit>>> {
        /* first step: get the same patterns but using TrackImportUnit */
        let source = self.apply_memory(freq_type, mem, order, patterns);

        /* second step: prepare patterns with TrackUnit */
        let mut dest: Vec<Vec<Vec<TrackUnit>>> = source
            .iter()
            .map(|pattern| {
                pattern
                    .iter()
                    .map(|row| row.iter().map(|tiu| tiu.prepare_track_unit()).collect())
                    .collect()
            })
            .collect();

        /* third step: add effects */
        for ol in order.iter().flat_map(|inner| inner.iter()) {
            if *ol >= source.len() || *ol >= dest.len() {
                continue;
            }

            let pattern_s = &source[*ol];
            let pattern_d = &mut dest[*ol];

            for (index_row, row_s) in pattern_s.iter().enumerate() {
                let row_d = &mut pattern_d[index_row];

                for (index_ch, ch_s) in row_s.iter().enumerate() {
                    let ch_d = &mut row_d[index_ch];

                    // no use of GlobalEffects::PositionJump and GlobalEffects::PatternBreak
                    // due to implementation complexity and risk of infinite loop
                    // maybe one day someone will have the motivation if a bug too important appears

                    ch_d.effects = TrackImportEffect::to_track_effects(&ch_s.effects);
                }
            }
        }
        dest
    }
}