Struct rust_music::Note

source ·
pub struct Note { /* private fields */ }
Expand description

Represents a music note, with a pitch, a rhythm, and a dynamic (volume)

Implementations§

source§

impl Note

source

pub fn new(pitch: u7, rhythm: f64, dynamic: u7) -> Result<Note>

Returns a Note with the given rhythm, pitch, and dynamic

Arguments
  • pitch - The pitch of the note (between 0 and 127)
  • rhythm - The rhythm value of the note
  • dynamic - The dynamic (volume) of the note
Errors
  • Error::Note(Invalid::Rhythm) if rhythm is below 0.000_001
Examples found in repository?
examples/readme_example.rs (lines 14-18)
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
fn main() -> Result<(), Box<dyn Error>> {
    // Create a musical phrase that plays C-E-G (arpeggiated C Major chord)
    // with crotchets, at MezzoForte volume
    let mut phrase_to_repeat = Phrase::new();
    phrase_to_repeat.add_note(Note::new(
        compute_pitch(NoteName::C, Accidental::Natural, 4)?,
        CROTCHET,
        MF,
    )?);
    phrase_to_repeat.add_note(Note::new(
        compute_pitch(NoteName::E, Accidental::Natural, 4)?,
        CROTCHET,
        MF,
    )?);
    phrase_to_repeat.add_note(Note::new(
        compute_pitch(NoteName::G, Accidental::Natural, 4)?,
        CROTCHET,
        MF,
    )?);

    // Create a piano part that plays the phrase from beat 0
    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
    piano_part.add_phrase(phrase_to_repeat.clone(), 0.);

    // Create a Strings part that plays the phrase from beat 0.5
    // (at the same time as the piano but shifted half a beat)
    let mut violins_part = Part::new(Instrument::StringEnsemble1);
    violins_part.add_phrase(phrase_to_repeat, 0.5);

    // Create a score with a tempo of 60 (one beat per second) and add both parts
    let mut score = Score::new("my score", Tempo::new(60)?, None);
    score.add_part(piano_part);
    score.add_part(violins_part);

    // Write the score to a MIDI file for playback
    score.write_midi_file(File::create("readme_example.mid")?)?;
    Ok(())
}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (line 53)
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
fn right_hand() -> Result<Phrase> {
    let mut right_hand = Phrase::new();
    let mut add_bar = |pitch1: u7, pitch2: u7, pitch3: u7| -> Result<()> {
        for _ in 0..=1 {
            right_hand.add_rest(QUAVER);
            for _ in 0..=1 {
                right_hand.add_note(Note::new(pitch1, SEMIQUAVER, MF)?);
                right_hand.add_note(Note::new(pitch2, SEMIQUAVER, MF)?);
                right_hand.add_note(Note::new(pitch3, SEMIQUAVER, MF)?);
            }
        }
        Ok(())
    };
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
        compute_pitch(NN::A, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::F, Acc::Sharp, 4)?,
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::G, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    Ok(right_hand)
}

fn left_hand_low_note() -> Result<Phrase> {
    let mut lhln = Phrase::new();
    let mut add_bar = |pitch: u7| -> Result<()> {
        for _ in 0..=1 {
            lhln.add_note(Note::new(pitch, MINIM, MF)?);
        }
        Ok(())
    };
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::A, Acc::Natural, 3)?)?;

    Ok(lhln)
}

fn left_hand_high_note() -> Result<Phrase> {
    let mut lhhn = Phrase::new();
    let mut add_bar = |pitch: u7| -> Result<()> {
        for _ in 0..=1 {
            lhhn.add_rest(SEMIQUAVER);
            lhhn.add_note(Note::new(pitch, DOTTED_QUAVER + CROTCHET, MF)?);
        }
        Ok(())
    };
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;

    Ok(lhhn)
}
examples/praeludium_no1_organ_piano.rs (line 51)
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
fn right_hand() -> Result<Phrase> {
    let mut right_hand = Phrase::new();
    let mut add_bar = |pitch1: u7, pitch2: u7, pitch3: u7| -> Result<()> {
        for _ in 0..=1 {
            right_hand.add_rest(QUAVER);
            for _ in 0..=1 {
                right_hand.add_note(Note::new(pitch1, SEMIQUAVER, MF)?);
                right_hand.add_note(Note::new(pitch2, SEMIQUAVER, MF)?);
                right_hand.add_note(Note::new(pitch3, SEMIQUAVER, MF)?);
            }
        }
        Ok(())
    };
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
        compute_pitch(NN::A, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::F, Acc::Sharp, 4)?,
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::G, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    Ok(right_hand)
}

