Swiftpack.co - Package - dn-m/MusicXML


Swift Version Platforms Build Status Code Coverage

A work-in-progress implementation of the musicXML specification in Swift.

The goal of this project is to allow Swift users to read, manipulate, and write musicXML files in a richly-typed manner on any platform supported by Swift.

Hello, world!

Let's construct the "Hello, world!" score example from the musicXML documentation. This musical composition consists of one measure that contains a whole note on middle C, based in 4/4 time.

Graphical Representation

When rendered graphically, this score example should look something like this:

Hello, world!

XML Representation

The musicXML representation looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE score-partwise PUBLIC
    "-//Recordare//DTD MusicXML 3.1 Partwise//EN"
<score-partwise version="3.1">
    <score-part id="P1">
  <part id="P1">
    <measure number="1">

MusicXML Representation

To construct the "Hello, world!" example in Swift looks like this:

let note = Note(
    pitch: Pitch(step: .c, octave: 4), 
    duration: 4, 
    type: .whole
let key = Key(fifths: 0)
let time = Time(4,4)
let clef = Clef(sign: .g, line: 2)
let attributes = Attributes(
    divisions: 1,
    keys: [key],
    times: [time],
    clefs: [clef]
let measure = Partwise.Measure(
    number: "1",
    musicData: [
let part = Partwise.Part(id: "P1", measures: [measure])
let header = Header(
    partList: [
        .part(ScorePart(id: "P1", name: "Music"))
let traversal = Partwise(header: header, parts: [part])
let score = Score.partwise(traversal)

🧬 Decoding musicXML into a Score

You can decode a Score in a variety of ways:

let fromData = try Score(data: data)
let fromString = try Score(string: string)
let fromURL = try Score(url: url)

🚧 Work-in-progress: Encoding a Score into musicXML

Pre-release version 0.3.0 will see the completion of the encoding from a Score into the musicXML format.

Getting Started


Use the Swift Package Manager to include the MusicXML module into your project.


If you want to use the MusicXML module in your own project, add the MusicXML package to the dependencies section of your Package.swift file:

let package = Package(
    name: ...,
    products: [ ... ],
    dependencies: [
        .package(url: "https://github.com/dn-m/MusicXML", from: "0.2.1")
    targets: [ ... ]


To contribute to the MusicXML package, clone the git repository:

git clone https://github.com/dn-m/MusicXML && cd MusicXML

Build the package:

swift build

Run the tests:

swift test

If you use the Xcode IDE, use the Swift Package Manager to generate an .xcodeproj file:

swift package generate-xcodeproj

Development Roadmap

The upcoming pre-release versions will be focused on completing different tasks.


Pre-release version 0.3.0 will be defined by completing the implementation of the encoding of abstract musical content. The LilyPond Test Suite tests will be transformed into round-trip tests to ensure that the plumbing is clean.


Pre-release version 0.4.0 will be defined by refining the public interfaces exposed by the MusicXML package. Up until this point, pubic initializers may be somewhat clumsy.

More Resources

See the MusicXML XSD Schema Reference for more information about how musicXML is structured.


Stars: 13


Used By

Total: 0


Decoding Fixes and API Refinement - 2019-11-20 14:57:57

@DJBen and @bwetherfield made great strides in improving the decoding process (e.g., #201, #203, #204, #206) and standardizing the API (e.g., #207, #208).

Under the hood, we added all of the MusicXML sample files to the test suite, and made the testing infrastructure more flexible.

Resolve XMLCoder version - 2019-10-24 18:11:26

Streamline Score structure - 2019-10-21 14:54:47

  • Score is now the first-class citizen of the MusicXML package
  • Header elements have been fixed
  • Generalized testing infrastructure has been added (thanks @DJBen!)

Decoding the LilyPond test suite - 2019-10-13 18:29:51

Pre-release version 0.2.0 now successfully parses the LilyPond test suite in its entirety, with the exception of two tests:

  • Compressed-MusicXML, which is not yet critical to support yet
  • PartNoId, which is invalid as per the MusicXML specification.

Thanks to @DJBen and @bwetherfield for making this possible 🎉 !

Hello, World! - 2018-12-04 00:58:55

Provide a model and API for deserializing only the "Hello World" example provided by MakeMusic.