diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2019-09-17 20:12:20 +0200 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2019-09-18 13:10:33 +0200 |
| commit | a82b9b56d44ed37c587648685826ad56b9177533 (patch) | |
| tree | bd72d8becf6caaf2079cd4f31db42db4f7cba289 /examples/play_long_file.py | |
| parent | e8253a615d6f70f7a1f6bc662e703a857fd2e8c4 (diff) | |
DOC: Update examples
Diffstat (limited to 'examples/play_long_file.py')
| -rwxr-xr-x | examples/play_long_file.py | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/examples/play_long_file.py b/examples/play_long_file.py index 90e41c9..e018fd9 100755 --- a/examples/play_long_file.py +++ b/examples/play_long_file.py @@ -1,24 +1,27 @@ #!/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. +The soundfile module (https://PySoundFile.readthedocs.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. +A similar example could of course be implemented using NumPy, +but this example shows what can be done when NumPy is not available. + """ -from __future__ import division, print_function import argparse -try: - import queue # Python 3.x -except ImportError: - import Queue as queue # Python 2.x +import queue import sys import threading +import sounddevice as sd +import soundfile as sf + + def int_or_str(text): """Helper function for argument parsing.""" try: @@ -26,16 +29,32 @@ def int_or_str(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 = argparse.ArgumentParser(add_help=False) +parser.add_argument( + '-l', '--list-devices', action='store_true', + help='show list of audio devices and exit') +args, remaining = parser.parse_known_args() +if args.list_devices: + print(sd.query_devices()) + parser.exit(0) +parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + parents=[parser]) +parser.add_argument( + 'filename', metavar='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() +args = parser.parse_args(remaining) if args.blocksize == 0: parser.error('blocksize must not be zero') if args.buffersize < 1: @@ -65,16 +84,12 @@ def callback(outdata, frames, time, status): 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, dtype='float32') 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', |
