Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bus error when using the reduce_noise in PyAudio streaming callback function #87

Open
yujie-tao opened this issue Jul 24, 2023 · 8 comments

Comments

@yujie-tao
Copy link

Hi

I hope to do real-time de-noising of microphone input, and when running the reduce_noise function (v2 and v3), I am experiencing bus error (on the Mac M1 system). Do we have any pointers on why this is happening?

The function works smoothly when processing a local file or moving the function outside of the callback function.

def callback(in_data, frame_count, time_info, flag):
    # using Numpy to convert to array for processing
    audio_data = np.hstack(np.frombuffer(in_data, dtype=np.int16))
    audio_data = np.ndarray.astype(audio_data, float) / 32768
    reduced_noise = nr.reduce_noise(
        y=audio_data,
        sr=RATE,
    )

    return reduced_noise, pyaudio.paContinue
stream = p.open(
    format=pyaudio.paInt16,
    channels=CHANNELS,
    rate=RATE,
    output=True,
    input=True,
    input_device_index=0,
    output_device_index=1,
    frames_per_buffer=16000,
    stream_callback=callback,
)
@timsainb
Copy link
Owner

Can you paste the error you get? If it’s just one audio stream you shouldn’t be running into bus issues. Maybe the channels and samples are transposed in streaming and so the algorithm thinks there’s 16k channels

@yujie-tao
Copy link
Author

Thanks for the response!

In the terminal, the only error message I got was [1] 57294 bus error python3 stream-noise-cancel.py

I also got more detail reports here after the error:

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               Python [57342]
Path:                  /opt/homebrew/*/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS/Python
Identifier:            org.python.python
Version:               3.9.13 (3.9.13)
Code Type:             ARM-64 (Native)
Parent Process:        zsh [35530]
Responsible:           iTerm2 [620]
User ID:               501

Date/Time:             2023-07-26 14:50:44.0138 -0600
OS Version:            macOS 12.5 (21G72)
Report Version:        12

Time Awake Since Boot: 260000 seconds
Time Since Wake:       3428 seconds

System Integrity Protection: enabled

Crashed Thread:        20  com.apple.audio.IOThread.client

Exception Type:        EXC_BAD_ACCESS (SIGBUS)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00000001701e3ff8
Exception Codes:       0x0000000000000002, 0x00000001701e3ff8
Exception Note:        EXC_CORPSE_NOTIFY

Termination Reason:    Namespace SIGNAL, Code 10 Bus error: 10
Terminating Process:   exc handler [57342]

Also, I found it seem to be a Mac issue? I run the same code on a Windows laptop and it works.

@timsainb
Copy link
Owner

Hm... do you get the same error when you remove noisereduce entirely from the callback? If so you would want to cross post this to pyaudio

@yujie-tao
Copy link
Author

I don't get this error when moving noisereduce outside of the callback function.

@timsainb
Copy link
Owner

timsainb commented Jul 26, 2023 via email

@yujie-tao
Copy link
Author

Sorry about the confusion!

In both cases you mentioned:
(1) running noisereduce independently outside of the callback function

(2) running the callback function without noisereduce in the code at all

It doesn't run into bus errors.

@timsainb
Copy link
Owner

timsainb commented Jul 27, 2023 via email

@karpp
Copy link

karpp commented Aug 4, 2023

Hello,

I've run into same issue, but using sounddevice library instead of pyaudio. I guess there's some conflict with portaudio because both libraries provide bindings for it.

My environment and error are equal to the ones mentioned by Yujie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants