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-6 of 6 total  RSS 

Generate tone frequency using au file

The first part of my attempt to produce a tone sound.
   1  
   2  from struct import pack
   3  from math import sin, pi
   4  
   5  def au_file(name='out.au', freq=400, length=2, A=0.5):
   6  	f = open(name, 'wb')
   7  	f.write('.snd')
   8  	f.write(pack('>5L', 24, -1, 2, 8000, 1))
   9  	T = 8000./freq
  10  	for i in range(length*8000):
  11  		angle = 2*pi*i/T
  12  		val = pack('b', A*sin(angle)*127)
  13  		f.write(val)
  14  	f.close()

The reason that I use '.au' instead of '.wav' is that
the format is much simpler. Both are supported on my 6600 phone.

Here's the second step, after removing some bug.
(data size can't be -1, I must calculate it too)
   1  
   2  from struct import pack
   3  from math import pi, sin
   4  import e32, audio 
   5  
   6  def tone(freq=440, duration=1000, volume=0.5):
   7      f = open('D:\\out.au', 'wb')    # temp file
   8      f.write('.snd' + pack('>5L', 24, 8*duration, 2, 8000, 1))  #header
   9      for i in range(duration*8):
  10          sin_i = sin(i * 2*pi*freq/8000)  # sine wave
  11          f.write(pack('b', volume*127*sin_i))
  12      f.close()
  13      # now play the file
  14      s = audio.Sound.open('D:\\out.au')
  15      s.play()
  16      while s.state()==2: # playing
  17          e32.ao_yield()
  18      s.close()

The code is still quite slow. I'm not sure why (haven't test).
Either because of sin() or pack() or f.write()
A solution could be using a smaller file and play it multiple times.
But this small code should be enough to demonstrate an idea.

Playing sound in Windows

   1  
   2  >>> from winsound import *
   3  
   4  >>> f = "C:/Windows/Media/chimes.wav"
   5  >>> PlaySound(f, SND_FILENAME)
   6  
   7  >>> PlaySound("SystemExit", SND_ALIAS)

See documentation.

Soundex

From Greg Jorgensen's recipe.
   1  
   2  def soundex(name, len=4):
   3  
   4      # digits holds the soundex values for the alphabet
   5      digits = '01230120022455012623010202'
   6      sndx = ''
   7      fc = ''
   8  
   9      # translate alpha chars in name to soundex digits
  10      for c in name.upper():
  11          if c.isalpha():
  12              if not fc: fc = c   # remember first letter
  13              d = digits[ord(c)-ord('A')]
  14              # duplicate consecutive soundex digits are skipped
  15              if not sndx or (d != sndx[-1]):
  16                  sndx += d
  17      
  18      sndx = fc + sndx[1:]   # replace first digit with first char
  19      sndx = sndx.replace('0','')       # remove all 0s
  20      return (sndx + (len * '0'))[:len] # padded to len characters

Similar words will return the same soundex
   1  
   2  >>> soundex('hello')
   3  'H400'
   4  >>> soundex('hola')
   5  'H400'

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. ^_^

Recording sound

py_s60 1.1.3 provide audio module which allow you to record sound.
   1  
   2  import e32, audio
   3  
   4  s = audio.Sound.open('C:\\test.amr')
   5  s.record()  # start recording
   6  e32.ao_sleep(5)  # do if for 5 seconds
   7  s.stop() # stop recording
   8  
   9  
  10  # the file is now created, ready to be played
  11  s.play()

Using this code, I can record sound longer than
the 1 minute limit by Nokia's 'Recorder' program.

Update:
It can record in 'wav', 'amr','au'.
I can't play the file recorded with 'wav', though.
It seems 6600 has the problem with uncompressed 16-bit wave.
(It can play other wave files fine.)

Playing sound

py_s60 1.1.3 provides audio module which allow you to
play sound. This should let people write some fun games.

Here's a quick example.
   1  
   2  from audio import *
   3  f = 'C:\\Nokia\\Sounds\\Digital\\28050.amr'
   4  s = Sound.open(f)
   5  s.play()

I have tried calling Sound.open(f).play() directly, but
it doesn't seem to work. I may be waiting for file loading
or wait for some callback. I don't know.

You can play it many times or repeat forever
   1  
   2  s.play(3) # 3 times
   3  s.play(KMdaRepeatForever) # or just use -2
   4  s.stop() # Ok, enough of it
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS