Skip to main content

to_abc

Function to_abc 

Source
pub fn to_abc(stream: &Stream, options: &ExportOptions) -> Result<String>
Expand description

Writes a score as ABC.

A score is one tune and an opus one tune for each score it holds, under the X: number its metadata gives it. A part alone, or a stream of loose notes, is a tune of one voice. The header carries the title, composer and origin, the meter, the unit note length, the tempo and the key with its mode; a score of several parts has a V: voice for each, and a part whose measures hold voices of their own has one for each of those. A voice’s clef is said on its V: line, unless it is a treble clef the notes fit, which is what a reader takes where none is said, and a part’s name goes there too; a tune of one voice has a V: line only for those. A part opening with an instrument that has a General MIDI program says so with abc2midi’s %%MIDI program, before its music.

What ABC has no way to say is refused with Error::Abc rather than left out: a microtone, a tuplet inside a tuplet, an unpitched stroke, a chord symbol or a dynamic standing where no note starts, a pedal with its bounces and gaps, an octave line, a glissando, a tremolo between notes, a rehearsal mark or a metric modulation. A barline inside a measure is drawn and not heard, and is written as nothing.

Three things are written as music21’s reader takes them, since from_abc is that reader. An accidental is written on every note that shows one and carries no further into the bar. A tuplet’s count of notes takes in any grace notes written among them. And a field changing the meter, the key or the tempo at a barline stands on lines of its own after that barline, where two fields together are read as belonging to the measure that follows.

use music21_rs::abc::{ExportOptions, from_abc, to_abc};

let tune = "X:1\nT:Scale\nM:4/4\nL:1/4\nK:G\nGABc|defg|a4|]\n";
let score = from_abc(tune)?;
let written = to_abc(&score, &ExportOptions::default())?;
assert_eq!(
    written,
    "X:1\nT:Scale\nM:4/4\nL:1/4\nK:G\nG A B c | d e f g | a4 |]\n"
);
// What was written reads back as the score it was written from.
assert_eq!(to_abc(&from_abc(&written)?, &ExportOptions::default())?, written);