1import math #import needed modules
2import pyaudio #Sudo apt-get install python-pyaudio
3
4PyAudio = pyaudio.PyAudio #initialize pyaudio
5
6#See https://en.wikipedia.org/wiki/Bit_rate#Audio
7BITRATE = 16000 #number of frames per second/frameset.
8
9FREQUENCY = 500 #Hz, waves per second, 261.63=C4-note.
10LENGTH = 1 #seconds to play sound
11
12if FREQUENCY > BITRATE:
13 BITRATE = FREQUENCY+100
14
15NUMBEROFFRAMES = int(BITRATE * LENGTH)
16RESTFRAMES = NUMBEROFFRAMES % BITRATE
17WAVEDATA = ''
18
19#generating wawes
20for x in xrange(NUMBEROFFRAMES):
21 WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))
22
23for x in xrange(RESTFRAMES):
24 WAVEDATA = WAVEDATA+chr(128)
25
26p = PyAudio()
27stream = p.open(format = p.get_format_from_width(1),
28 channels = 1,
29 rate = BITRATE,
30 output = True)
31
32stream.write(WAVEDATA)
33stream.stop_stream()
34stream.close()
35p.terminate()
36