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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
use crate::error::{GpResult, ToPrimitiveGp};
use crate::{
    io::primitive::*,
    model::{chord::*, effects::*, enums::*, key_signature::*, mix_table::*, note::*, song::*},
};

/// Parameters of beat display
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeatDisplay {
    break_beam: bool,
    force_beam: bool,
    beam_direction: VoiceDirection,
    tuplet_bracket: TupletBracket,
    break_secondary: u8,
    break_secondary_tuplet: bool,
    force_bracket: bool,
}
impl Default for BeatDisplay {
    fn default() -> Self {
        BeatDisplay {
            break_beam: false,
            force_beam: false,
            beam_direction: VoiceDirection::None,
            tuplet_bracket: TupletBracket::None,
            break_secondary: 0,
            break_secondary_tuplet: false,
            force_bracket: false,
        }
    }
}

/// A stroke effect for beats.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeatStroke {
    pub direction: BeatStrokeDirection,
    pub value: u16,
    pub swap: bool,
}
impl Default for BeatStroke {
    fn default() -> Self {
        BeatStroke {
            direction: BeatStrokeDirection::None,
            value: 0,
            swap: false,
        }
    }
}
impl BeatStroke {
    pub(crate) fn swap_direction(&mut self) {
        if self.direction == BeatStrokeDirection::Up {
            self.direction = BeatStrokeDirection::Down
        } else if self.direction == BeatStrokeDirection::Down {
            self.direction = BeatStrokeDirection::Up
        }
    }
}

/// A voice contains multiple beats
#[derive(Debug, Clone)]
pub struct Voice {
    //pub measure: Measure, //circular depth?
    pub measure_index: i16,
    pub beats: Vec<Beat>,
    pub directions: VoiceDirection,
}
impl Default for Voice {
    fn default() -> Self {
        Voice {
            measure_index: 0,
            /*measure: Measure::default(),*/ beats: Vec::new(),
            directions: VoiceDirection::None,
        }
    }
}

/// This class contains all beat effects
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeatEffects {
    pub stroke: BeatStroke,
    pub has_rasgueado: bool,
    pub pick_stroke: BeatStrokeDirection,
    pub chord: Option<Chord>,
    pub fade_in: bool,
    pub tremolo_bar: Option<BendEffect>,
    pub mix_table_change: Option<MixTableChange>,
    pub slap_effect: SlapEffect,
    pub vibrato: bool,
}
impl Default for BeatEffects {
    fn default() -> Self {
        BeatEffects {
            stroke: BeatStroke::default(),
            has_rasgueado: false,
            pick_stroke: BeatStrokeDirection::None,
            chord: None,
            fade_in: false,
            tremolo_bar: None,
            mix_table_change: None,
            slap_effect: SlapEffect::None,
            vibrato: false,
        }
    }
}
impl BeatEffects {
    pub(crate) fn is_chord(&self) -> bool {
        self.chord.is_some()
    }
    pub(crate) fn is_tremolo_bar(&self) -> bool {
        self.tremolo_bar.is_some()
    }
    pub(crate) fn is_slap_effect(&self) -> bool {
        self.slap_effect != SlapEffect::None
    }
    pub(crate) fn has_pick_stroke(&self) -> bool {
        self.pick_stroke != BeatStrokeDirection::None
    }
    pub(crate) fn is_default(&self) -> bool {
        let d = BeatEffects::default();
        self.stroke == d.stroke
            && self.has_rasgueado == d.has_rasgueado
            && self.pick_stroke == d.pick_stroke
            && self.fade_in == d.fade_in
            && self.vibrato == d.vibrato
            && self.tremolo_bar == d.tremolo_bar
            && self.slap_effect == d.slap_effect
    }
}

