Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I have tried a number of ways of playing a fixed frequency sound (e.g. 1000Hz) and nothing works.

I have downloaded "beep" and that makes no noise.

I tried interfacing to pyao, and that had no effect.

I tried interfacing to audiere, and get a runtime error indicating the library could not be found, despite installing it from the software centre.

Any guidance for installation of appropriate libraries and relevant code would be most appreciated.

I cannot generate .mp3/ .wav files for this, but need to generate the tones at run time.

Many thanks for you

share|improve this question
You need to make WAVE a float. Why not call it FREQ? Also the equation is not right. Try this: math.sin((x*2*math.pi*FREQ)/RATE)*127)+128 –  user216759 Nov 17 '13 at 20:46

2 Answers 2

import math
import pyaudio

#sudo apt-get install python-pyaudio
PyAudio = pyaudio.PyAudio

#See http://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000 #number of frames per second/frameset.      

#See http://www.phy.mtu.edu/~suits/notefreqs.html
FREQUENCY = 261.63 #Hz, waves per second, 261.63=C4-note.
LENGTH = 1.2232 #seconds to play sound

WAVEDATA = ''    

for x in xrange(NUMBEROFFRAMES):
 WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

#fill remainder of frameset with silence
for x in xrange(RESTFRAMES): 

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)
share|improve this answer
it should be (2*math.pi) –  J.F. Sebastian Oct 28 '14 at 15:29

You can do that with python-pyaudio (available at least in 12.04):

import math
import pyaudio
import sys

PyAudio = pyaudio.PyAudio
RATE = 16000
WAVE = 1000
data = ''.join([chr(int(math.sin(x/((RATE/WAVE)/math.pi))*127+128)) for x in xrange(RATE)])
p = PyAudio()

stream = p.open(format =
                channels = 1,
                rate = RATE,
                output = True)
for DISCARD in xrange(5):
share|improve this answer
Thank you for your help. –  user98415 Oct 22 '12 at 10:43
Thank you for your help. Unfortunately, I get loads of errors when running, and making any changes to RATE or WAVE results in no sound. The errors are not listed due to a lack of space but refer to "ALSA lib". How would I change a) to eliminate errors and b) to change duration and frequency? Many thanks, –  user98415 Oct 22 '12 at 10:52
I also get a bunch of error messages, but it plays just fine. Also, changing WAVE gives me different pitch. Are you using jack? Or just pulseaudio? –  taneli Oct 23 '12 at 17:33
@user98415: add at the top from __future__ import division to avoid unnecessary truncation on Python 2. –  J.F. Sebastian Oct 28 '14 at 15:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.