Score

Struct Score 

Source
pub struct Score { /* private fields */ }
Expand description

Describes a full Score

Implementations§

Source§

impl Score

Source

pub fn new<S: ToString>( name: S, tempo: Tempo, metadata: Option<Metadata>, ) -> Score

Returns a new empty Score from the given arguments

§Arguments
  • name - Title of the Score
  • tempo - Tempo of the Score
  • Metadata - Optional information
Examples found in repository?
examples/praeludium_no1_single_phrase.rs (lines 21-30)
16fn praeludium() -> Result<Score> {
17    let mut part = Part::new(Instrument::AcousticGrandPiano);
18
19    part.add_phrase(phrase()?, 0.);
20
21    let mut score = Score::new(
22        "Praeludium No 1 in C Major",
23        Tempo::new(96)?,
24        Some(Metadata {
25            key_signature: NN::C as i8,
26            mode: Mode::Major,
27            time_numerator: 4,
28            time_denominator: 4,
29        }),
30    );
31    score.add_part(part);
32    Ok(score)
33}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (lines 33-42)
26fn praeludium() -> Result<Score> {
27    let mut part: Part = Part::new(Instrument::AcousticGrandPiano);
28
29    part.add_phrase(right_hand()?, 0.);
30    part.add_phrase(left_hand_high_note()?, 0.);
31    part.add_phrase(left_hand_low_note()?, 0.);
32
33    let mut score = Score::new(
34        "Praeludium No 1 in C Major",
35        Tempo::new(96)?,
36        Some(Metadata {
37            key_signature: NN::C as i8,
38            mode: Mode::Major,
39            time_numerator: 4,
40            time_denominator: 4,
41        }),
42    );
43    score.add_part(part);
44    Ok(score)
45}
examples/praeludium_no1_organ_piano.rs (lines 30-39)
22fn praeludium() -> Result<Score> {
23    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
24    let mut organ_part = Part::new(Instrument::ChurchOrgan);
25
26    piano_part.add_phrase(right_hand()?, 0.);
27    organ_part.add_phrase(left_hand_high_note()?, 0.);
28    organ_part.add_phrase(left_hand_low_note()?, 0.);
29
30    let mut score = Score::new(
31        "Praeludium No 1 in C Major",
32        Tempo::new(96)?,
33        Some(Metadata {
34            key_signature: NN::C as i8,
35            mode: Mode::Major,
36            time_numerator: 4,
37            time_denominator: 4,
38        }),
39    );
40    score.add_part(piano_part);
41    score.add_part(organ_part);
42    Ok(score)
43}
examples/scales_example.rs (line 26)
11fn main() -> Result<(), Box<dyn Error>> {
12    // Create a simple C Minor Scale on octave 4 (this requires the `composition` feature)
13    let s = Scale::new(
14        compute_pitch(NoteName::Do, Accidental::Natural, 4)?,
15        ScaleMode::Aeolian,
16    );
17
18    // Create a phrase that just plays the scale as a sequence of quavers (half beat)
19    let phrase = Phrase::from_notes_sequence(Note::new_sequence(QUAVER, MF, s.n_pitches(15)))?;
20
21    // Create a piano part that plays the phrase from beat 0
22    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
23    piano_part.add_phrase(phrase, 0.);
24
25    // Create a score with a tempo of 60 (one beat per second) and add both parts
26    let mut score = Score::new("my score", Tempo::new(60)?, None);
27    score.add_part(piano_part);
28
29    // Write the score to a MIDI file for playback
30    score.write_midi_file(File::create("scale_example.mid")?)?;
31    Ok(())
32}
examples/readme_example.rs (line 40)
10fn main() -> Result<(), Box<dyn Error>> {
11    // Create a musical phrase that plays C-E-G (arpeggiated C Major chord)
12    // with crotchets, at MezzoForte volume
13    let mut phrase_to_repeat = Phrase::new();
14    phrase_to_repeat.add_note(Note::new(
15        compute_pitch(NoteName::C, Accidental::Natural, 4)?,
16        CROTCHET,
17        MF,
18    )?);
19    phrase_to_repeat.add_note(Note::new(
20        compute_pitch(NoteName::E, Accidental::Natural, 4)?,
21        CROTCHET,
22        MF,
23    )?);
24    phrase_to_repeat.add_note(Note::new(
25        compute_pitch(NoteName::G, Accidental::Natural, 4)?,
26        CROTCHET,
27        MF,
28    )?);
29
30    // Create a piano part that plays the phrase from beat 0
31    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
32    piano_part.add_phrase(phrase_to_repeat.clone(), 0.);
33
34    // Create a Strings part that plays the phrase from beat 0.5
35    // (at the same time as the piano but shifted half a beat)
36    let mut violins_part = Part::new(Instrument::StringEnsemble1);
37    violins_part.add_phrase(phrase_to_repeat, 0.5);
38
39    // Create a score with a tempo of 60 (one beat per second) and add both parts
40    let mut score = Score::new("my score", Tempo::new(60)?, None);
41    score.add_part(piano_part);
42    score.add_part(violins_part);
43
44    // Write the score to a MIDI file for playback
45    score.write_midi_file(File::create("readme_example.mid")?)?;
46    Ok(())
47}
Source

