guitarpro 0.3.0

Rust library and command line interface (CLI) for guitar tab files.
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
use crate::error::{GpError, GpResult, ToPrimitiveGp};
use crate::io::primitive::*;
use crate::model::{rse::*, song::*};
// use crate::gp::*;

/// A mix table item describes a mix parameter, e.g. volume or reverb
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MixTableItem {
    pub value: u8,
    pub duration: u8,
    pub all_tracks: bool,
}
//impl Default for MixTableItem { fn default() -> Self { MixTableItem { value: 0, duration: 0, all_tracks: false }}}

#[allow(dead_code)]
const WAH_EFFECT_OFF: i8 = -2;
const WAH_EFFECT_NONE: i8 = -1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WahEffect {
    pub value: i8,
    pub display: bool,
}
impl Default for WahEffect {
    fn default() -> Self {
        WahEffect {
            value: WAH_EFFECT_NONE,
            display: false,
        }
    }
}
impl WahEffect {
    pub(crate) fn _check_value(value: i8) -> GpResult<()> {
        if !(WAH_EFFECT_OFF..=100).contains(&value) {
            return Err(GpError::InvalidValue {
                context: "wah effect",
                value: value as i64,
            });
        }
        Ok(())
    }
    pub(crate) fn _is_on(&self) -> bool {
        self.value >= 0 && self.value <= 100
    }
    pub(crate) fn _is_off(&self) -> bool {
        self.value == WAH_EFFECT_OFF
    }
    pub(crate) fn _is_none(&self) -> bool {
        self.value == WAH_EFFECT_NONE
    }
}

/// A MixTableChange describes a change in mix parameters
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MixTableChange {
    pub instrument: Option<MixTableItem>,
    pub rse: RseInstrument,
    pub volume: Option<MixTableItem>,
    pub balance: Option<MixTableItem>,
    pub chorus: Option<MixTableItem>,
    pub reverb: Option<MixTableItem>,
    pub phaser: Option<MixTableItem>,
    pub tremolo: Option<MixTableItem>,
    pub tempo_name: String,
    pub tempo: Option<MixTableItem>,
    pub hide_tempo: bool,
    pub wah: Option<WahEffect>,
    pub use_rse: bool,
}
impl Default for MixTableChange {
    fn default() -> Self {
        MixTableChange {
            instrument: None,
            rse: RseInstrument::default(),
            volume: None,
            balance: None,
            chorus: None,
            reverb: None,
            phaser: None,
            tremolo: None,
            tempo_name: String::new(),
            tempo: None,
            hide_tempo: true,
            wah: None,
            use_rse: false,
        }
    }
}
impl MixTableChange {
    pub(crate) fn is_just_wah(&self) -> bool {
        self.instrument.is_none()
            && self.volume.is_none()
            && self.balance.is_none()
            && self.chorus.is_none()
            && self.reverb.is_none()
            && self.phaser.is_none()
            && self.tremolo.is_none()
            && self.tempo.is_none()
            && self.wah.is_none()
    }
}