/// A beat contains multiple notes
#[derive(Debug, Clone, PartialEq)]
pub struct Beat {
    pub notes: Vec<Note>,
    pub duration: Duration,
    pub text: String,
    pub start: Option<i64>,
    pub effect: BeatEffects,
    pub octave: Octave,
    pub display: BeatDisplay,
    pub status: BeatStatus,
}
impl Default for Beat {
    fn default() -> Self {
        Beat {
            //voice
            notes: Vec::with_capacity(12),
            duration: Duration::default(),
            text: String::new(),
            start: None,
            effect: BeatEffects::default(),
            octave: Octave::None,
            display: BeatDisplay::default(),
            status: BeatStatus::Normal,
        }
    }
}
impl Beat {
    //pub(crate) fn start_in_measure(&self) -> u16 {self.start - self.voice.measure.start}
    pub(crate) fn has_vibrato(&self) -> bool {
        for i in 0..self.notes.len() {
            if self.notes[i].effect.vibrato {
                return true;
            }
        }
        false
    }
    pub(crate) fn has_harmonic(&self) -> bool {
        for i in 0..self.notes.len() {
            if self.notes[i].effect.is_harmonic() {
                return true;
            }
        }
        false
    }
}

pub trait SongBeatOps {
    fn read_beat(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        voice: &mut Voice,
        start: i64,
        track_index: usize,
    ) -> GpResult<i64>;
    fn read_beat_v5(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        voice: &mut Voice,
        start: &mut i64,
        track_index: usize,
    ) -> GpResult<i64>;
    fn read_beat_effects_v3(
        &self,
        data: &[u8],
        seek: &mut usize,
        note_effect: &mut NoteEffect,
    ) -> GpResult<BeatEffects>;
    fn read_beat_effects_v4(&self, data: &[u8], seek: &mut usize) -> GpResult<BeatEffects>;
    fn read_beat_stroke(&self, data: &[u8], seek: &mut usize) -> GpResult<BeatStroke>;
    fn stroke_value(&self, value: i8) -> u8;
    fn read_tremolo_bar(&self, data: &[u8], seek: &mut usize) -> GpResult<BendEffect>;
    fn write_beat_v3(&self, data: &mut Vec<u8>, beat: &Beat) -> GpResult<()>;
    fn write_beat(
        &self,
        data: &mut Vec<u8>,
        beat: &Beat,
        strings: &[(i8, i8)],
        version: &(u8, u8, u8),
    ) -> GpResult<()>;
    fn write_beat_effect_v3(&self, data: &mut Vec<u8>, beat: &Beat) -> GpResult<()>;
    fn write_beat_effect_v4(
        &self,
        data: &mut Vec<u8>,
        beat: &Beat,
        version: &(u8, u8, u8),
    ) -> GpResult<()>;
    fn write_tremolo_bar(&self, data: &mut Vec<u8>, bar: &Option<BendEffect>) -> GpResult<()>;
    fn write_beat_stroke(
        &self,
        data: &mut Vec<u8>,
        stroke: &BeatStroke,
        version: &(u8, u8, u8),
    ) -> GpResult<()>;
    fn from_stroke_value(value: u8) -> i8;
}

impl SongBeatOps for Song {
    /// Read beat. The first byte is the beat flags. It lists the data present in the current beat:
    /// - *0x01*: dotted notes- *0x02*: presence of a chord diagram
    /// - *0x04*: presence of a text
    /// - *0x08*: presence of effects
    /// - *0x10*: presence of a mix table change event
    /// - *0x20*: the beat is a n-tuplet
    /// - *0x40*: status: True if the beat is empty of if it is a rest
    /// - *0x80*: *blank*
    ///
    /// Flags are followed by:
    /// - Status: `byte`. If flag at *0x40* is true, read one byte. If value of the byte is *0x00* then beat is empty, if value is *0x02* then the beat is rest.
    /// - Beat duration: `byte`. See `Duration::read()`.
    /// - Chord diagram. See `Chord::read()`.
    /// - Text: `int-byte-size-string`.
    /// - Beat effects. See `BeatEffects::read()`.
    /// - Mix table change effect. See `MixTableChange::read()`.
    fn read_beat(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        voice: &mut Voice,
        start: i64,
        track_index: usize,
    ) -> GpResult<i64> {
        let flags = read_byte(data, seek)?;
        //println!("read_beat(),    flags: {} \t seek: {}", flags, *seek);
        //get a beat — for GP5+, never merge beats at the same start time
        let mut b = 0;
        let mut new_beat = true;
        if self.version.number < (5, 0, 0) {
            for i in (0usize..voice.beats.len()).rev() {
                if voice.beats[i].start == Some(start) {
                    b = i;
                    new_beat = false;
                    break;
                }
            }
        }
        if new_beat {
            voice.beats.push(Beat {
                start: Some(start),
                ..Default::default()
            });
            b = voice.beats.len() - 1;
        }

        if (flags & 0x40) == 0x40 {
            voice.beats[b].status = get_beat_status(read_byte(data, seek)?);
        } //else { voice.beats[b].status = BeatStatus::Normal;}
        let duration = read_duration(data, seek, flags)?;
        let mut note_effect = NoteEffect::default();
        if (flags & 0x02) == 0x02 {
            voice.beats[b].effect.chord = Some(
                self.read_chord(
                    data,
                    seek,
                    self.tracks[track_index]
                        .strings
                        .len()
                        .to_u8_gp("string count")?,
                )?,
            );
        }
        if (flags & 0x04) == 0x04 {
            voice.beats[b].text = read_int_byte_size_string(data, seek)?;
        }
        if (flags & 0x08) == 0x08 {
            let chord = voice.beats[b].effect.chord.clone();
            if self.version.number.0 == 3 {
                voice.beats[b].effect = self.read_beat_effects_v3(data, seek, &mut note_effect)?;
            } else {
                voice.beats[b].effect = self.read_beat_effects_v4(data, seek)?;
            }
            voice.beats[b].effect.chord = chord;
        }
        if (flags & 0x10) == 0x10 {
            let mtc = self.read_mix_table_change(data, seek)?;
            voice.beats[b].effect.mix_table_change = Some(mtc);
        }
        self.read_notes(
            data,
            seek,
            track_index,
            &mut voice.beats[b],
            &duration,
            note_effect,
        )?;
        Ok(if voice.beats[b].status == BeatStatus::Empty {
            0
        } else {
            duration.time()?.to_i64_gp("beat duration time")?
        })
    }
    /// Read beat. First, beat is read is in Guitar Pro 3 `guitarpro.gp3.readBeat`. Then it is followed by set of flags stored in `short`.
    /// - *0x0001*: break beams
    /// - *0x0002*: direct beams down
    /// - *0x0004*: force beams
    /// - *0x0008*: direct beams up
    /// - *0x0010*: ottava (8va)
    /// - *0x0020*: ottava bassa (8vb)
    /// - *0x0040*: quindicesima (15ma)
    /// - *0x0100*: quindicesima bassa (15mb)
    /// - *0x0200*: start tuplet bracket here
    /// - *0x0400*: end tuplet bracket here
    /// - *0x0800*: break secondary beams
    /// - *0x1000*: break secondary tuplet
    /// - *0x2000*: force tuplet bracket
    /// - Break secondary beams: `byte`. Appears if flag at *0x0800* is set. Signifies how much beams should be broken.
    fn read_beat_v5(
        &mut self,
        data: &[u8],
        seek: &mut usize,
        voice: &mut Voice,
        start: &mut i64,
        track_index: usize,
    ) -> GpResult<i64> {
        let duration = self.read_beat(data, seek, voice, *start, track_index)?;
        //get the beat used in read_beat()
        let b = voice.beats.len() - 1;

        let flags2 = read_short(data, seek)?;
        //println!("read_beat_v5(), flags2: {} \t seek: {}", flags2, *seek);
        if (flags2 & 0x0010) == 0x0010 {
            voice.beats[b].octave = Octave::Ottava;
        }
        if (flags2 & 0x0020) == 0x0020 {
            voice.beats[b].octave = Octave::OttavaBassa;
        }
        if (flags2 & 0x0040) == 0x0040 {
            voice.beats[b].octave = Octave::Quindicesima;
        }
        if (flags2 & 0x0100) == 0x0100 {
            voice.beats[b].octave = Octave::QuindicesimaBassa;
        }

        voice.beats[b].display.break_beam = (flags2 & 0x0001) == 0x0001;
        voice.beats[b].display.force_beam = (flags2 & 0x0004) == 0x0004;
        voice.beats[b].display.force_bracket = (flags2 & 0x2000) == 0x2000;
        voice.beats[b].display.break_secondary_tuplet = (flags2 & 0x1000) == 0x1000;
        if (flags2 & 0x0002) == 0x0002 {
            voice.beats[b].display.beam_direction = VoiceDirection::Down;
        }
        if (flags2 & 0x0008) == 0x0008 {
            voice.beats[b].display.beam_direction = VoiceDirection::Up;
        }
        if (flags2 & 0x0200) == 0x0200 {
            voice.beats[b].display.tuplet_bracket = TupletBracket::Start;
        }
        if (flags2 & 0x0400) == 0x0400 {
            voice.beats[b].display.tuplet_bracket = TupletBracket::End;
        }
        if (flags2 & 0x0800) == 0x0800 {
            voice.beats[b].display.break_secondary = read_byte(data, seek)?;
        }

        Ok(duration)
    }

    /// Read beat effects. The first byte is effects flags:
    /// - *0x01*: vibrato- *0x02*: wide vibrato
    /// - *0x04*: natural harmonic
    /// - *0x08*: artificial harmonic
    /// - *0x10*: fade in
    /// - *0x20*: tremolo bar or slap effect
    /// - *0x40*: beat stroke direction
    /// - *0x80*: *blank*
    /// - Tremolo bar or slap effect: `byte`. If it's 0 then tremolo bar should be read (see `TremoloBar::read()`). Else it's tapping and values of the byte map to:
    /// - *1*: tap
    /// - *2*: slap
    /// - *3*: pop
    /// - Beat stroke direction. See `BeatStroke::read()`
    fn read_beat_effects_v3(
        &self,
        data: &[u8],
        seek: &mut usize,
        note_effect: &mut NoteEffect,
    ) -> GpResult<BeatEffects> {
        //println!("read_beat_effects()");
        let mut be = BeatEffects::default();
        let flags = read_byte(data, seek)?;
        note_effect.vibrato = (flags & 0x01) == 0x01 || note_effect.vibrato;
        be.vibrato = (flags & 0x02) == 0x02 || be.vibrato;
        be.fade_in = (flags & 0x10) == 0x10;
        if (flags & 0x20) == 0x20 {
            be.slap_effect = get_slap_effect(read_byte(data, seek)?)?;
            if be.slap_effect == SlapEffect::None {
                be.tremolo_bar = Some(self.read_tremolo_bar(data, seek)?);
            } else {
                read_int(data, seek)?;
            }
        }
        if (flags & 0x40) == 0x40 {
            be.stroke = self.read_beat_stroke(data, seek)?;
        }
        //In GP3 harmonics apply to the whole beat, not the individual notes. Here we set the noteEffect for all the notes in the beat.
        if (flags & 0x04) == 0x04 {
            note_effect.harmonic = Some(HarmonicEffect::default());
        }
        if (flags & 0x08) == 0x08 {
            note_effect.harmonic = Some(HarmonicEffect {
                kind: HarmonicType::Artificial,
                ..Default::default()
            });
        }
        Ok(be)
    }
    ///Read beat effects. Beat effects are read using two byte flags. The first byte of flags is:
    /// - *0x01*: *blank*
    /// - *0x02*: wide vibrato
    /// - *0x04*: *blank*
    /// - *0x08*: *blank*
    /// - *0x10*: fade in
    /// - *0x20*: slap effect
    /// - *0x40*: beat stroke
    /// - *0x80*: *blank*
    ///
    /// The second byte of flags is:
    /// - *0x01*: rasgueado
    /// - *0x02*: pick stroke
    /// - *0x04*: tremolo bar
    /// - *0x08*: *blank*
    /// - *0x10*: *blank*
    /// - *0x20*: *blank*
    /// - *0x40*: *blank*
    /// - *0x80*: *blank*
    ///
    /// Flags are followed by:
    /// - Slap effect: `signed-byte`. For value mapping see `SlapEffect`.
    /// - Tremolo bar. See `readTremoloBar`.
    /// - Beat stroke. See `readBeatStroke`.
    /// - Pick stroke: `signed-byte`. For value mapping see `BeatStrokeDirection`.
    fn read_beat_effects_v4(&self, data: &[u8], seek: &mut usize) -> GpResult<BeatEffects> {
        let mut be = BeatEffects::default();
        let flags1 = read_signed_byte(data, seek)?;
        let flags2 = read_signed_byte(data, seek)?;
        be.vibrato = (flags1 & 0x02) == 0x02 || be.vibrato;
        be.fade_in = (flags1 & 0x10) == 0x10;
        if (flags1 & 0x20) == 0x20 {
            let v = read_signed_byte(data, seek)?;
            be.slap_effect = if v < 0 {
                SlapEffect::None
            } else {
                get_slap_effect(v as u8)?
            };
        }
        if (flags2 & 0x04) == 0x04 {
            be.tremolo_bar = self.read_bend_effect(data, seek)?;
        }
        if (flags1 & 0x40) == 0x40 {
            be.stroke = self.read_beat_stroke(data, seek)?;
        }
        be.has_rasgueado = (flags2 & 0x01) == 0x01;
        if (flags2 & 0x02) == 0x02 {
            be.pick_stroke = get_beat_stroke_direction(read_signed_byte(data, seek)?)?;
        }
        //println!("Beat effect: {:?}", be);
        Ok(be)
    }
    /// Read beat stroke. Beat stroke consists of two `Bytes <byte>` which correspond to stroke up
    /// and stroke down speed. See `BeatStrokeDirection` for value mapping.
    fn read_beat_stroke(&self, data: &[u8], seek: &mut usize) -> GpResult<BeatStroke> {
        //println!("read_beat_stroke()");
        let mut bs = BeatStroke::default();
        let down = read_signed_byte(data, seek)?;
        let up = read_signed_byte(data, seek)?;
        if up > 0 {
            bs.direction = BeatStrokeDirection::Up;
            bs.value = self.stroke_value(up).to_u16_gp("stroke value")?;
        }
        if down > 0 {
            bs.direction = BeatStrokeDirection::Down;
            bs.value = self.stroke_value(down).to_u16_gp("stroke value")?;
        }
        if self.version.number >= (5, 0, 0) {
            bs.swap_direction();
        }
        Ok(bs)
    }

