annotate service/audioInputLevels/audioInputLevelsPulse.py @ 887:64a2ba088665

audio input levels to graphite Ignore-this: b1422609f3bf51778b8b2284ca914916 darcs-hash:20130802145545-312f9-64fa9ffcb82dc26e67591f28b16633add6299cd0
author drewp <drewp@bigasterisk.com>
date Fri, 02 Aug 2013 07:55:45 -0700
parents
children f84129fbbe38
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
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 import socket, argparse
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
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 import galena
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
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106 out = galena.Galena(host='bang')
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 prefix = 'system.house.audio.%s' % socket.gethostname()
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 monitor = PeakMonitor(args.source, METER_RATE)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 for sample in monitor:
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 #print ' %3d %s' % (sample, '>' * sample)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 out.send(prefix + ".max", sample / 128)
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 if __name__ == '__main__':
64a2ba088665 audio input levels to graphite
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 main()