# How to play a fixed frequency sound using Python [closed]

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

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

• 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, 2013 at 20:46
• Better implementations can be found here: stackoverflow.com/questions/974071/… Feb 12, 2020 at 20:47

``````import math
#sudo apt-get install python-pyaudio
from pyaudio import 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

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''

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

#fill remainder of frameset with silence
for x in xrange(RESTFRAMES):
WAVEDATA += chr(128)

p = PyAudio()
stream = p.open(
format=p.get_format_from_width(1),
channels=1,
rate=BITRATE,
output=True,
)
stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()
``````
• it should be `(2*math.pi)`
– jfs
Oct 28, 2014 at 15:29
• I had to had future division and 2*math*pi to make it work correctly. Python 2.8 32 bit
– chwi
Nov 10, 2015 at 10:26
• Python 2.7 plays this as a "smooth" sound whereas Python 3.4 produces a much "buzzier" sound. Why? I tried adding the ____future____ division, but it made no difference. Dec 19, 2015 at 20:29
• @Ubuntourist I am not sure, but i think your python 2.7 use a 8-Bit encoding and your Python 3.4 use UTF8, so the `char()` use 2 Bytes for some Values on Python 3.4 which is not what we want here. Jun 3, 2017 at 9:52
• If you use bytestrings instead of python3's charstrings you should get the desired effect Jul 20, 2017 at 20:35

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

``````from __future__ import division #Avoid division problems in Python 2
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 =
p.get_format_from_width(1),
channels = 1,
rate = RATE,
output = True)
• @user98415: add at the top `from __future__ import division` to avoid unnecessary truncation on Python 2.