Skip to content

Commit 0529a60

Browse files
committed
Remove logging, add get_status()
1 parent 4c16c86 commit 0529a60

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

examples/play_file.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
"""
77
import argparse
8+
import logging
89

910
parser = argparse.ArgumentParser(description=__doc__)
1011
parser.add_argument("filename", help="audio file to be played back")
@@ -15,7 +16,10 @@
1516
import sounddevice as sd
1617
import soundfile as sf
1718
data, fs = sf.read(args.filename, dtype='float32')
18-
sd.play(data, fs, device=args.device, blocking=True)
19+
sd.play(data, fs, device=args.device)
20+
status = sd.wait()
21+
if status:
22+
logging.warning(str(status))
1923
except BaseException as e:
2024
# This avoids printing the traceback, especially if Ctrl-C is used.
2125
raise SystemExit(str(e))

sounddevice.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import atexit as _atexit
2929
from cffi import FFI as _FFI
3030
import sys as _sys
31-
import logging as _logging
3231

3332
_ffi = _FFI()
3433
_ffi.cdef("""
@@ -440,6 +439,10 @@ def wait():
440439
If at least one buffer over-/underrun happened during the last
441440
playback/recording, a :class:`CallbackFlags` object is returned.
442441
442+
See Also
443+
--------
444+
get_status
445+
443446
"""
444447
global _last_callback
445448
if _last_callback:
@@ -460,6 +463,26 @@ def stop(ignore_errors=True):
460463
_last_callback.stream.close(ignore_errors)
461464

462465

466+
def get_status():
467+
"""Get information about over-/underflows in play()/rec()/playrec().
468+
469+
Returns
470+
-------
471+
CallbackFlags
472+
A :class:`CallbackFlags` object that holds information about the
473+
last invocation of :func:`play`, :func:`rec` or :func:`playrec`.
474+
475+
See Also
476+
--------
477+
wait
478+
479+
"""
480+
if _last_callback:
481+
return _last_callback.status
482+
else:
483+
raise RuntimeError("play()/rec()/playrec() was not called yet")
484+
485+
463486
def print_devices():
464487
"""Show information about all available audio devices.
465488
@@ -1749,12 +1772,15 @@ def __init__(self, flags=0x0):
17491772
self._flags = flags
17501773

17511774
def __repr__(self):
1752-
flags = ", ".join(name for name in dir(self)
1753-
if not name.startswith('_') and getattr(self, name))
1775+
flags = str(self)
17541776
if not flags:
17551777
flags = "no flags set"
17561778
return "<sounddevice.CallbackFlags: {0}>".format(flags)
17571779

1780+
def __str__(self):
1781+
return ", ".join(name.replace('_', ' ') for name in dir(self)
1782+
if not name.startswith('_') and getattr(self, name))
1783+
17581784
def __bool__(self):
17591785
return bool(self._flags)
17601786

@@ -2091,7 +2117,6 @@ def __init__(self):
20912117
raise ImportError(
20922118
"NumPy must be installed for play()/rec()/playrec()")
20932119
self.event = threading.Event()
2094-
self.logger = _logging.getLogger(__name__)
20952120
self.status = CallbackFlags()
20962121

20972122
def check_data(self, data, mapping):
@@ -2180,14 +2205,6 @@ def callback_exit(self):
21802205
self.frame += self.blocksize
21812206

21822207
def finished_callback(self):
2183-
if self.status.input_underflow:
2184-
self.logger.warning("input underflowed")
2185-
if self.status.input_overflow:
2186-
self.logger.warning("input overflowed")
2187-
if self.status.output_underflow:
2188-
self.logger.warning("output underflowed")
2189-
if self.status.output_overflow:
2190-
self.logger.warning("output overflowed")
21912208
self.event.set()
21922209

21932210
def start_stream(self, StreamClass, samplerate, channels, dtype, callback,

0 commit comments

Comments
 (0)