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
// midi_note.rs
//
// Copyright (c) 2020 All The Music, LLC
//
// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ or send
// a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

/// Error type for parsing [MIDINoteType](struct.MIDINoteType.html) from `&str`
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ParseMIDINoteTypeError {
    #[error("Unknown note type {input}")]
    UnknownNoteType { input: String },
}

/// MIDI note type
///
/// Represents each note in an octave, where each "*Sharp" value
/// is an enharmonic key.  Each note type must be combined with an
/// integer value to fully represent a key on the piano (see: [MIDINote](struct.MIDINote.html)).
/// The `Rest` note type represents silence, or the absence of a note.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum MIDINoteType {
    C,
    CSharp,
    D,
    DSharp,
    E,
    F,
    FSharp,
    G,
    GSharp,
    A,
    ASharp,
    B,
    Rest,
}

impl<'a> std::str::FromStr for MIDINoteType {
    type Err = ParseMIDINoteTypeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "bsharp"
            | "b#"
            | "c" => Ok(Self::C),
            "csharp"
            | "c#"
            | "dflat"
            | "d♭"
            | "db" => Ok(Self::CSharp),
            "d" => Ok(Self::D),
            "dsharp"
            | "d#"
            | "eflat"
            | "e♭"
            | "eb" => Ok(Self::DSharp),
            "e"
            | "fflat"
            | "f♭"
            | "fb" => Ok(Self::E),
            "esharp"
            | "e#"
            | "f" => Ok(Self::F),
            "fsharp"
            | "f#"
            | "gflat"
            | "g♭"
            | "gb" => Ok(Self::FSharp),
            "g" => Ok(Self::G),
            "gsharp"
            | "g#"
            | "aflat"
            | "a♭"
            | "ab" => Ok(Self::GSharp),
            "a" => Ok(Self::A),
            "asharp"
            | "a#"
            | "bflat"
            | "b♭"
            | "bb" => Ok(Self::ASharp),
            "cflat"
            | "c♭"
            | "cb"
            | "b" => Ok(Self::B),
            "rest" | "empty" => Ok(Self::Rest),
            _ => Err(ParseMIDINoteTypeError::UnknownNoteType {
                input: s.to_string(),
            }),
        }
    }
}

/// Error type for parsing [MIDINote](struct.MIDINote.html) from `&str`
///
/// Credit to [@ldesgoui](https://github.com/ldesgoui) for the suggestion.
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ParseMIDINoteError {
    #[error("Invalid note format (expected '<note>:<octave>', found {input})")]
    InvalidNoteFormat { input: String },
    #[error(transparent)]
    InvalidOctave(#[from] std::num::ParseIntError),
    #[error(transparent)]
    UnknownNoteType(#[from] ParseMIDINoteTypeError),
}

/// MIDI note
///
/// Represents key on a piano, combining a [note type](enum.MIDINoteType.html)
/// with an octave.  For example, middle C would be represented as
/// `MIDINote { note_type: MIDINoteType::C, octave: 4 }`.  For a detailed table
/// of MIDI notes and octave numbers, see document here:
/// <https://www.cs.cmu.edu/~music/cmsip/readings/Standard-MIDI-file-format-updated.pdf>.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MIDINote {
    pub note_type: MIDINoteType,
    pub octave: u32,
}

impl MIDINote {
    /// Create new `MIDINote`
    ///
    /// # Arguments
    ///
    /// * `note_type`: [MIDINoteType](enum.MIDINoteType.html)
    /// * `octave`: integer between -1 and 9
    ///
    /// # Examples
    ///
    /// ```rust
    /// // Middle C
    /// let note = libatm::MIDINote::new(libatm::MIDINoteType::C, 4);
    /// assert_eq!(60, note.convert());
    /// ```
    ///
    /// # Notes
    ///
    /// The `octave` parameter is not validated, but must be between
    /// -1 and 9 in order to represent a valid MIDI note.
    pub fn new(note_type: MIDINoteType, octave: u32) -> Self {
        Self { note_type, octave, }
    }

    /// Convert MIDI note to an integer representation (MIDI note number)
    ///
    /// The empty note (A.K.A. silence) is represented as `u32::max_value()`.
    /// The MIDI Tuning Standard only represents pitches up to 127, but instead
    /// of choosing some arbitrary number larger than 127, `u32::max_value()` is easily
    /// distinguishable.
    pub fn convert(&self) -> u32 {
        match &self.note_type {
            MIDINoteType::Rest => u32::max_value(),
            _ => (self.note_type as u32) + (self.octave + 1) * 12,
        }
    }
}

impl<'a> std::str::FromStr for MIDINote {
    type Err = ParseMIDINoteError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Split input on ':' to get note type and octave
        let split_pair: Vec<&str> = s.split(':').collect();
        // Ensure is a pair (length is 2)
        if split_pair.len() != 2 {
            return Err(ParseMIDINoteError::InvalidNoteFormat {
                input: s.to_string(),
            });
        }
        // Parse MIDINoteType from first item in pair
        let note_type = MIDINoteType::from_str(split_pair[0])?;
        // Parse octave (as u32) from second item in pair
        // TODO: Enforce octave range of -1 to 9
        let octave = split_pair[1].parse::<u32>()?;
        Ok(Self { note_type, octave })
    }
}

/// Error type for parsing [MIDINoteSet](struct.MIDINoteSet.html) and 
/// Vec<[MIDINote](struct.MIDINote.html)> from `&str`
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ParseMIDINoteSequenceError {
    #[error("Invalid note at index {0}")]
    ParseMIDINote(usize, #[source] ParseMIDINoteError),
}

/// Container for set of `MIDINote`
///
/// Implements the [FromStr](https://doc.rust-lang.org/nightly/core/str/trait.FromStr.html)
/// trait as a convenience method for parsing a set of `MIDINote` (from a command line
/// argument).
///
/// # Examples
///
/// ```rust
/// // Parse MIDI note set from &str
/// let note_set = "C:4,D:5,CSharp:8,DSharp:3".parse::<libatm::MIDINoteSet>().unwrap();
/// // Expected note set contains C:4, D:5, CSharp:8, and DSharp:3
/// let expected = libatm::MIDINoteSet(vec![
///     libatm::MIDINote::new(libatm::MIDINoteType::C, 4),
///     libatm::MIDINote::new(libatm::MIDINoteType::D, 5),
///     libatm::MIDINote::new(libatm::MIDINoteType::CSharp, 8),
///     libatm::MIDINote::new(libatm::MIDINoteType::DSharp, 3),
/// ].into_iter().collect::<std::collections::BTreeSet<libatm::MIDINote>>());
/// assert_eq!(expected, note_set);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct MIDINoteSet(pub std::collections::BTreeSet<MIDINote>);

impl std::ops::Deref for MIDINoteSet {
    type Target = std::collections::BTreeSet<MIDINote>;

    /// Allow dereferencing of tuple struct to underlying hash set
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::str::FromStr for MIDINoteSet {
    type Err = ParseMIDINoteSequenceError;

    /// Credit to [@ldesgoui](https://github.com/ldesgoui) for the implementation
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let notes = s
            .split(',')
            .enumerate()
            .map(|(idx, pair)| {
                pair.parse::<MIDINote>()
                    .map_err(|err| ParseMIDINoteSequenceError::ParseMIDINote(idx, err))
            })
            .collect::<Result<std::collections::BTreeSet<MIDINote>, ParseMIDINoteSequenceError>>()?;
        Ok(Self(notes))
    }
}

/// Container for vector of `MIDINote`
///
/// Implements the [FromStr](https://doc.rust-lang.org/nightly/core/str/trait.FromStr.html)
/// trait as a convenience method for parsing a list of `MIDINote` (from a command line
/// argument).
///
/// # Examples
///
/// ```rust
/// // Parse MIDI note set from &str
/// let note_set = "C:4,D:5,CSharp:8,DSharp:3".parse::<libatm::MIDINoteVec>().unwrap();
/// // Expected note set contains C:4, D:5, CSharp:8, and DSharp:3
/// let expected = libatm::MIDINoteVec(vec![
///     libatm::MIDINote::new(libatm::MIDINoteType::C, 4),
///     libatm::MIDINote::new(libatm::MIDINoteType::D, 5),
///     libatm::MIDINote::new(libatm::MIDINoteType::CSharp, 8),
///     libatm::MIDINote::new(libatm::MIDINoteType::DSharp, 3),
/// ]);
/// assert_eq!(expected, note_set);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct MIDINoteVec(pub Vec<MIDINote>);

impl std::ops::Deref for MIDINoteVec {
    type Target = Vec<MIDINote>;

    /// Allow dereferencing of tuple struct to underlying hash set
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::str::FromStr for MIDINoteVec {
    type Err = ParseMIDINoteSequenceError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let notes = s
            .split(',')
            .enumerate()
            .map(|(idx, pair)| {
                pair.parse::<MIDINote>()
                    .map_err(|err| ParseMIDINoteSequenceError::ParseMIDINote(idx, err))
            })
            .collect::<Result<Vec<MIDINote>, ParseMIDINoteSequenceError>>()?;
        Ok(Self(notes))
    }
}

impl std::iter::FromIterator<MIDINote> for MIDINoteVec {
    // Create MIDINoteVec from iterator over MIDINote
    fn from_iter<I: IntoIterator<Item=MIDINote>>(iter: I) -> Self {
        Self(iter.into_iter().collect::<Vec<MIDINote>>())
    }
}

impl From<MIDINoteSet> for MIDINoteVec {
    // Allow conversion from MIDINoteSet to Vec<MIDINote>
    fn from(set: MIDINoteSet) -> Self {
        set.0.into_iter().collect::<MIDINoteVec>()
    }
}

impl From<&MIDINoteSet> for MIDINoteVec {
    // Allow conversion from &MIDINoteSet to Vec<MIDINote>
    fn from(set: &MIDINoteSet) -> Self {
        set.iter().map(|n| n.clone()).collect::<MIDINoteVec>()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_midi_note_from_str_valid() {
        let observed = "C#:5".parse::<MIDINote>();
        let expected = Ok(MIDINote::new(MIDINoteType::CSharp, 5));
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_from_str_invalid_note_type() {
        let observed = "C$:5".parse::<MIDINote>();
        let expected = Err(
            ParseMIDINoteError::UnknownNoteType(
                ParseMIDINoteTypeError::UnknownNoteType { input: "C$".to_string() }
            )
        );
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_from_str_invalid_format() {
        let input = "C#;5".to_string();
        let observed = input.as_str().parse::<MIDINote>();
        let expected = Err(ParseMIDINoteError::InvalidNoteFormat { input });
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_from_str_invalid_octive() {
        let input = "C#:12.3".to_string();
        let observed = input.as_str().parse::<MIDINote>();
        // NOTE: Cannot compare to expected here because pattern matching on
        //       std::num::IntErrorKind is considered unstable.
        assert!(observed.is_err());
    }

    #[test]
    fn test_midi_note_set_from_str_valid_no_duplicate() {
        let observed = "C:4,D:4,E:4,F:4,F#:4,DFlat:5".parse::<MIDINoteSet>();
        let expected = Ok(MIDINoteSet(vec![
            MIDINote::new(MIDINoteType::C, 4),
            MIDINote::new(MIDINoteType::D, 4),
            MIDINote::new(MIDINoteType::E, 4),
            MIDINote::new(MIDINoteType::F, 4),
            MIDINote::new(MIDINoteType::FSharp, 4),
            MIDINote::new(MIDINoteType::CSharp, 5),
        ].into_iter().collect::<std::collections::BTreeSet<MIDINote>>()));
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_set_from_str_valid_with_duplicate() {
        let observed = "C:4,C:4,D:5".parse::<MIDINoteSet>();
        let expected = Ok(MIDINoteSet(vec![
            MIDINote::new(MIDINoteType::C, 4),
            MIDINote::new(MIDINoteType::D, 5),
        ].into_iter().collect::<std::collections::BTreeSet<MIDINote>>()));
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_set_from_str_invalid_note_type() {
        // NOTE: Any invalid MIDINote in the input sequence
        //       should trigger a ParseMIDINoteError, so only
        //       one is tested here (picked arbitarily).
        let input = "C:4,CL:8,D:6".to_string();
        let observed = input.as_str().parse::<MIDINoteSet>();
        let expected = Err(ParseMIDINoteSequenceError::ParseMIDINote(
            1, 
            ParseMIDINoteError::UnknownNoteType(ParseMIDINoteTypeError::UnknownNoteType { input: "CL".to_string(), }),
        ));
        assert_eq!(expected, observed);
    }

    #[test]
    fn test_midi_note_set_from_str_extra_comma() {
        // NOTE: Any invalid MIDINote in the input sequence
        //       should trigger a ParseMIDINoteError, so only
        //       one is tested here (picked arbitarily).
        let input = "C:4,C:8,D:6,".to_string();
        let observed = input.as_str().parse::<MIDINoteSet>();
        let expected = Err(ParseMIDINoteSequenceError::ParseMIDINote(
            3, 
            ParseMIDINoteError::InvalidNoteFormat { input: "".to_string() },
        ));
        assert_eq!(expected, observed);
    }
}