]> code.delx.au - pulseaudio/blobdiff - src/modules/module-sine.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / modules / module-sine.c
index baf37346b83b4dbcc70cbce027c7a63cce12f47d..573a7c0f1962dc8e6e5c91f2a9962757f3a5f21f 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
@@ -7,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -26,8 +24,6 @@
 #endif
 
 #include <stdio.h>
-#include <assert.h>
-#include <math.h>
 
 #include <pulse/xmalloc.h>
 
 
 #include "module-sine-symdef.h"
 
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("Sine wave generator")
-PA_MODULE_USAGE("sink=<sink to connect to> frequency=<frequency in Hz>")
-PA_MODULE_VERSION(PACKAGE_VERSION)
+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>");
 
 struct userdata {
     pa_core *core;
     pa_module *module;
     pa_sink_input *sink_input;
-    pa_memblock *memblock;
+    pa_memchunk memchunk;
     size_t peek_index;
 };
 
@@ -58,61 +57,71 @@ static const char* const valid_modargs[] = {
     NULL,
 };
 
-static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
     struct userdata *u;
-    assert(i && chunk && i->userdata);
-    u = i->userdata;
 
-    chunk->memblock = pa_memblock_ref(u->memblock);
-    chunk->index = u->peek_index;
-    chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+    pa_assert(chunk);
+
+    *chunk = u->memchunk;
+    pa_memblock_ref(chunk->memblock);
+
+    chunk->index += u->peek_index;
+    chunk->length -= u->peek_index;
+
+    u->peek_index = 0;
+
     return 0;
 }
 
-static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
     struct userdata *u;
-    assert(i && chunk && length && i->userdata);
-    u = i->userdata;
 
-    assert(chunk->memblock == u->memblock);
-    assert(length <= pa_memblock_get_length(u->memblock)-u->peek_index);
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
 
-    u->peek_index += length;
+    nbytes %= u->memchunk.length;
 
-    if (u->peek_index >= pa_memblock_get_length(u->memblock))
-        u->peek_index = 0;
+    if (u->peek_index >= nbytes)
+        u->peek_index -= nbytes;
+    else
+        u->peek_index = u->memchunk.length + u->peek_index - nbytes;
 }
 
-static void sink_input_kill(pa_sink_input *i) {
+static void sink_input_kill_cb(pa_sink_input *i) {
     struct userdata *u;
-    assert(i && i->userdata);
-    u = i->userdata;
 
-    pa_sink_input_disconnect(u->sink_input);
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_sink_input_unlink(u->sink_input);
     pa_sink_input_unref(u->sink_input);
     u->sink_input = NULL;
 
-    pa_module_unload_request(u->module);
+    pa_module_unload_request(u->module, true);
 }
 
-static void calc_sine(float *f, size_t l, float freq) {
-    size_t i;
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+    struct userdata *u;
 
-    l /= sizeof(float);
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
 
-    for (i = 0; i < l; i++)
-        f[i] = (float) sin((double) i/l*M_PI*2*freq)/2;
+    /* If we are added for the first time, ask for a rewinding so that
+     * we are heard right-away. */
+    if (PA_SINK_INPUT_IS_LINKED(state) &&
+        i->thread_info.state == PA_SINK_INPUT_INIT)
+        pa_sink_input_request_rewind(i, 0, false, true, true);
 }
 
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
     pa_modargs *ma = NULL;
     struct userdata *u;
     pa_sink *sink;
-    const char *sink_name;
     pa_sample_spec ss;
     uint32_t frequency;
-    char t[256];
-    void *p;
     pa_sink_input_new_data data;
 
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
@@ -120,15 +129,7 @@ int pa__init(pa_core *c, pa_module*m) {
         goto fail;
     }
 
-    m->userdata = u = pa_xmalloc(sizeof(struct userdata));
-    u->core = c;
-    u->module = m;
-    u->sink_input = NULL;
-    u->memblock = NULL;
-
-    sink_name = pa_modargs_get_value(ma, "sink", NULL);
-
-    if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
+    if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) {
         pa_log("No such sink.");
         goto fail;
     }
@@ -143,29 +144,36 @@ int pa__init(pa_core *c, pa_module*m) {
         goto fail;
     }
 
-    u->memblock = pa_memblock_new(c->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);
+    m->userdata = u = pa_xnew0(struct userdata, 1);
+    u->core = m->core;
+    u->module = m;
+    u->sink_input = NULL;
 
-    snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
+    u->peek_index = 0;
+    pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
 
     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;
+    pa_sink_input_new_data_set_sink(&data, sink, false);
+    pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "%u Hz Sine", frequency);
+    pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
+    pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
+    pa_sink_input_new_data_set_sample_spec(&data, &ss);
+
+    pa_sink_input_new(&u->sink_input, m->core, &data);
+    pa_sink_input_new_data_done(&data);
 
-    if (!(u->sink_input = pa_sink_input_new(c, &data, 0)))
+    if (!u->sink_input)
         goto fail;
 
-    u->sink_input->peek = sink_input_peek;
-    u->sink_input->drop = sink_input_drop;
-    u->sink_input->kill = sink_input_kill;
+    u->sink_input->pop = sink_input_pop_cb;
+    u->sink_input->process_rewind = sink_input_process_rewind_cb;
+    u->sink_input->kill = sink_input_kill_cb;
+    u->sink_input->state_change = sink_input_state_change_cb;
     u->sink_input->userdata = u;
 
-    u->peek_index = 0;
+    pa_sink_input_put(u->sink_input);
 
     pa_modargs_free(ma);
     return 0;
@@ -174,24 +182,25 @@ fail:
     if (ma)
         pa_modargs_free(ma);
 
-    pa__done(c, m);
+    pa__done(m);
     return -1;
 }
 
-void pa__done(pa_core *c, pa_module*m) {
-    struct userdata *u = m->userdata;
-    assert(c && m);
+void pa__done(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
 
-    if (!u)
+    if (!(u = m->userdata))
         return;
 
     if (u->sink_input) {
-        pa_sink_input_disconnect(u->sink_input);
+        pa_sink_input_unlink(u->sink_input);
         pa_sink_input_unref(u->sink_input);
     }
 
-    if (u->memblock)
-        pa_memblock_unref(u->memblock);
+    if (u->memchunk.memblock)
+        pa_memblock_unref(u->memchunk.memblock);
+
     pa_xfree(u);
 }
-