Struct rust_music::score::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)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn praeludium() -> Result<Score> {
    let mut part = Part::new(Instrument::AcousticGrandPiano);

    part.add_phrase(phrase()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(part);
    Ok(score)
}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (lines 33-42)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fn praeludium() -> Result<Score> {
    let mut part: Part = Part::new(Instrument::AcousticGrandPiano);

    part.add_phrase(right_hand()?, 0.);
    part.add_phrase(left_hand_high_note()?, 0.);
    part.add_phrase(left_hand_low_note()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(part);
    Ok(score)
}
examples/praeludium_no1_organ_piano.rs (lines 30-39)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fn praeludium() -> Result<Score> {
    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
    let mut organ_part = Part::new(Instrument::ChurchOrgan);

    piano_part.add_phrase(right_hand()?, 0.);
    organ_part.add_phrase(left_hand_high_note()?, 0.);
    organ_part.add_phrase(left_hand_low_note()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(piano_part);
    score.add_part(organ_part);
    Ok(score)
}
examples/readme_example.rs (line 40)
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(())
}
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)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn praeludium() -> Result<Score> {
    let mut part = Part::new(Instrument::AcousticGrandPiano);

    part.add_phrase(phrase()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(part);
    Ok(score)
}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (line 43)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fn praeludium() -> Result<Score> {
    let mut part: Part = Part::new(Instrument::AcousticGrandPiano);

    part.add_phrase(right_hand()?, 0.);
    part.add_phrase(left_hand_high_note()?, 0.);
    part.add_phrase(left_hand_low_note()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(part);
    Ok(score)
}
examples/praeludium_no1_organ_piano.rs (line 40)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fn praeludium() -> Result<Score> {
    let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
    let mut organ_part = Part::new(Instrument::ChurchOrgan);

    piano_part.add_phrase(right_hand()?, 0.);
    organ_part.add_phrase(left_hand_high_note()?, 0.);
    organ_part.add_phrase(left_hand_low_note()?, 0.);

    let mut score = Score::new(
        "Praeludium No 1 in C Major",
        Tempo::new(96)?,
        Some(Metadata {
            key_signature: NN::C as i8,
            mode: Mode::Major,
            time_numerator: 4,
            time_denominator: 4,
        }),
    );
    score.add_part(piano_part);
    score.add_part(organ_part);
    Ok(score)
}
examples/readme_example.rs (line 41)
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(())
}
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)
8
9
10
11
12
fn main() {
    let score = praeludium().unwrap();
    let out_file = File::create("praeludium_piano_organ.mid").unwrap();
    score.write_midi_file(out_file).unwrap()
}
More examples
Hide additional examples
examples/praeludium_no1_multi_phrase.rs (line 16)
13
14
15
16
17
fn main() {
    let score = praeludium().unwrap();
    let out_file = File::create("praeludium_multi_phrase.mid").unwrap();
    score.write_midi_file(out_file).unwrap()
}
examples/praeludium_no1_single_phrase.rs (line 11)
8
9
10
11
12
fn main() {
    let score = praeludium().unwrap();
    let out_file = File::create("praeludium_single_phrase.mid").unwrap();
    score.write_midi_file(out_file).unwrap()
}
examples/readme_example.rs (line 45)
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(())
}
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<'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

§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

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 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, 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.