-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
67 lines (58 loc) · 2.15 KB
/
speech.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
import speech_recognition as rs
import pyaudio
import audioop
import os
import math
from os import system
import threading
# Microphone stream config.
CHUNK = 1024 # CHUNKS of bytes to read each time from mic
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
THRESHOLD = 1500 # The threshold intensity that defines silence
# and noise signal (an int. lower than THRESHOLD is silence).
SILENCE_LIMIT = 1 # Silence limit in seconds. The max ammount of seconds where
# only silence is recorded. When this time passes the
# recording finishes and the file is delivered.
def audio_int(num_samples=50):
""" Gets average audio intensity of your mic sound. You can use it to get
average intensities while you're talking and/or silent. The average
is the avg of the 20% largest intensities recorded.
"""
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
values = [math.sqrt(abs(audioop.avg(stream.read(CHUNK), 4)))
for x in range(num_samples)]
values = sorted(values, reverse=True)
r = sum(values[:int(num_samples * 0.2)]) / int(num_samples * 0.2)
print(" Average audio intensity is ", r)
stream.close()
p.terminate()
if r > THRESHOLD:
listen(0)
threading.Timer(SILENCE_LIMIT, audio_int).start()
def listen(x):
r=rs.Recognizer()
if x == 0:
system('say Hi. How can I help?')
with rs.Microphone() as source:
audio=r.listen(source)
try:
text = r.recognize_google(audio)
y = process(text.lower())
return(y)
except:
if x == 1:
system('say Good Bye!')
else:
system('say I did not get that. Please say again.')
listen(1)
def process(text):
# ''''''''''''''''''''''''''''''''''''''''''''''''
# '''''''Your application goes here ''''''''''''''
# ''''''''''''''''''''''''''''''''''''''''''''''''