Over a million developers have joined DZone.

Generate And Play Midi On Mobile Phone

DZone 's Guide to

Generate And Play Midi On Mobile Phone

· ·
Free Resource
From a previous snippet, you can play any sound file
(including midi) with pys60. 

Now, what if you can generate a midi file as well?
I found a pure python midi file library.

I modify it a bit, just to make a single file for easy download.
With it, you can play a single note with the following code.

>>> import smidi
>>> m = smidi.MidiOutFile('C:\\out.mid')
>>> m.header()
>>> m.start_of_track()
>>> m.update_time(0)
>>> m.note_on(note=0x40)  # single note
>>> m.update_time(192)
>>> m.note_off(note=0x40) # stop it after 192
>>> m.update_time(0)
>>> m.end_of_track()
>>> m.eof()

>>> from audio import Sound
>>> s = Sound.open('C:\\out.mid')
>>> s.play()
>>> s.close()

These 10 lines can be made into a simple function.
Then, you can just type a line and play any note you like
on you mobile phone.

>>> from smidi import play
>>> play([(64,192), (32, 192)])

Ah... so pythonic. ^_^

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}