Namespaces
Variants
Actions
Revision as of 08:57, 18 September 2012 by hamishwillee (Talk | contribs)

Archived:How to play a tone sound using PySymbian

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

Platform Security
Capabilities: )

Article
Keywords: audio
Created: cyke64 (31 Mar 2007)
Last edited: hamishwillee (18 Sep 2012)

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.

153 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved