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 

MIDI Note number and frequency

Summary of MIDI Note Numbers
MIDI Note to Frequency

I can use them with my midi snippet.
   1  
   2  # from http://logic-users.org/forums/L-OT/295
   3  # Each note's frequency is 2^(1/12) times of the previous note.
   4  
   5  freq = 440 * 2^((n-69)/12)
   6  n = 69 + 12*log(freq/440)/log(2)
   7  
   8  # Doe, ray, me, fa, sol, la, tee, doe
   9  >>> play([(i, 100) for i in [60, 62, 64,65, 67, 69, 71,72]])

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. ^_^
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS