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
//! Structures representing parsed ABC music.

pub mod writer;

#[derive(Clone, Debug, PartialEq)]
pub struct TuneBook {
    pub header: Option<FileHeader>,
    pub tunes: Vec<Tune>,
}
impl TuneBook {
    pub fn new(header: Option<FileHeader>, tunes: Vec<Tune>) -> TuneBook {
        TuneBook { header, tunes }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileHeader {
    pub info: Vec<InfoField>,
}
impl FileHeader {
    pub fn new(info: Vec<InfoField>) -> FileHeader {
        FileHeader { info }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Tune {
    pub header: TuneHeader,
    pub body: Option<TuneBody>,
}
impl Tune {
    pub fn new(header: TuneHeader, body: Option<TuneBody>) -> Tune {
        Tune { header, body }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TuneHeader {
    pub info: Vec<InfoField>,
}
impl TuneHeader {
    pub fn new(info: Vec<InfoField>) -> TuneHeader {
        TuneHeader { info }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InfoField(pub char, pub String);
impl InfoField {
    pub fn new(c: char, s: String) -> InfoField {
        InfoField(c, s)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct TuneBody {
    pub music: Vec<MusicLine>,
}
impl TuneBody {
    pub fn new(music: Vec<MusicLine>) -> TuneBody {
        TuneBody { music }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct MusicLine {
    pub symbols: Vec<MusicSymbol>,
}
impl MusicLine {
    pub fn new(symbols: Vec<MusicSymbol>) -> MusicLine {
        MusicLine { symbols }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum MusicSymbol {
    Note {
        decorations: Vec<Decoration>,
        accidental: Option<Accidental>,
        note: Note,
        octave: i8,
        length: f32,
        tie: Option<Tie>,
    },
    Chord {
        decorations: Vec<Decoration>,
        notes: Vec<MusicSymbol>,
        length: f32,
    },
    GraceNotes {
        acciaccatura: Option<()>,
        notes: Vec<MusicSymbol>,
    },
    /// An `r` value of 0 indicates that the parser does not have enough information and it must
    /// be determined from the time signature.
    Tuplet {
        p: u32,
        q: u32,
        r: u32,
        notes: Vec<MusicSymbol>,
    },
    Bar(String),
    Rest(Rest),
    Ending(u32),
    VisualBreak,
}

/// A tie which may apply between a note and the following note of the same pitch.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Tie {
    /// A normal tie.
    Solid,
    /// A dotted tie.
    Dotted,
}

static TUPLET_Q: [u32; 8] = [3, 2, 3, 0, 2, 0, 3, 0];
impl MusicSymbol {
    pub fn new_note(
        decorations: Vec<Decoration>,
        accidental: Option<Accidental>,
        note: Note,
        octave: i8,
        length: f32,
        tie: Option<Tie>,
    ) -> MusicSymbol {
        MusicSymbol::Note {
            decorations,
            accidental,
            note,
            octave,
            length,
            tie,
        }
    }

    pub fn note(note: Note) -> MusicSymbol {
        MusicSymbol::Note {
            decorations: vec![],
            accidental: None,
            note,
            octave: 1,
            length: 1.0,
            tie: None,
        }
    }

    pub fn note_from_length(note: Note, length: f32) -> MusicSymbol {
        MusicSymbol::Note {
            decorations: vec![],
            accidental: None,
            note,
            octave: 1,
            length,
            tie: None,
        }
    }

    /// # Panics
    /// If the number of notes is not equal to `p`
    pub fn tuplet_with_defaults(
        p: u32,
        q: Option<u32>,
        r: Option<u32>,
        notes: Vec<MusicSymbol>,
    ) -> Result<MusicSymbol, &'static str> {
        assert_eq!(notes.len(), p as usize);

        Self::new_tuplet(
            p,
            q.unwrap_or(TUPLET_Q[(p - 2) as usize]),
            r.unwrap_or(p),
            notes,
        )
    }

    pub fn new_tuplet(
        p: u32,
        q: u32,
        r: u32,
        notes: Vec<MusicSymbol>,
    ) -> Result<MusicSymbol, &'static str> {
        if p < 2 {
            return Err("Tuplet too small");
        }
        if p > 9 {
            return Err("Tuplet too large");
        }
        if r == 0 {
            return Err("R can't be 0");
        }

        Ok(MusicSymbol::Tuplet { p, q, r, notes })
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Note {
    C,
    D,
    E,
    F,
    G,
    A,
    B,
}

impl From<Note> for char {
    fn from(note: Note) -> Self {
        match note {
            Note::C => 'C',
            Note::D => 'D',
            Note::E => 'E',
            Note::F => 'F',
            Note::G => 'G',
            Note::A => 'A',
            Note::B => 'B',
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Decoration {
    Staccato,
    Roll,
    Fermata,
    Accent,
    LowerMordent,
    Coda,
    UpperMordent,
    Segno,
    Trill,
    UpBow,
    DownBow,
    Unresolved(String),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Accidental {
    Natural,
    Sharp,
    Flat,
    DoubleSharp,
    DoubleFlat,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Rest {
    Note(u32),
    Measure(u32),
    NoteHidden(u32),
    MeasureHidden(u32),
}