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