<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: midi code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:35:01 GMT</pubDate>
    <description>DZone Snippets: midi code</description>
    <item>
      <title>MIDI Note number and frequency</title>
      <link>http://snippets.dzone.com/posts/show/1450</link>
      <description>&lt;a href=http://www.harmony-central.com/MIDI/Doc/table2.html&gt;Summary of MIDI Note Numbers&lt;/a&gt;&lt;br /&gt;&lt;a href=http://www.borg.com/~jglatt/tutr/notefreq.htm&gt;MIDI Note to Frequency&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I can use them with my &lt;a href=http://bigbold.com/snippets/posts/show/572&gt;midi snippet&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# from http://logic-users.org/forums/L-OT/295&lt;br /&gt;# Each note's frequency is 2^(1/12) times of the previous note.&lt;br /&gt;&lt;br /&gt;freq = 440 * 2^((n-69)/12)&lt;br /&gt;n = 69 + 12*log(freq/440)/log(2)&lt;br /&gt;&lt;br /&gt;# Doe, ray, me, fa, sol, la, tee, doe&lt;br /&gt;&gt;&gt;&gt; play([(i, 100) for i in [60, 62, 64,65, 67, 69, 71,72]])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 10 Feb 2006 08:27:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1450</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Generate and play midi on mobile phone</title>
      <link>http://snippets.dzone.com/posts/show/572</link>
      <description>From a previous snippet, you can play any sound file&lt;br /&gt;(including midi) with pys60. &lt;br /&gt;http://bigbold.com/snippets/posts/show/400&lt;br /&gt;&lt;br /&gt;Now, what if you can generate a midi file as well?&lt;br /&gt;I found a pure python midi file library.&lt;br /&gt;http://www.mxm.dk/products/public/pythonmidi&lt;br /&gt;&lt;br /&gt;I modify it a bit, just to make a single file for easy download.&lt;br /&gt;http://larndham.net/service/pys60/smidi.py&lt;br /&gt;With it, you can play a single note with the following code.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; import smidi&lt;br /&gt;&gt;&gt;&gt; m = smidi.MidiOutFile('C:\\out.mid')&lt;br /&gt;&gt;&gt;&gt; m.header()&lt;br /&gt;&gt;&gt;&gt; m.start_of_track()&lt;br /&gt;&gt;&gt;&gt; m.update_time(0)&lt;br /&gt;&gt;&gt;&gt; m.note_on(note=0x40)  # single note&lt;br /&gt;&gt;&gt;&gt; m.update_time(192)&lt;br /&gt;&gt;&gt;&gt; m.note_off(note=0x40) # stop it after 192&lt;br /&gt;&gt;&gt;&gt; m.update_time(0)&lt;br /&gt;&gt;&gt;&gt; m.end_of_track()&lt;br /&gt;&gt;&gt;&gt; m.eof()&lt;br /&gt;&lt;br /&gt;&gt;&gt;&gt; from audio import Sound&lt;br /&gt;&gt;&gt;&gt; s = Sound.open('C:\\out.mid')&lt;br /&gt;&gt;&gt;&gt; s.play()&lt;br /&gt;&gt;&gt;&gt; s.close()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;These 10 lines can be made into a simple function.&lt;br /&gt;Then, you can just type a line and play any note you like&lt;br /&gt;on you mobile phone.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; from smidi import play&lt;br /&gt;&gt;&gt;&gt; play([(64,192), (32, 192)])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Ah... so pythonic. ^_^</description>
      <pubDate>Wed, 10 Aug 2005 00:37:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/572</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
