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

« 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.
# from http://logic-users.org/forums/L-OT/295
# Each note's frequency is 2^(1/12) times of the previous note.

freq = 440 * 2^((n-69)/12)
n = 69 + 12*log(freq/440)/log(2)

# Doe, ray, me, fa, sol, la, tee, doe
>>> 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.
>>> 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. ^_^
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS