Skip to main content

from_mscx

Function from_mscx 

Source
pub fn from_mscx(document: &str) -> Result<Stream>
Expand description

Reads a MuseScore file into a score.

The text is an uncompressed .mscx as MuseScore 4 saves it, or as MuseScore 3 does where the two formats are the same. A .mscz is a zip archive holding one; the library opens no file and unpacks nothing, so the caller hands over the text of the .mscx inside it.

Every <Part> is a part, and a part on several staves – a piano – is a part for each staff joined by a brace, as a MusicXML reader makes it. Pitches are spelled as the file spells them, and a transposing instrument’s are written as it reads them.

§What is read

  • Parts, their names and abbreviations, instruments, MIDI programs and channels, transpositions, and the brackets and braces joining staves.
  • Measures and their numbers – pickups, measures left out of the count, sections that start again from one – and up to four voices in each.
  • Notes, chords and rests, measure rests among them: lengths, dots, tuplets and tuplets inside tuplets, ties, written accidentals, noteheads, colours, velocities, stems the file fixes, and grace notes before and after a note.
  • Clefs, key signatures, meters, tempo marks, dynamics, staff and system text, lyrics and chord symbols.
  • Articulations, fingerings, breath marks, fermatas on notes and on barlines, ornaments and arpeggios.
  • Barlines, repeats, endings, and the signs and words that send a player elsewhere: segno, coda, To Coda, Fine, D.C. and D.S.
  • Slurs, hairpins and pedal lines.
  • The title, composer and the rest of the file’s metaTags.

A MuseScore file leaves some of what a page shows to be worked out when the page is laid out, and that is worked out here as MuseScore does it: beams from the meter and the beam modes of the notes, whether a tuplet takes a bracket, the clef and key signature a staff starts with when none is written, and the final barline.

§What is not read

What is only drawn – frames, page and system breaks, positions, fonts, rehearsal marks, and whatever is marked invisible – is passed over. MuseScore 4 keeps a score’s style in a file of its own beside the .mscx, which this function is not given; where the style decides something, MuseScore’s default is taken.

What the crate has no value for, or cannot read yet, is refused with an Error::MuseScore naming it rather than dropped: percussion and tablature staves, files in a format older than MuseScore 3’s, tremolos, trill, octave, glissando and text lines, arpeggios across staves, ornaments with accidentals, key signatures of a score’s own making, measure repeats, fret diagrams, figured bass, instrument changes, shape notes, and a chord symbol whose name is none the crate knows.

use music21_rs::musescore::from_mscx;

let score = from_mscx(
    r#"<museScore version="4.20"><Score>
    <Part><Staff/><trackName>Flute</trackName>
      <Instrument><longName>Flute</longName><trackName>Flute</trackName>
      <Channel><program value="73"/></Channel></Instrument></Part>
    <Staff id="1"><Measure><voice>
      <TimeSig><sigN>2</sigN><sigD>4</sigD></TimeSig>
      <Chord><durationType>quarter</durationType><Note><pitch>60</pitch><tpc>14</tpc></Note></Chord>
      <Chord><durationType>quarter</durationType><Note><pitch>63</pitch><tpc>11</tpc></Note></Chord>
    </voice></Measure></Staff>
    </Score></museScore>"#,
)?;
let names: Vec<String> = score.parts()[0]
    .pitches()
    .iter()
    .map(|pitch| pitch.name_with_octave())
    .collect();
assert_eq!(names, ["C4", "Eb4"]);