ittech 0.3.0

Impulse Tracker file parser and writer
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
//! Parsing functions

use crate::data::*;
use crate::error::ContextError;
use bitflags::bitflags;
use nom::bytes::complete::{tag, take};
use nom::combinator::{all_consuming, map};
use nom::error::{ErrorKind, ParseError};
use nom::multi::{count, many_till};
use nom::number::complete::{be_i16, le_i16, le_i8, le_u16, le_u32, le_u8};
use nom::sequence::tuple;
use nom::{Err, IResult};
use pattern::pattern;
use std::convert::{TryFrom, TryInto};
use std::ops::RangeInclusive;


macro_rules! info {
    ( $($tt:tt)* ) => {
        #[cfg(feature = "tracing")]
        ::tracing::info!($($tt)*);
    };
}


mod pattern;
pub(crate) mod scan;
mod util;

pub use pattern::parse_effect as effect;

use util::*;
pub use scan::scan;


/// Parse Impulse Tracker module file (.it)
pub fn module_file<'i, E>(input: &'i [u8]) -> Result<Module, Err<E>>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]> + 'i,
{
    let (_, header) = module_header(input)?;

    // Offsets are relative to the start of the file, use the whole input every time.
    let (_, instruments) = offset_list(instrument, header.instrument_offsets)(input)?;
    let (_, sample_headers) = offset_list(sample_header, header.sample_offsets)(input)?;
    let patterns = {
        let mut patterns = Vec::with_capacity(header.pattern_offsets.len());
        for offset in header.pattern_offsets.into_iter().map(<_>::cast) {
            // Pattern parsing is inlined from `offset_list` because we need to handle the special
            // case of offset 0 here.
            if offset == 0 {
                patterns.push(Pattern {
                    active_channels: ActiveChannels::empty(),
                    rows: vec![Row::empty(); 64]
                });
                continue
            }
            if offset >= input.len() {
                return Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof)));
            }
            let (_, pat) = pattern(&input[offset..])?;
            patterns.push(pat);
        }
        patterns
    };

    let samples = sample_headers.into_iter()
        .map(|header| sample_data(header, input))
        .collect::<Result<Vec<_>, _>>()?;

    let message = {
        let offset = header.message_offset.cast::<usize>();
        if offset == 0 || offset >= input.len() {
            String::new()
        } else {
            let (_, bytes) = take(header.message_length.cast::<usize>())(input)?;
            String::from_utf8_lossy(bytes)
                .to_string()
        }
    };

    Ok(Module {
        name: header.name,
        highlight: header.highlight,
        made_with_version: header.made_with_version,
        compatible_with_version: header.compatible_with_version,
        flags: header.flags,
        global_volume: header.global_volume,
        sample_volume: header.sample_volume,
        speed: header.speed,
        tempo: header.tempo,
        pan_separation: header.pan_separation,
        pitch_wheel_depth: header.pitch_wheel_depth,
        message,
        orders: header.orders,
        init_channel_panning: header.init_channel_panning,
        init_channel_volume: header.init_channel_volume,
        instruments,
        samples,
        patterns,
    })
}

/// Parse Impulse Tracker instrument file (.iti)
pub fn instrument_file<'i, E>(input: &'i [u8]) -> Result<InstrumentFile, Err<E>>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]>,
{
    let (input2, instrument) = instrument(input)?;
    let (_, sample_headers) = count(sample_header, instrument.number_of_samples.into())(input2)?;
    let samples = sample_headers.into_iter()
        .map(|header| sample_data(header, input))
        .collect::<Result<Vec<_>, _>>()?;
    Ok(InstrumentFile { instrument, samples })
}

/// Parse Impulse Tracker sample file (.its)
pub fn sample_file<'i, E>(input: &'i [u8]) -> Result<Sample, Err<E>>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]>,
{
    let (_, header) = sample_header(input)?;
    sample_data(header, input)
}


