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
use std::{
    convert::TryFrom,
    ops::{Add, AddAssign, BitAnd, BitOr, Sub},
};

use num_rational::Ratio;

use super::instruments::StandartMidiInstrument;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// 0..8 on piano
pub struct Octave(i8);

impl Octave {
    pub const OCTO_CONTRA: Self = Self(-1);
    pub const SUB_CONTRA: Self = Self(0);
    pub const CONTRA: Self = Self(1);
    pub const GREAT: Self = Self(2);
    pub const SMALL: Self = Self(3);
    pub const ONE_LINED: Self = Self(4);
    pub const TWO_LINED: Self = Self(5);
    pub const THREE_LINED: Self = Self(6);
    pub const FOUR_LINED: Self = Self(7);
    pub const FIVE_LINED: Self = Self(8);
    pub const SIX_LINED: Self = Self(9);
    pub const SEVEN_LINED: Self = Self(10);
}

impl From<i8> for Octave {
    fn from(val: i8) -> Self {
        Self(val)
    }
}

#[derive(Debug, Clone, Copy, Default, Ord, PartialOrd, Eq, PartialEq)]
pub struct Interval(i8);

impl Interval {
    pub const fn zero() -> Self {
        Self(0)
    }

    pub const fn semi_tone() -> Self {
        Self(1)
    }

    pub const fn tone() -> Self {
        Self(2)
    }
}

impl From<i8> for Interval {
    fn from(val: i8) -> Self {
        Self(val)
    }
}

impl Add for Interval {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl AddAssign for Interval {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

#[rustfmt::skip]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PitchClass {
    // https://en.wikipedia.org/wiki/Enharmonic
    Bs , C , Dff,
    Bss, Cs, Df,
    Css, D , Eff,
    Ds , Ef, Fff,
    Dss, E , Ff,
    Es , F , Gff,
    Ess, Fs, Gf,
    Fss, G , Aff,
    Gs , Af,
    Gss, A , Bff,
    As , Bf, Cff,
    Ass, B , Cf,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AbsPitch(i8);

impl AbsPitch {
    pub const fn get_inner(self) -> i8 {
        self.0
    }
}

impl From<i8> for AbsPitch {
    fn from(x: i8) -> Self {
        Self(x)
    }
}

impl From<AbsPitch> for Interval {
    fn from(abs_pitch: AbsPitch) -> Self {
        Self(abs_pitch.0)
    }
}

impl From<AbsPitch> for (Octave, u8) {
    fn from(abs_pitch: AbsPitch) -> Self {
        let octave_size = Octave::semitones_number().0;
        let (octave, n) = (abs_pitch.0 / octave_size, abs_pitch.0 % octave_size);

        let (octave, n) = if n < 0 {
            (octave - 1, (n + octave_size))
        } else {
            (octave, n)
        };

        (
            Octave(octave),
            u8::try_from(n).expect("Negative interval found"),
        )
    }
}

impl From<Octave> for AbsPitch {
    fn from(octave: Octave) -> Self {
        let octave_size = Octave::semitones_number().0;
        Self(octave.0 * octave_size)
    }
}

impl Add<Interval> for AbsPitch {
    type Output = Self;

