summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Geier <Matthias.Geier@gmail.com>2016-05-14 11:15:33 +0200
committerMatthias Geier <Matthias.Geier@gmail.com>2016-05-14 11:15:33 +0200
commit68a30f8da513ae15e517c5d27d4ae4755ca9f368 (patch)
tree04390205777d2dfa4827d6631aaf3ac5781b3149
parentc8b72b905c3696948618177799756c0ff40cbd59 (diff)
Add example rec_unlimited.py
-rw-r--r--doc/examples.rst7
-rwxr-xr-xexamples/rec_unlimited.py75
2 files changed, 82 insertions, 0 deletions
diff --git a/doc/examples.rst b/doc/examples.rst
index e3e3f6c..4ba3bef 100644
--- a/doc/examples.rst
+++ b/doc/examples.rst
@@ -30,3 +30,10 @@ Real-Time Text-Mode Spectrogram
:download:`spectrogram.py <../examples/spectrogram.py>`
.. literalinclude:: ../examples/spectrogram.py
+
+Recording with Arbitrary Duration
+---------------------------------
+
+:download:`rec_unlimited.py <../examples/rec_unlimited.py>`
+
+.. literalinclude:: ../examples/rec_unlimited.py
diff --git a/examples/rec_unlimited.py b/examples/rec_unlimited.py
new file mode 100755
index 0000000..15a9174
--- /dev/null
+++ b/examples/rec_unlimited.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+"""Create a recording with arbitrary duration.
+
+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
+
+
+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(
+ '-l', '--list-devices', action='store_true',
+ help='show list of audio devices and exit')
+parser.add_argument(
+ '-d', '--device', type=int_or_str,
+ help='input device (numeric ID or substring)')
+parser.add_argument(
+ '-r', '--samplerate', type=int, help='sampling rate')
+parser.add_argument(
+ '-c', '--channels', type=int, default=1, help='number of input channels')
+parser.add_argument(
+ 'filename', nargs='?', metavar='FILENAME',
+ help='audio file to store recording to')
+parser.add_argument(
+ '-t', '--subtype', type=str, help='sound file subtype (e.g. "PCM_24")')
+args = parser.parse_args()
+
+try:
+ import sounddevice as sd
+ import soundfile as sf
+
+ if args.list_devices:
+ print(sd.query_devices())
+ parser.exit()
+ if args.samplerate is None:
+ device_info = sd.query_devices(args.device, 'input')
+ # soundfile expects an int, sounddevice provides a float:
+ args.samplerate = int(device_info['default_samplerate'])
+ if args.filename is None:
+ args.filename = tempfile.mktemp(prefix='rec_unlimited_',
+ suffix='.wav', dir='')
+ 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())
+
+ # Make sure the file is opened before recording anything:
+ with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate,
+ channels=args.channels, subtype=args.subtype) as file:
+ with sd.InputStream(samplerate=args.samplerate, device=args.device,
+ channels=args.channels, callback=callback):
+ print("#" * 80)
+ print("press Ctrl+C to stop the recording")
+ print("#" * 80)
+ while True:
+ file.write(queue.get())
+
+except KeyboardInterrupt:
+ parser.exit('\nRecording finished: ' + repr(args.filename))
+except Exception as e:
+ parser.exit(type(e).__name__ + ': ' + str(e))