bastibe/SoundCard

Record and send to SpeechRecogintion

Closed this issue · 1 comments

How to convert variable data to audio_data?
import soundcard as sc
import numpy
import speech_recognition as sr
r = sr.Recognizer()
speakers = sc.all_speakers()
mics = sc.all_microphones()
print(speakers)
print(mics)
default_mic = sc.get_microphone('2- High Definition Audio Device', include_loopback=True)
#print(default_mic)
data = default_mic.record(samplerate=48000, numframes=None)
with default_mic.recorder(samplerate=48000) as mic:
for _ in range(100):
data = mic.record(numframes=1024)
print(r.recognize_google(data))

Throw error
line 822, in recognize_google
assert isinstance(audio_data, AudioData), "audio_data must be audio data"
AssertionError: audio_data must be audio data

EDIT
i found the solution like this

but it takes much time to write and transcribe :'v

import soundcard as sc
import soundfile as sf
import numpy
import time
from scipy.io import wavfile
import speech_recognition as sr

recognizer = sr.Recognizer()

default_mic = sc.get_microphone('2- High Definition Audio Device', include_loopback=True)
record_object=default_mic.recorder(samplerate=44100)

print(record_object)

seconds=0.1
numframes=int(44100*seconds)
#30s
max_iterations=300
expected_time_array=numpy.arange(seconds,(max_iterations+1)*seconds,seconds)

my_audio_streaming=[]

with record_object as r:
start_time=time.time()
for number in range(max_iterations):
data=r.record(numframes)
my_audio_streaming.extend(data)
#sf.write('pysoundcard_record_continously_{}.wav'.format(number), data, 44100, 'PCM_32') # add more seconds, not recommended
now=time.time()
npArrrr=numpy.array(my_audio_streaming)
sf.write('sfsdfd.wav',npArrrr, 44100)
print("audio saved")
harvard = sr.AudioFile('sfsdfd.wav')
with harvard as source:
audio = recognizer.record(source)
txt = recognizer.recognize_google(audio, language="en-US")
print(txt)

That's an error in recognize_google, not soundcard. You'll have to ask in their repository.