Parameter file to store selected audio device
[ampy.git] / record.py
index 69cb10fa91f8c8e944053b8315c9714bab67fbfb..61020c011943f24a55655ae828297b81cb566d32 100755 (executable)
--- a/record.py
+++ b/record.py
@@ -4,32 +4,40 @@ PyAudio example: Record a few seconds of audio and save to a WAVE
 file.
 """
 
+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 = 5
+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 = []