×
Namespaces

Variants
Actions
Revision as of 01:43, 7 December 2008 by bogdan.galiceanu (Talk | contribs)

Archived:How to play a tone sound using PySymbian

From Nokia Developer Wiki
Jump to: navigation, search
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: (31 Mar 2007)
Last edited: bogdan.galiceanu (07 Dec 2008)

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.

72 page views in the last 30 days.
×