fn module_header<'i, E>(input: &'i [u8]) -> IResult<&'i [u8], ModuleHeader, E>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]>,
{
    // Parse static parts.
    let (input, _) = tag(b"IMPM")(input)?;
    let (input, songname) = name(input)?;
    let (input, highlight_minor) = le_u8(input)?;
    let (input, highlight_major) = le_u8(input)?;
    let (input, ordnum) = le_u16(input)?;
    let (input, insnum) = le_u16(input)?;
    let (input, smpnum) = le_u16(input)?;
    let (input, patnum) = le_u16(input)?;
    let (input, cwtv) = le_u16(input)?;
    let (input, cmwt) = le_u16(input)?;
    let (input, flags) = le_u16(input)?;
    let (input, special) = le_u16(input)?;
    let (input, globalvol) = le_u8(input)?;
    let (input, mv) = le_u8(input)?;
    let (input, speed) = le_u8(input)?;
    let (input, tempo) = le_u8(input)?;
    let (input, sep) = le_u8(input)?;
    let (input, pwd) = le_u8(input)?;
    let (input, msglength) = le_u16(input)?;
    let (input, msgoffset) = le_u32(input)?;
    let (input, _reserved) = le_u32(input)?;
    let (input, chnpan) = byte_array(input)?;
    let (input, chnvol) = byte_array(input)?;

    // Parse dynamic parts of the header.
    let (input, orders) = count(order, ordnum.into())(input)?;
    let orders = orders.into_iter().flatten().collect();
    let (input, ins_offsets) = count(le_u32, insnum.into())(input)?;
    let (input, sam_offsets) = count(le_u32, smpnum.into())(input)?;
    let (_rest, pat_offsets) = count(le_u32, patnum.into())(input)?;

    let flags = ModuleFlags::from_parts(flags, special);

    // Check ranged values and canonicalize out-of-range values.
    fn ranged(value: u8, range: RangeInclusive<u8>, or_else: impl FnOnce(u8) -> u8) -> u8 {
        if range.contains(&value) {
            value
        } else {
            let value = or_else(value);
            assert!(range.contains(&value), "BUG: fallback value is also out of range");
            value
        }
    }
    let globalvol = ranged(globalvol, 0..=128, |_| {
        info!(globalvol, "global_volume cannot be more than 128, clipping");
        128
    });
    let mv = ranged(mv, 0..=128, |_| {
        info!(mv, "sample_volume cannot be more than 128, clipping");
        128
    });
    let speed = ranged(speed, 1..=255, |_| {
        info!("speed must be at least 1, using default of 6");
        6
    });
    let tempo = ranged(tempo, 31..=255, |_| {
        info!("tempo must be at least 31, using default of 120");
        120
    });
    let sep = ranged(sep, 0..=128, |_| {
        info!("pan_separation cannot be more than 128, clipping");
        128
    });

    Ok((
        input,
        ModuleHeader {
            name: songname,
            highlight: (highlight_major, highlight_minor),
            made_with_version: cwtv,
            compatible_with_version: cmwt,
            flags,
            global_volume: globalvol.cast(),
            sample_volume: mv.cast(),
            speed: speed.cast(),
            tempo: tempo.cast(),
            pan_separation: sep.cast(),
            pitch_wheel_depth: pwd,
            message_length: msglength,
            message_offset: msgoffset,
            init_channel_panning: chnpan,
            init_channel_volume: chnvol,
            orders,
            instrument_offsets: ins_offsets,
            sample_offsets: sam_offsets,
            pattern_offsets: pat_offsets,
        },
    ))
}

fn order<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], Option<Order>, E> {
    map(
        le_u8,
        |value| match value {
            0 ..= 199 => Some(Order::Index(value.cast())),
            254 => Some(Order::Separator),
            255 => Some(Order::EndOfSong),
            // Invalid values get skipped.
            _ => {
                info!(value, "order value is out of range 0..=199,254,255, skipping");
                None
            }
        },
    )(input)
}

fn name<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], Name, E> {
    let (input, bytes) = byte_array(input)?;
    Ok((input, Name { bytes }))
}

fn dosfilename<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], DosFilename, E> {
    let (input, bytes) = byte_array(input)?;
    Ok((input, DosFilename { bytes }))
}

