]> code.delx.au - pulseaudio/blob - polyp/module-sine.c
add refernce counting for sinks, sources, sink-inputs and source-outputs
[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
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)
41
42 struct userdata {
43 struct pa_core *core;
44 struct pa_module *module;
45 struct pa_sink_input *sink_input;
46 struct pa_memblock *memblock;
47 size_t peek_index;
48 };
49
50 static const char* const valid_modargs[] = {
51 "sink",
52 "frequency",
53 NULL,
54 };
55
56 static int sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
57 struct userdata *u;
58 assert(i && chunk && i->userdata);
59 u = i->userdata;
60
61 chunk->memblock = pa_memblock_ref(u->memblock);
62 chunk->index = u->peek_index;
63 chunk->length = u->memblock->length - u->peek_index;
64 return 0;
65 }
66
67 static void sink_input_drop(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
68 struct userdata *u;
69 assert(i && chunk && length && i->userdata);
70 u = i->userdata;
71
72 assert(chunk->memblock == u->memblock && length <= u->memblock->length-u->peek_index);
73
74 u->peek_index += length;
75
76 if (u->peek_index >= u->memblock->length)
77 u->peek_index = 0;
78 }
79
80 static void sink_input_kill(struct pa_sink_input *i) {
81 struct userdata *u;
82 assert(i && i->userdata);
83 u = i->userdata;
84
85 pa_sink_input_disconnect(u->sink_input);
86 pa_sink_input_unref(u->sink_input);
87 u->sink_input = NULL;
88
89 pa_module_unload_request(u->module);
90 }
91
92 static void calc_sine(float *f, size_t l, float freq) {
93 size_t i;
94
95 l /= sizeof(float);
96
97 for (i = 0; i < l; i++)
98 f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
99 }
100
101 int pa__init(struct pa_core *c, struct pa_module*m) {
102 struct pa_modargs *ma = NULL;
103 struct userdata *u;
104 struct pa_sink *sink;
105 const char *sink_name;
106 struct pa_sample_spec ss;
107 uint32_t frequency;
108 char t[256];
109
110 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
111 pa_log(__FILE__": Failed to parse module arguments\n");
112 goto fail;
113 }
114
115 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
116 u->core = c;
117 u->module = m;
118 u->sink_input = NULL;
119 u->memblock = NULL;
120
121 sink_name = pa_modargs_get_value(ma, "sink", NULL);
122
123 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
124 pa_log(__FILE__": No such sink.\n");
125 goto fail;
126 }
127
128 ss.format = PA_SAMPLE_FLOAT32;
129 ss.rate = sink->sample_spec.rate;
130 ss.channels = 1;
131
132 frequency = 440;
133 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
134 pa_log(__FILE__": Invalid frequency specification\n");
135 goto fail;
136 }
137
138 u->memblock = pa_memblock_new(pa_bytes_per_second(&ss), c->memblock_stat);
139 calc_sine(u->memblock->data, u->memblock->length, frequency);
140
141 snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
142 if (!(u->sink_input = pa_sink_input_new(sink, t, &ss, 0)))
143 goto fail;
144
145 u->sink_input->peek = sink_input_peek;
146 u->sink_input->drop = sink_input_drop;
147 u->sink_input->kill = sink_input_kill;
148 u->sink_input->userdata = u;
149 u->sink_input->owner = m;
150
151 u->peek_index = 0;
152
153 pa_modargs_free(ma);
154 return 0;
155
156 fail:
157 if (ma)
158 pa_modargs_free(ma);
159
160 pa__done(c, m);
161 return -1;
162 }
163
164 void pa__done(struct pa_core *c, struct pa_module*m) {
165 struct userdata *u = m->userdata;
166 assert(c && m);
167
168 if (!u)
169 return;
170
171 if (u->sink_input) {
172 pa_sink_input_disconnect(u->sink_input);
173 pa_sink_input_unref(u->sink_input);
174 }
175
176 if (u->memblock)
177 pa_memblock_unref(u->memblock);
178 pa_xfree(u);
179 }
180