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_file.py | |
| parent | e8253a615d6f70f7a1f6bc662e703a857fd2e8c4 (diff) | |
DOC: Update examples
Diffstat (limited to 'examples/play_file.py')
| -rwxr-xr-x | examples/play_file.py | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/examples/play_file.py b/examples/play_file.py index c474a63..8386027 100755 --- a/examples/play_file.py +++ b/examples/play_file.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 """Load an audio file into memory and play its contents. -NumPy and the soundfile module (http://PySoundFile.rtfd.io/) must be -installed for this to work. +NumPy and the soundfile module (https://PySoundFile.readthedocs.io/) +must be installed for this to work. This example program loads the whole file into memory before starting playback. @@ -11,6 +11,9 @@ To play very long files, you should use play_long_file.py instead. """ import argparse +import sounddevice as sd +import soundfile as sf + def int_or_str(text): """Helper function for argument parsing.""" @@ -19,21 +22,34 @@ 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)') -args = parser.parse_args() + +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)') +args = parser.parse_args(remaining) try: - import sounddevice as sd - import soundfile as sf data, fs = sf.read(args.filename, dtype='float32') sd.play(data, fs, device=args.device) status = sd.wait() - if status: - parser.exit('Error during playback: ' + str(status)) except KeyboardInterrupt: parser.exit('\nInterrupted by user') except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e)) +if status: + parser.exit('Error during playback: ' + str(status)) |
