2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.1 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
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/namereg.h>
34 #include <pulsecore/log.h>
36 #include "module-sine-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Sine wave generator");
40 PA_MODULE_VERSION(PACKAGE_VERSION
);
41 PA_MODULE_LOAD_ONCE(FALSE
);
43 "sink=<sink to connect to> "
44 "frequency=<frequency in Hz>");
49 pa_sink_input
*sink_input
;
54 static const char* const valid_modargs
[] = {
60 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
63 pa_sink_input_assert_ref(i
);
64 pa_assert_se(u
= i
->userdata
);
68 pa_memblock_ref(chunk
->memblock
);
70 chunk
->index
+= u
->peek_index
;
71 chunk
->length
-= u
->peek_index
;
78 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
81 pa_sink_input_assert_ref(i
);
82 pa_assert_se(u
= i
->userdata
);
84 nbytes
%= u
->memchunk
.length
;
86 if (u
->peek_index
>= nbytes
)
87 u
->peek_index
-= nbytes
;
89 u
->peek_index
= u
->memchunk
.length
+ u
->peek_index
- nbytes
;
92 static void sink_input_kill_cb(pa_sink_input
*i
) {
95 pa_sink_input_assert_ref(i
);
96 pa_assert_se(u
= i
->userdata
);
98 pa_sink_input_unlink(u
->sink_input
);
99 pa_sink_input_unref(u
->sink_input
);
100 u
->sink_input
= NULL
;
102 pa_module_unload_request(u
->module
, TRUE
);
105 /* Called from IO thread context */
106 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
109 pa_sink_input_assert_ref(i
);
110 pa_assert_se(u
= i
->userdata
);
112 /* If we are added for the first time, ask for a rewinding so that
113 * we are heard right-away. */
114 if (PA_SINK_INPUT_IS_LINKED(state
) &&
115 i
->thread_info
.state
== PA_SINK_INPUT_INIT
)
116 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
119 int pa__init(pa_module
*m
) {
120 pa_modargs
*ma
= NULL
;
125 pa_sink_input_new_data data
;
127 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
128 pa_log("Failed to parse module arguments");
132 if (!(sink
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink", NULL
), PA_NAMEREG_SINK
))) {
133 pa_log("No such sink.");
137 ss
.format
= PA_SAMPLE_FLOAT32
;
138 ss
.rate
= sink
->sample_spec
.rate
;
142 if (pa_modargs_get_value_u32(ma
, "frequency", &frequency
) < 0 || frequency
< 1 || frequency
> ss
.rate
/2) {
143 pa_log("Invalid frequency specification");
147 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
150 u
->sink_input
= NULL
;
153 pa_memchunk_sine(&u
->memchunk
, m
->core
->mempool
, ss
.rate
, frequency
);
155 pa_sink_input_new_data_init(&data
);
156 data
.driver
= __FILE__
;
158 pa_sink_input_new_data_set_sink(&data
, sink
, FALSE
);
159 pa_proplist_setf(data
.proplist
, PA_PROP_MEDIA_NAME
, "%u Hz Sine", frequency
);
160 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_ROLE
, "abstract");
161 pa_proplist_setf(data
.proplist
, "sine.hz", "%u", frequency
);
162 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
164 pa_sink_input_new(&u
->sink_input
, m
->core
, &data
);
165 pa_sink_input_new_data_done(&data
);
170 u
->sink_input
->pop
= sink_input_pop_cb
;
171 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
172 u
->sink_input
->kill
= sink_input_kill_cb
;
173 u
->sink_input
->state_change
= sink_input_state_change_cb
;
174 u
->sink_input
->userdata
= u
;
176 pa_sink_input_put(u
->sink_input
);
189 void pa__done(pa_module
*m
) {
194 if (!(u
= m
->userdata
))
198 pa_sink_input_unlink(u
->sink_input
);
199 pa_sink_input_unref(u
->sink_input
);
202 if (u
->memchunk
.memblock
)
203 pa_memblock_unref(u
->memchunk
.memblock
);