-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdtmf.py
40 lines (31 loc) · 823 Bytes
/
dtmf.py
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
# Author: Peter Sovietov
from __future__ import division
import math
import struct
import wave
SR = 44100
def write_wave(filename, samples):
f = wave.open(filename, "w")
f.setparams((1, 2, SR, len(samples), "NONE", ""))
f.writeframes(b"".join(
[struct.pack('<h', round(x * 32767)) for x in samples]))
f.close()
def sec(x):
return SR * x
def sines(bank, t):
mix = 0
for f in bank:
mix += math.sin(2 * math.pi * f * t / SR)
return mix
DTMF = [
[697, 1209], [697, 1336], [697, 1477],
[770, 1209], [770, 1336], [770, 1477],
[852, 1209], [852, 1336], [852, 1477]
]
samples = []
for d in [3, 1, 1, 5, 5, 5, 2, 3, 6, 8]:
for t in range(int(sec(0.05))):
samples.append(0.5 * sines(DTMF[d - 1], t))
for t in range(int(sec(0.05))):
samples.append(0)
write_wave("dtmf.wav", samples)