spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
1.01k stars 147 forks source link

sounddevice.PortAudioError: Error opening InputStream: Device unavailable [PaErrorCode -9985] #173

Open demongolem opened 5 years ago

demongolem commented 5 years ago

When trying to record, I use the line

myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')

This leads to

> Traceback (most recent call last):
  File "sd_test.py", line 7, in <module>
    myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
  File "C:\Users\gwerner004\AppData\Local\Programs\Python\Python36\lib\site-packages\sounddevice.py", line 224, in rec
    ctx.input_dtype, callback, blocking, **kwargs)
  File "C:\Users\gwerner004\AppData\Local\Programs\Python\Python36\lib\site-packages\sounddevice.py", line 2417, in start_stream
    **kwargs)
  File "C:\Users\gwerner004\AppData\Local\Programs\Python\Python36\lib\site-packages\sounddevice.py", line 1301, in __init__
    **_remove_self(locals()))
  File "C:\Users\gwerner004\AppData\Local\Programs\Python\Python36\lib\site-packages\sounddevice.py", line 780, in __init__
    'Error opening {0}'.format(self.__class__.__name__))
  File "C:\Users\gwerner004\AppData\Local\Programs\Python\Python36\lib\site-packages\sounddevice.py", line 2572, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening InputStream: Device unavailable [PaErrorCode -9985]

When I do python -m sounddevice I get this line:

>  1 Microphone (Plantronics C320), MME (2 in, 0 out)

Why would this device not be available for recording?

demongolem commented 5 years ago

Upon further probing, even though the above line shows the headset microphone as the default and it is in fact the default for the Windows system I am using, the only device that works is

17 Jack Mic (Realtek HD Audio Front Mic input), Windows WDM-KS (2 in, 0 out)

If I used the plotline.py example for example which is in the repository and I unplug my headset, I see it run without error and pick up my voice.

Usually, I will have my headset plugged in so I can google hangout people online. It doesn't seem right that I have to try to find which device number is Jack Mic and then use that overwriting the default input device for my computer which works just fine on hangout.

mgeier commented 5 years ago

The error message and the behavior you are describing are directly provided by the underlying PortAudio library. There is nothing I can do in the sounddevice module to change that, any changes would have to be done in PortAudio itself.

You should probably ask about this on the PortAudio mailing list: http://portaudio.com/contacts.html

mgeier commented 5 years ago

@demongolem Are there any news on this issue?

demongolem commented 5 years ago

No, I did report to PortAudio, but there has been no response on that.

mgeier commented 5 years ago

OK, thanks for reporting this, could you please provide a link to the issue so we can follow it here?

demongolem commented 5 years ago

https://www.assembla.com/spaces/portaudio/tickets/293-portaudio-not-able-to-use-my-default-recording-device

mgeier commented 5 years ago

Thanks for the link!

You would probably reach more people (including PortAudio users, not only core developers) when you ask on the mailing list.

Probably there is somebody else who could try the Plantronics C320 on Windows/MME?

I guess this is the device: https://www.plantronics.com/us/en/product/blackwire-310-320

And just to make sure: This problem only occurs with this one device, right? Did you try other external USB devices?

Ananthu27 commented 4 years ago

i have the same problem too .

rkuchkarov commented 4 years ago

I have this problem too

mgeier commented 4 years ago

