annotate service/audioInputLevels/audioInputLevelsPulse.py @ 1112:03b4882517dd

audiolevels output to influxdb Ignore-this: e5f3559928a638bb311eb70dc751ef87 darcs-hash:e419b63c2335d1dd2a5267440bf73ee9b1bf3291
author drewp <drewp@bigasterisk.com>
date Fri, 16 Sep 2016 01:22:11 -0700
parents 64a2ba088665
children 30cd6cb833e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
887
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 # based on http://freshfoo.com/blog/pulseaudio_monitoring
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 from __future__ import division
1112
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
3 import socket, argparse, time
887
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 from Queue import Queue
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 from ctypes import POINTER, c_ubyte, c_void_p, c_ulong, cast
1112
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
6 from influxdb import InfluxDBClient
887
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 # From https://github.com/Valodim/python-pulseaudio
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 from pulseaudio import lib_pulseaudio as P
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 METER_RATE = 1
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 MAX_SAMPLE_VALUE = 127
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 class PeakMonitor(object):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 def __init__(self, source_name, rate):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 self.source_name = source_name
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 self.rate = rate
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 # Wrap callback methods in appropriate ctypefunc instances so
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 # that the Pulseaudio C API can call them
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 self._context_notify_cb = P.pa_context_notify_cb_t(self.context_notify_cb)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 self._source_info_cb = P.pa_source_info_cb_t(self.source_info_cb)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 self._stream_read_cb = P.pa_stream_request_cb_t(self.stream_read_cb)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 # stream_read_cb() puts peak samples into this Queue instance
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 self._samples = Queue()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 # Create the mainloop thread and set our context_notify_cb
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 # method to be called when there's updates relating to the
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 # connection to Pulseaudio
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 _mainloop = P.pa_threaded_mainloop_new()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 _mainloop_api = P.pa_threaded_mainloop_get_api(_mainloop)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 context = P.pa_context_new(_mainloop_api, 'peak_demo')
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 P.pa_context_set_state_callback(context, self._context_notify_cb, None)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 P.pa_context_connect(context, None, 0, None)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 P.pa_threaded_mainloop_start(_mainloop)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 def __iter__(self):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 while True:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 yield self._samples.get()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 def context_notify_cb(self, context, _):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44 state = P.pa_context_get_state(context)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 if state == P.PA_CONTEXT_READY:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 print "Pulseaudio connection ready..."
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 # Connected to Pulseaudio. Now request that source_info_cb
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 # be called with information about the available sources.
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 o = P.pa_context_get_source_info_list(context, self._source_info_cb, None)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 P.pa_operation_unref(o)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 elif state == P.PA_CONTEXT_FAILED :
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 print "Connection failed"
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 elif state == P.PA_CONTEXT_TERMINATED:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 print "Connection terminated"
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 def source_info_cb(self, context, source_info_p, _, __):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 if not source_info_p:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 return
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 source_info = source_info_p.contents
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 if source_info.name == self.source_name:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 # Found the source we want to monitor for peak levels.
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 # Tell PA to call stream_read_cb with peak samples.
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 print 'setting up peak recording using', source_info.name
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 print 'description:', source_info.description
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 samplespec = P.pa_sample_spec()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 samplespec.channels = 1
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 samplespec.format = P.PA_SAMPLE_U8
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 samplespec.rate = self.rate
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 pa_stream = P.pa_stream_new(context, "peak detect demo", samplespec, None)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 P.pa_stream_set_read_callback(pa_stream,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 self._stream_read_cb,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 source_info.index)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 P.pa_stream_connect_record(pa_stream,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 source_info.name,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 None,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 P.PA_STREAM_PEAK_DETECT)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 def stream_read_cb(self, stream, length, index_incr):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 data = c_void_p()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 P.pa_stream_peek(stream, data, c_ulong(length))
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 data = cast(data, POINTER(c_ubyte))
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 for i in xrange(length):
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 # When PA_SAMPLE_U8 is used, samples values range from 128
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 # to 255 because the underlying audio data is signed but
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 # it doesn't make sense to return signed peaks.
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 self._samples.put(data[i] - 128)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 P.pa_stream_drop(stream)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 def main():
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98 parser = argparse.ArgumentParser()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 parser.add_argument(
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 '--source', required=True,
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 help='pulseaudio source name (use `pactl list sources | grep Name`)')
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103 args = parser.parse_args()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104
1112
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
105 def ipv6Init(self, host="localhost", port=2003):
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
106 self._addr = (host, port, 0, 0)
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
107 self._sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
108 self._sock.connect(self._addr)
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
109
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
110 influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main')
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
111
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
112 hostname = socket.gethostname()
887
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 monitor = PeakMonitor(args.source, METER_RATE)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 for sample in monitor:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 #print ' %3d %s' % (sample, '>' * sample)
1112
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
116 influx.write_points([{'measurement': 'audioLevel',
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
117 "tags": dict(stat='max', location=hostname),
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
118 "fields": {"value": sample / 128},
03b4882517dd audiolevels output to influxdb
drewp <drewp@bigasterisk.com>
parents: 887
diff changeset
119 "time": int(time.time())}], time_precision='s')
887
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 if __name__ == '__main__':
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 main()