fn left_hand_low_note() -> Result<Phrase> {
    let mut lhln = Phrase::new();
    let mut add_bar = |pitch: u7| -> Result<()> {
        for _ in 0..=1 {
            lhln.add_note(Note::new(pitch, MINIM, MF)?);
        }
        Ok(())
    };
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::B, Acc::Natural, 3)?)?;
    add_bar(compute_pitch(NN::A, Acc::Natural, 3)?)?;

    Ok(lhln)
}

fn left_hand_high_note() -> Result<Phrase> {
    let mut lhhn = Phrase::new();
    let mut add_bar = |pitch: u7| -> Result<()> {
        for _ in 0..=1 {
            lhhn.add_rest(SEMIQUAVER);
            lhhn.add_note(Note::new(pitch, DOTTED_QUAVER + CROTCHET, MF)?);
        }
        Ok(())
    };
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::E, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::D, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;
    add_bar(compute_pitch(NN::C, Acc::Natural, 4)?)?;

    Ok(lhhn)
}
examples/praeludium_no1_single_phrase.rs (line 42)
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
fn phrase() -> Result<Phrase> {
    let mut phrase = Phrase::new();
    let mut add_bar = |pitch1: u7, pitch2: u7, pitch3: u7, pitch4: u7, pitch5: u7| -> Result<()> {
        for _ in 0..=1 {
            // Using a single note Chord to define a note that lasts while the next notes play.
            // The next note/chord starts at the end of the Chord's rhythm value, but the notes in the
            // chord can have a longer duration.
            phrase.add_chord(Chord::new(SEMIQUAVER, vec![Note::new(pitch1, MINIM, MF)?])?);
            phrase.add_chord(Chord::new(
                SEMIQUAVER,
                vec![Note::new(pitch2, DOTTED_QUAVER + CROTCHET, MF)?],
            )?);
            for _ in 0..=1 {
                phrase.add_note(Note::new(pitch3, SEMIQUAVER, MF)?);
                phrase.add_note(Note::new(pitch4, SEMIQUAVER, MF)?);
                phrase.add_note(Note::new(pitch5, SEMIQUAVER, MF)?);
            }
        }
        Ok(())
    };
    add_bar(
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 4)?,
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::B, Acc::Natural, 3)?,
        compute_pitch(NN::D, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::F, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 5)?,
        compute_pitch(NN::A, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 4)?,
        compute_pitch(NN::F, Acc::Sharp, 4)?,
        compute_pitch(NN::A, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::B, Acc::Natural, 3)?,
        compute_pitch(NN::D, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::D, Acc::Natural, 5)?,
        compute_pitch(NN::G, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::B, Acc::Natural, 3)?,
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    add_bar(
        compute_pitch(NN::A, Acc::Natural, 3)?,
        compute_pitch(NN::C, Acc::Natural, 4)?,
        compute_pitch(NN::E, Acc::Natural, 4)?,
        compute_pitch(NN::G, Acc::Natural, 4)?,
        compute_pitch(NN::C, Acc::Natural, 5)?,
    )?;
    Ok(phrase)
}
source

pub fn pitch(&self) -> u7

Returns the pitch of the note

source

pub fn rhythm(&self) -> f64

Returns the rhythm value of the note

source

pub fn dynamic(&self) -> u7

Returns the dynamic value of the note

Trait Implementations§

source§

impl Clone for Note

source§

fn clone(&self) -> Note

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Note

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<Note> for Note

source§

fn eq(&self, other: &Note) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Note

Auto Trait Implementations§

§

impl RefUnwindSafe for Note

§

impl Send for Note

§

impl Sync for Note

§

impl Unpin for Note

§

impl UnwindSafe for Note

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.