MIDI (.mid)

Background & Context

    • MIME type: audio/midi
    • Standard MIDI file format.
    • Audio format.
    • Used for controlling electronic musical instruments, computer sound cards, and sequencers.
    • Stores a sequence of discrete sound events such as the beginning and end of a note, velocity and timbre information, and instrument and sound bank codes.
    • MIDI is an acronym for Musical Instrument Digital Interface.
    • Binary file format.
    • Introduced in 1983.
    • Developed and published by the MIDI Manufacturers Association (US) and the Association of Musical Electronic Industry MIDI committee (Japan).

Import & Export

  • Import["file.mid"] imports a MIDI file, returning a sound object.
  • Export["file.mid",expr] exports a sound object to MIDI.
  • Import["file.mid",elem] imports the specified element from a MIDI file.
  • Import["file.mid",{elem,suba,subb,}] imports a subelement.
  • Import["file.mid",{{elem1,elem2,}}] imports multiple elements.
  • The import format can be specified with Import["file","MIDI"] or Import["file",{"MIDI",elem,}].
  • Export["file.mid",Sound[]] creates a MIDI file from a Sound expression consisting of SoundNote primitives.
  • Export["file.mid",expr,elem] creates a MIDI file by treating expr as specifying element elem.
  • The file format can be explicitly specified using Export["file",Sound[],"MIDI"].
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Data representation elements:
  • "Sound"MIDI file represented as a Sound object
    "Sound",nn^(th) tracks of the MIDI file given as a Sound object
    "SoundNotes"sound notes of all tracks in the MIDI file
    "SoundNotes",nsound notes of the n^(th) tracks in the MIDI file
    "RawData"raw events of all tracks in the MIDI file
    "RawData",nraw events of the n^(th) tracks in the MIDI file
  • Metadata elements:
  • "Metadata"metadata of all tracks in the MIDI file
    "Metadata",nmetadata of the n^(th) tracks in the MIDI file
    "Duration"duration in seconds
    "Header"header in the MIDI file
    "TrackCount"number of tracks in the MIDI file
  • Data representation element:
  • "Sound"a Sound expression consisting of SoundNote primitives
  • Export by default uses the "Sound" element for MIDI files.
  • Export["file.mid",Sound[ SoundNote[0]]] creates a MIDI file representing the "middle C" note played for 1 second in the default "Piano" style.
  • The pitch specification 0 in the Wolfram Language corresponds to MIDI pitch level 60.
  • Export["file.mid",Sound[SoundNote[pitch,t,"style"]]] specifies a duration t and an instrument style. The following General MIDI styles are supported:
  • "Accordion""Agogo""AltoSax""Applause"
  • Percussion sounds are represented as an expression of the form SoundNote["perc",t]. Possible percussion styles include:
  • "BassDrum""BassDrum2""BellTree""Cabasa"
  • Import by default uses the "Sound" element when importing from MIDI.


Basic Examples  (6)

A sequence of three notes, each played for 1.75 seconds:

Export to a MIDI file:

This exports some chord notes to MIDI:

Export a few percussion sounds to MIDI:

This creates a musical fragment in the style of WolframTones by taking cellular automaton data and mapping it to musical notes:

Export to MIDI:

Generate a basic algorithmic composition and export it to MIDI:

Import a MIDI file as a Sound object:

Import the SoundNote objects contained in a MIDI file:

Replay the notes in the MIDI file using an organ instead of a harp: