Skip to main content

Crate music21_rs

Crate music21_rs 

Source
Expand description

Music theory analysis in Rust, ported from Python’s music21.

The crate covers the analysis side of music21: pitches, intervals, chords, keys, scales, roman numerals, chord symbols, figured bass, durations, meters, tone rows, sieves, tempo marks and voice leading. It keeps music21’s data model and gives music21’s answers, so a chord is named, a numeral realized and a scale spelled the way music21 does it. Beyond music21, it has tuning systems, regular temperaments and polyrhythms.

use music21_rs::Chord;

let chord: Chord = "C E- G B-".parse()?;
assert_eq!(chord.pitched_common_name(), "C-minor seventh chord");
assert_eq!(chord.forte_class().as_deref(), Some("4-26"));
assert_eq!(chord.chord_symbol().as_deref(), Some("Cm7"));

The main types are re-exported at the crate root; each module holds the rest of its area. Every fallible call returns the crate-wide Result.

§Features

No features are on by default.

  • serde derives Serialize and Deserialize for the public types.
  • scala-archive bundles the Scala scale archive (about 4,000 .scl files, roughly 1 MB) and enables ScalaArchive::bundled().
  • language-detection bundles music21’s excerpts in seven languages (about 1.9 MB) and enables features::language and music21’s language feature, TX1, among the native extractors.
  • musicxml reads and writes MusicXML (musicxml::from_musicxml, musicxml::to_musicxml).
  • musescore runs an installed MuseScore to read and write the formats it opens and saves; it turns on musicxml. MuseScore’s own .mscx is read without it, by musescore::from_mscx.

Re-exports§

