]> code.delx.au - pulseaudio/blob - src/modules/module-sine-source.c
make module-sine-source actually work
[pulseaudio] / src / modules / module-sine-source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/ioctl.h>
35 #include <sys/poll.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/timeval.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
49 #include <pulsecore/rtclock.h>
50
51 #include "module-sine-source-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("Sine wave generator source");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(FALSE);
57 PA_MODULE_USAGE(
58 "rate=<sample rate> "
59 "source_name=<name for the source> "
60 "frequency=<frequency in Hz>");
61
62 #define DEFAULT_SOURCE_NAME "sine_input"
63 #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
64
65 struct userdata {
66 pa_core *core;
67 pa_module *module;
68 pa_source *source;
69
70 pa_thread *thread;
71 pa_thread_mq thread_mq;
72 pa_rtpoll *rtpoll;
73
74 pa_memchunk memchunk;
75 size_t peek_index;
76
77 pa_usec_t block_usec; /* how much to push at once */
78 pa_usec_t timestamp; /* when to push next */
79 };
80
81 static const char* const valid_modargs[] = {
82 "rate",
83 "source_name",
84 "frequency",
85 NULL
86 };
87
88 static int source_process_msg(
89 pa_msgobject *o,
90 int code,
91 void *data,
92 int64_t offset,
93 pa_memchunk *chunk) {
94
95 struct userdata *u = PA_SOURCE(o)->userdata;
96
97 switch (code) {
98
99 case PA_SINK_MESSAGE_SET_STATE:
100
101 if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
102 u->timestamp = pa_rtclock_usec();
103
104 break;
105
106 case PA_SOURCE_MESSAGE_GET_LATENCY: {
107 pa_usec_t now, left_to_fill;
108
109 now = pa_rtclock_usec();
110 left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
111
112 *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
113
114 return 0;
115 }
116 }
117
118 return pa_source_process_msg(o, code, data, offset, chunk);
119 }
120
121 static void source_update_requested_latency_cb(pa_source *s) {
122 struct userdata *u;
123
124 pa_source_assert_ref(s);
125 pa_assert_se(u = s->userdata);
126
127 u->block_usec = pa_source_get_requested_latency_within_thread(s);
128
129 if (u->block_usec == (pa_usec_t) -1)
130 u->block_usec = s->thread_info.max_latency;
131
132 pa_log_debug("new block msec = %lu", (unsigned long) (u->block_usec / PA_USEC_PER_MSEC));
133 }
134
135 static void process_render(struct userdata *u, pa_usec_t now) {
136 pa_assert(u);
137
138 while (u->timestamp < now + u->block_usec) {
139 pa_memchunk chunk;
140 size_t k;
141
142 k = pa_usec_to_bytes_round_up(now + u->block_usec - u->timestamp, &u->source->sample_spec);
143
144 chunk.memblock = u->memchunk.memblock;
145 chunk.index = u->peek_index;
146 chunk.length = PA_MIN(u->memchunk.length - u->peek_index, k);
147
148 pa_log_debug("posting %lu", (unsigned long) chunk.length);
149 pa_source_post(u->source, &chunk);
150
151 u->peek_index += chunk.length;
152 while (u->peek_index >= u->memchunk.length)
153 u->peek_index -= u->memchunk.length;
154
155 u->timestamp += pa_bytes_to_usec(chunk.length, &u->source->sample_spec);
156 }
157 }
158
159 static void thread_func(void *userdata) {
160 struct userdata *u = userdata;
161
162 pa_assert(u);
163
164 pa_log_debug("Thread starting up");
165
166 pa_thread_mq_install(&u->thread_mq);
167 pa_rtpoll_install(u->rtpoll);
168
169 u->timestamp = pa_rtclock_usec();
170
171 for (;;) {
172 int ret;
173
174 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
175 pa_usec_t now;
176
177 now = pa_rtclock_usec();
178
179 if (u->timestamp <= now)
180 process_render(u, now);
181
182 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
183 } else
184 pa_rtpoll_set_timer_disabled(u->rtpoll);
185
186 /* Hmm, nothing to do. Let's sleep */
187 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
188 goto fail;
189
190 if (ret == 0)
191 goto finish;
192 }
193
194 fail:
195 /* If this was no regular exit from the loop we have to continue
196 * processing messages until we received PA_MESSAGE_SHUTDOWN */
197 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
198 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
199
200 finish:
201 pa_log_debug("Thread shutting down");
202 }
203
204 static void calc_sine(float *f, size_t l, double freq) {
205 size_t i;
206
207 l /= sizeof(float);
208
209 for (i = 0; i < l; i++)
210 *(f++) = (float) 0.5f * sin((double) i*M_PI*2*freq / (double) l);
211 }
212
213 int pa__init(pa_module*m) {
214 struct userdata *u;
215 pa_modargs *ma;
216 pa_source_new_data data;
217 uint32_t frequency;
218 pa_sample_spec ss;
219 void *p;
220 unsigned n, d;
221 size_t l;
222
223 pa_assert(m);
224
225 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
226 pa_log("failed to parse module arguments.");
227 goto fail;
228 }
229
230 ss.format = PA_SAMPLE_FLOAT32;
231 ss.channels = 1;
232 ss.rate = 44100;
233
234 if (pa_modargs_get_value_u32(ma, "rate", &ss.rate) < 0 || ss.rate <= 1) {
235 pa_log("Invalid rate specification");
236 goto fail;
237 }
238
239 frequency = 440;
240 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
241 pa_log("Invalid frequency specification");
242 goto fail;
243 }
244
245 m->userdata = u = pa_xnew0(struct userdata, 1);
246 u->core = m->core;
247 u->module = m;
248 u->rtpoll = pa_rtpoll_new();
249 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
250
251 u->peek_index = 0;
252 pa_memchunk_reset(&u->memchunk);
253
254 n = ss.rate;
255 d = frequency;
256 pa_reduce(&n, &d);
257
258 l = pa_mempool_block_size_max(m->core->mempool) / pa_frame_size(&ss);
259
260 l /= n;
261 if (l <= 0) l = 1;
262 l *= n;
263
264 u->memchunk.length = l * pa_frame_size(&ss);
265 u->memchunk.memblock = pa_memblock_new(m->core->mempool, u->memchunk.length);
266
267 p = pa_memblock_acquire(u->memchunk.memblock);
268 calc_sine(p, u->memchunk.length, pa_bytes_to_usec(u->memchunk.length, &ss) * frequency / PA_USEC_PER_SEC);
269 pa_memblock_release(u->memchunk.memblock);
270
271 pa_source_new_data_init(&data);
272 data.driver = __FILE__;
273 data.module = m;
274 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
275 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Sine source at %u Hz", (unsigned) frequency);
276 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
277 pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
278 pa_source_new_data_set_sample_spec(&data, &ss);
279
280 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
281 pa_source_new_data_done(&data);
282
283 if (!u->source) {
284 pa_log("Failed to create source.");
285 goto fail;
286 }
287
288 u->source->parent.process_msg = source_process_msg;
289 u->source->update_requested_latency = source_update_requested_latency_cb;
290 u->source->userdata = u;
291
292 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
293 pa_source_set_rtpoll(u->source, u->rtpoll);
294
295 pa_source_set_latency_range(u->source, (pa_usec_t) -1, MAX_LATENCY_USEC);
296 u->block_usec = u->source->thread_info.max_latency;
297
298 if (!(u->thread = pa_thread_new(thread_func, u))) {
299 pa_log("Failed to create thread.");
300 goto fail;
301 }
302
303 pa_source_put(u->source);
304
305 pa_modargs_free(ma);
306
307 return 0;
308
309 fail:
310 if (ma)
311 pa_modargs_free(ma);
312
313 pa__done(m);
314
315 return -1;
316 }
317
318 void pa__done(pa_module*m) {
319 struct userdata *u;
320
321 pa_assert(m);
322
323 if (!(u = m->userdata))
324 return;
325
326 if (u->source)
327 pa_source_unlink(u->source);
328
329 if (u->thread) {
330 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
331 pa_thread_free(u->thread);
332 }
333
334 pa_thread_mq_done(&u->thread_mq);
335
336 if (u->source)
337 pa_source_unref(u->source);
338
339 if (u->memchunk.memblock)
340 pa_memblock_unref(u->memchunk.memblock);
341
342 if (u->rtpoll)
343 pa_rtpoll_free(u->rtpoll);
344
345 pa_xfree(u);
346 }