]> code.delx.au - pulseaudio/blob - src/modules/module-sine.c
i18n: Update Greek translation
[pulseaudio] / src / modules / module-sine.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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 PulseAudio; 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
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/namereg.h>
34 #include <pulsecore/log.h>
35
36 #include "module-sine-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Sine wave generator");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(false);
42 PA_MODULE_USAGE(
43 "sink=<sink to connect to> "
44 "rate=<sample rate> "
45 "frequency=<frequency in Hz>");
46
47 struct userdata {
48 pa_core *core;
49 pa_module *module;
50 pa_sink_input *sink_input;
51 pa_memchunk memchunk;
52 size_t peek_index;
53 };
54
55 static const char* const valid_modargs[] = {
56 "sink",
57 "rate",
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 = u->memchunk;
70 pa_memblock_ref(chunk->memblock);
71
72 chunk->index += u->peek_index;
73 chunk->length -= u->peek_index;
74
75 u->peek_index = 0;
76
77 return 0;
78 }
79
80 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
81 struct userdata *u;
82
83 pa_sink_input_assert_ref(i);
84 pa_assert_se(u = i->userdata);
85
86 nbytes %= u->memchunk.length;
87
88 if (u->peek_index >= nbytes)
89 u->peek_index -= nbytes;
90 else
91 u->peek_index = u->memchunk.length + u->peek_index - nbytes;
92 }
93
94 static void sink_input_kill_cb(pa_sink_input *i) {
95 struct userdata *u;
96
97 pa_sink_input_assert_ref(i);
98 pa_assert_se(u = i->userdata);
99
100 pa_sink_input_unlink(u->sink_input);
101 pa_sink_input_unref(u->sink_input);
102 u->sink_input = NULL;
103
104 pa_module_unload_request(u->module, true);
105 }
106
107 /* Called from IO thread context */
108 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
109 struct userdata *u;
110
111 pa_sink_input_assert_ref(i);
112 pa_assert_se(u = i->userdata);
113
114 /* If we are added for the first time, ask for a rewinding so that
115 * we are heard right-away. */
116 if (PA_SINK_INPUT_IS_LINKED(state) &&
117 i->thread_info.state == PA_SINK_INPUT_INIT)
118 pa_sink_input_request_rewind(i, 0, false, true, true);
119 }
120
121 int pa__init(pa_module*m) {
122 pa_modargs *ma = NULL;
123 struct userdata *u;
124 pa_sink *sink;
125 pa_sample_spec ss;
126 uint32_t frequency;
127 pa_sink_input_new_data data;
128
129 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
130 pa_log("Failed to parse module arguments");
131 goto fail;
132 }
133
134 if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) {
135 pa_log("No such sink.");
136 goto fail;
137 }
138
139 ss.format = PA_SAMPLE_FLOAT32;
140 ss.rate = sink->sample_spec.rate;
141 ss.channels = 1;
142
143 if (pa_modargs_get_sample_rate(ma, &ss.rate) < 0) {
144 pa_log("Invalid rate specification");
145 goto fail;
146 }
147
148 frequency = 440;
149 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
150 pa_log("Invalid frequency specification");
151 goto fail;
152 }
153
154 m->userdata = u = pa_xnew0(struct userdata, 1);
155 u->core = m->core;
156 u->module = m;
157 u->sink_input = NULL;
158
159 u->peek_index = 0;
160 pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
161
162 pa_sink_input_new_data_init(&data);
163 data.driver = __FILE__;
164 data.module = m;
165 pa_sink_input_new_data_set_sink(&data, sink, false);
166 pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "%u Hz Sine", frequency);
167 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
168 pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
169 pa_sink_input_new_data_set_sample_spec(&data, &ss);
170
171 pa_sink_input_new(&u->sink_input, m->core, &data);
172 pa_sink_input_new_data_done(&data);
173
174 if (!u->sink_input)
175 goto fail;
176
177 u->sink_input->pop = sink_input_pop_cb;
178 u->sink_input->process_rewind = sink_input_process_rewind_cb;
179 u->sink_input->kill = sink_input_kill_cb;
180 u->sink_input->state_change = sink_input_state_change_cb;
181 u->sink_input->userdata = u;
182
183 pa_sink_input_put(u->sink_input);
184
185 pa_modargs_free(ma);
186 return 0;
187
188 fail:
189 if (ma)
190 pa_modargs_free(ma);
191
192 pa__done(m);
193 return -1;
194 }
195
196 void pa__done(pa_module*m) {
197 struct userdata *u;
198
199 pa_assert(m);
200
201 if (!(u = m->userdata))
202 return;
203
204 if (u->sink_input) {
205 pa_sink_input_unlink(u->sink_input);
206 pa_sink_input_unref(u->sink_input);
207 }
208
209 if (u->memchunk.memblock)
210 pa_memblock_unref(u->memchunk.memblock);
211
212 pa_xfree(u);
213 }