1

I am 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 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

chikitin
  • 762
  • 6
  • 28

0 Answers0