]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[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 <assert.h>
30 #include <math.h>
31
32 #include <pulse/xmalloc.h>
33
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>
39
40 #include "module-sine-symdef.h"
41
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)
46
47 struct userdata {
48 pa_core *core;
49 pa_module *module;
50 pa_sink_input *sink_input;
51 pa_memblock *memblock;
52 size_t peek_index;
53 };
54
55 static const char* const valid_modargs[] = {
56 "sink",
57 "frequency",
58 NULL,
59 };
60
61 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
62 struct userdata *u;
63 assert(i && chunk && i->userdata);
64 u = i->userdata;
65
66 chunk->memblock = pa_memblock_ref(u->memblock);
67 chunk->index = u->peek_index;
68 chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
69 return 0;
70 }
71
72 static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
73 struct userdata *u;
74 assert(i && chunk && length && i->userdata);
75 u = i->userdata;
76
77 assert(chunk->memblock == u->memblock);
78 assert(length <= pa_memblock_get_length(u->memblock)-u->peek_index);
79
80 u->peek_index += length;
81
82 if (u->peek_index >= pa_memblock_get_length(u->memblock))
83 u->peek_index = 0;
84 }
85
86 static void sink_input_kill(pa_sink_input *i) {
87 struct userdata *u;
88 assert(i && i->userdata);
89 u = i->userdata;
90
91 pa_sink_input_disconnect(u->sink_input);
92 pa_sink_input_unref(u->sink_input);
93 u->sink_input = NULL;
94
95 pa_module_unload_request(u->module);
96 }
97
98 static void calc_sine(float *f, size_t l, float freq) {
99 size_t i;
100
101 l /= sizeof(float);
102
103 for (i = 0; i < l; i++)
104 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
105 }
106
107 int pa__init(pa_core *c, pa_module*m) {
108 pa_modargs *ma = NULL;
109 struct userdata *u;
110 pa_sink *sink;
111 const char *sink_name;
112 pa_sample_spec ss;
113 uint32_t frequency;
114 char t[256];
115 void *p;
116 pa_sink_input_new_data data;
117
118 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
119 pa_log("Failed to parse module arguments");
120 goto fail;
121 }
122
123 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
124 u->core = c;
125 u->module = m;
126 u->sink_input = NULL;
127 u->memblock = NULL;
128
129 sink_name = pa_modargs_get_value(ma, "sink", NULL);
130
131 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
132 pa_log("No such sink.");
133 goto fail;
134 }
135
136 ss.format = PA_SAMPLE_FLOAT32;
137 ss.rate = sink->sample_spec.rate;
138 ss.channels = 1;
139
140 frequency = 440;
141 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
142 pa_log("Invalid frequency specification");
143 goto fail;
144 }
145
146 u->memblock = pa_memblock_new(c->mempool, pa_bytes_per_second(&ss));
147 p = pa_memblock_acquire(u->memblock);
148 calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
149 pa_memblock_release(u->memblock);
150
151 snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
152
153 pa_sink_input_new_data_init(&data);
154 data.sink = sink;
155 data.driver = __FILE__;
156 data.name = t;
157 pa_sink_input_new_data_set_sample_spec(&data, &ss);
158 data.module = m;
159
160 if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
161 goto fail;
162
163 u->sink_input->peek = sink_input_peek;
164 u->sink_input->drop = sink_input_drop;
165 u->sink_input->kill = sink_input_kill;
166 u->sink_input->userdata = u;
167
168 u->peek_index = 0;
169
170 pa_modargs_free(ma);
171 return 0;
172
173 fail:
174 if (ma)
175 pa_modargs_free(ma);
176
177 pa__done(c, m);
178 return -1;
179 }
180
181 void pa__done(pa_core *c, pa_module*m) {
182 struct userdata *u = m->userdata;
183 assert(c && m);
184
185 if (!u)
186 return;
187
188 if (u->sink_input) {
189 pa_sink_input_disconnect(u->sink_input);
190 pa_sink_input_unref(u->sink_input);
191 }
192
193 if (u->memblock)
194 pa_memblock_unref(u->memblock);
195 pa_xfree(u);
196 }
197