From 5408733e7f7b3ed1f350bbae8d74e758a00f0be7 Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Fri, 5 Aug 2016 09:40:51 +0200 Subject: Accept device substrings in all examples --- examples/play_file.py | 11 ++++++++++- examples/spectrogram.py | 13 +++++++++++-- examples/wire.py | 14 ++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/play_file.py b/examples/play_file.py index 402d814..29a594c 100755 --- a/examples/play_file.py +++ b/examples/play_file.py @@ -7,9 +7,18 @@ PySoundFile (https://github.com/bastibe/PySoundFile/) has to be installed! import argparse import logging + +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, help="device ID") +parser.add_argument('-d', '--device', type=int_or_str, + help='output device (numeric ID or substring)') args = parser.parse_args() try: diff --git a/examples/spectrogram.py b/examples/spectrogram.py index c952b95..d0d5026 100755 --- a/examples/spectrogram.py +++ b/examples/spectrogram.py @@ -7,6 +7,14 @@ import shutil usage_line = ' press to quit, + or - to change scaling ' + +def int_or_str(text): + """Helper function for argument parsing.""" + try: + return int(text) + except ValueError: + return text + try: columns, _ = shutil.get_terminal_size() except AttributeError: @@ -20,7 +28,8 @@ parser.add_argument('-b', '--block-duration', type=float, help='block size (default %(default)s milliseconds)') parser.add_argument('-c', '--columns', type=int, default=columns, help='width of spectrogram') -parser.add_argument('-d', '--device', type=int, help='input device ID') +parser.add_argument('-d', '--device', type=int_or_str, + help='input device (numeric ID or substring)') parser.add_argument('-g', '--gain', type=float, default=10, help='initial gain factor (default %(default)s)') parser.add_argument('-r', '--range', type=float, nargs=2, @@ -30,7 +39,7 @@ args = parser.parse_args() low, high = args.range if high <= low: - parser.error("HIGH must be greater than LOW") + parser.error('HIGH must be greater than LOW') # Create a nice output gradient using ANSI escape sequences. # Stolen from https://gist.github.com/maurisvh/df919538bcef391bc89f diff --git a/examples/wire.py b/examples/wire.py index ce9cfe7..a980d7f 100755 --- a/examples/wire.py +++ b/examples/wire.py @@ -7,9 +7,19 @@ See https://www.assembla.com/spaces/portaudio/subversion/source/HEAD/portaudio/t import argparse import logging + +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("-i", "--input-device", type=int, help="input device ID") -parser.add_argument("-o", "--output-device", type=int, help="output device ID") +parser.add_argument("-i", "--input-device", type=int_or_str, + help="input device ID or substring") +parser.add_argument("-o", "--output-device", type=int_or_str, + help="output device ID or substring") parser.add_argument("-c", "--channels", type=int, default=2, help="number of channels") parser.add_argument("-t", "--dtype", help="audio data type") -- cgit v1.2.3