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
31 #include <pulse/xmalloc.h>
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>
40 #include "module-sine-symdef.h"
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>");
51 pa_sink_input
*sink_input
;
52 pa_memblock
*memblock
;
56 static const char* const valid_modargs
[] = {
62 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
65 pa_sink_input_assert_ref(i
);
66 pa_assert_se(u
= i
->userdata
);
69 chunk
->memblock
= pa_memblock_ref(u
->memblock
);
70 chunk
->length
= pa_memblock_get_length(chunk
->memblock
);
76 static void sink_input_kill_cb(pa_sink_input
*i
) {
79 pa_sink_input_assert_ref(i
);
80 pa_assert_se(u
= i
->userdata
);
82 pa_sink_input_unlink(u
->sink_input
);
83 pa_sink_input_unref(u
->sink_input
);
86 pa_module_unload_request(u
->module
);
89 static void calc_sine(float *f
, size_t l
, float freq
) {
94 for (i
= 0; i
< l
; i
++)
95 f
[i
] = (float) sin((double) i
/l
*M_PI
*2*freq
)/2;
98 int pa__init(pa_module
*m
) {
99 pa_modargs
*ma
= NULL
;
106 pa_sink_input_new_data data
;
108 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
109 pa_log("Failed to parse module arguments");
113 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
116 u
->sink_input
= NULL
;
120 if (!(sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink", NULL
), PA_NAMEREG_SINK
, 1))) {
121 pa_log("No such sink.");
125 ss
.format
= PA_SAMPLE_FLOAT32
;
126 ss
.rate
= sink
->sample_spec
.rate
;
130 if (pa_modargs_get_value_u32(ma
, "frequency", &frequency
) < 0 || frequency
< 1 || frequency
> ss
.rate
/2) {
131 pa_log("Invalid frequency specification");
135 u
->memblock
= pa_memblock_new(m
->core
->mempool
, pa_bytes_per_second(&ss
));
136 p
= pa_memblock_acquire(u
->memblock
);
137 calc_sine(p
, pa_memblock_get_length(u
->memblock
), frequency
);
138 pa_memblock_release(u
->memblock
);
140 pa_snprintf(t
, sizeof(t
), "%u Hz Sine", frequency
);
142 pa_sink_input_new_data_init(&data
);
144 data
.driver
= __FILE__
;
145 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_NAME
, t
);
146 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_ROLE
, "abstract");
147 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
150 u
->sink_input
= pa_sink_input_new(m
->core
, &data
, 0);
151 pa_sink_input_new_data_done(&data
);
156 u
->sink_input
->pop
= sink_input_pop_cb
;
157 u
->sink_input
->kill
= sink_input_kill_cb
;
158 u
->sink_input
->userdata
= u
;
160 pa_sink_input_put(u
->sink_input
);
173 void pa__done(pa_module
*m
) {
178 if (!(u
= m
->userdata
))
182 pa_sink_input_unlink(u
->sink_input
);
183 pa_sink_input_unref(u
->sink_input
);
187 pa_memblock_unref(u
->memblock
);