tempfile.mkstemp to io.BytesIO()
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import io
|
||||
import os
|
||||
from io import BytesIO
|
||||
import tempfile
|
||||
import threading
|
||||
import wave
|
||||
@@ -8,7 +8,7 @@ from datetime import timedelta
|
||||
import pyaudiowpatch as pyaudio
|
||||
|
||||
PHRASE_TIMEOUT = 3.05
|
||||
MAX_PHRASES = 5
|
||||
MAX_PHRASES = 2
|
||||
|
||||
class AudioTranscriber:
|
||||
def __init__(self, speaker, source, language):
|
||||
@@ -34,14 +34,15 @@ class AudioTranscriber:
|
||||
|
||||
text = ''
|
||||
try:
|
||||
fd, path = tempfile.mkstemp(suffix=".wav")
|
||||
os.close(fd)
|
||||
audio_data = self.audio_sources["process_data_func"](path)
|
||||
# fd, path = tempfile.mkstemp(suffix=".wav")
|
||||
# os.close(fd)
|
||||
audio_data = self.audio_sources["process_data_func"]()
|
||||
text = self.audio_recognizer.recognize_google(audio_data, language=self.language)
|
||||
except Exception as e:
|
||||
pass
|
||||
finally:
|
||||
os.unlink(path)
|
||||
pass
|
||||
# os.unlink(path)
|
||||
|
||||
if text != '':
|
||||
self.update_transcript(text)
|
||||
@@ -61,14 +62,16 @@ class AudioTranscriber:
|
||||
audio_data = sr.AudioData(self.audio_sources["last_sample"], self.audio_sources["sample_rate"], self.audio_sources["sample_width"])
|
||||
return audio_data
|
||||
|
||||
def process_speaker_data(self, path):
|
||||
with wave.open(path, 'wb') as wf:
|
||||
def process_speaker_data(self):
|
||||
temp_file = io.BytesIO()
|
||||
with wave.open(temp_file, 'wb') as wf:
|
||||
wf.setnchannels(self.audio_sources["channels"])
|
||||
p = pyaudio.PyAudio()
|
||||
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
|
||||
wf.setframerate(self.audio_sources["sample_rate"])
|
||||
wf.writeframes(self.audio_sources["last_sample"])
|
||||
with sr.AudioFile(path) as source:
|
||||
temp_file.seek(0)
|
||||
with sr.AudioFile(temp_file) as source:
|
||||
audio = self.audio_recognizer.record(source)
|
||||
return audio
|
||||
|
||||
@@ -84,6 +87,7 @@ class AudioTranscriber:
|
||||
transcript[0] = text
|
||||
|
||||
def get_transcript(self):
|
||||
print(self.transcript_data)
|
||||
if len(self.transcript_data) > 0:
|
||||
text = self.transcript_data.pop(-1)
|
||||
else:
|
||||
|
||||
53
test_main.py
53
test_main.py
@@ -5,33 +5,36 @@ import AudioTranscriber
|
||||
import AudioRecorder
|
||||
import audio_utils
|
||||
|
||||
mic_audio_queue = queue.Queue()
|
||||
mic_device = audio_utils.get_default_input_device()
|
||||
mic_audio_recorder = AudioRecorder.SelectedMicRecorder(mic_device)
|
||||
mic_audio_recorder.record_into_queue(mic_audio_queue)
|
||||
try:
|
||||
mic_audio_queue = queue.Queue()
|
||||
mic_device = audio_utils.get_default_input_device()
|
||||
mic_audio_recorder = AudioRecorder.SelectedMicRecorder(mic_device)
|
||||
mic_audio_recorder.record_into_queue(mic_audio_queue)
|
||||
|
||||
mic_transcriber = AudioTranscriber.AudioTranscriber(speaker=False, source=mic_audio_recorder.source, language="ja-JP")
|
||||
mic_transcribe = threading.Thread(target=mic_transcriber.transcribe_audio_queue, args=(mic_audio_queue,))
|
||||
mic_transcribe.daemon = True
|
||||
mic_transcribe.start()
|
||||
mic_transcriber = AudioTranscriber.AudioTranscriber(speaker=False, source=mic_audio_recorder.source, language="ja-JP")
|
||||
mic_transcribe = threading.Thread(target=mic_transcriber.transcribe_audio_queue, args=(mic_audio_queue,))
|
||||
mic_transcribe.daemon = True
|
||||
mic_transcribe.start()
|
||||
|
||||
time.sleep(2)
|
||||
time.sleep(2)
|
||||
|
||||
spk_audio_queue = queue.Queue()
|
||||
spk_device = audio_utils.get_default_output_device()
|
||||
spk_audio_recorder = AudioRecorder.SelectedSpeakerRecorder(spk_device)
|
||||
spk_audio_recorder.record_into_queue(spk_audio_queue)
|
||||
spk_audio_queue = queue.Queue()
|
||||
spk_device = audio_utils.get_default_output_device()
|
||||
spk_audio_recorder = AudioRecorder.SelectedSpeakerRecorder(spk_device)
|
||||
spk_audio_recorder.record_into_queue(spk_audio_queue)
|
||||
|
||||
spk_transcriber = AudioTranscriber.AudioTranscriber(speaker=True, source=spk_audio_recorder.source, language="ja-JP")
|
||||
spk_transcribe = threading.Thread(target=spk_transcriber.transcribe_audio_queue, args=(spk_audio_queue,))
|
||||
spk_transcribe.daemon = True
|
||||
spk_transcribe.start()
|
||||
spk_transcriber = AudioTranscriber.AudioTranscriber(speaker=True, source=spk_audio_recorder.source, language="ja-JP")
|
||||
spk_transcribe = threading.Thread(target=spk_transcriber.transcribe_audio_queue, args=(spk_audio_queue,))
|
||||
spk_transcribe.daemon = True
|
||||
spk_transcribe.start()
|
||||
|
||||
while True:
|
||||
text = mic_transcriber.get_transcript()
|
||||
if len(text) > 0:
|
||||
print("mic:", text)
|
||||
text = spk_transcriber.get_transcript()
|
||||
if len(text) > 0:
|
||||
print("spk:", text)
|
||||
time.sleep(0.1)
|
||||
while True:
|
||||
text = mic_transcriber.get_transcript()
|
||||
if len(text) > 0:
|
||||
print("mic:", text)
|
||||
text = spk_transcriber.get_transcript()
|
||||
if len(text) > 0:
|
||||
print("spk:", text)
|
||||
time.sleep(0.1)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
Reference in New Issue
Block a user