-
See Also
- Import
- Export
- CloudExport
- CloudImport
-
- Formats
- AIFF
- AU
- FLAC
- SND
- WAV
- Related Guides
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",MusicScore[…]] creates a MIDI file from a MusicScore expression consisting of symbolic music 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, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import 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",n n
tracks of the MIDI file given as a Sound object"SoundNotes" sound notes of all tracks in the MIDI file "SoundNotes",n sound notes of the n
tracks in the MIDI file"MusicScore" MIDI file represented as a MusicNote object "MusicVoice" MusicVoice objects from the tracks of the MIDI file "MusicVoice",n MusicVoice object from the n
track of the MIDI file"RawData" raw events of all tracks in the MIDI file "RawData",n raw events of the n
tracks in the MIDI file - Metadata elements:
-
"Metadata" metadata of all tracks in the MIDI file "Metadata",n metadata of the n
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" "Atmosphere" "Bagpipe" "Bandoneon" "Banjo" "BaritoneSax" "Bass" "BassAndLead" "Bassoon" "Bird" "BlownBottle" "Bowed" "BrassSection" "Breath" "Brightness" "BrightPiano" "Calliope" "Celesta" "Cello" "Charang" "Chiff" "Choir" "Clarinet" "Clavi" "Contrabass" "Crystal" "DrawbarOrgan" "Dulcimer" "Echoes" "ElectricBass" "ElectricGrandPiano" "ElectricGuitar" "ElectricPiano" "ElectricPiano2" "EnglishHorn" "Fiddle" "Fifths" "Flute" "FrenchHorn" "FretlessBass" "FretNoise" "Glockenspiel" "Goblins" "Guitar" "GuitarDistorted" "GuitarHarmonics" "GuitarMuted" "GuitarOverdriven" "Gunshot" "Halo" "Harmonica" "Harp" "Harpsichord" "Helicopter" "HonkyTonkPiano" "JazzGuitar" "Kalimba" "Koto" "Marimba" "MelodicTom" "Metallic" "MusicBox" "MutedTrumpet" "NewAge" "Oboe" "Ocarina" "OrchestraHit" "Organ" "PanFlute" "PercussiveOrgan" "Piano" "Piccolo" "PickedBass" "PizzicatoStrings" "Polysynth" "Rain" "Recorder" "ReedOrgan" "ReverseCymbal" "RockOrgan" "Sawtooth" "SciFi" "Seashore" "Shakuhachi" "Shamisen" "Shanai" "Sitar" "SlapBass" "SlapBass2" "SopranoSax" "Soundtrack" "Square" "Steeldrums" "SteelGuitar" "Strings" "Strings2" "Sweep" "SynthBass" "SynthBass2" "SynthBrass" "SynthBrass2" "SynthDrum" "SynthStrings" "SynthStrings2" "SynthVoice" "Taiko" "Telephone" "TenorSax" "Timpani" "Tinklebell" "TremoloStrings" "Trombone" "Trumpet" "Tuba" "TubularBells" "Vibraphone" "Viola" "Violin" "Voice" "VoiceAahs" "VoiceOohs" "Warm" "Whistle" "Woodblock" "Xylophone" - Percussion sounds are represented as an expression of the form SoundNote["perc",t]. Possible percussion styles include:
-
"BassDrum" "BassDrum2" "BellTree" "Cabasa" "Castanets" "ChineseCymbal" "Clap" "Claves" "Cowbell" "CrashCymbal" "CrashCymbal2" "ElectricSnare" "GuiroLong" "GuiroShort" "HighAgogo" "HighBongo" "HighCongaMute" "HighCongaOpen" "HighFloorTom" "HighTimbale" "HighTom" "HighWoodblock" "HiHatClosed" "HiHatOpen" "HiHatPedal" "JingleBell" "LowAgogo" "LowBongo" "LowConga" "LowFloorTom" "LowTimbale" "LowTom" "LowWoodblock" "Maracas" "MetronomeBell" "MetronomeClick" "MidTom" "MidTom2" "MuteCuica" "MuteSurdo" "MuteTriangle" "OpenCuica" "OpenSurdo" "OpenTriangle" "RideBell" "RideCymbal" "RideCymbal2" "ScratchPull" "ScratchPush" "Shaker" "SideStick" "Slap" "Snare" "SplashCymbal" "SquareClick" "Sticks" "Tambourine" "Vibraslap" "WhistleLong" "WhistleShort" - Import by default uses the "Sound" element when importing from MIDI.
Options
- General Import options:
-
MusicTempo Automatic force a specific tempo when importing "MusicScore" and "MusicVoice" elements "QuantizationLevel" Automatic rounding of timestamps to be used in "MusicScore" and "MusicVoice" elements - Possible settings for the "QuantizationLevel" option include:
-
Automatic automatically computed rounding according to the statistics of the input None no time rounding d times are quantized as multiples of d whole notes
Examples
open all close allBasic Examples (6)
A sequence of three notes, each played for 1.75 seconds:
Sound[SoundNote[#, 1.75, "Trumpet"]& /@ {0, 7, 12}]Export["sequence.mid", %]This exports some chord notes to MIDI:
Sound[{SoundNote[{-12, 0, 4, 7, 12, 16}, 0.2, "Trumpet"], SoundNote[{-12, 0, 3, 7, 12, 15}, 2.2, "Trumpet"]}]Export["chords.mid", %]Export a few percussion sounds to MIDI:
Export["perc.mid", Sound[ SoundNote["RideCymbal", #] & /@ {0.26, 0.1, 0.26, 0.1, 2} ] ]A simple MusicScore object with a single voice:
MusicScore[{"G", "A", "D", "C"}]Export["score.mid", %]MusicScore[{MusicVoice@{"G", "A", "D", "C"}, MusicVoice[{"C", "D", "F", "G"}]}]Export["score2.mid", %]Import a MIDI file as a Sound object:
Import["ExampleData/scaleprogression.mid"]Import a MIDI file as a MusicScore object:
Import["ExampleData/scaleprogression.mid", "MusicScore"]Import a MIDI file as a list of MusicVoice objects:
Import["ExampleData/scaleprogression.mid", "MusicVoice"]Scope (3)
This creates a musical fragment in the style of WolframTones by taking cellular automaton data and mapping it to musical notes:
Sound[SoundNote[#, 1 / 5, "Harpsichord"]& /@ (Pick[{0, 4, 7, 12, 16}, #, 1]& /@ CellularAutomaton[ 60, {{1, 0, 0, 0, 0}, 0}, 15, {15, 4}]) ]Export["rule60.mid", %]Generate a basic algorithmic composition and export it to MIDI:
Sound[Table[SoundNote[ #, 1 / 12, "Harp" ]& /@ Pick[Range[12], IntegerDigits[scale, 2, 12], 1], {scale, 2304, 2352}]]Export["scaleprogression.mid", %]Import the SoundNote objects contained in a MIDI file:
sn = Import["ExampleData/scaleprogression.mid", "SoundNotes"];
Take[sn[[1]], 10]Replay the notes in the MIDI file using an organ instead of a harp:
Sound[sn /. {"Harp" -> "Organ"}]Import Options (2)
"QuantizationLevel" (1)
Import a MIDI file as a MusicScore object:
Import["ExampleData/scaleprogression.mid", "MusicScore"]Import["ExampleData/scaleprogression.mid", "MusicScore", "QuantizationLevel" -> None]Set the quantization level to 1/32:
Import["ExampleData/scaleprogression.mid", "MusicScore", "QuantizationLevel" -> 1 / 32]MusicTempo (1)
Import a MIDI file as a MusicScore object:
Import["ExampleData/scaleprogression.mid", "MusicScore"]Note that the note durations are a bit unusual:
MusicMeasurements[%, "NoteList"][[1, ;; 4]]Force the import to use 180 beats per minute as tempo by using the MusicTempo option:
Import["ExampleData/scaleprogression.mid", "MusicScore", MusicTempo -> 180]MusicMeasurements[%, "NoteList"][[1, ;; 4]]Related Guides
History
Introduced in 2007 (6.0) | Updated in 2026 (15.0)