]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
Convert most snprintf() calls to pa_snprintf()
[pulseaudio] / 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 #include <pulsecore/core-util.h>
40
41 #include "module-sine-symdef.h"
42
43 PA_MODULE_AUTHOR("Lennart Poettering")
44 PA_MODULE_DESCRIPTION("Sine wave generator")
45 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
46 PA_MODULE_VERSION(PACKAGE_VERSION)
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_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
63 struct userdata *u;
64
65 pa_assert(i);
66 u = i->userdata;
67 pa_assert(u);
68 pa_assert(chunk);
69
70 chunk->memblock = pa_memblock_ref(u->memblock);
71 chunk->index = u->peek_index;
72 chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
73
74 return 0;
75 }
76
77 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
78 struct userdata *u;
79 size_t l;
80
81 pa_assert(i);
82 u = i->userdata;
83 pa_assert(u);
84 pa_assert(length > 0);
85
86 u->peek_index += length;
87
88 l = pa_memblock_get_length(u->memblock);
89
90 while (u->peek_index >= l)
91 u->peek_index -= l;
92 }
93
94 static void sink_input_kill_cb(pa_sink_input *i) {
95 struct userdata *u;
96
97 pa_assert(i);
98 u = i->userdata;
99 pa_assert(u);
100
101 pa_sink_input_disconnect(u->sink_input);
102 pa_sink_input_unref(u->sink_input);
103 u->sink_input = NULL;
104
105 pa_module_unload_request(u->module);
106 }
107
108 static void calc_sine(float *f, size_t l, float freq) {
109 size_t i;
110
111 l /= sizeof(float);
112
113 for (i = 0; i < l; i++)
114 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
115 }
116
117 int pa__init(pa_core *c, pa_module*m) {
118 pa_modargs *ma = NULL;
119 struct userdata *u;
120 pa_sink *sink;
121 pa_sample_spec ss;
122 uint32_t frequency;
123 char t[256];
124 void *p;
125 pa_sink_input_new_data data;
126
127 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
128 pa_log("Failed to parse module arguments");
129 goto fail;
130 }
131
132 m->userdata = u = pa_xnew0(struct userdata, 1);
133 u->core = c;
134 u->module = m;
135 u->sink_input = NULL;
136 u->memblock = NULL;
137 u->peek_index = 0;
138
139 if (!(sink = pa_namereg_get(c, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK, 1))) {
140 pa_log("No such sink.");
141 goto fail;
142 }
143
144 ss.format = PA_SAMPLE_FLOAT32;
145 ss.rate = sink->sample_spec.rate;
146 ss.channels = 1;
147
148 frequency = 440;
149 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
150 pa_log("Invalid frequency specification");
151 goto fail;
152 }
153
154 u->memblock = pa_memblock_new(c->mempool, pa_bytes_per_second(&ss));
155 p = pa_memblock_acquire(u->memblock);
156 calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
157 pa_memblock_release(u->memblock);
158
159 pa_snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
160
161 pa_sink_input_new_data_init(&data);
162 data.sink = sink;
163 data.driver = __FILE__;
164 data.name = t;
165 pa_sink_input_new_data_set_sample_spec(&data, &ss);
166 data.module = m;
167
168 if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
169 goto fail;
170
171 u->sink_input->peek = sink_input_peek_cb;
172 u->sink_input->drop = sink_input_drop_cb;
173 u->sink_input->kill = sink_input_kill_cb;
174 u->sink_input->userdata = u;
175
176 pa_sink_input_put(u->sink_input);
177
178 pa_modargs_free(ma);
179 return 0;
180
181 fail:
182 if (ma)
183 pa_modargs_free(ma);
184
185 pa__done(c, m);
186 return -1;
187 }
188
189 void pa__done(pa_core *c, pa_module*m) {
190 struct userdata *u;
191
192 pa_assert(c);
193 pa_assert(m);
194
195 if (!(u = m->userdata))
196 return;
197
198 if (u->sink_input) {
199 pa_sink_input_disconnect(u->sink_input);
200 pa_sink_input_unref(u->sink_input);
201 }
202
203 if (u->memblock)
204 pa_memblock_unref(u->memblock);
205
206 pa_xfree(u);
207 }
208