pub fn to_midi(stream: &Stream, options: &ExportOptions) -> Result<Vec<u8>>Expand description
Writes a stream as a standard MIDI file, as music21’s
streamToMidiFile(...).writestr() writes it.
A score is a track for each part after a conductor track holding every
tempo, meter and key signature, the first of each at any one time; any
other stream is one part. The conductor says a tempo of 120 and a meter
of 4/4 where the score says neither. Each part names itself after its
first instrument, takes a channel for each instrument’s program, and
plays each note at the velocity its volume comes to under the dynamic
in force where it stands and its articulations; tied notes sound as one.
A pitch between the keys is played on a channel of its own, bent from
the key below. Unpitched strokes sound on the tenth channel, as the drum
their instrument’s percussion map names. Chord symbols sound as the
chords they are, for as long as they last.
use music21_rs::midi::{from_midi, to_midi, ExportOptions};
use music21_rs::{Duration, Note, Stream, StreamKind};
let mut part = Stream::with_kind(StreamKind::Part);
part.push(Note::from_name("C4")?.with_duration(Duration::half()));
part.push(Note::from_name("E4")?.with_duration(Duration::half()));
let bytes = to_midi(&part, &ExportOptions::default())?;
assert_eq!(&bytes[..4], b"MThd");
assert_eq!(from_midi(&bytes)?.notes().len(), 2);Repeats are played out first, as Stream::expand_repeats plays them.
§Errors
Repeats music21 cannot make sense of; more pitches between the keys sounding at once than there are channels to bend; and a run of tied notes after the first lasting no time.