    fn stroke_value(&self, value: i8) -> u8 {
        match value {
            1 => DURATION_HUNDRED_TWENTY_EIGHTH,
            2 => DURATION_SIXTY_FOURTH,
            3 => DURATION_THIRTY_SECOND,
            4 => DURATION_SIXTEENTH,
            5 => DURATION_EIGHTH,
            6 => DURATION_QUARTER,
            _ => DURATION_SIXTY_FOURTH,
        }
    }
    /// Read tremolo bar beat effect. The only type of tremolo bar effect Guitar Pro 3 supports is `dip <BendType::Dip>`. The value of the
    /// effect is encoded in `Int` and shows how deep tremolo bar is pressed.
    fn read_tremolo_bar(&self, data: &[u8], seek: &mut usize) -> GpResult<BendEffect> {
        //println!("read_tremolo_bar()");
        let mut be = BendEffect {
            kind: BendType::Dip,
            ..Default::default()
        };
        be.value = read_int(data, seek)?.to_i16_gp("tremolo bar value")?;
        be.points.push(BendPoint {
            position: 0,
            value: 0,
            ..Default::default()
        });
        be.points.push(BendPoint {
            position: BEND_EFFECT_MAX_POSITION / 2,
            value: (-f32::from(be.value) / GP_BEND_SEMITONE)
                .round()
                .to_i8_gp("bend point value")?,
            ..Default::default()
        });
        be.points.push(BendPoint {
            position: BEND_EFFECT_MAX_POSITION,
            value: 0,
            ..Default::default()
        });
        Ok(be)
    }

    fn write_beat_v3(&self, data: &mut Vec<u8>, beat: &Beat) -> GpResult<()> {
        let mut flags = 0u8;
        if beat.duration.dotted {
            flags |= 0x01;
        }
        if beat.effect.is_chord() {
            flags |= 0x02;
        }
        if !beat.text.is_empty() {
            flags |= 0x04;
        }
        if !beat.effect.is_default() || beat.has_harmonic() || beat.has_vibrato() {
            flags |= 0x08;
        }
        if let Some(mtc) = &beat.effect.mix_table_change {
            if mtc.is_just_wah() {
                flags |= 0x10;
            }
        }
        if !beat.duration.is_default_tuplet() {
            flags |= 0x20;
        }
        if beat.status != BeatStatus::Normal {
            flags |= 0x40;
        }
        write_byte(data, flags);
        if (flags & 0x40) == 0x40 {
            write_byte(data, from_beat_status(&beat.status));
        }
        beat.duration.write_duration(data, flags)?;
        if (flags & 0x02) == 0x02 {
            self.write_chord(data, beat);
        }
        if (flags & 0x04) == 0x04 {
            write_int_byte_size_string(data, &beat.text);
        }
        if (flags & 0x08) == 0x08 {
            self.write_beat_effect_v3(data, beat)?;
        }
        if (flags & 0x10) == 0x10 {
            self.write_mix_table_change(data, &beat.effect.mix_table_change, &(3, 0, 0));
        }
        self.write_notes(data, beat, &Vec::new(), &(3, 0, 0))?;
        Ok(())
    }

