]> code.delx.au - pulseaudio/blob - polyp/module-sine.c
393f929aa953684e7ce8f95d2d06b694d5ca33d0
[pulseaudio] / polyp / module-sine.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <math.h>
29
30 #include "sink-input.h"
31 #include "module.h"
32 #include "modargs.h"
33 #include "xmalloc.h"
34 #include "namereg.h"
35 #include "log.h"
36 #include "module-sine-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering")
39 PA_MODULE_DESCRIPTION("Sine wave generator")
40 PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
41 PA_MODULE_VERSION(PACKAGE_VERSION)
42
43 struct userdata {
44 struct pa_core *core;
45 struct pa_module *module;
46 struct pa_sink_input *sink_input;
47 struct pa_memblock *memblock;
48 size_t peek_index;
49 };
50
51 static const char* const valid_modargs[] = {
52 "sink",
53 "frequency",
54 NULL,
55 };
56
57 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
58 struct userdata *u;
59 assert(i && chunk && i->userdata);
60 u = i->userdata;
61
62 chunk->memblock = pa_memblock_ref(u->memblock);
63 chunk->index = u->peek_index;
64 chunk->length = u->memblock->length - u->peek_index;
65 return 0;
66 }
67
68 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
69 struct userdata *u;
70 assert(i && chunk && length && i->userdata);
71 u = i->userdata;
72
73 assert(chunk->memblock == u->memblock && length <= u->memblock->length-u->peek_index);
74
75 u->peek_index += length;
76
77 if (u->peek_index >= u->memblock->length)
78 u->peek_index = 0;
79 }
80
81 static void sink_input_kill(struct pa_sink_input *i) {
82 struct userdata *u;
83 assert(i && i->userdata);
84 u = i->userdata;
85
86 pa_sink_input_disconnect(u->sink_input);
87 pa_sink_input_unref(u->sink_input);
88 u->sink_input = NULL;
89
90 pa_module_unload_request(u->module);
91 }
92
93 static void calc_sine(float *f, size_t l, float freq) {
94 size_t i;
95
96 l /= sizeof(float);
97
98 for (i = 0; i < l; i++)
99 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
100 }
101
102 int pa__init(struct pa_core *c, struct pa_module*m) {
103 struct pa_modargs *ma = NULL;
104 struct userdata *u;
105 struct pa_sink *sink;
106 const char *sink_name;
107 struct pa_sample_spec ss;
108 uint32_t frequency;
109 char t[256];
110
111 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
112 pa_log(__FILE__": Failed to parse module arguments\n");
113 goto fail;
114 }
115
116 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
117 u->core = c;
118 u->module = m;
119 u->sink_input = NULL;
120 u->memblock = NULL;
121
122 sink_name = pa_modargs_get_value(ma, "sink", NULL);
123
124 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
125 pa_log(__FILE__": No such sink.\n");
126 goto fail;
127 }
128
129 ss.format = PA_SAMPLE_FLOAT32;
130 ss.rate = sink->sample_spec.rate;
131 ss.channels = 1;
132
133 frequency = 440;
134 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
135 pa_log(__FILE__": Invalid frequency specification\n");
136 goto fail;
137 }
138
139 u->memblock = pa_memblock_new(pa_bytes_per_second(&ss), c->memblock_stat);
140 calc_sine(u->memblock->data, u->memblock->length, frequency);
141
142 snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
143 if (!(u->sink_input = pa_sink_input_new(sink, t, &ss, 0, -1)))
144 goto fail;
145
146 u->sink_input->peek = sink_input_peek;
147 u->sink_input->drop = sink_input_drop;
148 u->sink_input->kill = sink_input_kill;
149 u->sink_input->userdata = u;
150 u->sink_input->owner = m;
151
152 u->peek_index = 0;
153
154 pa_modargs_free(ma);
155 return 0;
156
157 fail:
158 if (ma)
159 pa_modargs_free(ma);
160
161 pa__done(c, m);
162 return -1;
163 }
164
165 void pa__done(struct pa_core *c, struct pa_module*m) {
166 struct userdata *u = m->userdata;
167 assert(c && m);
168
169 if (!u)
170 return;
171
172 if (u->sink_input) {
173 pa_sink_input_disconnect(u->sink_input);
174 pa_sink_input_unref(u->sink_input);
175 }
176
177 if (u->memblock)
178 pa_memblock_unref(u->memblock);
179 pa_xfree(u);
180 }
181