-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_recog.py
215 lines (160 loc) · 4.93 KB
/
speech_recog.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import wave
import struct
import scipy
import pyaudio
from numpy import *
import Tkinter as Tk
import sys
import os
import random as r
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
global myCanvas
global toolbar
global f
# a tk.DrawingArea
def displayHist():
global f
#Test wave
#wavFile = wave.open('C:\Users\Tyler\Desktop\warning.wav')
#Record wave file
paud = pyaudio.PyAudio()
chunk = 1024
bitStr = paud.open(format = pyaudio.paInt16, channels = 1,
rate = 44100, input = True, output = True,
frames_per_buffer = 1024)
print 'Recording word'
#ATTN: Change this to control how many seconds it
#records for
secsToRecord = 1
all = []
for i in range(0, 44100 / chunk * secsToRecord):
data = bitStr.read(chunk)
all.append(data)
print 'Done recording...analyzing'
bitStr.stop_stream()
bitStr.close()
paud.terminate()
waveFile = os.getcwd() + 'curr_word.wav'
data = ''.join(all)
wf = wave.open(waveFile, 'wb')
wf.setnchannels(1)
wf.setsampwidth(paud.get_sample_size(pyaudio.paInt16))
wf.setframerate(44100)
wf.writeframes(data)
wf.close()
wavFile = wave.open(waveFile,'r')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = wavFile.getparams()
frames = wavFile.readframes (nframes * nchannels)
out = struct.unpack_from ("%dh" % nframes * nchannels, frames)
wavFile.close()
# Convert 2 channles to numpy arrays
if nchannels == 2:
left = array(list(out[0::2]))
right = array(list(out[1::2]))
else:
left = array (out)
right = left
# transformedData = scipy.fft(left)
# totalTransformed = transformedData.size
# magnitude = zeros(totalTransformed)
# i = 0
# for datum in transformedData:
# magnitude[i] = sqrt(square(datum.real) + square(datum.imag))
# i += 1
# magnitudeHalved = magnitude[0:totalTransformed/2]
# totalSum = sum(magnitudeHalved)
# relMag = zeros(magnitudeHalved.size)
# Xdb = 20*scipy.log10(scipy.absolute(transformedData))
# fn = scipy.linspace(0, 44100, magnitudeHalved.size)
xfft = abs(scipy.fft(left))
where(xfft,0,1e-17)
mag = 20*scipy.log10(xfft)
fn2 = scipy.linspace(0,44100,mag.size)
minIdx = nonzero(fn2 < 2000)
mag = mag[minIdx]
fn2 = fn2[minIdx]
#ATTN: This controls how many bins you have
binSize = 20
freqInBin = 2000/binSize
j = 0
#ATTN: This contains the data you want to input into
#the neural network. It is the average of the magnitude of the
#frequencies over each histogram range
magBin = zeros(len(range(0,binSize)))
magLabel = list()
minFreqBin = 10000000
for i in range(0,binSize):
if i == 0:
minIdx = nonzero(fn2 < (i+1)*freqInBin)
else:
minIdx = nonzero(logical_and(fn2 < (i+1)*freqInBin,fn2 > (i)*freqInBin))
if mag[minIdx].size == 0:
magBin[i] = 0
else:
magBin[i] = average(mag[minIdx])
baseStr = str(int((i+1)*freqInBin))
magLabel.append(baseStr + ' Hz')
if magBin[i] < minFreqBin:
minFreqBin = magBin[i]
magBin = magBin - minFreqBin
a = f.add_subplot(121)
a.clear()
a2 = f.add_subplot(122)
a2.clear()
# fucnt = scipy.linspace(0, 44100, num=transformedData2.size/2)
# print(fucnt)
a.plot(fn2,mag)
a.set_title('FFT Coefficient Magnitude vs. Frequency')
width = 0.35
a2.bar(range(0,binSize), magBin, width)
a2.set_xticklabels(magLabel)
a2.set_title('FFT Frequencies')
myCanvas.show()
myCanvas.get_tk_widget().pack()
toolbar.update()
myCanvas._tkcanvas.pack()
#ATTN: this is the call to the function you should
#fill in with AI code, with the variable you want included.
parseNewData(magBin)
#ATTN: fill in with AT code :P
def parseNewData(magBin):
#TODO: word recog
return
#sets all weights in a weight array to a random number
#between -1, and 1
def set_random_weights(weight_vector):
for index in range(len(weight_vector)):
weight_vector[index] = r.uniform(-1,1)
return weight_vector
root = Tk.Tk()
root.wm_title("Embedding in TK")
frame = Tk.Frame(root)
frame.grid(row = 0, column = 0)
frame.pack()
frame2 = Tk.Frame(root)
frame2.grid(row = 0, column = 1)
frame2.pack()
f = Figure(figsize=(12,6), dpi=100)
myCanvas = FigureCanvasTkAgg(f, master=frame)
toolbar = NavigationToolbar2TkAgg( myCanvas, frame )
button = Tk.Button(root, text="Record", command=displayHist)
button.grid(row = 1, column = 0)
button.pack(side = Tk.LEFT, padx=8,pady=8)
label = Tk.Label(root, text='Machine word:')
label.pack(side = Tk.LEFT, padx = 12,pady=8)
entry = Tk.Entry(root, width = 20)
entry.pack(side = Tk.LEFT, padx=0,pady=8)
label = Tk.Label(root, text='Correct word:')
label.pack(side = Tk.LEFT, padx = 12,pady=8)
entry = Tk.Entry(root, width = 20)
entry.pack(side = Tk.LEFT, padx=0,pady=8)
# button2 = Tk.Button(root, text="Learn")
# button2.grid(row = 1, column = 1)
# button2.pack(side = Tk.RIGHT, padx=8,pady=8)
root.mainloop()
# button = Tk.Button(frame, text="QUIT", command="frame.quit")
# button.pack(side=LEFT)