So I'm working on a chatbot like system on Ubuntu using google clouds (not gTTS) real time speech to text API, and whenever it speaks back to me, it'll hear itself and repeat the input. How would I go about solving this? Here is some of my code, for the life of me I cant figure this out. When I say something, it speaks back to me, then the computer hears it and creates and infinite loop. I've tried muting the mic through os.system, but it doesn't seem to work. I think my only option here may be something to do with threading? Any help would be great.
class MicrophoneStream(object):
def __init__(self, rate, chunk):
self._rate = rate
self._chunk = chunk
self._buff = queue.Queue()
self.closed = True
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=1,
rate=self._rate,
input=True,
frames_per_buffer=self._chunk,
stream_callback=self._fill_buffer,
)
self.closed = False
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
sleep(stop())
except queue.Empty:
break
yield b"".join(data)
def listen_print_loop(responses):
global chat_log
num_chars_printed = 0
for response in responses:
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
overwrite_chars = " " * (num_chars_printed - (len(transcript) - 1))
if not result.is_final:
flusher = transcript + overwrite_chars + "\r"
sys.stdout.write(flusher)
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
returnString = transcript + overwrite_chars
if returnString[0] == " ":
returnString = returnString[1:]
print(returnString)
if re.search(r"\b(Goodbye)\b", transcript, re.I):
print("Exiting..")
speak("I enjoyed our talk. Goodbye.")
break
question = transcript
answer = ask(question, chat_log)
chat_log = keepContext(question, answer, chat_log)
speak(answer)
print(answer)
num_chars_printed = 0