-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
131 lines (100 loc) · 4.22 KB
/
Utils.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
import threading
from threading import Thread
import logging
import speech_recognition as sr
from kalliope import Utils, SettingLoader
from kalliope.stt import OwnSpeech
import os
import time
from pydub import AudioSegment
#import wave
logging.basicConfig()
logger = logging.getLogger("kalliope")
OWN_AUDIO_FILE = '/tmp/kalliope/tmp_uploaded_audio/own_audio_file.wav'
HOTWORD_FILE = '/tmp/kalliope/tmp_uploaded_audio/hotword_file.wav'
class SpeechRecognition(Thread):
def __init__(self, audio_file=None):
"""
Thread used to caught n audio from the microphone and pass it to a callback method
"""
super(SpeechRecognition, self).__init__()
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
self.callback = None
self.audio_stream = None
# get global configuration
sl = SettingLoader()
self.settings = sl.settings
if self.audio_file_exist(OWN_AUDIO_FILE): # Maybe provided by the APP
self.audio_file = OWN_AUDIO_FILE
else:
if self.load_status() == 'is_recording': # Record thread is still active
while not self.record_is_finished():
time.sleep(0.1)
else:
SR = SpeechRecorder()
SR.start()
while not self.record_is_finished():
time.sleep(0.1)
if self.audio_file_exist(HOTWORD_FILE): # If there is a hotword_file, then merge both togther
self.merge_audio()
if self.audio_file:
with sr.AudioFile(self.audio_file) as source:
self.audio_stream = self.recognizer.record(source)
os.remove(self.audio_file) # we need to remove it, otherwise it would end in a loop
if self.audio_file_exist(HOTWORD_FILE):
os.remove(HOTWORD_FILE)
def run(self):
self.callback(self.recognizer, self.audio_stream)
def audio_file_exist(self, file):
if os.path.exists(file):
return True
return False
def record_is_finished(self):
if self.load_status() == 'is_recording':
while True:
if self.load_status() == "record_finished":
self.audio_file = OWN_AUDIO_FILE
return True
return False
def load_status(self):
with open('/tmp/kalliope/record_status', 'r') as status:
return status.read()
def merge_audio(self):
sound1 = AudioSegment.from_wav(OWN_AUDIO_FILE)
sound2 = AudioSegment.from_wav(HOTWORD_FILE)
combined_sounds = sound2 + sound1
combined_sounds.export(OWN_AUDIO_FILE, format="wav")
def start_processing(self):
"""
A method to start the thread
"""
self.start()
def set_callback(self, callback):
"""
set the callback method that will receive the audio stream caught by the microphone
:param callback: callback method
:return:
"""
self.callback = callback
class SpeechRecorder(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
sl = SettingLoader()
self.settings = sl.settings # To set the multiplier and energy_ratio in settings.yml, it need to be set in models/settings etc.
def run(self):
#responsive = OwnSpeech.ResponsiveRecognizer(multiplier=self.settings.options.ownspeech_energy_ratio,
# energy_ratio=self.settings.options.ownspeech_multiplier)
responsive = OwnSpeech.ResponsiveRecognizer(multiplier=1.0,
energy_ratio=1.5)
mic = OwnSpeech.MutableMicrophone()
Utils.print_success("[SpeechRecorder] Listening...")
self.write_status("is_recording")
with mic as source:
audio_data = responsive.listen(source)
with open(OWN_AUDIO_FILE, 'wb') as file:
file.write(audio_data.get_wav_data())
self.write_status("record_finished")
def write_status(self, status):
with open('/tmp/kalliope/record_status', 'w') as record_status:
record_status.write(status)