Advertisement
Guest User

two_side_transcription

a guest
May 29th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. import threading
  2. import soundcard as sc
  3. import azure.cognitiveservices.speech as speechsdk
  4. import queue
  5. import warnings
  6. from dotenv import load_dotenv
  7. import os
  8.  
  9. # Suppress the SoundcardRuntimeWarning
  10. warnings.filterwarnings("ignore", category=sc.SoundcardRuntimeWarning)
  11.  
  12. # Your Azure subscription key and region
  13. load_dotenv()
  14.  
  15. audio_key = os.getenv("audio_key")
  16. audio_region = os.getenv("audio_region")
  17.  
  18. # Queue for passing audio data between threads
  19. mic_queue = queue.Queue()
  20. speaker_queue = queue.Queue()
  21.  
  22. # Function to capture microphone audio
  23. def capture_mic_audio(mic_queue):
  24.     mic = sc.get_microphone(id=str(sc.default_microphone().name))
  25.     with mic.recorder(samplerate=48000) as mic_recorder:
  26.         while True:
  27.             data = mic_recorder.record(numframes=1024)
  28.             mic_queue.put(data)
  29.  
  30.  
  31. # Function to capture speaker audio
  32. def capture_speaker_audio(speaker_queue):
  33.     speaker = sc.get_microphone(id=str(sc.default_speaker().name), include_loopback=True)
  34.     with speaker.recorder(samplerate=48000) as speaker_recorder:
  35.         while True:
  36.             data = speaker_recorder.record(numframes=1024)
  37.             speaker_queue.put(data)
  38.  
  39. # Function to create an audio input stream for Azure Speech SDK
  40. def create_audio_input_stream(audio_queue):
  41.     class AudioInputStream(speechsdk.audio.PullAudioInputStreamCallback):
  42.         def __init__(self):
  43.             super().__init__()
  44.        
  45.         def read(self, buffer):
  46.             try:
  47.                 data = audio_queue.get(block=False)
  48.                 buffer[:len(data)] = data
  49.                 return len(data)
  50.             except queue.Empty:
  51.                 return 0
  52.  
  53.         def close(self):
  54.             pass
  55.  
  56.     return speechsdk.audio.AudioConfig(stream=speechsdk.audio.PullAudioInputStream(AudioInputStream()))
  57.  
  58. # Function to start Azure speech recognition
  59. def start_recognition(audio_config, speech_config, name):
  60.     transcriber = speechsdk.transcription.ConversationTranscriber(speech_config=speech_config, audio_config=audio_config)
  61.     print("starting recognition")
  62.  
  63.     def transcribed(evt):
  64.         print(f"{name} transcribed!")
  65.  
  66.     def transcribing(evt):
  67.         print(f"{name} transcribed!")
  68.    
  69.     transcriber.transcribed.connect(lambda evt: transcribed(evt))
  70.     transcriber.transcribing.connect(lambda evt: transcribing(evt))
  71.     transcriber.start_transcribing_async()
  72.     print("started transcirbing")
  73.     return transcriber
  74.  
  75. # Main function
  76. def main():
  77.     speech_config = speechsdk.SpeechConfig(subscription=audio_key, region=audio_region)
  78.     speech_config.speech_recognition_language = "en-US"
  79.  
  80.     # Start capturing audio
  81.     threading.Thread(target=capture_mic_audio, args=(mic_queue,), daemon=True).start()
  82.     threading.Thread(target=capture_speaker_audio, args=(speaker_queue,), daemon=True).start()
  83.  
  84.     # Create audio input streams
  85.     mic_audio_input = create_audio_input_stream(mic_queue)
  86.     speaker_audio_input = create_audio_input_stream(speaker_queue)
  87.  
  88.     # Start speech recognizers
  89.     mic_transcriber= start_recognition(mic_audio_input, speech_config, "Microphone")
  90.     speaker_transcriber = start_recognition(speaker_audio_input, speech_config, "Speaker")
  91.  
  92.     # Keep the program running to process transcriptions
  93.     try:
  94.         while True:
  95.             pass
  96.     except KeyboardInterrupt:
  97.         mic_transcriber.stop_transcribing_async()
  98.         speaker_transcriber.stop_transcribing_async()
  99.  
  100. if __name__ == "__main__":
  101.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement