merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / modules / module-sine.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <math.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/sink-input.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/modargs.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/core-util.h>
39
40 #include "module-sine-symdef.h"
41
42 PA_MODULE_AUTHOR("Lennart Poettering");
43 PA_MODULE_DESCRIPTION("Sine wave generator");
44 PA_MODULE_VERSION(PACKAGE_VERSION);
45 PA_MODULE_LOAD_ONCE(FALSE);
46 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>");
47
48 struct userdata {
49     pa_core *core;
50     pa_module *module;
51     pa_sink_input *sink_input;
52     pa_memblock *memblock;
53     size_t peek_index;
54 };
55
56 static const char* const valid_modargs[] = {
57     "sink",
58     "frequency",
59     NULL,
60 };
61
62 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
63     struct userdata *u;
64
65     pa_sink_input_assert_ref(i);
66     pa_assert_se(u = i->userdata);
67     pa_assert(chunk);
68
69     chunk->memblock = pa_memblock_ref(u->memblock);
70     chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
71     chunk->index = u->peek_index;
72
73     u->peek_index = 0;
74
75     return 0;
76 }
77
78 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
79     size_t l;
80     struct userdata *u;
81
82     pa_sink_input_assert_ref(i);
83     pa_assert_se(u = i->userdata);
84
85     l = pa_memblock_get_length(u->memblock);
86     nbytes %= l;
87
88     if (u->peek_index >= nbytes)
89         u->peek_index -= nbytes;
90     else
91         u->peek_index = l + u->peek_index - nbytes;
92 }
93
94 static void sink_input_kill_cb(pa_sink_input *i) {
95     struct userdata *u;
96
97     pa_sink_input_assert_ref(i);
98     pa_assert_se(u = i->userdata);
99
100     pa_sink_input_unlink(u->sink_input);
101     pa_sink_input_unref(u->sink_input);
102     u->sink_input = NULL;
103
104     pa_module_unload_request(u->module);
105 }
106
107 /* Called from IO thread context */
108 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
109     struct userdata *u;
110
111     pa_sink_input_assert_ref(i);
112     pa_assert_se(u = i->userdata);
113
114     /* If we are added for the first time, ask for a rewinding so that
115      * we are heard right-away. */
116     if (PA_SINK_INPUT_IS_LINKED(state) &&
117         i->thread_info.state == PA_SINK_INPUT_INIT)
118         pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
119 }
120
121 static void calc_sine(float *f, size_t l, float freq) {
122     size_t i;
123
124     l /= sizeof(float);
125
126     for (i = 0; i < l; i++)
127         f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
128 }
129
130 int pa__init(pa_module*m) {
131     pa_modargs *ma = NULL;
132     struct userdata *u;
133     pa_sink *sink;
134     pa_sample_spec ss;
135     uint32_t frequency;
136     void *p;
137     pa_sink_input_new_data data;
138
139     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
140         pa_log("Failed to parse module arguments");
141         goto fail;
142     }
143
144     m->userdata = u = pa_xnew0(struct userdata, 1);
145     u->core = m->core;
146     u->module = m;
147     u->sink_input = NULL;
148     u->memblock = NULL;
149     u->peek_index = 0;
150
151     if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK, 1))) {
152         pa_log("No such sink.");
153         goto fail;
154     }
155
156     ss.format = PA_SAMPLE_FLOAT32;
157     ss.rate = sink->sample_spec.rate;
158     ss.channels = 1;
159
160     frequency = 440;
161     if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
162         pa_log("Invalid frequency specification");
163         goto fail;
164     }
165
166     u->memblock = pa_memblock_new(m->core->mempool, pa_bytes_per_second(&ss));
167     p = pa_memblock_acquire(u->memblock);
168     calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
169     pa_memblock_release(u->memblock);
170
171     pa_sink_input_new_data_init(&data);
172     data.sink = sink;
173     data.driver = __FILE__;
174     pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "%u Hz Sine", frequency);
175     pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
176     pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
177     pa_sink_input_new_data_set_sample_spec(&data, &ss);
178     data.module = m;
179
180     u->sink_input = pa_sink_input_new(m->core, &data, 0);
181     pa_sink_input_new_data_done(&data);
182
183     if (!u->sink_input)
184         goto fail;
185
186     u->sink_input->pop = sink_input_pop_cb;
187     u->sink_input->process_rewind = sink_input_process_rewind_cb;
188     u->sink_input->kill = sink_input_kill_cb;
189     u->sink_input->state_change = sink_input_state_change_cb;
190     u->sink_input->userdata = u;
191
192     pa_sink_input_put(u->sink_input);
193
194     pa_modargs_free(ma);
195     return 0;
196
197 fail:
198     if (ma)
199         pa_modargs_free(ma);
200
201     pa__done(m);
202     return -1;
203 }
204
205 void pa__done(pa_module*m) {
206     struct userdata *u;
207
208     pa_assert(m);
209
210     if (!(u = m->userdata))
211         return;
212
213     if (u->sink_input) {
214         pa_sink_input_unlink(u->sink_input);
215         pa_sink_input_unref(u->sink_input);
216     }
217
218     if (u->memblock)
219         pa_memblock_unref(u->memblock);
220
221     pa_xfree(u);
222 }