Struct rust_music::Part

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

Describes a score’s part. A Part is played by a single instrument and can contain multiple phrases, played sequentially or simultaneously

Implementations§

source§

impl Part

source

pub fn new(instrument: Instrument) -> Part

Returns a new empty Part with the given name and instrument

Arguments
  • name - title of the Part
  • instrument - instrument playing the Part
Examples found in repository?
examples/praeludium_no1_single_phrase.rs (line 17)
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 27)
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 23)
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 31)
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_name<S: ToString>(&mut self, name: S)

Sets a name for the Part. The name does not have to be unique.

source

pub fn add_phrase(&mut self, phrase: Phrase, start_beat: f64)

Inserts a Phrase in the Part. The phrase will start at beat start_beat. Each beat corresponds to 1.0 in rhythm value. The Phrase can be played in parallel with other phrases if start_beat is smaller then their length.

Examples found in repository?
examples/praeludium_no1_single_phrase.rs (line 19)
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 29)
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 26)
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 32)
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 append_phrase_to_previous(&mut self, phrase: Phrase)

Appends a Phrase immediately at the end of the last added Phrase. If phrases added before the last one were longer, they can be played in parallel with the new Phrase.

source

pub fn append_phrase_to_part_end(&mut self, phrase: Phrase)

Appends a Phrase immediately at the end of the entire Part, i.e. the end of the Phrase that ends the latest

source

pub fn name(&self) -> &str

Returns the title of the Part

source

pub fn phrases(&self) -> &[(f64, Phrase)]

Returns the map of phrases of the Part

source

pub fn instrument(&self) -> Instrument

Returns the instrument playing the Part

source

pub fn duration(&self) -> f64

Trait Implementations§

source§

impl Clone for Part

source§

fn clone(&self) -> Part

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

Auto Trait Implementations§

§

impl RefUnwindSafe for Part

§

impl Send for Part

§

impl Sync for Part

§

impl Unpin for Part

§

impl UnwindSafe for Part

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.