drop chunk argument from various drop() functions, since it doesn't make any sense...
[profile/ivi/pulseaudio-panda.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 <assert.h>
30 #include <math.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/sink-input.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/modargs.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/log.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_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
45 PA_MODULE_VERSION(PACKAGE_VERSION)
46
47 struct userdata {
48     pa_core *core;
49     pa_module *module;
50     pa_sink_input *sink_input;
51     pa_memblock *memblock;
52     size_t peek_index;
53 };
54
55 static const char* const valid_modargs[] = {
56     "sink",
57     "frequency",
58     NULL,
59 };
60
61 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
62     struct userdata *u;
63     
64     pa_assert(i);
65     u = i->userdata;
66     pa_assert(u);
67     pa_assert(chunk);
68
69     chunk->memblock = pa_memblock_ref(u->memblock);
70     chunk->index = u->peek_index;
71     chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
72     
73     return 0;
74 }
75
76 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
77     struct userdata *u;
78     size_t l;
79     
80     pa_assert(i);
81     u = i->userdata;
82     pa_assert(u);
83     pa_assert(length > 0);
84
85     u->peek_index += length;
86
87     l = pa_memblock_get_length(u->memblock);
88     
89     while (u->peek_index >= l)
90         u->peek_index -= l;
91 }
92
93 static void sink_input_kill_cb(pa_sink_input *i) {
94     struct userdata *u;
95     
96     pa_assert(i);
97     u = i->userdata;
98     pa_assert(u);
99
100     pa_sink_input_disconnect(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 static void calc_sine(float *f, size_t l, float freq) {
108     size_t i;
109
110     l /= sizeof(float);
111
112     for (i = 0; i < l; i++)
113         f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
114 }
115
116 int pa__init(pa_core *c, pa_module*m) {
117     pa_modargs *ma = NULL;
118     struct userdata *u;
119     pa_sink *sink;
120     pa_sample_spec ss;
121     uint32_t frequency;
122     char t[256];
123     void *p;
124     pa_sink_input_new_data data;
125
126     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
127         pa_log("Failed to parse module arguments");
128         goto fail;
129     }
130
131     m->userdata = u = pa_xnew0(struct userdata, 1);
132     u->core = c;
133     u->module = m;
134     u->sink_input = NULL;
135     u->memblock = NULL;
136     u->peek_index = 0;
137
138     if (!(sink = pa_namereg_get(c, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK, 1))) {
139         pa_log("No such sink.");
140         goto fail;
141     }
142
143     ss.format = PA_SAMPLE_FLOAT32;
144     ss.rate = sink->sample_spec.rate;
145     ss.channels = 1;
146
147     frequency = 440;
148     if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
149         pa_log("Invalid frequency specification");
150         goto fail;
151     }
152
153     u->memblock = pa_memblock_new(c->mempool, pa_bytes_per_second(&ss));
154     p = pa_memblock_acquire(u->memblock);
155     calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
156     pa_memblock_release(u->memblock);
157
158     snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
159
160     pa_sink_input_new_data_init(&data);
161     data.sink = sink;
162     data.driver = __FILE__;
163     data.name = t;
164     pa_sink_input_new_data_set_sample_spec(&data, &ss);
165     data.module = m;
166
167     if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
168         goto fail;
169
170     u->sink_input->peek = sink_input_peek_cb;
171     u->sink_input->drop = sink_input_drop_cb;
172     u->sink_input->kill = sink_input_kill_cb;
173     u->sink_input->userdata = u;
174
175     pa_sink_input_put(u->sink_input);
176     
177     pa_modargs_free(ma);
178     return 0;
179
180 fail:
181     if (ma)
182         pa_modargs_free(ma);
183
184     pa__done(c, m);
185     return -1;
186 }
187
188 void pa__done(pa_core *c, pa_module*m) {
189     struct userdata *u;
190     
191     pa_assert(c);
192     pa_assert(m);
193
194     if (!(u = m->userdata))
195         return;
196
197     if (u->sink_input) {
198         pa_sink_input_disconnect(u->sink_input);
199         pa_sink_input_unref(u->sink_input);
200     }
201
202     if (u->memblock)
203         pa_memblock_unref(u->memblock);
204     
205     pa_xfree(u);
206 }
207