fn instrument<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], Instrument, E> {
    let (input, _) = tag(b"IMPI")(input)?;
    let (input, filename) = dosfilename(input)?;
    let (input, nna) = le_u8(input)?;
    let (input, dct) = le_u8(input)?;
    let (input, dca) = le_u8(input)?;
    let (input, fadeout) = le_u16(input)?;
    let (input, pps) = le_i8(input)?;
    let (input, ppc) = le_u8(input)?;
    let (input, gbv) = le_u8(input)?;
    let (input, dfp) = le_u8(input)?;
    let (input, rv) = le_u8(input)?;
    let (input, rp) = le_u8(input)?;
    let (input, trkver) = le_u16(input)?;
    let (input, nos) = le_u8(input)?;
    let (input, _reserved) = le_u8(input)?;
    let (input, name) = name(input)?;
    let (input, ifc) = le_u8(input)?;
    let (input, ifr) = le_u8(input)?;
    let (input, mch) = le_u8(input)?;
    let (input, mpr) = le_u8(input)?;
    let (input, mbank) = byte_array(input)?;
    let (input, sample_map) = sample_map(input)?;
    let (input, volenv) = envelope(input)?;
    let (input, panenv) = envelope(input)?;
    let (input, pitchenv) = envelope(input)?;
    let (input, _dummy) = byte_array::<_, 4>(input)?;

    let mut flags = InstrumentFlags::default();

    if dfp & Instrument::dfp_ignorePanning == 0 {
        flags |= InstrumentFlags::ENABLE_PANNING;
    }
    let dfp = dfp & !Instrument::dfp_ignorePanning;

    if ifc & Instrument::ifc_enableCutoff != 0 {
        flags |= InstrumentFlags::ENABLE_FILTER_CUTOFF;
    }
    let ifc = ifc & !Instrument::ifc_enableCutoff;

    if ifr & Instrument::ifr_enableResonance != 0 {
        flags |= InstrumentFlags::ENABLE_FILTER_RESONANCE;
    }
    let ifr = ifr & !Instrument::ifr_enableResonance;

    Ok((
        input,
        Instrument {
            name,
            filename,
            flags,
            new_note_action: nna,
            duplicate_check_type: dct,
            duplicate_check_action: dca,
            instrument_fadeout: fadeout.try_into().unwrap(),
            pitch_pan_separation: pps,
            pitch_pan_centre: ppc,
            global_volume: gbv,
            default_panning: dfp.try_into().unwrap(),
            random_volume_variation: rv.try_into().unwrap(),
            random_panning_variation: rp.try_into().unwrap(),
            trkver,
            number_of_samples: nos,
            initial_filter_cutoff: ifc.try_into().unwrap(),
            initial_filter_resonance: ifr.try_into().unwrap(),
            mch,
            mpr,
            mbank,
            sample_map,
            volume_envelope: volenv,
            panning_envelope: panenv,
            pitch_filter_envelope: pitchenv,
        },
    ))
}

fn sample_map<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], SampleMap, E> {
    scan_count(
        120,
        tuple((le_u8, le_u8)),
        SampleMap::default(),
        |sm: &mut SampleMap, (note, sample)| {
            match (note, sample) {
                (0..=119, 0) => {
                    // Explicit map to None, but don't override previous mapping.
                }
                (0..=119, 1..=99) => {
                    let sample = SampleId::try_from(sample - 1).unwrap();
                    sm.map[usize::from(note)] = Some(sample);
                }
                _ => {
                    info!(
                        note, sample,
                        "note or sample out of range 0..=119 and 0..=99 respectively"
                    );
                }
            }
        },
    )(input)
}

fn envelope<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], Envelope, E> {
    let (input, flags) = le_u8(input)?;
    let (input, num) = le_u8(input)?;
    let (input, lpb) = le_u8(input)?;
    let (input, lpe) = le_u8(input)?;
    let (input, slb) = le_u8(input)?;
    let (input, sle) = le_u8(input)?;
    let (input, data): (_, [_; 25]) = array(node)(input)?;
    let (input, _reserved) = le_u8(input)?;

    let envelope_loop;
    let sustain_loop;
    let num = if num > 25 {
        info!(len = num, "envelope size out of range 0..=25, using 0");
        envelope_loop = None;
        sustain_loop = None;
        0
    } else {
        envelope_loop = if lpb <= lpe && lpe < num {
            Some(EnvelopeLoop { start: lpb, end: lpe })
        } else {
            info!(
                start = lpb, end = lpe, len = num,
                "invalid loop points, ignoring envelope loop",
            );
            None
        };
        sustain_loop = if slb <= sle && sle < num {
            Some(EnvelopeLoop { start: slb, end: sle })
        } else {
            info!(
                start = lpb, end = lpe, len = num,
                "invalid loop points, ignoring sustain loop",
            );
            None
        };
        num
    };

    let flags = EnvelopeFlags::from_bits_truncate(flags);
    let nodes = Vec::from(&data[..usize::from(num)]);

    Ok((
        input,
        Envelope {
            flags,
            envelope_loop,
            sustain_loop,
            nodes,
        },
    ))
}

fn node<'i, E: ParseError<&'i [u8]> + ContextError<&'i [u8]>>(input: &'i [u8]) -> IResult<&'i [u8], Node, E> {
    let (input, value) = le_i8(input)?;
    let (input, tick) = le_u16(input)?;
    Ok((input, Node { value, tick }))
}

