Err403
Err403

Reputation: 123

How to save output in music21 as a MIDI file?

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).

Upvotes: 10

Views: 10636

Answers (3)

user2153553
user2153553

Reputation: 405

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.

Upvotes: 5

If s is your Stream, just call:

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

or to hear it immediately

s.show('midi')

Upvotes: 19

memoselyk
memoselyk

Reputation: 4118

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()

Upvotes: 2

Related Questions