4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
32 #include <pulse/xmalloc.h>
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>
40 #include "module-sine-symdef.h"
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
)
50 pa_sink_input
*sink_input
;
51 pa_memblock
*memblock
;
55 static const char* const valid_modargs
[] = {
61 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
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
;
76 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
84 pa_assert(length
> 0);
86 u
->peek_index
+= length
;
88 l
= pa_memblock_get_length(u
->memblock
);
90 while (u
->peek_index
>= l
)
94 static void sink_input_kill_cb(pa_sink_input
*i
) {
96 pa_assert(i
&& i
->userdata
);
99 pa_sink_input_disconnect(u
->sink_input
);
100 pa_sink_input_unref(u
->sink_input
);
101 u
->sink_input
= NULL
;
103 pa_module_unload_request(u
->module
);
106 static void calc_sine(float *f
, size_t l
, float freq
) {
111 for (i
= 0; i
< l
; i
++)
112 f
[i
] = (float) sin((double) i
/l
*M_PI
*2*freq
)/2;
115 int pa__init(pa_core
*c
, pa_module
*m
) {
116 pa_modargs
*ma
= NULL
;
123 pa_sink_input_new_data data
;
125 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
126 pa_log("Failed to parse module arguments");
130 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
133 u
->sink_input
= NULL
;
137 if (!(sink
= pa_namereg_get(c
, pa_modargs_get_value(ma
, "sink", NULL
), PA_NAMEREG_SINK
, 1))) {
138 pa_log("No such sink.");
142 ss
.format
= PA_SAMPLE_FLOAT32
;
143 ss
.rate
= sink
->sample_spec
.rate
;
147 if (pa_modargs_get_value_u32(ma
, "frequency", &frequency
) < 0 || frequency
< 1 || frequency
> ss
.rate
/2) {
148 pa_log("Invalid frequency specification");
152 u
->memblock
= pa_memblock_new(c
->mempool
, pa_bytes_per_second(&ss
));
153 p
= pa_memblock_acquire(u
->memblock
);
154 calc_sine(p
, pa_memblock_get_length(u
->memblock
), frequency
);
155 pa_memblock_release(u
->memblock
);
157 snprintf(t
, sizeof(t
), "Sine Generator at %u Hz", frequency
);
159 pa_sink_input_new_data_init(&data
);
161 data
.driver
= __FILE__
;
163 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
166 if (!(u
->sink_input
= pa_sink_input_new(c
, &data
, 0)))
169 u
->sink_input
->peek
= sink_input_peek_cb
;
170 u
->sink_input
->drop
= sink_input_drop_cb
;
171 u
->sink_input
->kill
= sink_input_kill_cb
;
172 u
->sink_input
->userdata
= u
;
174 pa_sink_input_put(u
->sink_input
);
187 void pa__done(pa_core
*c
, pa_module
*m
) {
193 if (!(u
= m
->userdata
))
197 pa_sink_input_disconnect(u
->sink_input
);
198 pa_sink_input_unref(u
->sink_input
);
202 pa_memblock_unref(u
->memblock
);