    fn write_beat(
        &self,
        data: &mut Vec<u8>,
        beat: &Beat,
        strings: &[(i8, i8)],
        version: &(u8, u8, u8),
    ) -> GpResult<()> {
        let mut flags = 0u8;
        if beat.duration.dotted {
            flags |= 0x01;
        }
        if beat.effect.is_chord() {
            flags |= 0x02;
        }
        if !beat.text.is_empty() {
            flags |= 0x04;
        }
        if !beat.effect.is_default() {
            flags |= 0x08;
        }
        if let Some(mtc) = &beat.effect.mix_table_change {
            if mtc.is_just_wah() || version.0 > 4 {
                flags |= 0x10;
            }
        }
        if !beat.duration.is_default_tuplet() {
            flags |= 0x20;
        }
        if beat.status != BeatStatus::Normal {
            flags |= 0x40;
        }
        write_byte(data, flags);
        if (flags & 0x40) == 0x40 {
            write_byte(data, from_beat_status(&beat.status));
        }
        beat.duration.write_duration(data, flags)?;
        if (flags & 0x02) == 0x02 {
            self.write_chord_v4(data, beat);
        }
        if (flags & 0x04) == 0x04 {
            write_int_byte_size_string(data, &beat.text);
        }
        if (flags & 0x08) == 0x08 {
            self.write_beat_effect_v4(data, beat, version)?;
        }
        if (flags & 0x10) == 0x10 {
            self.write_mix_table_change(data, &beat.effect.mix_table_change, version);
        }
        self.write_notes(data, beat, strings, version)?;
        if version.0 == 5 {
            let mut flags2 = 0i16;
            if beat.display.break_beam {
                flags2 |= 0x0001;
            }
            if beat.display.beam_direction == VoiceDirection::Down {
                flags2 |= 0x0002;
            }
            if beat.display.force_beam {
                flags2 |= 0x0004;
            }
            if beat.display.beam_direction == VoiceDirection::Up {
                flags2 |= 0x0008;
            }
            if beat.octave == Octave::Ottava {
                flags2 |= 0x0010;
            }
            if beat.octave == Octave::OttavaBassa {
                flags2 |= 0x0020;
            }
            if beat.octave == Octave::Quindicesima {
                flags2 |= 0x0040;
            }
            if beat.octave == Octave::QuindicesimaBassa {
                flags2 |= 0x0100;
            }
            if beat.display.tuplet_bracket == TupletBracket::Start {
                flags2 |= 0x0200;
            }
            if beat.display.tuplet_bracket == TupletBracket::End {
                flags2 |= 0x0400;
            }
            if beat.display.break_secondary != 0 {
                flags2 |= 0x0800;
            }
            if beat.display.break_secondary_tuplet {
                flags2 |= 0x1000;
            }
            if beat.display.force_bracket {
                flags2 |= 0x2000;
            }
            write_i16(data, flags2);
            if (flags2 & 0x0800) == 0x0800 {
                write_byte(data, beat.display.break_secondary);
            }
        }
        Ok(())
    }

    fn write_beat_effect_v3(&self, data: &mut Vec<u8>, beat: &Beat) -> GpResult<()> {
        let mut flags1: u8 = 0;
        if beat.has_vibrato() {
            flags1 |= 0x01;
        }
        if beat.effect.vibrato {
            flags1 |= 0x02;
        }
        if beat.has_harmonic() {
            for n in 0..beat.notes.len() {
                if let Some(h) = &beat.notes[n].effect.harmonic {
                    if h.kind == HarmonicType::Natural {
                        flags1 |= 0x04;
                    }
                    if h.kind == HarmonicType::Artificial {
                        flags1 |= 0x08;
                    }
                }
            }
        }
        if beat.effect.fade_in {
            flags1 |= 0x10;
        }
        if beat.effect.is_tremolo_bar() || beat.effect.is_slap_effect() {
            flags1 |= 0x20;
        }
        if beat.effect.stroke.direction != BeatStrokeDirection::None
            && beat.effect.stroke.value != 0
        {
            flags1 |= 0x40;
        }
        write_byte(data, flags1);
        if (flags1 & 0x20) == 0x20 {
            write_byte(data, from_slap_effect(&beat.effect.slap_effect));
            self.write_tremolo_bar(data, &beat.effect.tremolo_bar)?;
        }
        if (flags1 & 0x40) == 0x40 {
            self.write_beat_stroke(data, &beat.effect.stroke, &(3, 0, 0))?;
        }
        Ok(())
    }

    fn write_beat_effect_v4(
        &self,
        data: &mut Vec<u8>,
        beat: &Beat,
        version: &(u8, u8, u8),
    ) -> GpResult<()> {
        let mut flags1: i8 = 0;
        if beat.effect.vibrato {
            flags1 |= 0x02;
        }
        if beat.effect.fade_in {
            flags1 |= 0x10;
        }
        if beat.effect.is_slap_effect() {
            flags1 |= 0x20;
        }
        if beat.effect.stroke.direction != BeatStrokeDirection::None
            && beat.effect.stroke.value != 0
        {
            flags1 |= 0x40;
        }
        write_signed_byte(data, flags1);

        let mut flags2 = 0i8;
        if beat.effect.has_rasgueado {
            flags2 |= 0x01;
        }
        if beat.effect.has_pick_stroke() {
            flags2 |= 0x02;
        }
        if beat.effect.is_tremolo_bar() {
            flags2 |= 0x04;
        }
        write_signed_byte(data, flags2);

        if (flags1 & 0x20) == 0x20 {
            write_signed_byte(
                data,
                from_slap_effect(&beat.effect.slap_effect).to_i8_gp("slap effect")?,
            );
        }
        if (flags2 & 0x04) == 0x04 {
            self.write_bend(data, &beat.effect.tremolo_bar)?;
        } //write tremolo bar
        if (flags1 & 0x40) == 0x40 {
            self.write_beat_stroke(data, &beat.effect.stroke, version)?;
        }
        if (flags2 & 0x02) == 0x02 {
            write_signed_byte(data, from_beat_stroke_direction(&beat.effect.pick_stroke));
        }
        Ok(())
    }

    fn write_tremolo_bar(&self, data: &mut Vec<u8>, bar: &Option<BendEffect>) -> GpResult<()> {
        if let Some(b) = bar {
            write_i32(data, b.value.to_i32_gp("tremolo bar value")?);
        } else {
            write_i32(data, 0);
        }
        Ok(())
    }
    fn write_beat_stroke(
        &self,
        data: &mut Vec<u8>,
        stroke: &BeatStroke,
        version: &(u8, u8, u8),
    ) -> GpResult<()> {
        let mut stroke = stroke.clone();
        if version.0 == 5 {
            stroke.swap_direction();
        }
        let mut stroke_down = 0i8;
        let mut stroke_up = 0i8;
        if stroke.direction == BeatStrokeDirection::Up {
            stroke_up = Self::from_stroke_value(stroke.value.to_u8_gp("stroke value")?);
        } else if stroke.direction == BeatStrokeDirection::Down {
            stroke_down = Self::from_stroke_value(stroke.value.to_u8_gp("stroke value")?);
        }
        write_signed_byte(data, stroke_down);
        write_signed_byte(data, stroke_up);
        Ok(())
    }

    fn from_stroke_value(value: u8) -> i8 {
        if value == DURATION_HUNDRED_TWENTY_EIGHTH {
            1
        } else if value == DURATION_SIXTY_FOURTH {
            2
        } else if value == DURATION_THIRTY_SECOND {
            3
        } else if value == DURATION_SIXTEENTH {
            4
        } else if value == DURATION_EIGHTH {
            5
        } else if value == DURATION_QUARTER {
            6
        } else {
            1
        }
    }
}