pub trait SongMixTableOps {
    fn read_mix_table_change(&mut self, data: &[u8], seek: &mut usize) -> GpResult<MixTableChange>;
    fn read_mix_table_change_values(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<()>;
    fn read_mix_table_change_durations(
        &self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<()>;
    fn read_mix_table_change_flags(
        &self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<i8>;
    fn read_wah_effect(&self, data: &[u8], seek: &mut usize, flags: i8) -> GpResult<WahEffect>;
    fn write_mix_table_change(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &Option<MixTableChange>,
        version: &(u8, u8, u8),
    );
    fn write_mix_table_change_values(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
        version: &(u8, u8, u8),
    );
    fn write_mix_table_change_durations(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
        version: &(u8, u8, u8),
    );
    fn write_mix_table_change_flags_v4(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
    );
    fn write_mix_table_change_flags_v5(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
    );
}

impl SongMixTableOps for Song {
    /// Read mix table change. List of values is read first. See `read_values()`.
    ///
    /// List of values is followed by the list of durations for parameters that have changed. See `read_durations()`.
    ///
    /// Mix table change in Guitar Pro 4 format extends Guitar Pro 3 format. It constists of `values <read_mix_table_change_values()>`,
    /// `durations <read_mix_table_change_durations()>`, and, new to GP3, `flags <read_mix_table_change_flags()>`.
    ///
    /// Mix table change was modified to support RSE instruments. It is read as in Guitar Pro 3 and is followed by:
    /// - Wah effect. See :meth:`read_wah_effect()`.
    /// - RSE instrument effect. See :meth:`read_rse_instrument_effect()`.
    fn read_mix_table_change(&mut self, data: &[u8], seek: &mut usize) -> GpResult<MixTableChange> {
        let mut tc = MixTableChange::default();
        self.read_mix_table_change_values(data, seek, &mut tc)?;
        self.read_mix_table_change_durations(data, seek, &mut tc)?;
        //println!("read_mix_table_change()");
        if self.version.number >= (4, 0, 0) {
            let flags = self.read_mix_table_change_flags(data, seek, &mut tc)?;
            if self.version.number >= (5, 0, 0) {
                tc.wah = Some(self.read_wah_effect(data, seek, flags)?);
                self.read_rse_instrument_effect(data, seek, &mut tc.rse)?;
            }
        }
        Ok(tc)
    }
    /// Read mix table change values. Mix table change values consist of 7 `signed-byte` and an `int`, which correspond to:
    /// - instrument
    /// - RSE instrument. See `read_rse_instrument()` (GP5).
    /// - volume
    /// - balance
    /// - chorus
    /// - reverb
    /// - phaser
    /// - tremolo
    /// - Tempo name: `int-byte-size-string` (GP5).
    /// - tempo
    ///
    /// If signed byte is *-1* then corresponding parameter hasn't changed.
    fn read_mix_table_change_values(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<()> {
        //instrument
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.instrument = Some(MixTableItem {
                value: b.to_u8_gp("mix table instrument")?,
                ..Default::default()
            });
        }
        //RSE instrument GP5
        if self.version.number.0 == 5 {
            mtc.rse = self.read_rse_instrument(data, seek)?;
        }
        if self.version.number == (5, 0, 0) {
            *seek += 1;
        }
        //volume
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.volume = Some(MixTableItem {
                value: b.to_u8_gp("mix table volume")?,
                ..Default::default()
            });
        }
        //balance
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.balance = Some(MixTableItem {
                value: b.to_u8_gp("mix table balance")?,
                ..Default::default()
            });
        }
        //chorus
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.chorus = Some(MixTableItem {
                value: b.to_u8_gp("mix table chorus")?,
                ..Default::default()
            });
        }
        //reverb
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.reverb = Some(MixTableItem {
                value: b.to_u8_gp("mix table reverb")?,
                ..Default::default()
            });
        }
        //phaser
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.phaser = Some(MixTableItem {
                value: b.to_u8_gp("mix table phaser")?,
                ..Default::default()
            });
        }
        //tremolo
        let b = read_signed_byte(data, seek)?;
        if b >= 0 {
            mtc.tremolo = Some(MixTableItem {
                value: b.to_u8_gp("mix table tremolo")?,
                ..Default::default()
            });
        }
        //tempo
        if self.version.number >= (5, 0, 0) {
            mtc.tempo_name = read_int_byte_size_string(data, seek)?;
        }
        let b = read_int(data, seek)?;
        if b >= 0 {
            mtc.tempo = Some(MixTableItem {
                value: b.clamp(0, 255) as u8,
                ..Default::default()
            });
        }
        Ok(())
    }
    /// Read mix table change durations. Durations are read for each non-null `MixTableItem`. Durations are encoded in `signed-byte`.
    ///
    /// If tempo did change, then one :ref:`bool` is read. If it's true, then tempo change won't be displayed on the score.
    fn read_mix_table_change_durations(
        &self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<()> {
        if let Some(ref mut item) = mtc.volume {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.balance {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.chorus {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.reverb {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.phaser {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.tremolo {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
        }
        if let Some(ref mut item) = mtc.tempo {
            item.duration = read_signed_byte(data, seek)?.max(0) as u8;
            mtc.hide_tempo = false;
            if self.version.number >= (5, 0, 0) {
                mtc.hide_tempo = read_bool(data, seek)?;
            }
        }
        Ok(())
    }

    /// Read mix table change flags (Guitar Pro 4). The meaning of flags:
    /// - *0x01*: change volume for all tracks
    /// - *0x02*: change balance for all tracks
    /// - *0x04*: change chorus for all tracks
    /// - *0x08*: change reverb for all tracks
    /// - *0x10*: change phaser for all tracks
    /// - *0x20*: change tremolo for all tracks
    ///
    /// In GP5, there is one additional flag:
    /// - *0x40*: use RSE
    /// - *0x80*: show wah-wah
    fn read_mix_table_change_flags(
        &self,
        data: &[u8],
        seek: &mut usize,
        mtc: &mut MixTableChange,
    ) -> GpResult<i8> {
        let flags = read_signed_byte(data, seek)?;
        //println!("read_mix_table_change_flags(), flags:  {}", flags);
        if let Some(mut e) = mtc.volume.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.volume = Some(e);
        }
        if let Some(mut e) = mtc.balance.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.balance = Some(e);
        }
        if let Some(mut e) = mtc.chorus.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.chorus = Some(e);
        }
        if let Some(mut e) = mtc.reverb.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.reverb = Some(e);
        }
        if let Some(mut e) = mtc.phaser.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.phaser = Some(e);
        }
        if let Some(mut e) = mtc.tremolo.take() {
            e.all_tracks = (flags & 0x01) == 0x01;
            mtc.tremolo = Some(e);
        }
        if self.version.number >= (5, 0, 0) {
            mtc.use_rse = (flags & 0x40) == 0x40;
        }
        Ok(flags)
    }

    /// Read wah-wah.
    /// - Wah value: :ref:`signed-byte`. See `WahEffect` for value mapping.
    fn read_wah_effect(&self, data: &[u8], seek: &mut usize, flags: i8) -> GpResult<WahEffect> {
        Ok(WahEffect {
            value: read_signed_byte(data, seek)?,
            display: (flags & -0x80) == -0x80, /*(flags & 0x80) == 0x80*/
        })
    }

    fn write_mix_table_change(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &Option<MixTableChange>,
        version: &(u8, u8, u8),
    ) {
        if let Some(mtc) = mix_table_change {
            self.write_mix_table_change_values(data, mtc, version);
            self.write_mix_table_change_durations(data, mtc, version);
            if version.0 == 4 {
                self.write_mix_table_change_flags_v4(data, mtc);
            }
            if version.0 == 5 {
                self.write_mix_table_change_flags_v5(data, mtc);
                if let Some(w) = &mtc.wah {
                    write_signed_byte(data, w.value);
                } else {
                    write_signed_byte(data, WAH_EFFECT_NONE);
                } //write wah effect
                if *version > (5, 0, 0) {
                    self.write_rse_instrument_effect(data, &mtc.rse);
                }
            }
        }
    }
    fn write_mix_table_change_values(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
        version: &(u8, u8, u8),
    ) {
        //instrument
        if let Some(i) = &mix_table_change.instrument {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        if version.0 >= 5 {
            self.write_rse_instrument(data, &mix_table_change.rse, version);
        }
        if version == &(5, 0, 0) {
            write_placeholder_default(data, 1);
        }
        //volume
        if let Some(i) = &mix_table_change.volume {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //balance
        if let Some(i) = &mix_table_change.balance {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //chorus
        if let Some(i) = &mix_table_change.chorus {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //reverb
        if let Some(i) = &mix_table_change.reverb {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //phaser
        if let Some(i) = &mix_table_change.phaser {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //tremolo
        if let Some(i) = &mix_table_change.tremolo {
            write_signed_byte(data, i.value as i8);
        } else {
            write_signed_byte(data, -1);
        }
        //tempo: int in spec (can be > 127 for BPM)
        if version.0 >= 5 {
            write_int_byte_size_string(data, &mix_table_change.tempo_name);
        }
        if let Some(t) = &mix_table_change.tempo {
            write_i32(data, t.value as i32);
        } else {
            write_i32(data, -1);
        }
    }
    fn write_mix_table_change_durations(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
        version: &(u8, u8, u8),
    ) {
        //only write duration for fields that are actually set (reader skips None fields)
        if let Some(i) = &mix_table_change.volume {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.balance {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.chorus {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.reverb {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.phaser {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.tremolo {
            write_signed_byte(data, i.duration as i8);
        }
        if let Some(i) = &mix_table_change.tempo {
            write_signed_byte(data, i.duration as i8);
            if version >= &(5, 0, 0) {
                write_bool(data, mix_table_change.hide_tempo);
            }
        }
    }
    fn write_mix_table_change_flags_v4(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
    ) {
        let mut flags = 0i8;
        if let Some(i) = &mix_table_change.volume {
            if i.all_tracks {
                flags |= 0x01;
            }
        }
        if let Some(i) = &mix_table_change.balance {
            if i.all_tracks {
                flags |= 0x02;
            }
        }
        if let Some(i) = &mix_table_change.chorus {
            if i.all_tracks {
                flags |= 0x04;
            }
        }
        if let Some(i) = &mix_table_change.reverb {
            if i.all_tracks {
                flags |= 0x08;
            }
        }
        if let Some(i) = &mix_table_change.phaser {
            if i.all_tracks {
                flags |= 0x10;
            }
        }
        if let Some(i) = &mix_table_change.tremolo {
            if i.all_tracks {
                flags |= 0x20;
            }
        }
        write_signed_byte(data, flags);
    }
    fn write_mix_table_change_flags_v5(
        &self,
        data: &mut Vec<u8>,
        mix_table_change: &MixTableChange,
    ) {
        let mut flags = 0u8;
        if let Some(i) = &mix_table_change.volume {
            if i.all_tracks {
                flags |= 0x01;
            }
        }
        if let Some(i) = &mix_table_change.balance {
            if i.all_tracks {
                flags |= 0x02;
            }
        }
        if let Some(i) = &mix_table_change.chorus {
            if i.all_tracks {
                flags |= 0x04;
            }
        }
        if let Some(i) = &mix_table_change.reverb {
            if i.all_tracks {
                flags |= 0x08;
            }
        }
        if let Some(i) = &mix_table_change.phaser {
            if i.all_tracks {
                flags |= 0x10;
            }
        }
        if let Some(i) = &mix_table_change.tremolo {
            if i.all_tracks {
                flags |= 0x20;
            }
        }
        if mix_table_change.use_rse {
            flags |= 0x40;
        }
        if let Some(w) = &mix_table_change.wah {
            if w.display {
                flags |= 0x80;
            }
        }
        write_byte(data, flags);
    }
}