Skip to content

Commit

Permalink
silence stderr for cython audio recording
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-huan authored and eshrh committed Aug 10, 2022
1 parent ca57285 commit 0e41a1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion contrib/cython/src/crecord.pyx
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions contrib/cython/src/fileio.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cdef extern from "<fcntl.h>":
int open(const char *pathname, int flags)
int close(int fd)
enum:
O_RDONLY
O_WRONLY
O_RDWR

cdef extern from "<unistd.h>":
int dup(int oldfd)
int dup2(int oldfd, int newfd)

0 comments on commit 0e41a1c

Please sign in to comment.