]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
fix shutdown when --disallow-module-loading=1 is passed
[pulseaudio] / src / modules / module-sine.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 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 <stdio.h>
27 #include <math.h>
28
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/modargs.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/core-util.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_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(FALSE);
44 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>");
45
46 struct userdata {
47 pa_core *core;
48 pa_module *module;
49 pa_sink_input *sink_input;
50 pa_memblock *memblock;
51 size_t peek_index;
52 };
53
54 static const char* const valid_modargs[] = {
55 "sink",
56 "frequency",
57 NULL,
58 };
59
60 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
61 struct userdata *u;
62
63 pa_sink_input_assert_ref(i);
64 pa_assert_se(u = i->userdata);
65 pa_assert(chunk);
66
67 chunk->memblock = pa_memblock_ref(u->memblock);
68 chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
69 chunk->index = u->peek_index;
70
71 u->peek_index = 0;
72
73 return 0;
74 }
75
76 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
77 size_t l;
78 struct userdata *u;
79
80 pa_sink_input_assert_ref(i);
81 pa_assert_se(u = i->userdata);
82
83 l = pa_memblock_get_length(u->memblock);
84 nbytes %= l;
85
86 if (u->peek_index >= nbytes)
87 u->peek_index -= nbytes;
88 else
89 u->peek_index = l + u->peek_index - nbytes;
90 }
91
92 static void sink_input_kill_cb(pa_sink_input *i) {
93 struct userdata *u;
94
95 pa_sink_input_assert_ref(i);
96 pa_assert_se(u = i->userdata);
97
98 pa_sink_input_unlink(u->sink_input);
99 pa_sink_input_unref(u->sink_input);
100 u->sink_input = NULL;
101
102 pa_module_unload_request(u->module, TRUE);
103 }
104
105 /* Called from IO thread context */
106 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
107 struct userdata *u;
108
109 pa_sink_input_assert_ref(i);
110 pa_assert_se(u = i->userdata);
111
112 /* If we are added for the first time, ask for a rewinding so that
113 * we are heard right-away. */
114 if (PA_SINK_INPUT_IS_LINKED(state) &&
115 i->thread_info.state == PA_SINK_INPUT_INIT)
116 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
117 }
118
119 static void calc_sine(float *f, size_t l, float freq) {
120 size_t i;
121
122 l /= sizeof(float);
123
124 for (i = 0; i < l; i++)
125 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
126 }
127
128 int pa__init(pa_module*m) {
129 pa_modargs *ma = NULL;
130 struct userdata *u;
131 pa_sink *sink;
132 pa_sample_spec ss;
133 uint32_t frequency;
134 void *p;
135 pa_sink_input_new_data data;
136
137 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
138 pa_log("Failed to parse module arguments");
139 goto fail;
140 }
141
142 m->userdata = u = pa_xnew0(struct userdata, 1);
143 u->core = m->core;
144 u->module = m;
145 u->sink_input = NULL;
146 u->memblock = NULL;
147 u->peek_index = 0;
148
149 if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK, 1))) {
150 pa_log("No such sink.");
151 goto fail;
152 }
153
154 ss.format = PA_SAMPLE_FLOAT32;
155 ss.rate = sink->sample_spec.rate;
156 ss.channels = 1;
157
158 frequency = 440;
159 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
160 pa_log("Invalid frequency specification");
161 goto fail;
162 }
163
164 u->memblock = pa_memblock_new(m->core->mempool, pa_bytes_per_second(&ss));
165 p = pa_memblock_acquire(u->memblock);
166 calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
167 pa_memblock_release(u->memblock);
168
169 pa_sink_input_new_data_init(&data);
170 data.sink = sink;
171 data.driver = __FILE__;
172 pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "%u Hz Sine", frequency);
173 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
174 pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
175 pa_sink_input_new_data_set_sample_spec(&data, &ss);
176 data.module = m;
177
178 u->sink_input = pa_sink_input_new(m->core, &data, 0);
179 pa_sink_input_new_data_done(&data);
180
181 if (!u->sink_input)
182 goto fail;
183
184 u->sink_input->pop = sink_input_pop_cb;
185 u->sink_input->process_rewind = sink_input_process_rewind_cb;
186 u->sink_input->kill = sink_input_kill_cb;
187 u->sink_input->state_change = sink_input_state_change_cb;
188 u->sink_input->userdata = u;
189
190 pa_sink_input_put(u->sink_input);
191
192 pa_modargs_free(ma);
193 return 0;
194
195 fail:
196 if (ma)
197 pa_modargs_free(ma);
198
199 pa__done(m);
200 return -1;
201 }
202
203 void pa__done(pa_module*m) {
204 struct userdata *u;
205
206 pa_assert(m);
207
208 if (!(u = m->userdata))
209 return;
210
211 if (u->sink_input) {
212 pa_sink_input_unlink(u->sink_input);
213 pa_sink_input_unref(u->sink_input);
214 }
215
216 if (u->memblock)
217 pa_memblock_unref(u->memblock);
218
219 pa_xfree(u);
220 }