pub use abc::abc_chord;
pub use abc::abc_duration;
pub use abc::abc_note;
pub use abc::abc_rest;
pub use abc::pitch_name_from_abc_note;
pub use abc::pitch_names_from_abc_chord;
pub use analysis::KeyEstimate;
pub use analysis::KeyProfile;
pub use analysis::estimate_key_from_chords;
pub use analysis::estimate_key_from_chords_with;
pub use analysis::estimate_key_from_pitches;
pub use analysis::estimate_key_from_pitches_with;
pub use analysis::tonal_certainty;
pub use analysis::tonal_certainty_from_scores;
pub use articulations::Articulation;
pub use articulations::ArticulationKind;
pub use bar::Barline;
pub use bar::BarlineType;
pub use bar::Ending;
pub use bar::RepeatDirection;
pub use chord::Chord;
pub use chord::ChordResolutionSuggestion;
pub use chord::ChordTableAddress;
pub use chord::GuitarFingering;
pub use chord::GuitarStringFingering;
pub use chord::GuitarTuning;
pub use chord::GuitarTuningString;
pub use chord::IntoNotes;
pub use chord::KnownChordType;
pub use chord::TriadQuality;
pub use chord::format_vector_string;
pub use chordsymbol::ChordAlteration;
pub use chordsymbol::ChordQuality;
pub use chordsymbol::ChordStepModification;
pub use chordsymbol::ChordStepModificationType;
pub use chordsymbol::ChordSymbol;
pub use chordsymbol::Music21ChordType;
pub use chordsymbol::chord_symbol_figure_from_chord;
pub use chordsymbol::chord_symbol_from_chord;
pub use chordsymbol::known_chord_symbol_types;
pub use chordsymbol::realize_chord_symbol_durations;
pub use clef::Clef;
pub use clef::ClefKind;
pub use duration::Duration;
pub use duration::DurationTuple;
pub use duration::DurationType;
pub use duration::Grace;
pub use duration::Tuplet;
pub use duration::TupletBracket;
pub use duration::TupletShow;
pub use duration::TupletType;
pub use duration::quarter_length_to_closest_type;
pub use dynamics::Dynamic;
pub use error::Error;
pub use error::Result;
pub use instrument::Instrument;
pub use instrument::SearchLanguage;
pub use instrument::ensemble_name_by_size;
pub use interval::ChromaticInterval;
pub use interval::DiatonicInterval;
pub use interval::GenericInterval;
pub use interval::Interval;
pub use interval::IntervalDirection;
pub use interval::Specifier;
pub use interval::absolute_higher_pitch;
pub use interval::absolute_lower_pitch;
pub use interval::convert_diatonic_number_to_step;
pub use interval::convert_generic;
pub use interval::convert_semitone_to_specifier_generic;
pub use interval::convert_semitone_to_specifier_generic_microtone;
pub use interval::intervals_to_diatonic;
pub use interval::notes_to_chromatic;
pub use interval::notes_to_generic;
pub use interval::parse_specifier;
pub use interval::staff_distance_to_generic_number;
pub use interval::written_higher_pitch;
pub use interval::written_lower_pitch;
pub use key::Key;
pub use key::KeySignature;
pub use key::convert_key_string_to_music21_key_string;
pub use metadata::Metadata;
pub use metadata::MetadataValue;
pub use meter::BeamedNote;
pub use meter::BeatDivision;
pub use meter::MeterTerminal;
pub use meter::OffsetAlign;
pub use meter::TimeSignature;
pub use midi::DEFAULT_TICKS_PER_QUARTER;
pub use midi::MidiNote;
pub use midi::midi_notes_from_stream;
pub use midi::read_midi_bytes;
pub use midi::read_midi_bytes_with_tempo;
pub use midi::stream_from_midi_notes;
pub use midi::write_midi_bytes;
pub use notation::Beam;
pub use notation::BeamDirection;
pub use notation::BeamType;
pub use notation::Beams;
pub use notation::Justification;
pub use notation::Lyric;
pub use notation::NoteSize;
pub use notation::Notehead;
pub use notation::Placement;
pub use notation::StemDirection;
pub use notation::Syllabic;
pub use notation::Tie;
pub use notation::TieStyle;
pub use notation::TieType;
pub use note::IntoNote;
pub use note::Note;
pub use percussion::PercussionChord;
pub use percussion::PercussionNote;
pub use percussion::Unpitched;
pub use pitch::Accidental;
pub use pitch::AccidentalAttribute;
pub use pitch::AccidentalDisplayOptions;
pub use pitch::AccidentalSpecifier;
pub use pitch::CHROMATIC_PITCH_CLASS_NAMES;
pub use pitch::Microtone;
pub use pitch::MicrotoneSpecifier;
pub use pitch::Pitch;
pub use pitch::PitchClass;
pub use pitch::PitchClassSpecifier;
pub use pitch::PitchName;
pub use pitch::PitchOptions;
pub use pitch::convert_pitch_class_to_str;
pub use pitch::dissonance_score;
pub use pitch::pitch_class_name;
pub use pitch::simplify_multiple_enharmonics;
pub use polyrhythm::Polyrhythm;
pub use polyrhythm::PolyrhythmAnalysis;
pub use polyrhythm::PolyrhythmEvent;
pub use polyrhythm::PolyrhythmRatioTone;
pub use repeat::RepeatExpression;
pub use repeat::RepeatExpressionKind;
pub use rest::Rest;
pub use roman::roman_numeral_from_chord;
pub use roman::ImpliedQuality;
pub use roman::Minor67Default;
pub use roman::NumeralAlone;
pub use roman::RomanNumeral;
pub use roman::analyze_chord;
pub use roman::analyze_chord_with_root;
pub use roman::identify_as_tonic_or_dominant;
pub use roman::roman_inversion_name;
pub use scale::BluesForm;
pub use scale::DegreeComparison;
pub use scale::Scale;
pub use scale::ScaleType;
pub use scale::Simplification;
pub use scale::SolfegVariant;
pub use scale::StepScale;
pub use scale::WeightedHexatonicBlues;
pub use serial::HISTORICAL_ROWS;
pub use serial::HistoricalRow;
pub use serial::IndexedTransformation;
pub use serial::LinkClassification;
pub use serial::ToneRow;
pub use serial::Transformation;
pub use serial::TransformationConvention;
pub use serial::TwelveToneMatrix;
pub use serial::historical_row_by_name;
pub use serial::row_to_matrix;
pub use sieve::Sieve;
pub use spanner::Glissando;
pub use spanner::LineEnd;
pub use spanner::LineEnds;
pub use spanner::OctaveShift;
pub use spanner::Pedal;
pub use spanner::PedalForm;
pub use spanner::PedalObject;
pub use spanner::PedalObjectKind;
pub use spanner::PedalType;
pub use spanner::SlideType;
pub use spanner::Spanner;
pub use spanner::SpannerKind;
pub use stream::BarTogether;
pub use stream::StaffGroup;
pub use stream::Stream;
pub use stream::StreamElement;
pub use stream::StreamEvent;
pub use stream::StreamKind;
pub use tempo::DEFAULT_TEMPO_VALUES;
pub use tempo::MetricModulation;
pub use tempo::MetronomeMark;
pub use tempo::ModulationSide;
pub use tempo::TempoText;
pub use tempo::convert_tempo_by_referent;
pub use tempo::interpolate_elements;
pub use tuningsystem::ALL_TUNING_SYSTEMS;
pub use tuningsystem::COMMON_EQUAL_TEMPERAMENTS;
pub use tuningsystem::COMMON_TWELVE_TONE_TUNING_SYSTEMS;
pub use tuningsystem::EqualDivision;
pub use tuningsystem::HISTORICAL_TEMPERAMENTS;
pub use tuningsystem::Monzo;
pub use tuningsystem::Mos;
pub use tuningsystem::MosScale;
pub use tuningsystem::OCTAVE_CENTS;
pub use tuningsystem::Ratio;
pub use tuningsystem::TRITAVE_CENTS;
pub use tuningsystem::Temperament;
pub use tuningsystem::TuningSystem;
pub use tuningsystem::Val;
pub use tuningsystem::moment_of_symmetry_sizes;
pub use tuningsystem::scala::ScalaArchive;
pub use tuningsystem::scala::ScalaDegree;
pub use tuningsystem::scala::ScalaScale;
pub use voiceleading::MotionType;
pub use voiceleading::QuartetOptions;
pub use voiceleading::VoiceLeadingQuartet;
pub use voiceleading::iterate_all_voice_leading_quartets;
pub use voiceleading::verticality_at;
pub use volume::DynamicContext;
pub use volume::Volume;
pub use volume::realize_volume;