pub fn add_part(&mut self, part: Part)

Adds a Part to the Score. Warning: q Score can contain unlimited Parts but if exporting to Standard MIDI File, any Score with more than 16 Parts will fail because MIDI only supports 16 channels.

Examples found in repository?
examples/praeludium_no1_single_phrase.rs (line 31)
16fn praeludium() -> Result<Score> {
17    let mut part = Part::new(Instrument::AcousticGrandPiano);
18
19    part.add_phrase(phrase()?, 0.);
20
21    let mut score = Score::new(
22        "Praeludium No 1 in C Major",
23        Tempo::new(96)?,
24        Some(Metadata {
25            key_signature: NN::C as i8,
26            mode: Mode::Major,
27            time_numerator: 4,
28            time_denominator: 4,
29        }),
30    );
31    score.add_part(part);
32    Ok(score)
33}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (line 43)
26fn praeludium() -> Result<Score> {
27    let mut part: Part = Part::new(Instrument::AcousticGrandPiano);
28
29    part.add_phrase(right_hand()?, 0.);
30    part.add_phrase(left_hand_high_note()?, 0.);
31    part.add_phrase(left_hand_low_note()?, 0.);
32
33    let mut score = Score::new(
34        "Praeludium No 1 in C Major",
35        Tempo::new(96)?,
36        Some(Metadata {
37            key_signature: NN::C as i8,
38            mode: Mode::Major,
39            time_numerator: 4,
40            time_denominator: 4,
41        }),
42    );
43    score.add_part(part);
44    Ok(score)
45}
examples/praeludium_no1_organ_piano.rs (line 40)
22fn praeludium() -> Result<Score> {
23    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
24    let mut organ_part = Part::new(Instrument::ChurchOrgan);
25
26    piano_part.add_phrase(right_hand()?, 0.);
27    organ_part.add_phrase(left_hand_high_note()?, 0.);
28    organ_part.add_phrase(left_hand_low_note()?, 0.);
29
30    let mut score = Score::new(
31        "Praeludium No 1 in C Major",
32        Tempo::new(96)?,
33        Some(Metadata {
34            key_signature: NN::C as i8,
35            mode: Mode::Major,
36            time_numerator: 4,
37            time_denominator: 4,
38        }),
39    );
40    score.add_part(piano_part);
41    score.add_part(organ_part);
42    Ok(score)
43}
examples/scales_example.rs (line 27)
11fn main() -> Result<(), Box<dyn Error>> {
12    // Create a simple C Minor Scale on octave 4 (this requires the `composition` feature)
13    let s = Scale::new(
14        compute_pitch(NoteName::Do, Accidental::Natural, 4)?,
15        ScaleMode::Aeolian,
16    );
17
18    // Create a phrase that just plays the scale as a sequence of quavers (half beat)
19    let phrase = Phrase::from_notes_sequence(Note::new_sequence(QUAVER, MF, s.n_pitches(15)))?;
20
21    // Create a piano part that plays the phrase from beat 0
22    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
23    piano_part.add_phrase(phrase, 0.);
24
25    // Create a score with a tempo of 60 (one beat per second) and add both parts
26    let mut score = Score::new("my score", Tempo::new(60)?, None);
27    score.add_part(piano_part);
28
29    // Write the score to a MIDI file for playback
30    score.write_midi_file(File::create("scale_example.mid")?)?;
31    Ok(())
32}
examples/readme_example.rs (line 41)
10fn main() -> Result<(), Box<dyn Error>> {
11    // Create a musical phrase that plays C-E-G (arpeggiated C Major chord)
12    // with crotchets, at MezzoForte volume
13    let mut phrase_to_repeat = Phrase::new();
14    phrase_to_repeat.add_note(Note::new(
15        compute_pitch(NoteName::C, Accidental::Natural, 4)?,
16        CROTCHET,
17        MF,
18    )?);
19    phrase_to_repeat.add_note(Note::new(
20        compute_pitch(NoteName::E, Accidental::Natural, 4)?,
21        CROTCHET,
22        MF,
23    )?);
24    phrase_to_repeat.add_note(Note::new(
25        compute_pitch(NoteName::G, Accidental::Natural, 4)?,
26        CROTCHET,
27        MF,
28    )?);
29
30    // Create a piano part that plays the phrase from beat 0
31    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
32    piano_part.add_phrase(phrase_to_repeat.clone(), 0.);
33
34    // Create a Strings part that plays the phrase from beat 0.5
35    // (at the same time as the piano but shifted half a beat)
36    let mut violins_part = Part::new(Instrument::StringEnsemble1);
37    violins_part.add_phrase(phrase_to_repeat, 0.5);
38
39    // Create a score with a tempo of 60 (one beat per second) and add both parts
40    let mut score = Score::new("my score", Tempo::new(60)?, None);
41    score.add_part(piano_part);
42    score.add_part(violins_part);
43
44    // Write the score to a MIDI file for playback
45    score.write_midi_file(File::create("readme_example.mid")?)?;
46    Ok(())
47}
Source

