Never been to DZone Snippets before?

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

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Generate and play midi on mobile phone

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.
   1  
   2  >>> import smidi
   3  >>> m = smidi.MidiOutFile('C:\\out.mid')
   4  >>> m.header()
   5  >>> m.start_of_track()
   6  >>> m.update_time(0)
   7  >>> m.note_on(note=0x40)  # single note
   8  >>> m.update_time(192)
   9  >>> m.note_off(note=0x40) # stop it after 192
  10  >>> m.update_time(0)
  11  >>> m.end_of_track()
  12  >>> m.eof()
  13  
  14  >>> from audio import Sound
  15  >>> s = Sound.open('C:\\out.mid')
  16  >>> s.play()
  17  >>> 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.
   1  
   2  >>> from smidi import play
   3  >>> play([(64,192), (32, 192)])

Ah... so pythonic. ^_^

Playing sound

py_s60 1.1.3 provides audio module which allow you to
play sound. This should let people write some fun games.

Here's a quick example.
   1  
   2  from audio import *
   3  f = 'C:\\Nokia\\Sounds\\Digital\\28050.amr'
   4  s = Sound.open(f)
   5  s.play()

I have tried calling Sound.open(f).play() directly, but
it doesn't seem to work. I may be waiting for file loading
or wait for some callback. I don't know.

You can play it many times or repeat forever
   1  
   2  s.play(3) # 3 times
   3  s.play(KMdaRepeatForever) # or just use -2
   4  s.stop() # Ok, enough of it
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS