1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| from math import sin, pi
from array import array
from cStringIO import StringIO
import wave
tones = {
'1' : (697, 1209),
'2' : (697, 1336),
'3' : (697, 1477),
'A' : (697, 1633),
'4' : (770, 1209),
'5' : (770, 1336),
'6' : (770, 1477),
'B' : (770, 1633),
'7' : (852, 1209),
'8' : (852, 1336),
'9' : (852, 1477),
'C' : (852, 1633),
'*' : (941, 1209),
'0' : (941, 1336),
'#' : (941, 1477),
'D' : (941, 1633)
}
def dial(number, tone_duration=0.3, spacing=0.05, samprate=44100, sampwidth=2):
sound = []
mult = 2**((sampwidth*8)-2)-1
samp = 2*pi/samprate
for c in number:
if c in tones:
for i in range(int(samprate*tone_duration)):
s = sin(tones[c][0]*samp*i) + sin(tones[c][1]*samp*i)
s = int(s * mult + 0.5)
sound.append(s)
sound.extend([0]*int(spacing*samprate))
a = array('h', sound).tostring()
return a
def make_wave(f, number):
w = wave.open(f, 'w')
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(44100)
w.writeframes(dial(number))
w.close()
if __name__ == '__main__':
f = StringIO()
make_wave(f, '123456789*0#')
# Windows only:
import winsound
winsound.PlaySound(f.getvalue(), winsound.SND_MEMORY) |
Partager