summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/examples.rst7
-rwxr-xr-xexamples/play_file.py11
-rwxr-xr-xexamples/play_long_file.py92
3 files changed, 107 insertions, 3 deletions
diff --git a/doc/examples.rst b/doc/examples.rst
index 4ba3bef..c7d9d6b 100644
--- a/doc/examples.rst
+++ b/doc/examples.rst
@@ -10,6 +10,13 @@ Play a Sound File
.. literalinclude:: ../examples/play_file.py
+Play a Very Long Sound File
+---------------------------
+
+:download:`play_long_file.py <../examples/play_long_file.py>`
+
+.. literalinclude:: ../examples/play_long_file.py
+
Input to Output Pass-Through
----------------------------
diff --git a/examples/play_file.py b/examples/play_file.py
index 29a594c..d3fabb2 100755
--- a/examples/play_file.py
+++ b/examples/play_file.py
@@ -1,7 +1,12 @@
#!/usr/bin/env python3
-"""Load an audio file and play its contents.
+"""Load an audio file into memory and play its contents.
-PySoundFile (https://github.com/bastibe/PySoundFile/) has to be installed!
+NumPy and the soundfile module (http://PySoundFile.rtfd.io/) must be
+installed for this to work.
+
+This example program loads the whole file into memory before starting
+playback.
+To play very long files, you should use play_long_file.py instead.
"""
import argparse
@@ -16,7 +21,7 @@ def int_or_str(text):
return text
parser = argparse.ArgumentParser(description=__doc__)
-parser.add_argument("filename", help="audio file to be played back")
+parser.add_argument('filename', help='audio file to be played back')
parser.add_argument('-d', '--device', type=int_or_str,
help='output device (numeric ID or substring)')
args = parser.parse_args()
diff --git a/examples/play_long_file.py b/examples/play_long_file.py
new file mode 100755
index 0000000..64eef52
--- /dev/null
+++ b/examples/play_long_file.py
@@ -0,0 +1,92 @@
+#!/usr/bin/env python3
+"""Play an audio file using a limited amount of memory.
+
+The soundfile module (http://PySoundFile.rtfd.io/) must be installed for
+this to work. NumPy is not needed.
+
+In contrast to play_file.py, which loads the whole file into memory
+before starting playback, this example program only holds a given number
+of audio blocks in memory and is therefore able to play files that are
+larger than the available RAM.
+
+"""
+from __future__ import division
+import argparse
+try:
+ import queue # Python 3.x
+except ImportError:
+ import Queue as queue # Python 2.x
+import threading
+
+def int_or_str(text):
+ """Helper function for argument parsing."""
+ try:
+ return int(text)
+ except ValueError:
+ return text
+
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument('filename', help='audio file to be played back')
+parser.add_argument('-d', '--device', type=int_or_str,
+ help='output device (numeric ID or substring)')
+parser.add_argument('-b', '--blocksize', type=int, default=2048,
+ help='block size (default: %(default)s)')
+parser.add_argument(
+ '-q', '--buffersize', type=int, default=20,
+ help='number of blocks used for buffering (default: %(default)s)')
+args = parser.parse_args()
+if args.blocksize == 0:
+ parser.error('blocksize must not be zero')
+if args.buffersize < 1:
+ parser.error('buffersize must be at least 1')
+
+q = queue.Queue(maxsize=args.buffersize)
+event = threading.Event()
+
+
+def callback(outdata, frames, time, status):
+ assert frames == args.blocksize
+ if status.output_underflow:
+ parser.error("An output underflow occurred: increase blocksize?")
+ raise sd.CallbackAbort
+ try:
+ data = q.get_nowait()
+ except queue.Empty:
+ parser.error('Buffer is empty: increase buffersize?')
+ raise sd.CallbackAbort
+ if len(data) < len(outdata):
+ outdata[:len(data)] = data
+ outdata[len(data):] = b'\x00' * (len(outdata) - len(data))
+ raise sd.CallbackStop
+ else:
+ outdata[:] = data
+
+
+try:
+ import sounddevice as sd
+ import soundfile as sf
+
+ with sf.SoundFile(args.filename) as f:
+ for _ in range(args.buffersize):
+ data = f.buffer_read(args.blocksize, ctype='float')
+ if not data:
+ break
+ q.put_nowait(data) # Pre-fill queue
+
+ stream = sd.RawOutputStream(
+ samplerate=f.samplerate, blocksize=args.blocksize,
+ device=args.device, channels=f.channels, dtype='float32',
+ callback=callback, finished_callback=event.set)
+ with stream:
+ timeout = args.blocksize * args.buffersize / f.samplerate
+ while data:
+ data = f.buffer_read(args.blocksize, ctype='float')
+ q.put(data, timeout=timeout)
+ event.wait() # Wait until playback is finished
+except KeyboardInterrupt:
+ parser.exit('\nInterrupted by user')
+except (queue.Full):
+ # A timeout occured, i.e. there was an error in the callback
+ parser.exit(1)
+except Exception as e:
+ parser.exit(type(e).__name__ + ': ' + str(e))