Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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. 
http://bigbold.com/snippets/posts/show/400

Now, what if you can generate a midi file as well?
I found a pure python midi file library.
http://www.mxm.dk/products/public/pythonmidi

I modify it a bit, just to make a single file for easy download.
http://larndham.net/service/pys60/smidi.py
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. ^_^
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}