summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Geier <Matthias.Geier@gmail.com>2015-06-21 13:10:40 +0200
committerMatthias Geier <Matthias.Geier@gmail.com>2015-07-01 15:37:03 +0200
commit0529a60f464801f27817817b68410e5cd1e088f6 (patch)
tree305c802b402e75e19f2a45c7cd802e80c88b849e
parent4c16c864062dacf03914f3b87cdbc91f70cd7337 (diff)
Remove logging, add get_status()
-rwxr-xr-xexamples/play_file.py6
-rw-r--r--sounddevice.py41
2 files changed, 34 insertions, 13 deletions
diff --git a/examples/play_file.py b/examples/play_file.py
index 7161ceb..8483777 100755
--- a/examples/play_file.py
+++ b/examples/play_file.py
@@ -5,6 +5,7 @@ PySoundFile (https://github.com/bastibe/PySoundFile/) has to be installed!
"""
import argparse
+import logging
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("filename", help="audio file to be played back")
@@ -15,7 +16,10 @@ try:
import sounddevice as sd
import soundfile as sf
data, fs = sf.read(args.filename, dtype='float32')
- sd.play(data, fs, device=args.device, blocking=True)
+ sd.play(data, fs, device=args.device)
+ status = sd.wait()
+ if status:
+ logging.warning(str(status))
except BaseException as e:
# This avoids printing the traceback, especially if Ctrl-C is used.
raise SystemExit(str(e))
diff --git a/sounddevice.py b/sounddevice.py
index 700e628..b1c377a 100644
--- a/sounddevice.py
+++ b/sounddevice.py
@@ -28,7 +28,6 @@ __version__ = "0.1.0"
import atexit as _atexit
from cffi import FFI as _FFI
import sys as _sys
-import logging as _logging
_ffi = _FFI()
_ffi.cdef("""
@@ -440,6 +439,10 @@ def wait():
If at least one buffer over-/underrun happened during the last
playback/recording, a :class:`CallbackFlags` object is returned.
+ See Also
+ --------
+ get_status
+
"""
global _last_callback
if _last_callback:
@@ -460,6 +463,26 @@ def stop(ignore_errors=True):
_last_callback.stream.close(ignore_errors)
+def get_status():
+ """Get information about over-/underflows in play()/rec()/playrec().
+
+ Returns
+ -------
+ CallbackFlags
+ A :class:`CallbackFlags` object that holds information about the
+ last invocation of :func:`play`, :func:`rec` or :func:`playrec`.
+
+ See Also
+ --------
+ wait
+
+ """
+ if _last_callback:
+ return _last_callback.status
+ else:
+ raise RuntimeError("play()/rec()/playrec() was not called yet")
+
+
def print_devices():
"""Show information about all available audio devices.
@@ -1749,12 +1772,15 @@ class CallbackFlags(object):
self._flags = flags
def __repr__(self):
- flags = ", ".join(name for name in dir(self)
- if not name.startswith('_') and getattr(self, name))
+ flags = str(self)
if not flags:
flags = "no flags set"
return "<sounddevice.CallbackFlags: {0}>".format(flags)
+ def __str__(self):
+ return ", ".join(name.replace('_', ' ') for name in dir(self)
+ if not name.startswith('_') and getattr(self, name))
+
def __bool__(self):
return bool(self._flags)
@@ -2091,7 +2117,6 @@ class _CallbackContext(object):
raise ImportError(
"NumPy must be installed for play()/rec()/playrec()")
self.event = threading.Event()
- self.logger = _logging.getLogger(__name__)
self.status = CallbackFlags()
def check_data(self, data, mapping):
@@ -2180,14 +2205,6 @@ class _CallbackContext(object):
self.frame += self.blocksize
def finished_callback(self):
- if self.status.input_underflow:
- self.logger.warning("input underflowed")
- if self.status.input_overflow:
- self.logger.warning("input overflowed")
- if self.status.output_underflow:
- self.logger.warning("output underflowed")
- if self.status.output_overflow:
- self.logger.warning("output overflowed")
self.event.set()
def start_stream(self, StreamClass, samplerate, channels, dtype, callback,