Skip to content

Commit 5408733

Browse files
committed
Accept device substrings in all examples
1 parent 104d912 commit 5408733

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

examples/play_file.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
import argparse
88
import logging
99

10+
11+
def int_or_str(text):
12+
"""Helper function for argument parsing."""
13+
try:
14+
return int(text)
15+
except ValueError:
16+
return text
17+
1018
parser = argparse.ArgumentParser(description=__doc__)
1119
parser.add_argument("filename", help="audio file to be played back")
12-
parser.add_argument("-d", "--device", type=int, help="device ID")
20+
parser.add_argument('-d', '--device', type=int_or_str,
21+
help='output device (numeric ID or substring)')
1322
args = parser.parse_args()
1423

1524
try:

examples/spectrogram.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
usage_line = ' press <enter> to quit, +<enter> or -<enter> to change scaling '
99

10+
11+
def int_or_str(text):
12+
"""Helper function for argument parsing."""
13+
try:
14+
return int(text)
15+
except ValueError:
16+
return text
17+
1018
try:
1119
columns, _ = shutil.get_terminal_size()
1220
except AttributeError:
@@ -20,7 +28,8 @@
2028
help='block size (default %(default)s milliseconds)')
2129
parser.add_argument('-c', '--columns', type=int, default=columns,
2230
help='width of spectrogram')
23-
parser.add_argument('-d', '--device', type=int, help='input device ID')
31+
parser.add_argument('-d', '--device', type=int_or_str,
32+
help='input device (numeric ID or substring)')
2433
parser.add_argument('-g', '--gain', type=float, default=10,
2534
help='initial gain factor (default %(default)s)')
2635
parser.add_argument('-r', '--range', type=float, nargs=2,
@@ -30,7 +39,7 @@
3039

3140
low, high = args.range
3241
if high <= low:
33-
parser.error("HIGH must be greater than LOW")
42+
parser.error('HIGH must be greater than LOW')
3443

3544
# Create a nice output gradient using ANSI escape sequences.
3645
# Stolen from https://gist.github.com/maurisvh/df919538bcef391bc89f

examples/wire.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
import argparse
88
import logging
99

10+
11+
def int_or_str(text):
12+
"""Helper function for argument parsing."""
13+
try:
14+
return int(text)
15+
except ValueError:
16+
return text
17+
1018
parser = argparse.ArgumentParser(description=__doc__)
11-
parser.add_argument("-i", "--input-device", type=int, help="input device ID")
12-
parser.add_argument("-o", "--output-device", type=int, help="output device ID")
19+
parser.add_argument("-i", "--input-device", type=int_or_str,
20+
help="input device ID or substring")
21+
parser.add_argument("-o", "--output-device", type=int_or_str,
22+
help="output device ID or substring")
1323
parser.add_argument("-c", "--channels", type=int, default=2,
1424
help="number of channels")
1525
parser.add_argument("-t", "--dtype", help="audio data type")

0 commit comments

Comments
 (0)