diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2016-08-05 09:40:51 +0200 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2016-08-05 09:40:51 +0200 |
| commit | 5408733e7f7b3ed1f350bbae8d74e758a00f0be7 (patch) | |
| tree | bdac9db11b3e428a521342666ede37c087f2fc38 /examples | |
| parent | 104d912af4bd54bc34521c43ab53332e933425a0 (diff) | |
Accept device substrings in all examples
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/play_file.py | 11 | ||||
| -rwxr-xr-x | examples/spectrogram.py | 13 | ||||
| -rwxr-xr-x | examples/wire.py | 14 |
3 files changed, 33 insertions, 5 deletions
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 <enter> to quit, +<enter> or -<enter> 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") |