Modules§

abc
ABC notation export helpers. ABC notation format helpers.
alpha
music21’s experimental modules: notes hashed and streams aligned by them. music21’s alpha package: modules music21 counts as experimental.
analysis
Key-finding and compact analysis helpers.
articulations
Articulations: how a note is played. Articulations: music21’s articulations module.
audiosearch
Finding the notes in a recording: music21’s audioSearch. Finding the notes in a recording: music21’s audioSearch.
bar
Barlines and repeat signs. Barlines: music21’s bar module.
braille
Music in braille: music21’s braille package.
capella
Capella’s CapXML: music21’s capella.fromCapellaXML.
chord
Chord construction, common-name analysis and chord input conversion traits.
chordsymbol
Lead-sheet chord-symbol parsing.
clef
Clefs: which line a note is written on. Clefs: music21’s clef module.
duration
Rhythmic duration primitives.
dynamics
Dynamic marks and the loudness each stands for. Dynamic marks, ported from music21’s dynamics module.
error
Error and result types used by the crate.
expressions
Ornaments and what they play. Ornaments: the part of music21’s expressions module that changes what is played.
features
Features of a piece for machine learning: music21’s features. Features of a piece of music, extracted for machine learning and corpus study: music21’s features package.
figuredbass
Figured bass: the numbers written under a bass note. Figured bass: the numbers written under a bass note, and what they mean.
humdrum
Humdrum **kern scores. Humdrum: music21’s humdrum.spineParser, which reads **kern scores.
instrument
Instruments: music21’s instrument module. Instruments: music21’s instrument module.
interval
Public interval parsing, naming and transposition helpers.
key
Public key and key-signature helpers.
layout
How a score is laid out on the page: music21’s layout objects. How a score is laid out on the page: music21’s layout objects.
lily
LilyPond, written as music21 writes it. LilyPond, written as music21’s lily.translate writes it.
makenotation
Measures, ties, rests, voices, beams and stems worked out for a score that does not say them. Working out what a score leaves unsaid: music21’s stream.makeNotation.
mei
MEI scores. MEI: music21’s mei.base, which reads a score encoded by the Music Encoding Initiative’s schema.
metadata
A score’s title, composer and the rest of what is said about it. A score’s metadata: music21’s metadata.Metadata.
meter
Time signatures: bar length, beat count, beat length and beat division. Time signatures, ported from music21’s meter package.
midi
Minimal MIDI import/export helpers.
musedata
MuseScore’s own .mscx files, and MuseScore run as a converter for the formats it opens. MuseData, the Center for Computer Assisted Research in the Humanities’ encoding: music21’s musedata and musedata.translate.
musescore
MuseScore’s own files, read natively, and MuseScore run as a converter.
musicxmlmusicxml
MusicXML import and export: music21’s musicxml.xmlToM21 and musicxml.m21ToXml. MusicXML export: music21’s musicxml.m21ToXml.
notation
Ties, noteheads, stem direction, colour, lyrics and beams. Notation attached to a note or a chord: ties, noteheads, stem direction, colour and lyrics.
note
Note construction and pitch access helpers.
noteworthy
NoteWorthy Composer’s text format, .nwctxt: music21’s noteworthy.translate.
omr
Correcting scores read by optical music recognition: music21’s omr package. correctors finds the measures whose rhythm was misread and puts the most likely rhythm in their place; evaluators measures how close a score comes to its ground truth before and after.
percussion
Notes with no pitch, and chords of them. Notes with no pitch: music21’s note.Unpitched and percussion.PercussionChord.
pitch
Pitch construction, spelling and pitch-space helpers.
polyrhythm
Polyrhythm timing and pitch-set helpers.
repeat
Words and signs telling a player where to go next. Words and signs telling a player where to go next: music21’s repeat expressions, Segno, Coda, Fine, Da Capo and the rest.
rest
Rests: silence with a written length.
roman
Roman numeral parsing and compact harmonic analysis.
romantext
RomanText, a harmonic analysis written as roman numerals. RomanText: music21’s romanText, a harmonic analysis written measure by measure as roman numerals.
scale
Public scale helpers.
search
Finding music in streams: lyrics as text. Finding music in streams: music21’s search package. A StreamSearcher and the searches beside it find runs of notes by name, rhythm or anything else; the translations turn notes into text, which the approximate searches compare as Python’s difflib does; and a LyricSearcher finds text in a stream’s lyrics, and the notes it is sung to. index_score_parts cuts each part of a score into overlapping stretches of that text, and score_similarity compares every stretch of some scores with every other. A SegmentMatcher finds rows and sets of pitch classes in the notes of a stream, and labels them there.
serial
Tone rows, twelve-tone matrices and serial transformations. Tone rows and twelve-tone serial transformations, a port of music21’s serial module.
sieve
Xenakis sieves, ported from music21’s sieve module.
spanner
Marks joining several notes: slurs. Spanners: marks that join several notes, music21’s spanner module.
stream
Streams: notes, chords and other events placed on a timeline, nested as scores, parts and measures. Music on a timeline: music21’s stream.Stream and the subclasses it nests inside one another.
style
How an object is drawn: music21’s style objects. How an object is drawn: music21’s style objects, as far as the crate keeps them.
tablature
Notes on the strings and frets of a fretted instrument. Notes on the strings and frets of a fretted instrument: music21’s tablature.
tempo
Metronome marks and tempo-word conventions. Metronome marks, a port of the stream-free part of music21’s tempo.
text
Lyrics joined into words and lines, and the articles of titles. Text in scores: music21’s text module. The lyrics of a stream joined into words and lines, the articles of a title moved to its front or its end, and the TextBox a page carries.
tinynotation
TinyNotation, a line of notes written as letters and numbers. TinyNotation: music21’s tinyNotation, a line of notes written as letters and numbers.
tree
A stream’s elements as spans of time, and the moments where what sounds changes. A stream’s elements as spans of time, and the moments where what sounds changes: music21’s tree package, its timespan trees and verticalities.
tuningsystem
Tuning-system ratios, labels and frequency helpers.
variant
Other readings of a passage, kept beside it: music21’s variant module.
voiceleading
Two-voice voice-leading classification and parallel-interval checks. Two-voice voice-leading checks, a port of music21’s VoiceLeadingQuartet.
volpiano
Volpiano, the font chant is written in for the CANTUS database: music21’s volpiano.
volume
How loud a note is: music21’s volume.Volume.

Type Aliases§

FloatType
Floating-point type used for pitch space, quarter lengths, and frequencies.
FractionType
Fraction type used by ratio and interval helpers.
IntegerType
Signed integer type used by music21-rs APIs.
Octave
Optional octave number, matching music21’s absent-octave representation.
UnsignedIntegerType
Unsigned integer type used by count-like music21-rs APIs.