summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Geier <Matthias.Geier@gmail.com>2016-12-19 17:32:28 +0100
committerMatthias Geier <Matthias.Geier@gmail.com>2016-12-19 17:33:12 +0100
commitd5511cbe29b952ee8311ad4b0a473cc2bc581b10 (patch)
tree07605bc574932967f2fa09cec1e88f765f3b2cdd
parentb728182025874ba4293a7a4405fd61a3fdaa4da2 (diff)
Examples: Don't use the logging module, proper exit codes
-rwxr-xr-xexamples/play_file.py3
-rwxr-xr-xexamples/play_long_file.py8
-rwxr-xr-xexamples/plot_input.py21
-rwxr-xr-xexamples/rec_unlimited.py18
-rwxr-xr-xexamples/spectrogram.py13
-rwxr-xr-xexamples/wire.py9
6 files changed, 35 insertions, 37 deletions
diff --git a/examples/play_file.py b/examples/play_file.py
index d3fabb2..cf78815 100755
--- a/examples/play_file.py
+++ b/examples/play_file.py
@@ -10,7 +10,6 @@ To play very long files, you should use play_long_file.py instead.
"""
import argparse
-import logging
def int_or_str(text):
@@ -33,7 +32,7 @@ try:
sd.play(data, fs, device=args.device, blocking=True)
status = sd.get_status()
if status:
- logging.warning(str(status))
+ parser.exit('Error during playback: ' + str(status))
except KeyboardInterrupt:
parser.exit('\nInterrupted by user')
except Exception as e:
diff --git a/examples/play_long_file.py b/examples/play_long_file.py
index 9c464ff..cdc37d1 100755
--- a/examples/play_long_file.py
+++ b/examples/play_long_file.py
@@ -10,12 +10,13 @@ of audio blocks in memory and is therefore able to play files that are
larger than the available RAM.
"""
-from __future__ import division
+from __future__ import division, print_function
import argparse
try:
import queue # Python 3.x
except ImportError:
import Queue as queue # Python 2.x
+import sys
import threading
def int_or_str(text):
@@ -47,12 +48,13 @@ event = threading.Event()
def callback(outdata, frames, time, status):
assert frames == args.blocksize
if status.output_underflow:
- parser.error('An output underflow occurred: increase blocksize?')
+ print('Output underflow: increase blocksize?', file=sys.stderr)
raise sd.CallbackAbort
+ assert not status
try:
data = q.get_nowait()
except queue.Empty:
- parser.error('Buffer is empty: increase buffersize?')
+ print('Buffer is empty: increase buffersize?', file=sys.stderr)
raise sd.CallbackAbort
if len(data) < len(outdata):
outdata[:len(data)] = data
diff --git a/examples/plot_input.py b/examples/plot_input.py
index 1210ba3..5a105f4 100755
--- a/examples/plot_input.py
+++ b/examples/plot_input.py
@@ -1,7 +1,12 @@
#!/usr/bin/env python3
-"""Plot the live microphone signal(s) with matplotlib."""
+"""Plot the live microphone signal(s) with matplotlib.
+
+Matplotlib and NumPy have to be installed.
+
+"""
import argparse
-from queue import Queue, Empty
+import queue
+import sys
def int_or_str(text):
@@ -39,15 +44,15 @@ args = parser.parse_args()
if any(c < 1 for c in args.channels):
parser.error('argument CHANNEL: must be >= 1')
mapping = [c - 1 for c in args.channels] # Channel numbers start with 1
-queue = Queue()
+q = queue.Queue()
def audio_callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
- print(status, flush=True)
+ print(status, file=sys.stderr)
# Fancy indexing with mapping creates a (necessary!) copy:
- queue.put(indata[::args.downsample, mapping])
+ q.put(indata[::args.downsample, mapping])
def update_plot(frame):
@@ -61,8 +66,8 @@ def update_plot(frame):
block = True # The first read from the queue is blocking ...
while True:
try:
- data = queue.get(block=block)
- except Empty:
+ data = q.get(block=block)
+ except queue.Empty:
break
shift = len(data)
plotdata = np.roll(plotdata, -shift, axis=0)
@@ -81,7 +86,7 @@ try:
if args.list_devices:
print(sd.query_devices())
- parser.exit()
+ parser.exit(0)
if args.samplerate is None:
device_info = sd.query_devices(args.device, 'input')
args.samplerate = device_info['default_samplerate']
diff --git a/examples/rec_unlimited.py b/examples/rec_unlimited.py
index 5ff32ff..b8f448f 100755
--- a/examples/rec_unlimited.py
+++ b/examples/rec_unlimited.py
@@ -3,12 +3,11 @@
PySoundFile (https://github.com/bastibe/PySoundFile/) has to be installed!
-WARNING: This works only in Python 3.x!
-
"""
import argparse
import tempfile
-from queue import Queue
+import queue
+import sys
def int_or_str(text):
@@ -42,7 +41,7 @@ try:
if args.list_devices:
print(sd.query_devices())
- parser.exit()
+ parser.exit(0)
if args.samplerate is None:
device_info = sd.query_devices(args.device, 'input')
# soundfile expects an int, sounddevice provides a float:
@@ -50,13 +49,13 @@ try:
if args.filename is None:
args.filename = tempfile.mktemp(prefix='rec_unlimited_',
suffix='.wav', dir='')
- queue = Queue()
+ q = queue.Queue()
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
- print(status, flush=True)
- queue.put(indata.copy())
+ print(status, file=sys.stderr)
+ q.put(indata.copy())
# Make sure the file is opened before recording anything:
with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate,
@@ -67,9 +66,10 @@ try:
print('press Ctrl+C to stop the recording')
print('#' * 80)
while True:
- file.write(queue.get())
+ file.write(q.get())
except KeyboardInterrupt:
- parser.exit('\nRecording finished: ' + repr(args.filename))
+ print('\nRecording finished: ' + repr(args.filename))
+ parser.exit(0)
except Exception as e:
parser.exit(type(e).__name__ + ': ' + str(e))
diff --git a/examples/spectrogram.py b/examples/spectrogram.py
index 3c378f3..24ac465 100755
--- a/examples/spectrogram.py
+++ b/examples/spectrogram.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Show a text-mode spectrogram using live microphone data."""
import argparse
-import logging
import math
import numpy as np
import shutil
@@ -59,7 +58,7 @@ try:
if args.list_devices:
print(sd.query_devices())
- parser.exit()
+ parser.exit(0)
samplerate = sd.query_devices(args.device, 'input')['default_samplerate']
@@ -67,11 +66,11 @@ try:
fftsize = math.ceil(samplerate / delta_f)
low_bin = math.floor(low / delta_f)
- cumulated_status = sd.CallbackFlags()
-
def callback(indata, frames, time, status):
- global cumulated_status
- cumulated_status |= status
+ if status:
+ text = ' ' + str(status) + ' '
+ print('\x1b[34;40m', text.center(args.columns, '#'),
+ '\x1b[0m', sep='', flush=True)
if any(indata):
magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize))
magnitude *= args.gain / fftsize
@@ -97,8 +96,6 @@ try:
print('\x1b[31;40m', usage_line.center(args.columns, '#'),
'\x1b[0m', sep='', flush=True)
break
- if cumulated_status:
- logging.warning(str(cumulated_status))
except KeyboardInterrupt:
parser.exit('Interrupted by user')
except Exception as e:
diff --git a/examples/wire.py b/examples/wire.py
index ad9e3a5..2863b71 100755
--- a/examples/wire.py
+++ b/examples/wire.py
@@ -31,11 +31,9 @@ args = parser.parse_args()
try:
import sounddevice as sd
- cumulated_status = sd.CallbackFlags()
-
def callback(indata, outdata, frames, time, status):
- global cumulated_status
- cumulated_status |= status
+ if status:
+ print(status, flush=True)
outdata[:] = indata
with sd.Stream(device=(args.input_device, args.output_device),
@@ -46,9 +44,6 @@ try:
print('press Return to quit')
print('#' * 80)
input()
-
- if cumulated_status:
- logging.warning(str(cumulated_status))
except KeyboardInterrupt:
parser.exit('\nInterrupted by user')
except Exception as e: