--- /dev/null
+#!/usr/bin/python3
+
+import pyaudio
+
+p = pyaudio.PyAudio()
+
+print('Listing api:')
+for i in range(p.get_host_api_count()):
+ print(i, p.get_host_api_info_by_index(i)['name'])
+
+print('Listing devices:')
+for i in range(p.get_device_count()):
+ print(i, p.get_device_info_by_index(i)['name'])
--- /dev/null
+<?php
+$RRDFILE="/home/nirgal/ampy/power.rrd";
+
+$info=rrd_lastupdate($RRDFILE);
+if ($info === FALSE)
+ $result=[
+ 'last_update' => 'unknown',
+ 'watts' => 'unknown'
+ ];
+else
+ $result=[
+ 'last_update' => $info['last_update'],
+ 'watts' => $info['data'][0]
+ ];
+header("Content-Type: text/json");
+echo(json_encode($result));
+?>
--- /dev/null
+<?php
+$RRDFILE="/home/nirgal/ampy/power.rrd";
+$IMGFILE="/tmp/power.svg";
+$REFRESH=30;
+
+$result=rrd_graph("$IMGFILE", [
+ "--imgformat", "SVG",
+ "-s", "now-1h",
+ #"-e", "now-8h",
+ "--width", "1000",
+ "--height", "500",
+ "--vertical-label", "watts",
+ "--lower-limit", "0",
+ #"--logarithmic",
+ "DEF:watts=${RRDFILE}:watts:AVERAGE",
+ "LINE1:watts#FF0000"
+]);
+if ($result === FALSE) {
+ echo("Error in rrd_graph");
+ die();
+}
+header("Content-Type: image/svg+xml");
+#header("Refresh: $REFRESH");
+readfile($IMGFILE);
+?>
--- /dev/null
+<!DOCTYPE html>
+<meta charset="UTF-8">
+<script src='/javascript/jquery/jquery.js'></script>
+<script>
+function refresh_power() {
+ $.ajax({
+ url : 'currentjson.php',
+ success : function(result) {
+ document.getElementById('currentpower').innerHTML = result['watts'];
+ },
+ complete : function() {
+ setTimeout(refresh_power, 1000);
+ }
+ });
+}
+</script>
+
+Puissance instannée: <span id=currentpower>unknown</span> watts<br>
+<script>refresh_power();</script>
+
+
+<img src="graph.php">
--- /dev/null
+https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/yhdc-sct-013-000-ct-sensor-report
+
+rrdtool create power.rrd --start now-2h --step 1s DS:watts:GAUGE:1:0:6600 RRA:AVERAGE:0.5:1s:1d # Buggy: nécessite des updates toutes les 500 ms
--- /dev/null
+"""
+PyAudio example: Record a few seconds of audio and save to a WAVE
+file.
+"""
+
+import pyaudio
+import wave
+import sys
+
+CHUNK = 1024
+FORMAT = pyaudio.paInt16
+CHANNELS = 2
+RATE = 44100
+RECORD_SECONDS = 5
+WAVE_OUTPUT_FILENAME = "output.wav"
+DEVICE = 3
+
+if sys.platform == 'darwin':
+ CHANNELS = 1
+
+p = pyaudio.PyAudio()
+
+stream = p.open(format=FORMAT,
+ channels=CHANNELS,
+ rate=RATE,
+ input=True,
+ input_device_index=DEVICE,
+ frames_per_buffer=CHUNK)
+
+print("* recording")
+
+frames = []
+
+for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
+ data = stream.read(CHUNK, exception_on_overflow=False)
+ frames.append(data)
+
+print("* done recording")
+
+stream.stop_stream()
+stream.close()
+p.terminate()
+
+wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
+wf.setnchannels(CHANNELS)
+wf.setsampwidth(p.get_sample_size(FORMAT))
+wf.setframerate(RATE)
+wf.writeframes(b''.join(frames))
+wf.close()
--- /dev/null
+#!/usr/bin/python3
+"""
+PyAudio example: Record a few seconds of audio and save to a WAVE
+file.
+"""
+
+import os
+import pyaudio
+import wave
+import sys
+import struct
+
+DEVICE = 3 # which alsa decive to read
+CHUNK = 1024 # how many bytes at a time?
+FORMAT = pyaudio.paInt16 # Format
+RATE = 44100
+RECORD_SECONDS = 0.5
+
+VOLUME_CONSTANT = 11.975454545454545
+
+p = pyaudio.PyAudio()
+
+stream = p.open(format=FORMAT,
+ channels=1, # Our ampmeter always returns 0 on the second channel
+ rate=RATE,
+ input=True,
+ input_device_index=DEVICE,
+ frames_per_buffer=CHUNK)
+
+while(True):
+ total = 0
+ for i in range(0, int(RATE * RECORD_SECONDS / CHUNK)):
+ data = stream.read(CHUNK, exception_on_overflow=False)
+ values = struct.unpack('h'*CHUNK, data)
+ #print(values)
+ chunk_total = 0
+ for value in values:
+ if value > 0:
+ chunk_total += value
+ else:
+ chunk_total -= value
+ chunk_total /= len(values)
+ #print(chunk_total)
+ total += chunk_total
+ avg = total / int(RATE * RECORD_SECONDS / CHUNK)
+ watts = avg / VOLUME_CONSTANT
+ print("average {:.0f}W".format(watts))
+ os.system("rrdtool update power.rrd N:{:.0f}".format(watts))
+
+stream.stop_stream()
+stream.close()
+p.terminate()
+