pub fn set_tempo(&mut self, tempo: u32) -> Result<()>

Modifies the tempo of the Score

§Errors

Returns ScoreError::InvalidTempo if tempo is 0

Source

pub fn write_midi_file<W: Write>(&self, w: W) -> Result<()>

Examples found in repository?
examples/praeludium_no1_organ_piano.rs (line 11)
8fn main() {
9    let score = praeludium().unwrap();
10    let out_file = File::create("praeludium_piano_organ.mid").unwrap();
11    score.write_midi_file(out_file).unwrap()
12}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (line 16)
13fn main() {
14    let score = praeludium().unwrap();
15    let out_file = File::create("praeludium_multi_phrase.mid").unwrap();
16    score.write_midi_file(out_file).unwrap()
17}
examples/praeludium_no1_single_phrase.rs (line 11)
8fn main() {
9    let score = praeludium().unwrap();
10    let out_file = File::create("praeludium_single_phrase.mid").unwrap();
11    score.write_midi_file(out_file).unwrap()
12}
examples/scales_example.rs (line 30)
11fn main() -> Result<(), Box<dyn Error>> {
12    // Create a simple C Minor Scale on octave 4 (this requires the `composition` feature)
13    let s = Scale::new(
14        compute_pitch(NoteName::Do, Accidental::Natural, 4)?,
15        ScaleMode::Aeolian,
16    );
17
18    // Create a phrase that just plays the scale as a sequence of quavers (half beat)
19    let phrase = Phrase::from_notes_sequence(Note::new_sequence(QUAVER, MF, s.n_pitches(15)))?;
20
21    // Create a piano part that plays the phrase from beat 0
22    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
23    piano_part.add_phrase(phrase, 0.);
24
25    // Create a score with a tempo of 60 (one beat per second) and add both parts
26    let mut score = Score::new("my score", Tempo::new(60)?, None);
27    score.add_part(piano_part);
28
29    // Write the score to a MIDI file for playback
30    score.write_midi_file(File::create("scale_example.mid")?)?;
31    Ok(())
32}
examples/readme_example.rs (line 45)
10fn main() -> Result<(), Box<dyn Error>> {
11    // Create a musical phrase that plays C-E-G (arpeggiated C Major chord)
12    // with crotchets, at MezzoForte volume
13    let mut phrase_to_repeat = Phrase::new();
14    phrase_to_repeat.add_note(Note::new(
15        compute_pitch(NoteName::C, Accidental::Natural, 4)?,
16        CROTCHET,
17        MF,
18    )?);
19    phrase_to_repeat.add_note(Note::new(
20        compute_pitch(NoteName::E, Accidental::Natural, 4)?,
21        CROTCHET,
22        MF,
23    )?);
24    phrase_to_repeat.add_note(Note::new(
25        compute_pitch(NoteName::G, Accidental::Natural, 4)?,
26        CROTCHET,
27        MF,
28    )?);
29
30    // Create a piano part that plays the phrase from beat 0
31    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
32    piano_part.add_phrase(phrase_to_repeat.clone(), 0.);
33
34    // Create a Strings part that plays the phrase from beat 0.5
35    // (at the same time as the piano but shifted half a beat)
36    let mut violins_part = Part::new(Instrument::StringEnsemble1);
37    violins_part.add_phrase(phrase_to_repeat, 0.5);
38
39    // Create a score with a tempo of 60 (one beat per second) and add both parts
40    let mut score = Score::new("my score", Tempo::new(60)?, None);
41    score.add_part(piano_part);
42    score.add_part(violins_part);
43
44    // Write the score to a MIDI file for playback
45    score.write_midi_file(File::create("readme_example.mid")?)?;
46    Ok(())
47}
Source

pub fn name(&self) -> &str

Returns the title of the Score

Source

pub fn parts(&self) -> &[Part]

Returns the Parts of the Score

Source

pub fn tempo(&self) -> u32

Returns the tempo of the Score

Source

pub fn metadata(&self) -> Option<&Metadata>

Returns the metadata

Source

pub fn duration(&self) -> f64

Trait Implementations§

Source§

impl Clone for Score

Source§

fn clone(&self) -> Score

Returns a duplicate 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 Score

Source§

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

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

impl Default for Score

Source§

fn default() -> Score

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Score

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> TryFrom<&'a Score> for Smf<'a>

Source§

fn try_from(score: &'a Score) -> Result<Smf<'a>>

Converts a Score into a Standard MIDI File (midly::Smf)

§Arguments
  • score - Score to convert
§Errors

Returns ToMidiConversionError TODO: complete errors description

Source§

type Error = Error

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

impl StructuralPartialEq for Score

Auto Trait Implementations§

§

impl Freeze for Score

§

impl RefUnwindSafe for Score

§

impl Send for Score

§

impl Sync for Score

§

impl Unpin for Score

§

impl UnwindSafe for Score

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.