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