Skip to content

Commit 0e41a1c

Browse files
stephen-huaneshrh
authored andcommitted
silence stderr for cython audio recording
1 parent ca57285 commit 0e41a1c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

contrib/cython/src/crecord.pyx

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# cython: profile=False
22
from libc.stdlib cimport malloc, free
3+
from fileio cimport open as c_open, close, dup, dup2, O_WRONLY
34
from cysignals.signals cimport (
45
sig_on, sig_on_no_except, sig_off, cython_check_exception,
56
)
@@ -196,10 +197,23 @@ def record(sample_rate: float,
196197
cdef:
197198
PaDeviceIndex device
198199
const PaDeviceInfo *info
199-
int channels
200+
int channels, stderr_copy, devnull
200201
PaTime latency
201202

203+
# temporarily silence stderr: https://stackoverflow.com/questions/5081657/
204+
import sys, os
205+
stderr_copy = dup(2)
206+
devnull = c_open("/dev/null", O_WRONLY)
207+
dup2(devnull, 2)
208+
close(devnull)
209+
# allow Python to still write to stderr
210+
sys.stderr = os.fdopen(stderr_copy, "w")
211+
# portaudio initialization can be noisy
202212
__raise_error(Pa_Initialize())
213+
# restore original stderr
214+
dup2(stderr_copy, 2)
215+
close(stderr_copy)
216+
sys.stderr = sys.__stderr__
203217

204218
device = get_device(device_name)
205219
info = Pa_GetDeviceInfo(device)

contrib/cython/src/fileio.pxd

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cdef extern from "<fcntl.h>":
2+
int open(const char *pathname, int flags)
3+
int close(int fd)
4+
enum:
5+
O_RDONLY
6+
O_WRONLY
7+
O_RDWR
8+
9+
cdef extern from "<unistd.h>":
10+
int dup(int oldfd)
11+
int dup2(int oldfd, int newfd)
12+

0 commit comments

Comments
 (0)