]>
code.delx.au - pulseaudio/blob - polyp/module-sine.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include "sink-input.h"
37 PA_MODULE_AUTHOR("Lennart Poettering")
38 PA_MODULE_DESCRIPTION("Sine wave generator")
39 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
40 PA_MODULE_VERSION(PACKAGE_VERSION
)
44 struct pa_sink_input
*sink_input
;
45 struct pa_memblock
*memblock
;
49 static const char* const valid_modargs
[] = {
55 static int sink_input_peek(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
57 assert(i
&& chunk
&& i
->userdata
);
60 chunk
->memblock
= pa_memblock_ref(u
->memblock
);
61 chunk
->index
= u
->peek_index
;
62 chunk
->length
= u
->memblock
->length
- u
->peek_index
;
66 static void sink_input_drop(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
68 assert(i
&& chunk
&& length
&& i
->userdata
);
71 assert(chunk
->memblock
== u
->memblock
&& length
<= u
->memblock
->length
-u
->peek_index
);
73 u
->peek_index
+= length
;
75 if (u
->peek_index
>= u
->memblock
->length
)
79 static void sink_input_kill(struct pa_sink_input
*i
) {
81 assert(i
&& i
->userdata
);
84 pa_sink_input_free(u
->sink_input
);
88 static void calc_sine(float *f
, size_t l
, float freq
) {
93 for (i
= 0; i
< l
; i
++)
94 f
[i
] = (float) sin((double) i
/l
*M_PI
*2*freq
)/2;
97 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
98 struct pa_modargs
*ma
= NULL
;
100 struct pa_sink
*sink
;
101 const char *sink_name
;
102 struct pa_sample_spec ss
;
106 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
107 pa_log(__FILE__
": Failed to parse module arguments\n");
111 m
->userdata
= u
= pa_xmalloc(sizeof(struct userdata
));
113 u
->sink_input
= NULL
;
116 sink_name
= pa_modargs_get_value(ma
, "sink", NULL
);
118 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
, 1))) {
119 pa_log(__FILE__
": No such sink.\n");
123 ss
.format
= PA_SAMPLE_FLOAT32
;
124 ss
.rate
= sink
->sample_spec
.rate
;
128 if (pa_modargs_get_value_u32(ma
, "frequency", &frequency
) < 0 || frequency
< 1 || frequency
> ss
.rate
/2) {
129 pa_log(__FILE__
": Invalid frequency specification\n");
133 u
->memblock
= pa_memblock_new(pa_bytes_per_second(&ss
), c
->memblock_stat
);
134 calc_sine(u
->memblock
->data
, u
->memblock
->length
, frequency
);
136 snprintf(t
, sizeof(t
), "Sine Generator at %u Hz", frequency
);
137 if (!(u
->sink_input
= pa_sink_input_new(sink
, t
, &ss
)))
140 u
->sink_input
->peek
= sink_input_peek
;
141 u
->sink_input
->drop
= sink_input_drop
;
142 u
->sink_input
->kill
= sink_input_kill
;
143 u
->sink_input
->userdata
= u
;
144 u
->sink_input
->owner
= m
;
159 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
160 struct userdata
*u
= m
->userdata
;
167 pa_sink_input_free(u
->sink_input
);
169 pa_memblock_unref(u
->memblock
);