scales_example/
scales_example.rs

1use std::error::Error;
2use std::fs::File;
3use std::result::Result;
4
5use rust_music::{
6    composition::Scale, composition::ScaleMode, compute_pitch, dynamic::*, rhythm::*, Accidental,
7    Instrument, Note, NoteName, Part, Phrase, Score, Tempo,
8};
9
10// This example requires the `composition` feature.
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}