notseriouss
notseriouss

Reputation: 31

Is it possible to stream system sounds (speaker output) and recognize text from it in python?

I have looked through a lot of libraries for working with sound and have not found anywhere information on how to work with system sound (speakers output) I only found how to record this system sound for example for 5 seconds or more, but I'm looking for a way to stream this sound and further recognize the text

Here is the code that I need, only instead of the microphone output, I need the system sound output (speaker output) when I put the speaker index, I get an error that such a channel does not exist (although I have viewed all channels with their indexes)

import speech_recognition

recognizer = speech_recognition.Recognizer()

while True:
    try:
        with speech_recognition.Microphone(device_index=1) as mic:

            recognizer.adjust_for_ambient_noise(mic, duration=0.5)
            audio = recognizer.listen(mic, timeout=0.1)

            text = recognizer.recognize_google(audio_data=audio, language='en-US')
            text = text.lower()
    except Exception as e:
        print(e)

The code with which I can write the output from the speakers to a file, but not stream them directly

import soundcard as sc
import soundfile as sf

samplerate = 48000
recorde_sec = 10

while True:
   with sc.get_microphone(id=str(sc.default_speaker().name), include_loopback=True).recorder(
           samplerate=samplerate) as mic:

       data = mic.record(numframes=samplerate * recorde_sec)
       sf.write(file='out.wav', data=data[:, 0], samplerate=samplerate)

Upvotes: 1

Views: 583

Answers (0)

Related Questions