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
#![allow(dead_code)]
#![allow(non_upper_case_globals)]
use std::{ops::{Add, AddAssign}, cmp::Ordering};
use paste::paste;
use pest::Parser;
use crate::{named_pitch::{NamedPitch, HasNamedPitch}, interval::{Interval, HasEnharmonicDistance}, base::{HasStaticName, HasName, Parsable, Res}, chord::Chord, pitch::{HasFrequency, HasBaseFrequency, Pitch, HasPitch}, octave::{Octave, HasOctave}, parser::{ChordParser, Rule, note_str_to_note, octave_str_to_octave}};
macro_rules! define_note {
( $name:ident, $named_pitch:expr, $octave_num:ident, $octave:expr) => {
paste! {
pub const [<$name$octave_num>]: Note = Note {
named_pitch: $named_pitch,
octave: $octave,
};
}
};
}
macro_rules! define_octave {
($octave_num:ident, $octave:expr) => {
define_note!(FTripleFlat, NamedPitch::FTripleFlat, $octave_num, $octave);
define_note!(CTripleFlat, NamedPitch::CTripleFlat, $octave_num, $octave);
define_note!(GTripleFlat, NamedPitch::GTripleFlat, $octave_num, $octave);
define_note!(DTripleFlat, NamedPitch::DTripleFlat, $octave_num, $octave);
define_note!(ATripleFlat, NamedPitch::ATripleFlat, $octave_num, $octave);
define_note!(ETripleFlat, NamedPitch::ETripleFlat, $octave_num, $octave);
define_note!(BTripleFlat, NamedPitch::BTripleFlat, $octave_num, $octave);
define_note!(FDoubleFlat, NamedPitch::FDoubleFlat, $octave_num, $octave);
define_note!(CDoubleFlat, NamedPitch::CDoubleFlat, $octave_num, $octave);
define_note!(GDoubleFlat, NamedPitch::GDoubleFlat, $octave_num, $octave);
define_note!(DDoubleFlat, NamedPitch::DDoubleFlat, $octave_num, $octave);
define_note!(ADoubleFlat, NamedPitch::ADoubleFlat, $octave_num, $octave);
define_note!(EDoubleFlat, NamedPitch::EDoubleFlat, $octave_num, $octave);
define_note!(BDoubleFlat, NamedPitch::BDoubleFlat, $octave_num, $octave);
define_note!(FFlat, NamedPitch::FFlat, $octave_num, $octave);
define_note!(CFlat, NamedPitch::CFlat, $octave_num, $octave);
define_note!(GFlat, NamedPitch::GFlat, $octave_num, $octave);
define_note!(DFlat, NamedPitch::DFlat, $octave_num, $octave);
define_note!(AFlat, NamedPitch::AFlat, $octave_num, $octave);
define_note!(EFlat, NamedPitch::EFlat, $octave_num, $octave);
define_note!(BFlat, NamedPitch::BFlat, $octave_num, $octave);
define_note!(F, NamedPitch::F, $octave_num, $octave);
define_note!(C, NamedPitch::C, $octave_num, $octave);
define_note!(G, NamedPitch::G, $octave_num, $octave);
define_note!(D, NamedPitch::D, $octave_num, $octave);
define_note!(A, NamedPitch::A, $octave_num, $octave);
define_note!(E, NamedPitch::E, $octave_num, $octave);
define_note!(B, NamedPitch::B, $octave_num, $octave);
define_note!(FSharp, NamedPitch::FSharp, $octave_num, $octave);
define_note!(CSharp, NamedPitch::CSharp, $octave_num, $octave);
define_note!(GSharp, NamedPitch::GSharp, $octave_num, $octave);
define_note!(DSharp, NamedPitch::DSharp, $octave_num, $octave);
define_note!(ASharp, NamedPitch::ASharp, $octave_num, $octave);
define_note!(ESharp, NamedPitch::ESharp, $octave_num, $octave);
define_note!(BSharp, NamedPitch::BSharp, $octave_num, $octave);
define_note!(FDoubleSharp, NamedPitch::FDoubleSharp, $octave_num, $octave);
define_note!(CDoubleSharp, NamedPitch::CDoubleSharp, $octave_num, $octave);
define_note!(GDoubleSharp, NamedPitch::GDoubleSharp, $octave_num, $octave);
define_note!(DDoubleSharp, NamedPitch::DDoubleSharp, $octave_num, $octave);
define_note!(ADoubleSharp, NamedPitch::ADoubleSharp, $octave_num, $octave);
define_note!(EDoubleSharp, NamedPitch::EDoubleSharp, $octave_num, $octave);
define_note!(BDoubleSharp, NamedPitch::BDoubleSharp, $octave_num, $octave);
define_note!(FTripleSharp, NamedPitch::FTripleSharp, $octave_num, $octave);
define_note!(CTripleSharp, NamedPitch::CTripleSharp, $octave_num, $octave);
define_note!(GTripleSharp, NamedPitch::GTripleSharp, $octave_num, $octave);
define_note!(DTripleSharp, NamedPitch::DTripleSharp, $octave_num, $octave);
define_note!(ATripleSharp, NamedPitch::ATripleSharp, $octave_num, $octave);
define_note!(ETripleSharp, NamedPitch::ETripleSharp, $octave_num, $octave);
define_note!(BTripleSharp, NamedPitch::BTripleSharp, $octave_num, $octave);
};
}
pub trait IntoChord {
fn into_chord(self) -> Chord;
}
pub trait NoteRecreator {
fn with_named_pitch(self, named_pitch: NamedPitch) -> Self;
fn with_octave(self, octave: Octave) -> Self;
}
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct Note {
octave: Octave,
named_pitch: NamedPitch,
}
impl Note {
pub fn new(pitch: NamedPitch, octave: Octave) -> Self {
Self { named_pitch: pitch, octave }
}
}
impl HasPitch for Note {
fn pitch(&self) -> Pitch {
self.named_pitch.pitch()
}
}
impl HasNamedPitch for Note {
fn named_pitch(&self) -> NamedPitch {
self.named_pitch
}
}
impl HasOctave for Note {
fn octave(&self) -> Octave {
self.octave
}
}
impl HasStaticName for Note {
fn static_name(&self) -> &'static str {
self.named_pitch.static_name()
}
}
impl HasName for Note {
fn name(&self) -> String {
format!("{}{}", self.named_pitch.static_name(), self.octave.static_name())
}
}
impl HasFrequency for Note
{
fn frequency(&self) -> f32 {
let mut octave = self.octave();
let base_frequency = self.pitch().base_frequency();
match self.named_pitch {
NamedPitch::ATripleSharp | NamedPitch::BTripleSharp | NamedPitch::BDoubleSharp | NamedPitch::BSharp => {
octave += 1;
},
NamedPitch::DTripleFlat | NamedPitch::CTripleFlat | NamedPitch::CDoubleFlat | NamedPitch::CFlat => {
octave -= 1;
},
_ => {}
}
base_frequency * 2.0_f32.powf(octave as u8 as f32)
}
}
impl IntoChord for Note {
fn into_chord(self) -> Chord {
Chord::new(self)
}
}
impl Parsable for Note {
fn parse(input: &str) -> Res<Self> where Self: Sized {
let root = ChordParser::parse(Rule::note_with_octave, input)?.next().unwrap();
assert_eq!(Rule::note_with_octave, root.as_rule());
let mut components = root.into_inner();
let note = components.next().unwrap();
assert_eq!(Rule::note, note.as_rule());
let mut result = note_str_to_note(note.as_str())?;
if let Some(octave) = components.next() {
assert_eq!(Rule::octave, octave.as_rule());
let octave = octave_str_to_octave(octave.as_str())?;
result = result.with_octave(octave);
}
Ok(result)
}
}
impl NoteRecreator for Note {
fn with_named_pitch(self, named_pitch: NamedPitch) -> Self {
Self::new(named_pitch, self.octave)
}
fn with_octave(self, octave: Octave) -> Self {
Self::new(self.named_pitch, octave)
}
}
impl Add<Interval> for Note {
type Output = Self;
fn add(self, rhs: Interval) -> Self::Output {
let new_pitch = self.named_pitch() + rhs.enharmonic_distance();
let wrapping_octave = if new_pitch.pitch() < self.pitch() {
Octave::One
} else {
Octave::Zero
};
let special_octave = if new_pitch == NamedPitch::CFlat || new_pitch == NamedPitch::DTripleFlat {
1
} else if new_pitch == NamedPitch::BSharp || new_pitch == NamedPitch::BDoubleSharp || new_pitch == NamedPitch::BTripleSharp || new_pitch == NamedPitch::ATripleSharp {
-1
} else {
0
};
let interval_octave = rhs.octave();
Note {
octave: self.octave + wrapping_octave + special_octave + interval_octave,
named_pitch: new_pitch,
}
}
}
impl AddAssign<Interval> for Note {
fn add_assign(&mut self, rhs: Interval) {
*self = *self + rhs;
}
}
impl PartialOrd for Note {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.octave.cmp(&other.octave).then(self.pitch().cmp(&other.pitch())))
}
}
impl Ord for Note {
fn cmp(&self, other: &Self) -> Ordering {
self.octave.cmp(&other.octave).then(self.pitch().cmp(&other.pitch()))
}
}
define_octave!(Zero, Octave::Zero);
define_octave!(One, Octave::One);
define_octave!(Two, Octave::Two);
define_octave!(Three, Octave::Three);
define_octave!(Four, Octave::Four);
define_octave!(Five, Octave::Five);
define_octave!(Six, Octave::Six);
define_octave!(Seven, Octave::Seven);
define_octave!(Eight, Octave::Eight);
define_octave!(Nine, Octave::Nine);
define_octave!(Ten, Octave::Ten);
pub const FTripleFlat: Note = FTripleFlatFour;
pub const CTripleFlat: Note = CTripleFlatFour;
pub const GTripleFlat: Note = GTripleFlatFour;
pub const DTripleFlat: Note = DTripleFlatFour;
pub const ATripleFlat: Note = ATripleFlatFour;
pub const ETripleFlat: Note = ETripleFlatFour;
pub const BTripleFlat: Note = BTripleFlatFour;
pub const FDoubleFlat: Note = FDoubleFlatFour;
pub const CDoubleFlat: Note = CDoubleFlatFour;
pub const GDoubleFlat: Note = GDoubleFlatFour;
pub const DDoubleFlat: Note = DDoubleFlatFour;
pub const ADoubleFlat: Note = ADoubleFlatFour;
pub const EDoubleFlat: Note = EDoubleFlatFour;
pub const BDoubleFlat: Note = BDoubleFlatFour;
pub const FFlat: Note = FFlatFour;
pub const CFlat: Note = CFlatFour;
pub const GFlat: Note = GFlatFour;
pub const DFlat: Note = DFlatFour;
pub const AFlat: Note = AFlatFour;
pub const EFlat: Note = EFlatFour;
pub const BFlat: Note = BFlatFour;
pub const F: Note = FFour;
pub const C: Note = CFour;
pub const G: Note = GFour;
pub const D: Note = DFour;
pub const A: Note = AFour;
pub const E: Note = EFour;
pub const B: Note = BFour;
pub const FSharp: Note = FSharpFour;
pub const CSharp: Note = CSharpFour;
pub const GSharp: Note = GSharpFour;
pub const DSharp: Note = DSharpFour;
pub const ASharp: Note = ASharpFour;
pub const ESharp: Note = ESharpFour;
pub const BSharp: Note = BSharpFour;
pub const FDoubleSharp: Note = FDoubleSharpFour;
pub const CDoubleSharp: Note = CDoubleSharpFour;
pub const GDoubleSharp: Note = GDoubleSharpFour;
pub const DDoubleSharp: Note = DDoubleSharpFour;
pub const ADoubleSharp: Note = ADoubleSharpFour;
pub const EDoubleSharp: Note = EDoubleSharpFour;
pub const BDoubleSharp: Note = BDoubleSharpFour;
pub const FTripleSharp: Note = FTripleSharpFour;
pub const CTripleSharp: Note = CTripleSharpFour;
pub const GTripleSharp: Note = GTripleSharpFour;
pub const DTripleSharp: Note = DTripleSharpFour;
pub const ATripleSharp: Note = ATripleSharpFour;
pub const ETripleSharp: Note = ETripleSharpFour;
pub const BTripleSharp: Note = BTripleSharpFour;
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::{assert_eq};
#[test]
fn test_name() {
assert_eq!(CFlat.static_name(), "C♭");
}
#[test]
fn test_intervals() {
assert_eq!(C + Interval::PerfectUnison, C);
assert_eq!(C + Interval::DiminishedSecond, DDoubleFlat);
assert_eq!(C + Interval::AugmentedUnison, CSharp);
assert_eq!(C + Interval::MinorSecond, DFlat);
assert_eq!(C + Interval::MajorSecond, D);
assert_eq!(C + Interval::DiminishedThird, EDoubleFlat);
assert_eq!(C + Interval::AugmentedSecond, DSharp);
assert_eq!(C + Interval::MinorThird, EFlat);
assert_eq!(C + Interval::MajorThird, E);
assert_eq!(C + Interval::DiminishedFourth, FFlat);
assert_eq!(C + Interval::AugmentedThird, ESharp);
assert_eq!(C + Interval::PerfectFourth, F);
assert_eq!(C + Interval::AugmentedFourth, FSharp);
assert_eq!(C + Interval::DiminishedFifth, GFlat);
assert_eq!(C + Interval::PerfectFifth, G);
assert_eq!(C + Interval::DiminishedSixth, ADoubleFlat);
assert_eq!(C + Interval::AugmentedFifth, GSharp);
assert_eq!(C + Interval::MinorSixth, AFlat);
assert_eq!(C + Interval::MajorSixth, A);
assert_eq!(C + Interval::DiminishedSeventh, BDoubleFlat);
assert_eq!(C + Interval::AugmentedSixth, ASharp);
assert_eq!(C + Interval::MinorSeventh, BFlat);
assert_eq!(C + Interval::MajorSeventh, B);
assert_eq!(C + Interval::DiminishedOctave, CFlatFive);
assert_eq!(C + Interval::AugmentedSeventh, BSharp);
assert_eq!(C + Interval::PerfectOctave, CFive);
assert_eq!(C + Interval::PerfectOctave + Interval::PerfectFifth, GFive);
assert_eq!(C + Interval::MinorNinth, DFlatFive);
assert_eq!(C + Interval::MajorNinth, DFive);
assert_eq!(C + Interval::AugmentedNinth, DSharpFive);
assert_eq!(C + Interval::DiminishedEleventh, FFlatFive);
assert_eq!(C + Interval::PerfectEleventh, FFive);
assert_eq!(C + Interval::AugmentedEleventh, FSharpFive);
assert_eq!(C + Interval::MinorThirteenth, AFlatFive);
assert_eq!(C + Interval::MajorThirteenth, AFive);
assert_eq!(C + Interval::AugmentedThirteenth, ASharpFive);
assert_eq!(C + Interval::DiminishedOctave, CFlatFive);
assert_eq!(BFlat + Interval::MinorNinth, CFlatSix);
assert_eq!(BFlatThree + Interval::MinorNinth, CFlatFive);
assert_eq!(A + Interval::AugmentedNinth, BSharpFive);
assert_eq!(CSharp + Interval::AugmentedSeventh, BDoubleSharp);
}
#[test]
fn test_parse() {
assert_eq!(Note::parse("C").unwrap(), C);
assert_eq!(Note::parse("C#").unwrap(), CSharp);
assert_eq!(Note::parse("Bb3").unwrap(), BFlatThree);
assert_eq!(Note::parse("D#7").unwrap(), DSharpSeven);
}
#[test]
#[should_panic]
fn test_parse_panic() {
assert_eq!(Note::parse("C11").unwrap(), C);
}
#[test]
fn test_pitch() {
assert_eq!(Note::new(NamedPitch::C, Octave::Four).frequency(), (CThree + Interval::PerfectOctave).frequency());
assert_eq!(CFlatFour.frequency(), BThree.frequency());
assert_eq!(BSharp.frequency(), CFive.frequency());
assert_eq!(DTripleFlatFive.frequency(), B.frequency());
}
}