]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
commit glitch-free work
[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 <math.h>
30
31 #include <pulse/xmalloc.h>
32
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>
39
40 #include "module-sine-symdef.h"
41
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>");
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_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
63 struct userdata *u;
64
65 pa_sink_input_assert_ref(i);
66 pa_assert_se(u = i->userdata);
67 pa_assert(chunk);
68
69 chunk->memblock = pa_memblock_ref(u->memblock);
70 chunk->length = pa_memblock_get_length(chunk->memblock);
71 chunk->index = 0;
72
73 return 0;
74 }
75
76 static void sink_input_kill_cb(pa_sink_input *i) {
77 struct userdata *u;
78
79 pa_sink_input_assert_ref(i);
80 pa_assert_se(u = i->userdata);
81
82 pa_sink_input_unlink(u->sink_input);
83 pa_sink_input_unref(u->sink_input);
84 u->sink_input = NULL;
85
86 pa_module_unload_request(u->module);
87 }
88
89 static void calc_sine(float *f, size_t l, float freq) {
90 size_t i;
91
92 l /= sizeof(float);
93
94 for (i = 0; i < l; i++)
95 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
96 }
97
98 int pa__init(pa_module*m) {
99 pa_modargs *ma = NULL;
100 struct userdata *u;
101 pa_sink *sink;
102 pa_sample_spec ss;
103 uint32_t frequency;
104 char t[256];
105 void *p;
106 pa_sink_input_new_data data;
107
108 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
109 pa_log("Failed to parse module arguments");
110 goto fail;
111 }
112
113 m->userdata = u = pa_xnew0(struct userdata, 1);
114 u->core = m->core;
115 u->module = m;
116 u->sink_input = NULL;
117 u->memblock = NULL;
118 u->peek_index = 0;
119
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.");
122 goto fail;
123 }
124
125 ss.format = PA_SAMPLE_FLOAT32;
126 ss.rate = sink->sample_spec.rate;
127 ss.channels = 1;
128
129 frequency = 440;
130 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
131 pa_log("Invalid frequency specification");
132 goto fail;
133 }
134
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);
139
140 pa_snprintf(t, sizeof(t), "%u Hz Sine", frequency);
141
142 pa_sink_input_new_data_init(&data);
143 data.sink = sink;
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);
148 data.module = m;
149
150 u->sink_input = pa_sink_input_new(m->core, &data, 0);
151 pa_sink_input_new_data_done(&data);
152
153 if (!u->sink_input)
154 goto fail;
155
156 u->sink_input->pop = sink_input_pop_cb;
157 u->sink_input->kill = sink_input_kill_cb;
158 u->sink_input->userdata = u;
159
160 pa_sink_input_put(u->sink_input);
161
162 pa_modargs_free(ma);
163 return 0;
164
165 fail:
166 if (ma)
167 pa_modargs_free(ma);
168
169 pa__done(m);
170 return -1;
171 }
172
173 void pa__done(pa_module*m) {
174 struct userdata *u;
175
176 pa_assert(m);
177
178 if (!(u = m->userdata))
179 return;
180
181 if (u->sink_input) {
182 pa_sink_input_unlink(u->sink_input);
183 pa_sink_input_unref(u->sink_input);
184 }
185
186 if (u->memblock)
187 pa_memblock_unref(u->memblock);
188
189 pa_xfree(u);
190 }