How to save output in music21 as a MIDI file?
Asked Answered
Y

3

10

How do I save audio output in Python using the music21 module? I have read the entire [user's guide](http://music21.readthedocs.org/en/latest/usersGuide/index.html] of said module, but I couldn't find any information about saving output as an audio file that can be recognised by windows without any additional software (MIDI for example).

Yurev answered 20/12, 2015 at 14:49 Comment(0)
T
19

If s is your Stream, just call:

fp = s.write('midi', fp='pathToWhereYouWantToWriteIt')

or to hear it immediately

s.show('midi')

Tiruchirapalli answered 20/12, 2015 at 19:5 Comment(0)
C
5

Somewhere in this User's Guide Chapter 8, there is some important information about opening and saving file in many formats: http://web.mit.edu/music21/doc/usersGuide/usersGuide_08_installingMusicXML.html

if you have made your own music called 'stream1', you can easily save it as MIDI file like this:

stream1.write("midi", "blah.mid")

I am still new to this, but I think that's simpler than having to open file, etc.

Campus answered 13/3, 2017 at 11:51 Comment(0)
F
2

There is a MidiFile object, which knows how to write a midi file.

But the documentation on how to use it is non-existant.

However, in its source there is a testBasicExport test, probably it's a good start, it does something like this:

mt = MidiTrack(1)

# duration, pitch, velocity
data = [[1024, 60, 90], [1024, 50, 70], [1024, 51, 120],[1024, 62, 80], ]

# Omit this part here, but full code in the links above
populateTrackFromData(mt, data)

mf = MidiFile()
mf.ticksPerQuarterNote = 1024 # cannot use: 10080
mf.tracks.append(mt)

mf.open('/src/music21/music21/midi/out.mid', 'wb')
mf.write()
mf.close()
Fitted answered 20/12, 2015 at 17:20 Comment(1)
use stream.write('midi') instead as below. The MidiFile is mainly an internal use function.Tiruchirapalli

© 2022 - 2024 — McMap. All rights reserved.