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
use crate::error::{GpResult, ToPrimitiveGp};
use crate::{
    audio::midi::*,
    io::primitive::*,
    model::{enums::*, measure::*, rse::*, song::*},
};

/// Settings of the track.
#[derive(Debug, Clone)]
pub struct TrackSettings {
    pub tablature: bool,
    pub notation: bool,
    pub diagram_are_below: bool,
    pub show_rythm: bool,
    pub force_horizontal: bool,
    pub force_channels: bool,
    pub diagram_list: bool,
    pub diagram_in_score: bool,
    pub auto_let_ring: bool,
    pub auto_brush: bool,
    pub extend_rythmic: bool,
}
impl Default for TrackSettings {
    fn default() -> Self {
        TrackSettings {
            tablature: true,
            notation: true,
            diagram_are_below: false,
            show_rythm: false,
            force_horizontal: false,
            force_channels: false,
            diagram_list: true,
            diagram_in_score: false,
            auto_let_ring: false,
            auto_brush: false,
            extend_rythmic: false,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Track {
    pub number: i32,
    pub offset: i32,
    pub channel_index: usize, //pub channel_id: i32,
    pub solo: bool,
    pub mute: bool,
    pub visible: bool,
    pub name: String,
    pub short_name: String,
    /// A guitar string with a special tuning.
    pub strings: Vec<(i8, i8)>,
    pub color: i32,
    pub percussion_track: bool,
    pub twelve_stringed_guitar_track: bool,
    pub banjo_track: bool,
    pub port: u8,
    pub fret_count: u8,
    pub indicate_tuning: bool,
    pub use_rse: bool,
    pub rse: TrackRse,
    pub measures: Vec<Measure>,
    pub settings: TrackSettings,
    /// MIDI program from GPIF (GP6/GP7)
    pub midi_program_gpif: Option<i32>,
    /// Chromatic transposition (GP6/GP7)
    pub transpose_chromatic: i32,
    /// Octave transposition (GP6/GP7)
    pub transpose_octave: i32,
}
impl Default for Track {
    fn default() -> Self {
        Track {
            number: 1,
            offset: 0,
            channel_index: 0, //channel_id: 25,
            solo: false,
            mute: false,
            visible: true,
            name: String::from("Track 1"),
            short_name: String::new(),
            strings: vec![(1, 64), (2, 59), (3, 55), (4, 50), (5, 45), (6, 40)],
            banjo_track: false,
            twelve_stringed_guitar_track: false,
            percussion_track: false,
            fret_count: 24,
            color: 0xff0000,
            port: 1,
            indicate_tuning: false,
            use_rse: false,
            rse: TrackRse::default(),
            measures: Vec::new(),
            settings: TrackSettings::default(),
            midi_program_gpif: None,
            transpose_chromatic: 0,
            transpose_octave: 0,
        }
    }
}

pub trait SongTrackOps {
    fn read_tracks(&mut self, data: &[u8], seek: &mut usize, track_count: usize) -> GpResult<()>;
    fn read_tracks_v5(&mut self, data: &[u8], seek: &mut usize, track_count: usize)
    -> GpResult<()>;
    fn read_track(&mut self, data: &[u8], seek: &mut usize, number: usize) -> GpResult<()>;
    fn read_track_v5(&mut self, data: &[u8], seek: &mut usize, number: usize) -> GpResult<()>;
    fn write_tracks(&self, data: &mut Vec<u8>, version: &(u8, u8, u8)) -> GpResult<()>;
    fn write_track(&self, data: &mut Vec<u8>, number: usize) -> GpResult<()>;
    fn write_track_v5(
        &self,
        data: &mut Vec<u8>,
        number: usize,
        version: &(u8, u8, u8),
    ) -> GpResult<()>;
}

impl SongTrackOps for Song {
    /// Read tracks. The tracks are written one after another, their number having been specified previously in :meth:`GP3File.readSong`.
    /// - `track_count`: number of tracks to expect.
    fn read_tracks(&mut self, data: &[u8], seek: &mut usize, track_count: usize) -> GpResult<()> {
        //println!("read_tracks()");
        for i in 0..track_count {
            self.read_track(data, seek, i)?;
        }
        Ok(())
    }

    fn read_tracks_v5(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        track_count: usize,
    ) -> GpResult<()> {
        //println!("read_tracks_v5(): {:?} {}", self.version.number, self.version.number == (5,1,0));
        for i in 0..track_count {
            self.read_track_v5(data, seek, i)?;
        }
        *seek += if self.version.number == (5, 0, 0) {
            2
        } else {
            1
        };
        Ok(())
    }

    /// Read a  track. The first byte is the track's flags. It presides the track's attributes:
    ///
    /// | **bit 7 to 3** | **bit 2**   | **bit 1**                | **bit 0**   |
    /// |----------------|-------------|--------------------------|-------------|
    /// | Blank bits     | Banjo track | 12 stringed guitar track | Drums track |
    ///
    /// Flags are followed by:
    ///
    /// * **Name**: `string`. A 40 characters long string containing the track's name.
    /// * **Number of strings**: `integer`. An integer equal to the number of strings of the track.
    /// * **Tuning of the strings**: Table of integers. The tuning of the strings is stored as a 7-integers table, the "Number of strings" first integers being really used. The strings are stored from the highest to the lowest.
    /// * **Port**: `integer`. The number of the MIDI port used.
    /// * **Channel**: `integer`. The number of the MIDI channel used. The channel 10 is the drums channel.
    /// * **ChannelE**: `integer`. The number of the MIDI channel used for effects.
    /// * **Number of frets**: `integer`. The number of frets of the instrument.
    /// * **Height of the capo**: `integer`. The number of the fret on which a capo is present. If no capo is used, the value is `0x00000000`.
    /// * **Track's color**: `color`. The track's displayed color in Guitar Pro.
    fn read_track(&mut self, data: &[u8], seek: &mut usize, number: usize) -> GpResult<()> {
        let mut track = Track {
            number: number.to_i32_gp("track number")?,
            ..Default::default()
        };
        //read the flag
        let flags = read_byte(data, seek)?;
        //println!("read_track(), flags: {}", flags);
        track.percussion_track = (flags & 0x01) == 0x01; //Drums track
        track.twelve_stringed_guitar_track = (flags & 0x02) == 0x02; //12 stringed guitar track
        track.banjo_track = (flags & 0x04) == 0x04; //Banjo track

        track.name = read_byte_size_string(data, seek, 40)?;
        let string_count = read_int(data, seek)?.to_u8_gp("string count")?;
        track.strings.clear();
        for i in 0..7i8 {
            let i_tuning = read_int(data, seek)?.to_i8_gp("string tuning")?;
            if string_count.to_i8_gp("string count")? > i {
                track.strings.push((i + 1, i_tuning));
            }
        }
        //println!("tuning: {:?}", track.strings);
        track.port = read_int(data, seek)?.to_u8_gp("port")?;
        let index = self.read_channel(data, seek)?;
        if self.channels[index].channel == 9 {
            track.percussion_track = true;
        }
        track.fret_count = read_int(data, seek)?.to_u8_gp("fret count")?;
        track.offset = read_int(data, seek)?;
        track.color = read_color(data, seek)?;
        //println!("\tInstrument: {} \t Strings: {}/{} ({:?})", self.channels[index].get_instrument_name(), string_count, track.strings.len(), track.strings);
        self.tracks.push(track);
        Ok(())
    }

    /// Read track. If it's Guitar Pro 5.0 format and track is first then one blank byte is read. Then go track's flags. It presides the track's attributes:
    /// - *0x01*: drums track
    /// - *0x02*: 12 stringed guitar track
    /// - *0x04*: banjo track
    /// - *0x08*: track visibility
    /// - *0x10*: track is soloed
    /// - *0x20*: track is muted
    /// - *0x40*: RSE is enabled
    /// - *0x80*: show tuning in the header of the sheet.
    ///
    /// Flags are followed by:
    /// - Name: `String`. A 40 characters long string containing the track's name.
    /// - Number of strings: :ref:`int`. An integer equal to the number of strings of the track.
    /// - Tuning of the strings: `Table of integers`. The tuning of the strings is stored as a 7-integers table, the "Number of strings" first integers being really used. The strings are stored from the highest to the lowest.
    /// - Port: :ref:`int`. The number of the MIDI port used.
    /// - Channel. See `GP3File.readChannel`.
    /// - Number of frets: :ref:`int`. The number of frets of the instrument.
    /// - Height of the capo: :ref:`int`. The number of the fret on which a capo is set. If no capo is used, the value is 0.
    /// - Track's color. The track's displayed color in Guitar Pro.
    ///
    /// The properties are followed by second set of flags stored in a :ref:`short`:
    /// - *0x0001*: show tablature
    /// - *0x0002*: show standard notation
    /// - *0x0004*: chord diagrams are below standard notation
    /// - *0x0008*: show rhythm with tab
    /// - *0x0010*: force horizontal beams
    /// - *0x0020*: force channels 11 to 16
    /// - *0x0040*: diagram list on top of the score
    /// - *0x0080*: diagrams in the score
    /// - *0x0200*: auto let-ring
    /// - *0x0400*: auto brush
    /// - *0x0800*: extend rhythmic inside the tab
    ///
    /// Then follow:
    /// - Auto accentuation: :ref:`byte`. See :class:`guitarpro.models.Accentuation`.
    /// - MIDI bank: :ref:`byte`.
    /// - Track RSE. See `readTrackRSE`.
    fn read_track_v5(&mut self, data: &[u8], seek: &mut usize, number: usize) -> GpResult<()> {
        let mut track = Track {
            number: number.to_i32_gp("track number")?,
            ..Default::default()
        };
        if number == 0 || self.version.number == (5, 0, 0) {
            *seek += 1;
        } //always 0 //missing 3 skips?
        let flags1 = read_byte(data, seek)?;
        //println!("read_track_v5(), flags1: {} \t seek: {}", flags1, *seek);
        track.percussion_track = (flags1 & 0x01) == 0x01;
        track.banjo_track = (flags1 & 0x02) == 0x02;
        track.visible = (flags1 & 0x04) == 0x04;
        track.solo = (flags1 & 0x10) == 0x10;
        track.mute = (flags1 & 0x20) == 0x20;
        track.use_rse = (flags1 & 0x40) == 0x40;
        track.indicate_tuning = (flags1 & 0x80) == 0x80;
        track.name = read_byte_size_string(data, seek, 40)?;
        let sc = read_int(data, seek)?;
        //println!("read_track_v5(), track:name: \"{}\", string count: {}", track.name, sc);
        let string_count = sc.to_u8_gp("string count")?;
        track.strings.clear();
        for i in 0i8..7i8 {
            let i_tuning = read_int(data, seek)?.to_i8_gp("string tuning")?;
            if string_count.to_i8_gp("string count")? > i {
                track.strings.push((i + 1, i_tuning));
            }
        }
        track.port = read_int(data, seek)?.to_u8_gp("port")?;
        self.read_channel(data, seek)?;
        if self.channels[number].channel == 9 {
            track.percussion_track = true;
        }
        track.fret_count = read_int(data, seek)?.to_u8_gp("fret count")?;
        track.offset = read_int(data, seek)?;
        track.color = read_color(data, seek)?;

        let flags2 = read_short(data, seek)?;
        //println!("read_track_v5(), flags2: {}", flags2);
        track.settings.tablature = (flags2 & 0x0001) == 0x0001;
        track.settings.notation = (flags2 & 0x0002) == 0x0002;
        track.settings.diagram_are_below = (flags2 & 0x0004) == 0x0004;
        track.settings.show_rythm = (flags2 & 0x0008) == 0x0008;
        track.settings.force_horizontal = (flags2 & 0x0010) == 0x0010;
        track.settings.force_channels = (flags2 & 0x0020) == 0x0020;
        track.settings.diagram_list = (flags2 & 0x0040) == 0x0040;
        track.settings.diagram_in_score = (flags2 & 0x0080) == 0x0080;
        //0x0100 ???
        track.settings.auto_let_ring = (flags2 & 0x0200) == 0x0200;
        track.settings.auto_brush = (flags2 & 0x0400) == 0x0400;
        track.settings.extend_rythmic = (flags2 & 0x0800) == 0x0800;

        track.rse.auto_accentuation = get_accentuation(read_byte(data, seek)?)?;
        self.channels[number].bank = read_byte(data, seek)?;
        self.read_track_rse(data, seek, &mut track)?;
        self.tracks.push(track);
        Ok(())
    }

    fn write_tracks(&self, data: &mut Vec<u8>, version: &(u8, u8, u8)) -> GpResult<()> {
        for i in 0..self.tracks.len() {
            if version.0 < 5 {
                self.write_track(data, i)?;
            } else {
                self.write_track_v5(data, i, version)?;
            }
        }
        if version.0 == 5 {
            write_placeholder_default(data, if version == &(5, 0, 0) { 2 } else { 1 });
        }
        Ok(())
    }
    fn write_track(&self, data: &mut Vec<u8>, number: usize) -> GpResult<()> {
        let mut flags = 0x00;
        if self.tracks[number].percussion_track {
            flags |= 0x01;
        }
        if self.tracks[number].twelve_stringed_guitar_track {
            flags |= 0x02;
        }
        if self.tracks[number].banjo_track {
            flags |= 0x04;
        }
        write_byte(data, flags);
        write_byte_size_string(data, &self.tracks[number].name);
        write_placeholder_default(data, 40 - self.tracks[number].name.len());
        write_i32(
            data,
            self.tracks[number].strings.len().to_i32_gp("strings len")?,
        );
        for i in 0..7usize {
            let mut tuning = 0i8;
            if i < self.tracks[number].strings.len() {
                tuning = self.tracks[number].strings[i].1;
            }
            write_i32(data, tuning.to_i32_gp("string tuning")?);
        }
        write_i32(data, self.tracks[number].port.to_i32_gp("port")?);
        //write channel
        write_i32(
            data,
            self.channels[self.tracks[number].channel_index]
                .channel
                .to_i32_gp("channel")?
                + 1,
        );
        write_i32(
            data,
            self.channels[self.tracks[number].channel_index]
                .effect_channel
                .to_i32_gp("effect channel")?
                + 1,
        );
        //end write channel
        write_i32(
            data,
            self.tracks[number].fret_count.to_i32_gp("fret count")?,
        );
        write_i32(data, self.tracks[number].offset);
        write_color(data, self.tracks[number].color);
        Ok(())
    }
    fn write_track_v5(
        &self,
        data: &mut Vec<u8>,
        number: usize,
        version: &(u8, u8, u8),
    ) -> GpResult<()> {
        if number == 0 || version == &(5, 0, 0) {
            write_placeholder_default(data, 1);
        }
        let mut flags1 = 0u8;
        if self.tracks[number].percussion_track {
            flags1 |= 0x01;
        }
        if self.tracks[number].twelve_stringed_guitar_track {
            flags1 |= 0x02;
        }
        if self.tracks[number].banjo_track {
            flags1 |= 0x04;
        }
        if self.tracks[number].visible {
            flags1 |= 0x08;
        }
        if self.tracks[number].solo {
            flags1 |= 0x10;
        }
        if self.tracks[number].mute {
            flags1 |= 0x20;
        }
        if self.tracks[number].use_rse {
            flags1 |= 0x40;
        }
        if self.tracks[number].indicate_tuning {
            flags1 |= 0x80;
        }
        write_byte(data, flags1);

        write_byte_size_string(data, &self.tracks[number].name);
        write_placeholder_default(data, 40 - self.tracks[number].name.len());

        write_i32(
            data,
            self.tracks[number].strings.len().to_i32_gp("strings len")?,
        );
        for i in 0..7usize {
            let mut tuning = 0i8;
            if i < self.tracks[number].strings.len() {
                tuning = self.tracks[number].strings[i].1;
            }
            write_i32(data, tuning.to_i32_gp("string tuning")?);
        }
        write_i32(data, self.tracks[number].port.to_i32_gp("port")?);
        //write channel
        write_i32(
            data,
            self.channels[self.tracks[number].channel_index]
                .channel
                .to_i32_gp("channel")?
                + 1,
        );
        write_i32(
            data,
            self.channels[self.tracks[number].channel_index]
                .effect_channel
                .to_i32_gp("effect channel")?
                + 1,
        );
        //end write channel
        write_i32(
            data,
            self.tracks[number].fret_count.to_i32_gp("fret count")?,
        );
        write_i32(data, self.tracks[number].offset);
        write_color(data, self.tracks[number].color);

        let mut flags2 = 0i16;
        if self.tracks[number].settings.tablature {
            flags2 |= 0x0001;
        }
        if self.tracks[number].settings.notation {
            flags2 |= 0x0002;
        }
        if self.tracks[number].settings.diagram_are_below {
            flags2 |= 0x0004;
        }
        if self.tracks[number].settings.show_rythm {
            flags2 |= 0x0008;
        }
        if self.tracks[number].settings.force_horizontal {
            flags2 |= 0x0010;
        }
        if self.tracks[number].settings.force_channels {
            flags2 |= 0x0020;
        }
        if self.tracks[number].settings.diagram_list {
            flags2 |= 0x0040;
        }
        if self.tracks[number].settings.diagram_in_score {
            flags2 |= 0x0080;
        }
        if self.tracks[number].settings.auto_let_ring {
            flags2 |= 0x0200;
        }
        if self.tracks[number].settings.auto_brush {
            flags2 |= 0x0400;
        }
        if self.tracks[number].settings.extend_rythmic {
            flags2 |= 0x0800;
        }
        write_i16(data, flags2);

        write_byte(
            data,
            from_accentuation(&self.tracks[number].rse.auto_accentuation),
        );
        write_byte(data, self.channels[self.tracks[number].channel_index].bank);
        self.write_track_rse(data, &self.tracks[number].rse, version);
        Ok(())
    }
}