DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Generate And Play Midi On Mobile Phone

08.09.2005
| 11770 views |
  • submit to reddit
        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. ^_^