fn sample_header<'i, E>(input: &'i [u8]) -> IResult<&'i [u8], SampleHeader, E>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]>,
{
    let (input, _) = tag(b"IMPS")(input)?;
    let (input, filename) = dosfilename(input)?;
    let (input, gvl) = le_u8(input)?;
    let (input, flags) = le_u8(input)?;
    let (input, vol) = le_u8(input)?;
    let (input, name) = name(input)?;
    let (input, cvt) = le_u8(input)?;

    let flags = SampleFlags::from_parts(flags, cvt);

    let (input, dfp) = le_u8(input)?;
    let (input, length) = le_u32(input)?;
    let (input, loopbegin) = le_u32(input)?;
    let (input, loopend) = le_u32(input)?;
    let (input, c5speed) = le_u32(input)?;
    let (input, susloopbegin) = le_u32(input)?;
    let (input, susloopend) = le_u32(input)?;
    let (input, samplepointer) = le_u32(input)?;
    let (input, vis) = le_u8(input)?;
    let (input, vid) = le_u8(input)?;
    let (input, vir) = le_u8(input)?;
    let (input, vit) = le_u8(input)?;

    let loop_ = if flags.contains(SampleFlags::LOOP) {
        // TODO canonicalize/skip invalid values
        assert!(loopbegin < loopend);
        assert!(loopend <= length);
        Some(SampleLoop {
            start: loopbegin,
            end: loopend,
            bidi: flags.contains(SampleFlags::BIDI_LOOP),
        })
    } else {
        None
    };

    let sustain_loop = if flags.contains(SampleFlags::SUSTAIN) {
        // TODO canonicalize/skip invalid values
        assert!(susloopbegin < susloopend);
        assert!(susloopend <= length);
        Some(SampleLoop {
            start: susloopbegin,
            end: susloopend,
            bidi: flags.contains(SampleFlags::BIDI_SUSTAIN),
        })
    } else {
        None
    };

    Ok((
        input,
        SampleHeader {
            name,
            filename,
            global_volume: gvl,
            default_volume: vol,
            default_panning: dfp,
            loop_,
            sustain_loop,
            samplerate_c5: c5speed,
            vibrato_speed: vis,
            vibrato_depth: vid,
            vibrato_rate: vir,
            vibrato_type: vit,

            flags,
            data_offset: samplepointer,
            data_length: length,
        },
    ))
}

fn sample_data<'i, E>(header: SampleHeader, input: &'i [u8]) -> Result<Sample, Err<E>>
where
    E: ParseError<&'i [u8]> + ContextError<&'i [u8]>,
{
    let flags = header.flags;

    let data = if !flags.contains(SampleFlags::DATA_PRESENT) {
        None
    } else {
        // TODO add support for more sample formats, do not panic

        assert!(flags.contains(SampleFlags::DATA_SIGNED), "only signed samples are supported");
        assert!(!flags.contains(SampleFlags::STEREO), "only mono samples supported");

        assert!(!flags.contains(SampleFlags::COMPRESSED), "sample compression is not supported");
        assert!(!flags.contains(SampleFlags::OPL_INSTRUMENT), "OPL instrument is not supported");
        assert!(!flags.contains(SampleFlags::EXTERNAL_SAMPLE), "external samples are not supported");
        assert!(!flags.contains(SampleFlags::ADPCM_SAMPLE), "MODPlugin :(");
        assert!(!flags.contains(SampleFlags::DELTA), "delta samples are not supported");
        assert!(!flags.contains(SampleFlags::PTM8_TO_16), "PTM loader is not supported");

        let offset = header.data_offset.cast();
        let length = header.data_length.cast();
        let input = &input[offset..];

        let (_, data) = match (
            flags.contains(SampleFlags::DATA_16BIT),
            flags.contains(SampleFlags::DATA_BIG_ENDIAN),
        ) {
            (true, true) => count(map(be_i16, |s| f32::from(s) / f32::from(i16::MAX)), length)(input)?,
            (true, false) => count(map(le_i16, |s| f32::from(s) / f32::from(i16::MAX)), length)(input)?,
            (false, _) => count(map(le_i8, |s| f32::from(s) / f32::from(i8::MAX)), length)(input)?,
        };

        Some(data)
    };

    Ok(Sample {
        name: header.name,
        filename: header.filename,
        global_volume: header.global_volume,
        default_volume: header.default_volume,
        default_panning: header.default_panning,
        loop_: header.loop_,
        sustain_loop: header.sustain_loop,
        samplerate_c5: header.samplerate_c5,
        vibrato_speed: header.vibrato_speed,
        vibrato_depth: header.vibrato_depth,
        vibrato_rate: header.vibrato_rate,
        vibrato_type: header.vibrato_type,
        data,
    })
}