from struct import pack from math import sin, pi def au_file(name='out.au', freq=400, length=2, A=0.5): f = open(name, 'wb') f.write('.snd') f.write(pack('>5L', 24, -1, 2, 8000, 1)) T = 8000./freq for i in range(length*8000): angle = 2*pi*i/T val = pack('b', A*sin(angle)*127) f.write(val) 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)
from struct import pack from math import pi, sin import e32, audio def tone(freq=440, duration=1000, volume=0.5): f = open('D:\\out.au', 'wb') # temp file f.write('.snd' + pack('>5L', 24, 8*duration, 2, 8000, 1)) #header for i in range(duration*8): sin_i = sin(i * 2*pi*freq/8000) # sine wave f.write(pack('b', volume*127*sin_i)) f.close() # now play the file s = audio.Sound.open('D:\\out.au') s.play() while s.state()==2: # playing e32.ao_yield() 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.
Yes you are right it's very slow but it works !
file.au is Sun or Next audio file and in
standard library in Python 2.2.2 you can found three modules not provided with pys60 !
audiodev.py , sunau.py and sunaudio.py
I try only sunaudio module but it works without any modification.
send them to phone and try this snippet !