Archived:How to play a tone sound using PySymbian
Aquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.
All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
Article Metadata
Tested with
Devices(s): Nokia N95, Nokia E90
Compatibility
Platform(s): S60 1st Edition, S60 2nd Edition, S60 3rd Edition
Article
Keywords: audio
Created: cyke64
(31 Mar 2007)
Last edited: hamishwillee
(08 May 2013)
Overview
This snippet shows how to play a tone in Python.
Source code
from struct import pack
from math import sin, pi
import e32, audio
#Function for generating a tone file
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()
#Function for playing the tone file
def tone(freq=440, duration=1000, volume=0.5):
#Read the temporary file
f = open("D:\\out.au", 'wb')
f.write('.snd' + pack('>5L', 24, 8 * duration, 2, 8000, 1))
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()
#Play the file
s = audio.Sound.open("D:\\out.au")
s.play()
while s.state() == 2: #Playing
e32.ao_yield()
s.close()
au_file()
tone()
Postconditions
A sound with the given properties is played.


20 Sep
2009
23 Sep
2009