davabase / whisper_real_time

Real time transcription with OpenAI Whisper.
2.32k stars 393 forks source link

Transcribe from sound card #10

Open shoegazerstella opened 1 year ago

shoegazerstella commented 1 year ago

Hi all, Thank you for this implementation. I would like to transcribe from the soundcard, so I would need to specify here a different source.

This is the list of my mic devices:

Microphone with name "MacBook Pro Microphone" found for `Microphone(device_index=1)`
Microphone with name "MacBook Pro Speakers" found for `Microphone(device_index=2)`

So I am adding:

source = sr.Microphone(sample_rate=16000, device_index=2)

but I get the following error:

Traceback (most recent call last):
  File "transcribe_demo_soundcard.py", line 79, in main
    recorder.adjust_for_ambient_noise(source)
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/speech_recognition/__init__.py", line 383, in adjust_for_ambient_noise
    assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
AssertionError: Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "transcribe_demo_soundcard.py", line 155, in <module>
    main()
  File "transcribe_demo_soundcard.py", line 79, in main
    recorder.adjust_for_ambient_noise(source)
  File "/opt/anaconda3/envs/py38/lib/python3.8/site-packages/speech_recognition/__init__.py", line 189, in __exit__
    self.stream.close()
AttributeError: 'NoneType' object has no attribute 'close'

Any clue why? Thanks!

davabase commented 1 year ago

Looking at the error, the with statement is failing because source is None. Either the line where you set the source was never executed or sr.Microphone() returned None because of an error.

davabase commented 1 year ago

Your sound card probably doesn't support being used as a microphone directly. Sometimes Windows will show speakers along with microphones, but the speakers can't be used for recording. You can try using Stereo Mix or a third part virtual cable like vb-audio. See this post also: https://github.com/davabase/transcriber_app/issues/11

NicoLiendro14 commented 1 year ago

Hello everyone, I developed a solution so that I can take the microphone or audio from the system using a modified version of PyAudio. I understand that on Mac it could be different, I could help you by modifying this lib and demo.

martinshkreli commented 1 year ago

so do it lol

g-l-i-t-c-h-o-r-s-e commented 1 year ago

Hello everyone, I developed a solution so that I can take the microphone or audio from the system using a modified version of PyAudio. I understand that on Mac it could be different, I could help you by modifying this lib and demo.

pls post it

wprudencio commented 1 year ago

I'm recording direct from the sound card in Mac Os. I'm using https://github.com/ExistentialAudio/BlackHole

After setting this up, use the code below to list all sound devices.

import pyaudio
audio = pyaudio.PyAudio()
device_count = audio.get_device_count()
for i in range(device_count):
    device_info = audio.get_device_info_by_index(i)
    print(f"Device {i}: {device_info['name']}")
audio.terminate()

The output will be like that.

Device 0: BlackHole 2ch
Device 1: External Microphone
Device 2: External Headphones
Device 3: MacBook Pro Microphone
Device 4: MacBook Pro Speakers
Device 5: Multi-Output Device

Search for sr.Microphone line and add the BlackHole device_index

Screenshot 2023-07-04 at 13 24 05