From 0e41a1cd304013c3ec7ecfa2f5efbb6004c92ac5 Mon Sep 17 00:00:00 2001 From: Stephen Huan Date: Wed, 10 Aug 2022 02:47:45 -0400 Subject: [PATCH] silence stderr for cython audio recording --- contrib/cython/src/crecord.pyx | 16 +++++++++++++++- contrib/cython/src/fileio.pxd | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 contrib/cython/src/fileio.pxd diff --git a/contrib/cython/src/crecord.pyx b/contrib/cython/src/crecord.pyx index 0dddca6..2757e71 100644 --- a/contrib/cython/src/crecord.pyx +++ b/contrib/cython/src/crecord.pyx @@ -1,5 +1,6 @@ # cython: profile=False from libc.stdlib cimport malloc, free +from fileio cimport open as c_open, close, dup, dup2, O_WRONLY from cysignals.signals cimport ( sig_on, sig_on_no_except, sig_off, cython_check_exception, ) @@ -196,10 +197,23 @@ def record(sample_rate: float, cdef: PaDeviceIndex device const PaDeviceInfo *info - int channels + int channels, stderr_copy, devnull PaTime latency + # temporarily silence stderr: https://stackoverflow.com/questions/5081657/ + import sys, os + stderr_copy = dup(2) + devnull = c_open("/dev/null", O_WRONLY) + dup2(devnull, 2) + close(devnull) + # allow Python to still write to stderr + sys.stderr = os.fdopen(stderr_copy, "w") + # portaudio initialization can be noisy __raise_error(Pa_Initialize()) + # restore original stderr + dup2(stderr_copy, 2) + close(stderr_copy) + sys.stderr = sys.__stderr__ device = get_device(device_name) info = Pa_GetDeviceInfo(device) diff --git a/contrib/cython/src/fileio.pxd b/contrib/cython/src/fileio.pxd new file mode 100644 index 0000000..0fa6b4a --- /dev/null +++ b/contrib/cython/src/fileio.pxd @@ -0,0 +1,12 @@ +cdef extern from "": + int open(const char *pathname, int flags) + int close(int fd) + enum: + O_RDONLY + O_WRONLY + O_RDWR + +cdef extern from "": + int dup(int oldfd) + int dup2(int oldfd, int newfd) +