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 

Play MP3 files

public class Player
{
  private string _command;
  private bool isOpen;
 [DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
  
public Player()
 {
   
 }
public void Close()
 {
  _command = "close MediaFile";
  mciSendString(_command, null, 0, IntPtr.Zero);
       isOpen=false;
 }

public void Open(string sFileName)
 {
  _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
  mciSendString(_command, null, 0, IntPtr.Zero);
  isOpen = true;
 }

public void Play(bool loop)
 {
  if(isOpen)
  {
   _command = "play MediaFile";
   if (loop)
    _command += " REPEAT";
    mciSendString(_command, null, 0, IntPtr.Zero);
   }
 }

}

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