3 PyAudio example: Record a few seconds of audio and save to a WAVE
13 DEVICE = 3 # which alsa decive to read
14 CHUNK = 1024 # how many bytes at a time?
15 FORMAT = pyaudio.paInt16 # Format
19 VOLUME_CONSTANT = 11.975454545454545
23 stream = p.open(format=FORMAT,
24 channels=1, # Our ampmeter always returns 0 on the second channel
27 input_device_index=DEVICE,
28 frames_per_buffer=CHUNK)
32 for i in range(0, int(RATE * RECORD_SECONDS / CHUNK)):
33 data = stream.read(CHUNK, exception_on_overflow=False)
34 values = struct.unpack('h'*CHUNK, data)
42 chunk_total /= len(values)
45 avg = total / int(RATE * RECORD_SECONDS / CHUNK)
46 watts = avg / VOLUME_CONSTANT
47 print("average {:.0f}W".format(watts))
48 os.system("rrdtool update power.rrd N:{:.0f}".format(watts))