2004-09-01 12:21:06 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-09-01 12:21:06 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-09-01 12:21:06 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-09-01 12:21:06 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/sink-input.h>
|
|
|
|
|
#include <pulsecore/module.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/core-util.h>
|
2006-02-16 19:19:58 +00:00
|
|
|
|
2004-10-30 01:55:16 +00:00
|
|
|
#include "module-sine-symdef.h"
|
2004-09-01 12:21:06 +00:00
|
|
|
|
2007-11-09 18:25:40 +00:00
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering");
|
|
|
|
|
PA_MODULE_DESCRIPTION("Sine wave generator");
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION);
|
|
|
|
|
PA_MODULE_LOAD_ONCE(FALSE);
|
|
|
|
|
PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>");
|
2004-09-11 23:17:38 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
struct userdata {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_core *core;
|
|
|
|
|
pa_module *module;
|
|
|
|
|
pa_sink_input *sink_input;
|
|
|
|
|
pa_memblock *memblock;
|
2004-09-01 12:21:06 +00:00
|
|
|
size_t peek_index;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
"sink",
|
|
|
|
|
"frequency",
|
|
|
|
|
NULL,
|
|
|
|
|
};
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
|
2004-09-01 12:21:06 +00:00
|
|
|
struct userdata *u;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(i);
|
2004-09-01 12:21:06 +00:00
|
|
|
u = i->userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
|
|
|
|
pa_assert(chunk);
|
2004-09-01 12:21:06 +00:00
|
|
|
|
|
|
|
|
chunk->memblock = pa_memblock_ref(u->memblock);
|
|
|
|
|
chunk->index = u->peek_index;
|
2007-10-28 19:13:50 +00:00
|
|
|
chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
|
|
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
|
2004-09-01 12:21:06 +00:00
|
|
|
struct userdata *u;
|
2007-10-28 19:13:50 +00:00
|
|
|
size_t l;
|
2004-09-01 12:21:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(i);
|
|
|
|
|
u = i->userdata;
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
pa_assert(length > 0);
|
2004-09-01 12:21:06 +00:00
|
|
|
|
|
|
|
|
u->peek_index += length;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
l = pa_memblock_get_length(u->memblock);
|
|
|
|
|
|
|
|
|
|
while (u->peek_index >= l)
|
|
|
|
|
u->peek_index -= l;
|
2004-09-01 12:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void sink_input_kill_cb(pa_sink_input *i) {
|
2004-09-01 12:21:06 +00:00
|
|
|
struct userdata *u;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(i);
|
2004-09-01 12:21:06 +00:00
|
|
|
u = i->userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
2004-09-01 12:21:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_unlink(u->sink_input);
|
2004-09-14 20:53:25 +00:00
|
|
|
pa_sink_input_unref(u->sink_input);
|
2004-09-01 12:21:06 +00:00
|
|
|
u->sink_input = NULL;
|
2004-09-14 20:53:25 +00:00
|
|
|
|
|
|
|
|
pa_module_unload_request(u->module);
|
2004-09-01 12:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void calc_sine(float *f, size_t l, float freq) {
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
l /= sizeof(float);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
int pa__init(pa_module*m) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_modargs *ma = NULL;
|
2004-09-01 12:21:06 +00:00
|
|
|
struct userdata *u;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *sink;
|
|
|
|
|
pa_sample_spec ss;
|
2004-09-01 12:21:06 +00:00
|
|
|
uint32_t frequency;
|
|
|
|
|
char t[256];
|
2007-10-28 19:13:50 +00:00
|
|
|
void *p;
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data data;
|
2004-09-01 12:21:06 +00:00
|
|
|
|
|
|
|
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Failed to parse module arguments");
|
2004-09-01 12:21:06 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
m->userdata = u = pa_xnew0(struct userdata, 1);
|
|
|
|
|
u->core = m->core;
|
2004-09-14 20:53:25 +00:00
|
|
|
u->module = m;
|
2004-09-01 12:21:06 +00:00
|
|
|
u->sink_input = NULL;
|
|
|
|
|
u->memblock = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
u->peek_index = 0;
|
2004-09-01 12:21:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK, 1))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("No such sink.");
|
2004-09-01 12:21:06 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ss.format = PA_SAMPLE_FLOAT32;
|
|
|
|
|
ss.rate = sink->sample_spec.rate;
|
|
|
|
|
ss.channels = 1;
|
|
|
|
|
|
|
|
|
|
frequency = 440;
|
|
|
|
|
if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Invalid frequency specification");
|
2004-09-01 12:21:06 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
u->memblock = pa_memblock_new(m->core->mempool, pa_bytes_per_second(&ss));
|
|
|
|
|
p = pa_memblock_acquire(u->memblock);
|
|
|
|
|
calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
|
|
|
|
|
pa_memblock_release(u->memblock);
|
2006-11-06 13:06:01 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
|
2006-08-13 16:19:56 +00:00
|
|
|
|
|
|
|
|
pa_sink_input_new_data_init(&data);
|
|
|
|
|
data.sink = sink;
|
|
|
|
|
data.driver = __FILE__;
|
|
|
|
|
data.name = t;
|
|
|
|
|
pa_sink_input_new_data_set_sample_spec(&data, &ss);
|
|
|
|
|
data.module = m;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(u->sink_input = pa_sink_input_new(m->core, &data, 0)))
|
2004-09-01 12:21:06 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
u->sink_input->peek = sink_input_peek_cb;
|
|
|
|
|
u->sink_input->drop = sink_input_drop_cb;
|
|
|
|
|
u->sink_input->kill = sink_input_kill_cb;
|
2004-09-01 12:21:06 +00:00
|
|
|
u->sink_input->userdata = u;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_put(u->sink_input);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
fail:
|
|
|
|
|
if (ma)
|
|
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa__done(m);
|
2004-09-01 12:21:06 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa__done(pa_module*m) {
|
|
|
|
|
struct userdata *u;
|
|
|
|
|
|
|
|
|
|
pa_assert(m);
|
2004-09-01 12:21:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(u = m->userdata))
|
2004-09-01 12:21:06 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
if (u->sink_input) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_unlink(u->sink_input);
|
2004-09-14 20:53:25 +00:00
|
|
|
pa_sink_input_unref(u->sink_input);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
if (u->memblock)
|
|
|
|
|
pa_memblock_unref(u->memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2004-09-01 12:21:06 +00:00
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|