]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
rework memory block management to be thread-safe and mostly lock-free.
[pulseaudio] / src / modules / module-sine.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 <stdio.h>
27 #include <assert.h>
28 #include <math.h>
29
30 #include <pulse/xmalloc.h>
31
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/namereg.h>
36 #include <pulsecore/log.h>
37
38 #include "module-sine-symdef.h"
39
40 PA_MODULE_AUTHOR("Lennart Poettering")
41 PA_MODULE_DESCRIPTION("Sine wave generator")
42 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
43 PA_MODULE_VERSION(PACKAGE_VERSION)
44
45 struct userdata {
46 pa_core *core;
47 pa_module *module;
48 pa_sink_input *sink_input;
49 pa_memblock *memblock;
50 size_t peek_index;
51 };
52
53 static const char* const valid_modargs[] = {
54 "sink",
55 "frequency",
56 NULL,
57 };
58
59 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
60 struct userdata *u;
61 assert(i && chunk && i->userdata);
62 u = i->userdata;
63
64 chunk->memblock = pa_memblock_ref(u->memblock);
65 chunk->index = u->peek_index;
66 chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
67 return 0;
68 }
69
70 static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
71 struct userdata *u;
72 assert(i && chunk && length && i->userdata);
73 u = i->userdata;
74
75 assert(chunk->memblock == u->memblock);
76 assert(length <= pa_memblock_get_length(u->memblock)-u->peek_index);
77
78 u->peek_index += length;
79
80 if (u->peek_index >= pa_memblock_get_length(u->memblock))
81 u->peek_index = 0;
82 }
83
84 static void sink_input_kill(pa_sink_input *i) {
85 struct userdata *u;
86 assert(i && i->userdata);
87 u = i->userdata;
88
89 pa_sink_input_disconnect(u->sink_input);
90 pa_sink_input_unref(u->sink_input);
91 u->sink_input = NULL;
92
93 pa_module_unload_request(u->module);
94 }
95
96 static void calc_sine(float *f, size_t l, float freq) {
97 size_t i;
98
99 l /= sizeof(float);
100
101 for (i = 0; i < l; i++)
102 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
103 }
104
105 int pa__init(pa_core *c, pa_module*m) {
106 pa_modargs *ma = NULL;
107 struct userdata *u;
108 pa_sink *sink;
109 const char *sink_name;
110 pa_sample_spec ss;
111 uint32_t frequency;
112 char t[256];
113 void *p;
114 pa_sink_input_new_data data;
115
116 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
117 pa_log("Failed to parse module arguments");
118 goto fail;
119 }
120
121 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
122 u->core = c;
123 u->module = m;
124 u->sink_input = NULL;
125 u->memblock = NULL;
126
127 sink_name = pa_modargs_get_value(ma, "sink", NULL);
128
129 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
130 pa_log("No such sink.");
131 goto fail;
132 }
133
134 ss.format = PA_SAMPLE_FLOAT32;
135 ss.rate = sink->sample_spec.rate;
136 ss.channels = 1;
137
138 frequency = 440;
139 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
140 pa_log("Invalid frequency specification");
141 goto fail;
142 }
143
144 u->memblock = pa_memblock_new(c->mempool, pa_bytes_per_second(&ss));
145 p = pa_memblock_acquire(u->memblock);
146 calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
147 pa_memblock_release(u->memblock);
148
149 snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
150
151 pa_sink_input_new_data_init(&data);
152 data.sink = sink;
153 data.driver = __FILE__;
154 data.name = t;
155 pa_sink_input_new_data_set_sample_spec(&data, &ss);
156 data.module = m;
157
158 if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
159 goto fail;
160
161 u->sink_input->peek = sink_input_peek;
162 u->sink_input->drop = sink_input_drop;
163 u->sink_input->kill = sink_input_kill;
164 u->sink_input->userdata = u;
165
166 u->peek_index = 0;
167
168 pa_modargs_free(ma);
169 return 0;
170
171 fail:
172 if (ma)
173 pa_modargs_free(ma);
174
175 pa__done(c, m);
176 return -1;
177 }
178
179 void pa__done(pa_core *c, pa_module*m) {
180 struct userdata *u = m->userdata;
181 assert(c && m);
182
183 if (!u)
184 return;
185
186 if (u->sink_input) {
187 pa_sink_input_disconnect(u->sink_input);
188 pa_sink_input_unref(u->sink_input);
189 }
190
191 if (u->memblock)
192 pa_memblock_unref(u->memblock);
193 pa_xfree(u);
194 }
195