bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

Record and send to SpeechRecogintion #131

Closed adeariazhari closed 3 years ago

adeariazhari commented 3 years ago

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 defaultmic.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_recordcontinously{}.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)
bastibe commented 3 years ago

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