<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: play code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 21:25:00 GMT</pubDate>
    <description>DZone Snippets: play code</description>
    <item>
      <title>Play audio with a Ruby shell script</title>
      <link>http://snippets.dzone.com/posts/show/5744</link>
      <description>Play an ogg file from the command-line using the program ogg123 from the software package 'vorbis-tools'.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;#file: ogg-play.rb&lt;br /&gt;&lt;br /&gt;`ogg123 #{ARGV[0]} &gt; /dev/null 2&gt;&amp;1`&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;e.g. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;wget http://upload.wikimedia.org/wikipedia/en/4/4d/Elo_blue_sky.ogg&lt;br /&gt;./ogg-play.rb Elo_blue_sky.ogg&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 Jul 2008 19:15:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5744</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Play MP3 files</title>
      <link>http://snippets.dzone.com/posts/show/3893</link>
      <description>&lt;code&gt;&lt;br /&gt;public class Player&lt;br /&gt;{&lt;br /&gt;  private string _command;&lt;br /&gt;  private bool isOpen;&lt;br /&gt; [DllImport("winmm.dll")]&lt;br /&gt;private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);&lt;br /&gt;  &lt;br /&gt;public Player()&lt;br /&gt; {&lt;br /&gt;   &lt;br /&gt; }&lt;br /&gt;public void Close()&lt;br /&gt; {&lt;br /&gt;  _command = "close MediaFile";&lt;br /&gt;  mciSendString(_command, null, 0, IntPtr.Zero);&lt;br /&gt;       isOpen=false;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;public void Open(string sFileName)&lt;br /&gt; {&lt;br /&gt;  _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";&lt;br /&gt;  mciSendString(_command, null, 0, IntPtr.Zero);&lt;br /&gt;  isOpen = true;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;public void Play(bool loop)&lt;br /&gt; {&lt;br /&gt;  if(isOpen)&lt;br /&gt;  {&lt;br /&gt;   _command = "play MediaFile";&lt;br /&gt;   if (loop)&lt;br /&gt;    _command += " REPEAT";&lt;br /&gt;    mciSendString(_command, null, 0, IntPtr.Zero);&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 22:44:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3893</guid>
      <author>mstampar (Miroslav Stampar)</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>
