Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

import threading import soundcard as sc import azure.cognitiveservices.speech as…

  1. User

    import threading import soundcard as sc import azure.cognitiveservices.speech as speechsdk import queue import warnings

    Suppress the SoundcardRuntimeWarning

    warnings.filterwarnings("ignore", category=sc.SoundcardRuntimeWarning)

    Your Azure subscription key and region

    audio_key = 'YOUR_AZURE_SUBSCRIPTION_KEY' audio_region = 'YOUR_AZURE_REGION'

    Queue for passing audio data between threads

    mic_queue = queue.Queue() speaker_queue = queue.Queue()

    Function to capture microphone audio

    def capture_mic_audio(mic_queue): mic = sc.get_microphone(id=str(sc.default_microphone().name)) with mic.recorder(samplerate=48000) as mic_recorder: while True: data = mic_recorder.record(numframes=1024) mic_queue.put(data)

    Function to capture speaker audio

    def capture_speaker_audio(speaker_queue): speaker = sc.get_microphone(id=str(sc.default_speaker().name), include_loopback=True) with speaker.recorder(samplerate=48000) as speaker_recorder: while True: data = speaker_recorder.record(numframes=1024) speaker_queue.put(data)

    Function to create an audio input stream for Azure Speech SDK

    def create_audio_input_stream(audio_queue): class AudioInputStream(speechsdk.audio.PullAudioInputStreamCallback): def init(self): super().init()

        def read(self, buffer, size):
            try:
                data = audio_queue.get(block=False)
                buffer[:len(data)] = data
                return len(data)
            except queue.Empty:
                return 0
    
        def close(self):
            pass
    
    return speechsdk.audio.AudioConfig(stream=speechsdk.audio.PullAudioInputStream(AudioInputStream()))
    

    Function to start Azure speech recognition

    def start_recognition(audio_config, speech_config, name): recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    def recognized(evt):
        if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
            print(f"{name} recognized: {evt.result.text}")
        elif evt.result.reason == speechsdk.ResultReason.NoMatch:
            print(f"{name} recognized: No speech could be recognized")
        elif evt.result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = evt.result.cancellation_details
            print(f"{name} recognized: Canceled: {cancellation_details.reason}")
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                print(f"Error details: {cancellation_details.error_details}")
    
    recognizer.recognized.connect(recognized)
    recognizer.start_continuous_recognition_async()
    return recognizer
    

    Main function

    def main(): speech_config = speechsdk.SpeechConfig(subscription=audio_key, region=audio_region) speech_config.speech_recognition_language = "en-US"

    # Start capturing audio
    threading.Thread(target=capture_mic_audio, args=(mic_queue,), daemon=True).start()
    threading.Thread(target=capture_speaker_audio, args=(speaker_queue,), daemon=True).start()
    
    # Create audio input streams
    mic_audio_input = create_audio_input_stream(mic_queue)
    speaker_audio_input = create_audio_input_stream(speaker_queue)
    
    # Start speech recognizers
    mic_recognizer = start_recognition(mic_audio_input, speech_config, "Microphone")
    speaker_recognizer = start_recognition(speaker_audio_input, speech_config, "Speaker")
    
    # Keep the program running to process transcriptions
    try:
        while True:
            pass
    except KeyboardInterrupt:
        mic_recognizer.stop_continuous_recognition_async()
        speaker_recognizer.stop_continuous_recognition_async()
    

    if name == "main": main() CAN we do this without using api ....means can we do this with librraies

  2. Assistant

    Yes, you can perform speech recognition without relying on cloud services like Azure's Speech API. There are several libraries available that allow you to perform speech recognition entirely on your local machine. One popular library is SpeechRecognition, which works well with various speech recognition engines, including Google's Web Speech API, CMU Sphinx (PocketSphinx), and others.

    Below is an example of how you could modify the program to use the SpeechRecognition library and the pyaudio library for capturing audio from the microphone and speaker:

    1. Install the necessary libraries:
    pip install SpeechRecognition pyaudio
    
    1. Modify the code to use SpeechRecognition for performing speech recognition locally:
    import pyaudio
    import wave
    import speech_recognition as sr
    import threading
    import queue
    
    # Queue for passing audio data between threads
    mic_queue = queue.Queue()
    speaker_queue = queue.Queue()
    
    # Function to capture microphone audio
    def capture_mic_audio(mic_queue):
        r = sr.Recognizer()
        # Set up the microphone
        with sr.Microphone() as source:
            while True:
                # Capture audio from the microphone
                audio = r.listen(source)
                mic_queue.put(audio)
    
    # Function to recognize speech from audio
    def recognize_speech_from_queue(mic_queue):
        r = sr.Recognizer()
        while True:
            # Get audio from queue
            audio_data = mic_queue.get()
            try:
                # Recognize speech using Google Web Speech API
                text = r.recognize_google(audio_data)
                print(f"Microphone recognized: {text}")
            except sr.UnknownValueError:
                print("Microphone could not understand audio")
            except sr.RequestError as e:
                print(f"Could not request results; {e}")
    
    # Main function
    def main():
        # Start capturing audio
        threading.Thread(target=capture_mic_audio, args=(mic_queue,), daemon=True).start()
        
        # Start speech recognition in a separate thread
        threading.Thread(target=recognize_speech_from_queue, args=(mic_queue,), daemon=True).start()
    
        # Keep the program running to process transcriptions
        try:
            while True:
                pass
        except KeyboardInterrupt:
            print("Program terminated.")
    
    if __name__ == "__main__":
        main()
    

    Explanation of the Code

    • This code captures audio from the default microphone using the SpeechRecognition library.
    • The audio data is pushed to a queue for processing in a separate thread.
    • The speech is recognized using the Google Web Speech API, and the recognized text is printed to the console.
    • You can extend this code to capture audio from the speakers using a similar method or by configuring your system's audio settings to enable loopback (if your sound card supports it).

    Important Notes

    1. Internet Requirement: The example uses Google's Web Speech API, which requires an internet connection. If you want offline capabilities, consider using the CMU Sphinx (PocketSphinx) engine, which can be used for local recognition.
    2. Audio Quality: Make sure to adjust your microphone settings for optimal audio input quality.
    3. Threading: Care should be taken when using threads to handle audio input and speech recognition asynchronously.

    With the changes above, you should be able to run a basic speech recognition program without using an API like Azure's.

  3. User

    can u add some straemlit interface to this

  4. Assistant

    Certainly! Below is an example of how you can integrate the previous speech recognition functionality with a Streamlit interface. This will allow you to capture audio from the microphone and display the recognized text on a web interface.

    First, ensure that you have Streamlit installed:

    pip install streamlit SpeechRecognition pyaudio
    

    Complete Code for Streamlit Speech Recognition

    Here's how to set up a Streamlit app that captures microphone audio and displays the recognized text:

    import streamlit as st
    import pyaudio
    import speech_recognition as sr
    import threading
    import queue
    
    # Queue for passing audio data between threads
    mic_queue = queue.Queue()
    
    # Function to capture microphone audio
    def capture_mic_audio(mic_queue):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            st.write("Adjusting for ambient noise... Please wait.")
            r.adjust_for_ambient_noise(source)
            st.write("Listening...")
            while True:
                # Capture audio from the microphone
                audio = r.listen(source)
                mic_queue.put(audio)
    
    # Function to recognize speech from audio
    def recognize_speech_from_queue(mic_queue):
        r = sr.Recognizer()
        while True:
            # Get audio from queue
            audio_data = mic_queue.get()
            try:
                # Recognize speech using Google Web Speech API
                text = r.recognize_google(audio_data)
                st.session_state.recognized_text = text
                st.write(f"Microphone recognized: {text}")
            except sr.UnknownValueError:
                st.write("Microphone could not understand audio")
            except sr.RequestError as e:
                st.write(f"Could not request results; {e}")
    
    # Main Streamlit app
    def main():
        st.title("Speech Recognition App")
        st.write("Press the button and start speaking...")
    
        # Initialize session state variable
        if 'recognized_text' not in st.session_state:
            st.session_state.recognized_text = ""
    
        # Button to start the recognition
        if st.button("Start Listening"):
            st.write("Listening... Speak now.")
            
            # Start capturing audio
            threading.Thread(target=capture_mic_audio, args=(mic_queue,), daemon=True).start()
          
            # Start speech recognition in a separate thread
            threading.Thread(target=recognize_speech_from_queue, args=(mic_queue,), daemon=True).start()
    
        # Display recognized text
        st.write("Recognized Text:")
        st.write(st.session_state.recognized_text)
    
    if __name__ == "__main__":
        main()
    

    Explanation of the Code:

    1. Streamlit Interface: The app uses Streamlit to create a simple web interface where users can interact with the speech recognition system.
    2. Button to Start Listening: When the user clicks the "Start Listening" button, the app will begin capturing audio from the microphone.
    3. Audio Capture and Recognition: Audio is processed in separate threads. Captured audio is placed in a queue, from which the recognition thread retrieves it for analysis.
    4. Session State: Streamlit’s session state is used to keep track of the recognized text across interactions.

    Running the Streamlit App:

    To run your Streamlit app, save the code in a file named app.py (or any name you choose), and then use the following command in your terminal:

    streamlit run app.py
    

    This will start a local web server and open the Streamlit app in your web browser, allowing you to capture audio and see the recognized text.

    Notes:

    • Make sure your microphone is set up correctly and is the default input device on your system.
    • The app uses Google Web Speech API for recognition, which requires an internet connection.
    • This is a basic example; you may want to add additional error handling, user notifications, and features to enhance usability.
  5. User

    add some stop button also

  6. Assistant

    Certainly! To add a "Stop" button to the Streamlit application and gracefully stop the audio capture and recognition process, you can modify the code to include a mechanism for stopping the threads. We'll use a simple boolean flag to control when to stop the audio capturing thread.

    Here's the modified code with a "Stop Listening" button:

    import streamlit as st
    import pyaudio
    import speech_recognition as sr
    import threading
    import queue
    import time
    
    # Queue for passing audio data between threads
    mic_queue = queue.Queue()
    stop_flag = threading.Event()  # Event to signal stopping the threads
    
    # Function to capture microphone audio
    def capture_mic_audio(mic_queue, stop_flag):
        r = sr.Recognizer()
        with sr.Microphone() as source:
            st.write("Adjusting for ambient noise... Please wait.")
            r.adjust_for_ambient_noise(source)
            st.write("Listening...")
            while not stop_flag.is_set():
                try:
                    # Capture audio from the microphone
                    audio = r.listen(source, timeout=5)  # Timeout to avoid blocking
                    mic_queue.put(audio)
                except Exception as e:
                    st.write(f"Error capturing audio: {e}")
    
    # Function to recognize speech from audio
    def recognize_speech_from_queue(mic_queue, stop_flag):
        r = sr.Recognizer()
        while not stop_flag.is_set():
            # Get audio from queue
            if not mic_queue.empty():
                audio_data = mic_queue.get()
                try:
                    # Recognize speech using Google Web Speech API
                    text = r.recognize_google(audio_data)
                    st.session_state.recognized_text = text
                    st.write(f"Microphone recognized: {text}")
                except sr.UnknownValueError:
                    st.write("Microphone could not understand audio")
                except sr.RequestError as e:
                    st.write(f"Could not request results; {e}")
    
    # Main Streamlit app
    def main():
        st.title("Speech Recognition App")
        st.write("Press the button and start speaking...")
    
        # Initialize session state variable
        if 'recognized_text' not in st.session_state:
            st.session_state.recognized_text = ""
    
        # Buttons to start and stop the recognition
        if st.button("Start Listening"):
            st.write("Listening... Speak now.")
            
            # Reset the stop flag
            stop_flag.clear()
    
            # Start capturing audio
            threading.Thread(target=capture_mic_audio, args=(mic_queue, stop_flag), daemon=True).start()
          
            # Start speech recognition in a separate thread
            threading.Thread(target=recognize_speech_from_queue, args=(mic_queue, stop_flag), daemon=True).start()
    
        if st.button("Stop Listening"):
            st.write("Stopping...")
            stop_flag.set()  # Signal the threads to stop
            st.session_state.recognized_text = ""  # Optionally reset recognized text
    
        # Display recognized text
        st.write("Recognized Text:")
        st.write(st.session_state.recognized_text)
        
        if stop_flag.is_set():
            st.write("Listening stopped.")
    
    if __name__ == "__main__":
        main()
    

    Key Changes:

    1. Stop Flag: A threading.Event() is used as a flag to signal whether the capturing and recognition threads should stop running.
    2. Capture and Recognition Loops: Both functions check for the stop_flag status in their while loops, allowing them to exit when the user decides to stop.
    3. Stop Listening Button: Added a "Stop Listening" button that sets the stop_flag, signaling the threads to halt their operation gracefully.
    4. User Feedback: A simple message is displayed when the listening stops.

    To Run the App:

    Save this code in a file named app.py and run the Streamlit application with:

    streamlit run app.py
    

    When you press "Start Listening," your application will start capturing audio. Pressing "Stop Listening" will stop the audio capture and recognition, allowing you to manage the listening session directly from the Streamlit interface.