Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 19:46, 14 October 2007.

How to play a tone sound

From Forum Nokia Wiki


With sound.play you can play a sound file (mp3,midi etc.) supported by your device. But if you want play a simple tone with a certain frequency you can use that. So here's a trick how to do it.

The first part is to produce a tone sound.

#
# generate a tone file under format au with a freq of 400Hz
# au is more simple than wav and better supported by device
#
 
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()

Second part is play the generated tone file.

rom 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()
Related Discussions
Thread Thread Starter Forum Replies Last Post
Sound problem in real 7650 cooler1 Mobile Java General 0 2002-08-13 09:01
Sound Files Encryption/Decryption say2paul General Symbian C++ 0 2006-05-19 08:23
9500 & CMdaAudioPlayerUtility nansenst General Symbian C++ 0 2004-09-10 18:58
Unable to change default alarm sound, Unable to set the alarm to silent. seemaK Symbian Media (Graphics & Sounds) 0 2008-06-17 12:21
Sound problems on the 6600? ChiralSoftware Mobile Java Media (Graphics & Sounds) 1 2005-03-21 20:12
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZaudioQ
     qfnZtopicQUqfnTopicZpythonQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX