Parameter file to store selected audio device
[ampy.git] / record.py
old mode 100644 (file)
new mode 100755 (executable)
index a113104..61020c0
--- a/record.py
+++ b/record.py
@@ -1,33 +1,43 @@
+#!/usr/bin/python3
 """
 PyAudio example: Record a few seconds of audio and save to a WAVE
 file.
 """
 
-import pyaudio
-import wave
+import json
 import sys
+import wave
 
+import pyaudio
+
+PARAMFILE = '/var/lib/ampy/params.json'
 CHUNK = 1024
 FORMAT = pyaudio.paInt16
 CHANNELS = 2
 RATE = 44100
 RECORD_SECONDS = 5
-WAVE_OUTPUT_FILENAME = "output.wav"
-DEVICE = 3
+WAVE_OUTPUT_FILENAME = "/tmp/output.wav"
 
 if sys.platform == 'darwin':
     CHANNELS = 1
 
 p = pyaudio.PyAudio()
 
+try:
+    with open(PARAMFILE) as f:
+        params = json.loads(f.read())
+        alsadevice = params['alsadevice']
+except (FileNotFoundError, json.decoder.JSONDecodeError, KeyError):
+    print("Error reading the parameters. Please run device.py")
+
 stream = p.open(format=FORMAT,
                 channels=CHANNELS,
                 rate=RATE,
                 input=True,
-                input_device_index=DEVICE,
+                input_device_index=alsadevice,
                 frames_per_buffer=CHUNK)
 
-print("* recording")
+print("* recording to", WAVE_OUTPUT_FILENAME)
 
 frames = []