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

pub mod writer;

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

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

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

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

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

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

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

#[derive(Debug, PartialEq)]
pub enum MusicSymbol {
    Note {
        decoration: Option<Decoration>,
        accidental: Option<Accidental>,
        note: char,
        octave: i8,
        length: f32
    },
    Chord {
        decoration: Option<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()
}

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

    pub fn note(note: char) -> MusicSymbol {
        MusicSymbol::Note {
            decoration: None,
            accidental: None,
            note,
            octave: 1,
            length: 1.0
        }
    }

    pub fn note_from_length(note: char, length: f32) -> MusicSymbol {
        MusicSymbol::Note {
            decoration: None,
            accidental: None,
            note,
            octave: 1,
            length
        }
    }

    /// # 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);

        let r = r.unwrap_or(p);
        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"); }

        let q = q.unwrap_or(TUPLET_Q[(p-2) as usize]);

        Ok(MusicSymbol::Tuplet {
            p, q, r, 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(Debug, PartialEq)]
pub enum Decoration {
    Staccato(),
    Roll(),
    Fermata(),
    Accent(),
    LowerMordent(),
    Coda(),
    UpperMordent(),
    Segno(),
    Trill(),
    UpBow(),
    DownBow(),
    Unresolved(String)
}

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

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