Did you ask on the PortAudio mailing list (http://portaudio.com/contacts.html)?

csarami commented 3 years ago

I guess same error. I am also on MacMojave and try to play a sound file if a microphone signal goes above a threshold. I have :

import sounddevice as sd
import soundfile as sf
import numpy as np
duration = 5  # seconds

threshold = 30
filename = "./data/piano.wav"
data, fs = sf.read(filename, dtype='float32')  

def print_sound(indata, outdata, frames, time, status):
    if status:
        print(status)
    global threshold, data, fs
    volume_norm = 5*np.linalg.norm(indata)
    if volume_norm > threshold: 
        print("so loud!!!!")
        sd.play(data, fs)

with sd.Stream(callback=print_sound):
    sd.sleep(1000*duration)

When I run this the sound goes above the threshold, however, it cannot open the output Stream as follows;

$python3 main.py 
input underflow
input underflow
input underflow
so loud!!!!
so loud!!!!
input overflow, output underflow
so loud!!!!
input overflow, output underflow
so loud!!!!
||PaMacCore (AUHAL)|| Error on line 1316: err=''nope'', msg=Audio Hardware: Illegal Operation
From cffi callback <function _StreamBase.__init__.<locals>.callback_ptr at 0x10caf8680>:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 881, in callback_ptr
    callback, idata, odata, frames, time, status)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2678, in _wrap_callback
    callback(*args)
  File "main.py", line 17, in print_sound
    sd.play(data, fs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 177, in play
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2578, in start_stream
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 1489, in __init__
    **_remove_self(locals()))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 895, in __init__
    'Error opening {}'.format(self.__class__.__name__))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2738, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening OutputStream: Internal PortAudio error [PaErrorCode -9986]

I have restarted my iMac ( Mojave) and still get the same error.

Any help would be much appreciated.

CS

otori334 commented 3 years ago

I guess same error too. macOS Big Sur 11.5 beta, sounddevice 0.4.1.

# Used to convert waveform data into a 2-byte numeric sequence
import wave as wave
import numpy as np
import sounddevice as sd

#Sample files
sample_wave_file="./CMU_ARCTIC/cmu_us_aew_arctic/wav/arctic_a0001.wav"

#Load a file
wav=wave.open(sample_wave_file)

#Reads waveform data in PCM format
data=wav.readframes(wav.getnframes())

#Convert data to a 2-byte numeric string
data=np.frombuffer(data, dtype=np.int16)

#Playback data
sd.play(data,wav.getframerate())

status = sd.wait()

When I run this;

(.env) otori334@OTRMBP SSS % python3 /Users/otori334/GitHub/SSS/playback.py            
||PaMacCore (AUHAL)|| Error on line 1316: err='-66748', msg=Unknown Error
Traceback (most recent call last):
  File "/Users/otori334/GitHub/SSS/playback.py", line 20, in <module>
    sd.play(data,wav.getframerate())
  File "/Users/otori334/GitHub/SSS/.env/lib/python3.9/site-packages/sounddevice.py", line 174, in play
    ctx.start_stream(OutputStream, samplerate, ctx.output_channels,
  File "/Users/otori334/GitHub/SSS/.env/lib/python3.9/site-packages/sounddevice.py", line 2573, in start_stream
    self.stream = StreamClass(samplerate=samplerate,
  File "/Users/otori334/GitHub/SSS/.env/lib/python3.9/site-packages/sounddevice.py", line 1488, in __init__
    _StreamBase.__init__(self, kind='output', wrap_callback='array',
  File "/Users/otori334/GitHub/SSS/.env/lib/python3.9/site-packages/sounddevice.py", line 892, in __init__
    _check(_lib.Pa_OpenStream(self._ptr, iparameters, oparameters,
  File "/Users/otori334/GitHub/SSS/.env/lib/python3.9/site-packages/sounddevice.py", line 2738, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening OutputStream: Internal PortAudio error [PaErrorCode -9986]
otori334 commented 3 years ago

My problem was solved with this method #299

brew install portaudio --HEAD

Thanks

staniko commented 2 years ago

stream.start()did the magic for me, before invoking stream.read().

warrenarea commented 1 year ago

sounddevice.PortAudioError: Error opening RawStream: Unanticipated host error [PaErrorCode -9999]: 'Undefined external error.' [MME error 1]

I resolved this by enabling "Let apps access your microphone" in Windows 10/11. a setting in Privacy & Security > Microphone

joachimpoutaraud commented 1 year ago

Uninstalling sounddevice from pip and reinstalling using conda worked for me

pip uninstall sounddevice
conda install -c conda-forge python-sounddevice
bfalik commented 1 year ago

Thank you, @warrenarea I tried it and now the problem is solved.

wnorthrup commented 15 hours ago

Uninstalling sounddevice from pip and reinstalling using conda worked for me

pip uninstall sounddevice
conda install -c conda-forge python-sounddevice

This worked for me. Thanks.