Skip to content

Commit

Permalink
repo: readme: improve example with a canon-like song
Browse files Browse the repository at this point in the history
  • Loading branch information
paveyry committed Sep 5, 2023
1 parent c2c5568 commit d4f70bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
40 changes: 24 additions & 16 deletions README.md
Expand Up @@ -29,37 +29,45 @@ use std::error::Error;
use std::fs::File;

use rust_music::{
score::*,
part::Part,
phrase::Phrase,
note::*,
rhythm::CROTCHET,
dynamic::MF,
Instrument,
dynamic::MF, note::*, part::Part, phrase::Phrase, rhythm::CROTCHET, score::*, Instrument,
};

fn main() -> Result<(), Box<dyn Error>> {
// Create a musical phrase that plays C-E-G (arpeggiated C Major chord) with crotchets, at MezzoForte volume
// 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)?);
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 guitar part that plays the phrase from beat 2 (at the same time as piano plays the E)
let mut guitar_part = Part::new(Instrument::NylonGuitar);
guitar_part.add_phrase(phrase_to_repeat, 1.);
// 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(guitar_part);
score.add_part(violins_part);

// Write the score to a MIDI file for playback
score.write_midi_file(File::create("my_score.mid")?)?;
score.write_midi_file(File::create("readme_example.mid")?)?;
Ok(())
}
```
Expand Down
14 changes: 8 additions & 6 deletions examples/readme_example.rs
Expand Up @@ -6,7 +6,8 @@ use rust_music::{
};

fn main() -> Result<(), Box<dyn Error>> {
// Create a musical phrase that plays C-E-G (arpeggiated C Major chord) with crotchets, at MezzoForte volume
// 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)?,
Expand All @@ -28,16 +29,17 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut piano_part = Part::new(Instrument::AcousticGrandPiano);
piano_part.add_phrase(phrase_to_repeat.clone(), 0.);

// Create a guitar part that plays the phrase from beat 2 (at the same time as piano plays the E)
let mut guitar_part = Part::new(Instrument::NylonGuitar);
guitar_part.add_phrase(phrase_to_repeat, 1.);
// 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(guitar_part);
score.add_part(violins_part);

// Write the score to a MIDI file for playback
score.write_midi_file(File::create("my_score.mid")?)?;
score.write_midi_file(File::create("readme_example.mid")?)?;
Ok(())
}

0 comments on commit d4f70bc

Please sign in to comment.