+#!/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 = []