    fn add(self, rhs: Interval) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl Sub for AbsPitch {
    type Output = Interval;

    fn sub(self, rhs: Self) -> Self::Output {
        Interval(self.0 - rhs.0)
    }
}

impl From<Interval> for AbsPitch {
    fn from(int: Interval) -> Self {
        Self(int.0)
    }
}

impl From<PitchClass> for Interval {
    fn from(pc: PitchClass) -> Self {
        let val = match pc {
            PitchClass::Cff => -2,
            PitchClass::Cf => -1,
            PitchClass::C => 0,
            PitchClass::Cs => 1,
            PitchClass::Css => 2,
            PitchClass::Dff => 0,
            PitchClass::Df => 1,
            PitchClass::D => 2,
            PitchClass::Ds => 3,
            PitchClass::Dss => 4,
            PitchClass::Eff => 2,
            PitchClass::Ef => 3,
            PitchClass::E => 4,
            PitchClass::Es => 5,
            PitchClass::Ess => 6,
            PitchClass::Fff => 3,
            PitchClass::Ff => 4,
            PitchClass::F => 5,
            PitchClass::Fs => 6,
            PitchClass::Fss => 7,
            PitchClass::Gff => 5,
            PitchClass::Gf => 6,
            PitchClass::G => 7,
            PitchClass::Gs => 8,
            PitchClass::Gss => 9,
            PitchClass::Aff => 7,
            PitchClass::Af => 8,
            PitchClass::A => 9,
            PitchClass::As => 10,
            PitchClass::Ass => 11,
            PitchClass::Bff => 9,
            PitchClass::Bf => 10,
            PitchClass::B => 11,
            PitchClass::Bs => 12,
            PitchClass::Bss => 13,
        };

        Self(val)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pitch {
    class: PitchClass,
    octave: Octave,
}

macro_rules! def_pitch_constructor {
    ($pitch: ident) => {
        #[allow(non_snake_case)]
        pub const fn $pitch(octave: Octave) -> Self {
            Self::new(PitchClass::$pitch, octave)
        }
    };


    ( $( $pitch: ident ),+ $(,)? ) => {
        $(
            def_pitch_constructor!($pitch);
        )+
    }
}

impl Pitch {
    pub const fn new(class: PitchClass, octave: Octave) -> Self {
        Self { class, octave }
    }

    def_pitch_constructor![Aff, Af, A, As, Ass];
    def_pitch_constructor![Bff, Bf, B, Bs, Bss];
    def_pitch_constructor![Cff, Cf, C, Cs, Css];
    def_pitch_constructor![Dff, Df, D, Ds, Dss];
    def_pitch_constructor![Eff, Ef, E, Es, Ess];
    def_pitch_constructor![Fff, Ff, F, Fs, Fss];
    def_pitch_constructor![Gff, Gf, G, Gs, Gss];

    pub fn abs(self) -> AbsPitch {
        AbsPitch::from(self.octave) + Interval::from(self.class)
    }

    pub const CONCERT_A_FREQUENCY: f64 = 440.0;

    pub fn get_frequency(self) -> f64 {
        let a4 = Self::A(Octave::ONE_LINED);
        let interval_to_a4 = self.abs() - a4.abs();
        let octaves_from_a4 = f64::from(interval_to_a4.0) / f64::from(Octave::semitones_number().0);
        octaves_from_a4.exp2() * Self::CONCERT_A_FREQUENCY
    }
}

impl Octave {
    const MINIMAL_PITCHES: [PitchClass; 12] = [
        PitchClass::C,
        PitchClass::Cs,
        PitchClass::D,
        PitchClass::Ds,
        PitchClass::E,
        PitchClass::F,
        PitchClass::Fs,
        PitchClass::G,
        PitchClass::Gs,
        PitchClass::A,
        PitchClass::As,
        PitchClass::B,
    ];

    fn semitones_number() -> Interval {
        let len = i8::try_from(Self::MINIMAL_PITCHES.len()).unwrap();
        Interval(len)
    }
}

impl Pitch {
    pub fn trans(self, delta: Interval) -> Self {
        let abs = self.abs() + delta;
        Self::from(abs)
    }

    pub fn next(self) -> Self {
        self.trans(Interval::semi_tone())
    }

    pub fn prev(self) -> Self {
        self.trans(Interval::from(-1))
    }

    pub fn get_scale<I>(self, intervals: &[I]) -> impl Iterator<Item = Self> + '_
    where
        I: Copy + Into<Interval>,
    {
        intervals
            .iter()
            .scan(Interval::zero(), |tonic_distance, &interval| {
                *tonic_distance += interval.into();
                Some(*tonic_distance)
            })
            .map(move |distance| self.trans(distance))
    }

    pub fn major_scale(self) -> impl Iterator<Item = Self> {
        self.get_scale(&[0, 2, 2, 1, 2, 2, 2, 1])
    }

    pub fn natural_minor_scale(self) -> impl Iterator<Item = Self> {
        self.get_scale(&[0, 2, 1, 2, 2, 1, 2, 2])
    }
}

impl From<AbsPitch> for Pitch {
    fn from(abs_pitch: AbsPitch) -> Self {
        let (octave, interval) = abs_pitch.into();
        let n = usize::from(interval);

        Self {
            class: Octave::MINIMAL_PITCHES[n],
            octave,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dur(u8, u8);

impl Dur {
    const fn from_integer(i: u8) -> Self {
        Self(i, 1)
    }

    const fn new(num: u8, denom: u8) -> Self {
        Self(num, denom)
    }

    pub fn into_rational(self) -> Ratio<u8> {
        Ratio::new(self.0, self.1)
    }

    pub const ZERO: Self = Self::from_integer(0);
    pub const BN: Self = Self::from_integer(2); // brevis note
    pub const WN: Self = Self::from_integer(1); // whole note
    pub const HN: Self = Self::new(1, 2); // half note
    pub const QN: Self = Self::new(1, 4); // quarter note
    pub const EN: Self = Self::new(1, 8); // eighth note
    pub const SN: Self = Self::new(1, 16); // sixteenth note
    pub const TN: Self = Self::new(1, 32); // thirty-second note
    pub const SFN: Self = Self::new(1, 64); // sixty-fourth note

    pub const DWN: Self = Self::new(3, 2); // dotted whole note
    pub const DHN: Self = Self::new(3, 4); // dotted half note
    pub const DQN: Self = Self::new(3, 8); // dotted quarter note
    pub const DEN: Self = Self::new(3, 16); // dotted eighth note
    pub const DSN: Self = Self::new(3, 32); // dotted sixteenth note
    pub const DTN: Self = Self::new(3, 64); // dotted thirty-second note

    pub const DDHN: Self = Self::new(7, 8); // double-dotted half note
    pub const DDQN: Self = Self::new(7, 16); // double-dotted quarter note
    pub const DDEN: Self = Self::new(7, 32); // double-dotted eighth note

    pub const fn double(self) -> Self {
        if self.1 & 1 == 0 {
            //&Self::WN => Self::BN,
            //&Self::HN => Self::WN,
            //&Self::QN => Self::HN,
            //&Self::EN => Self::QN,
            //&Self::SN => Self::EN,
            //&Self::TN => Self::SN,
            //&Self::SFN => Self::TN,
            //
            //&Self::DHN => Self::DWN,
            //&Self::DQN => Self::DHN,
            //&Self::DEN => Self::DQN,
            //&Self::DSN => Self::DEN,
            //&Self::DTN => Self::DSN,
            //
            //&Self::DDQN => Self::DDHN,
            //&Self::DDEN => Self::DDQN,

            Self::new(self.0, self.1 >> 1)
        } else {
            Self::new(self.0 << 1, self.1)
        }
    }

    pub const fn halve(self) -> Self {
        if self.0 & 1 == 0 {
            Self::new(self.0 >> 1, self.1)
        } else {
            Self::new(self.0, self.1 << 1)
        }
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Primitive<P> {
    Note(Dur, P),
    Rest(Dur),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PlayerName(String);

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Mode {
    Major,
    Minor,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Control {
    Tempo(Dur), // scale the tempo
    Transpose(AbsPitch),
    Instrument(StandartMidiInstrument),
    //FIXME: implement later
    //Phrase(Vec<PhraseAttribute>),
    Player(PlayerName),
    KeySig(PitchClass, Mode), // key signature and mode
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Music<P = Pitch> {
    Prim(Primitive<P>),
    Sequential(Box<Self>, Box<Self>),
    Parallel(Box<Self>, Box<Self>),
    Modify(Control, Box<Self>),
}

/// Sequential composition
impl<P> BitAnd for Music<P> {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self::Sequential(Box::new(self), Box::new(rhs))
    }
}

/// Parallel composition
impl<P> BitOr for Music<P> {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self::Parallel(Box::new(self), Box::new(rhs))
    }
}

macro_rules! def_note_constructor {
    ($pitch: ident) => {
        #[allow(non_snake_case)]
        pub const fn $pitch(octave: Octave, duration: Dur) -> Self {
            Self::note(duration, Pitch::$pitch(octave))
        }
    };

    ( $( $pitch: ident ),+ $(,)? ) => {
        $(
            def_note_constructor!($pitch);
        )+
    }
}

impl<P> Music<P> {
    pub const fn note(duration: Dur, pitch: P) -> Self {
        Self::Prim(Primitive::Note(duration, pitch))
    }

    pub const fn rest(duration: Dur) -> Self {
        Self::Prim(Primitive::Rest(duration))
    }

    pub fn tempo(duration: Dur, music: Self) -> Self {
        Self::Modify(Control::Tempo(duration), Box::new(music))
    }

    pub fn transpose(abs_pitch: AbsPitch, music: Self) -> Self {
        Self::Modify(Control::Transpose(abs_pitch), Box::new(music))
    }

    pub fn instrument(name: StandartMidiInstrument, music: Self) -> Self {
        Self::Modify(Control::Instrument(name), Box::new(music))
    }

    //fn phrase(attributes: Vec<PhraseAttribute>, music: Self) -> Self {
    //    Music::Modify(Control::Phrase(attributes), Box::new(music))
    //}

    pub fn player(name: PlayerName, music: Self) -> Self {
        Self::Modify(Control::Player(name), Box::new(music))
    }

    pub fn key_sig(pitch_class: PitchClass, mode: Mode, music: Self) -> Self {
        Self::Modify(Control::KeySig(pitch_class, mode), Box::new(music))
    }
}

impl Music {
    def_note_constructor![Aff, Af, A, As, Ass];
    def_note_constructor![Bff, Bf, B, Bs, Bss];
    def_note_constructor![Cff, Cf, C, Cs, Css];
    def_note_constructor![Dff, Df, D, Ds, Dss];
    def_note_constructor![Eff, Ef, E, Es, Ess];
    def_note_constructor![Fff, Ff, F, Fs, Fss];
    def_note_constructor![Gff, Gf, G, Gs, Gss];

    /// Exercise 2.5
    ///
    /// In contrast to the annotation of the `Music` with `Transpose`
    /// (as part of the `Control` data type, which in turn is part of the `Music`),
    /// this function actually changes each note in a `Music<Pitch>` value by
    /// transposing it by the interval specified.
    pub fn trans(self, abs_pitch: AbsPitch) -> Self {
        match self {
            Self::Prim(Primitive::Note(duration, pitch)) => {
                Self::note(duration, pitch.trans(abs_pitch.into()))
            }
            Self::Prim(Primitive::Rest(duration)) => Self::rest(duration),
            Self::Sequential(m1, m2) => {
                Self::Sequential(Box::new(m1.trans(abs_pitch)), Box::new(m2.trans(abs_pitch)))
            }
            Self::Parallel(m1, m2) => {
                Self::Parallel(Box::new(m1.trans(abs_pitch)), Box::new(m2.trans(abs_pitch)))
            }
            Self::Modify(control, m) => Self::Modify(control, Box::new(m.trans(abs_pitch))),
        }
    }

    pub fn line(musics: Vec<Self>) -> Self {
        musics
            .into_iter()
            .fold(Self::rest(Dur::ZERO), |acc, m| acc & m)
    }

    pub fn chord(musics: Vec<Self>) -> Self {
        musics
            .into_iter()
            .fold(Self::rest(Dur::ZERO), |acc, m| acc | m)
    }
}

pub mod rests {
    use super::{Dur, Music};

    macro_rules! def_rest {
        ($rest_name: ident) => {
            pub const $rest_name: Music = Music::rest(Dur::$rest_name);
        };
    }

    def_rest!(BN);
    def_rest!(WN);
    def_rest!(HN);
    def_rest!(QN);
    def_rest!(EN);
    def_rest!(SN);
    def_rest!(TN);
    def_rest!(SFN);
    def_rest!(DWN);
    def_rest!(DHN);
    def_rest!(DQN);
    def_rest!(DEN);
    def_rest!(DSN);
    def_rest!(DTN);
    def_rest!(DDHN);
    def_rest!(DDQN);
    def_rest!(DDEN);
}

#[allow(dead_code)]
const A440: Music = Music::A(Octave(4), Dur::WN);

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

    #[test]
    fn double_duration() {
        assert_eq!(Dur::WN.double(), Dur::BN);
        assert_eq!(Dur::HN.double(), Dur::WN);
        assert_eq!(Dur::QN.double(), Dur::HN);
        assert_eq!(Dur::EN.double(), Dur::QN);
        assert_eq!(Dur::SN.double(), Dur::EN);
        assert_eq!(Dur::TN.double(), Dur::SN);
        assert_eq!(Dur::SFN.double(), Dur::TN);

        assert_eq!(Dur::DHN.double(), Dur::DWN);
        assert_eq!(Dur::DQN.double(), Dur::DHN);
        assert_eq!(Dur::DEN.double(), Dur::DQN);
        assert_eq!(Dur::DSN.double(), Dur::DEN);
        assert_eq!(Dur::DTN.double(), Dur::DSN);

        assert_eq!(Dur::DDQN.double(), Dur::DDHN);
        assert_eq!(Dur::DDEN.double(), Dur::DDQN);
    }

    #[test]
    fn major() {
        let oc3 = Octave(3);
        let middle_c = Pitch::C(oc3);
        let major: Vec<_> = middle_c.major_scale().collect();

        assert_eq!(
            major,
            vec![
                Pitch::C(oc3),
                Pitch::D(oc3),
                Pitch::E(oc3),
                Pitch::F(oc3),
                Pitch::G(oc3),
                Pitch::A(oc3),
                Pitch::B(oc3),
                Pitch::C(Octave(4)),
            ]
        );
    }

    #[test]
    fn minor() {
        let oc4 = Octave(4);
        let oc5 = Octave(5);

        let concert_a = Pitch::A(oc4);
        let minor: Vec<_> = concert_a.natural_minor_scale().collect();

        assert_eq!(
            minor,
            vec![
                Pitch::A(oc4),
                Pitch::B(oc4),
                Pitch::C(oc5),
                Pitch::D(oc5),
                Pitch::E(oc5),
                Pitch::F(oc5),
                Pitch::G(oc5),
                Pitch::A(oc5),
            ]
        );
    }

    #[test]
    fn get_a440_freq() {
        let pitch = Pitch::A(Octave::ONE_LINED);
        assert!((pitch.get_frequency() - 440.0).abs() < f64::EPSILON);
    }

    fn assert_is_close_freq(f1: f64, f2: f64) {
        assert!(
            (f1 - f2).abs() < 0.001,
            "The {} and {} are definitely not the same frequencies",
            f1,
            f2
        );
    }

    #[test]
    fn get_middle_c_freq() {
        let pitch = Pitch::C(Octave::ONE_LINED);
        assert_is_close_freq(pitch.get_frequency(), 261.626);
    }

    #[test]
    fn get_1_herz_freq() {
        let pitch = Pitch::C(Octave(-4));
        assert_is_close_freq(pitch.get_frequency(), 1.022);
    }
}