4 This file is part of PulseAudio.
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.
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.
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
30 #include <pulse/xmalloc.h>
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>
38 #include "module-sine-symdef.h"
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)
48 pa_sink_input *sink_input;
49 pa_memblock *memblock;
53 static const char* const valid_modargs[] = {
59 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
61 assert(i && chunk && i->userdata);
64 chunk->memblock = pa_memblock_ref(u->memblock);
65 chunk->index = u->peek_index;
66 chunk->length = u->memblock->length - u->peek_index;
70 static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
72 assert(i && chunk && length && i->userdata);
75 assert(chunk->memblock == u->memblock && length <= u->memblock->length-u->peek_index);
77 u->peek_index += length;
79 if (u->peek_index >= u->memblock->length)
83 static void sink_input_kill(pa_sink_input *i) {
85 assert(i && i->userdata);
88 pa_sink_input_disconnect(u->sink_input);
89 pa_sink_input_unref(u->sink_input);
92 pa_module_unload_request(u->module);
95 static void calc_sine(float *f, size_t l, float freq) {
100 for (i = 0; i < l; i++)
101 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
104 int pa__init(pa_core *c, pa_module*m) {
105 pa_modargs *ma = NULL;
108 const char *sink_name;
112 pa_sink_input_new_data data;
114 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
115 pa_log("Failed to parse module arguments");
119 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
122 u->sink_input = NULL;
125 sink_name = pa_modargs_get_value(ma, "sink", NULL);
127 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
128 pa_log("No such sink.");
132 ss.format = PA_SAMPLE_FLOAT32;
133 ss.rate = sink->sample_spec.rate;
137 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
138 pa_log("Invalid frequency specification");
142 u->memblock = pa_memblock_new(c->mempool, pa_bytes_per_second(&ss));
143 calc_sine(u->memblock->data, u->memblock->length, frequency);
145 snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
147 pa_sink_input_new_data_init(&data);
149 data.driver = __FILE__;
151 pa_sink_input_new_data_set_sample_spec(&data, &ss);
154 if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
157 u->sink_input->peek = sink_input_peek;
158 u->sink_input->drop = sink_input_drop;
159 u->sink_input->kill = sink_input_kill;
160 u->sink_input->userdata = u;
175 void pa__done(pa_core *c, pa_module*m) {
176 struct userdata *u = m->userdata;
183 pa_sink_input_disconnect(u->sink_input);
184 pa_sink_input_unref(u->sink_input);
188 pa_memblock_unref(u->memblock);