]> code.delx.au - pulseaudio/blobdiff - src/modules/module-sine-source.c
bluetooth: Use a helper function for setting device_info_valid
[pulseaudio] / src / modules / module-sine-source.c
index dcc341620ea585f5089f4ab6775e89aeb4ea0397..bb3f8c41146e652d5d911c6beda853e96aad7829 100644 (file)
@@ -5,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
 #endif
 
 #include <stdlib.h>
-#include <sys/stat.h>
 #include <stdio.h>
 #include <errno.h>
-#include <string.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <limits.h>
-#include <sys/ioctl.h>
-#include <sys/poll.h>
 
-#include <pulse/xmalloc.h>
+#include <pulse/rtclock.h>
 #include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
 
-#include <pulsecore/core-error.h>
 #include <pulsecore/source.h>
 #include <pulsecore/module.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/thread-mq.h>
 #include <pulsecore/rtpoll.h>
-#include <pulsecore/rtclock.h>
 
 #include "module-sine-source-symdef.h"
 
 PA_MODULE_AUTHOR("Lennart Poettering");
 PA_MODULE_DESCRIPTION("Sine wave generator source");
 PA_MODULE_VERSION(PACKAGE_VERSION);
-PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_LOAD_ONCE(false);
 PA_MODULE_USAGE(
-        "rate=<sample rate> "
         "source_name=<name for the source> "
+        "source_properties=<properties for the source> "
+        "rate=<sample rate> "
         "frequency=<frequency in Hz>");
 
 #define DEFAULT_SOURCE_NAME "sine_input"
-#define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
+#define BLOCK_USEC (PA_USEC_PER_SEC * 2)
 
 struct userdata {
     pa_core *core;
@@ -79,8 +73,9 @@ struct userdata {
 };
 
 static const char* const valid_modargs[] = {
-    "rate",
     "source_name",
+    "source_properties",
+    "rate",
     "frequency",
     NULL
 };
@@ -96,17 +91,17 @@ static int source_process_msg(
 
     switch (code) {
 
-        case PA_SINK_MESSAGE_SET_STATE:
+        case PA_SOURCE_MESSAGE_SET_STATE:
 
-            if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
-                u->timestamp = pa_rtclock_usec();
+            if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
+                u->timestamp = pa_rtclock_now();
 
             break;
 
         case PA_SOURCE_MESSAGE_GET_LATENCY: {
             pa_usec_t now, left_to_fill;
 
-            now = pa_rtclock_usec();
+            now = pa_rtclock_now();
             left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
 
             *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
@@ -129,7 +124,7 @@ static void source_update_requested_latency_cb(pa_source *s) {
     if (u->block_usec == (pa_usec_t) -1)
         u->block_usec = s->thread_info.max_latency;
 
-    pa_log("new block msec = %lu", (unsigned long) (u->block_usec / PA_USEC_PER_MSEC));
+    pa_log_debug("new block msec = %lu", (unsigned long) (u->block_usec / PA_USEC_PER_MSEC));
 }
 
 static void process_render(struct userdata *u, pa_usec_t now) {
@@ -141,13 +136,12 @@ static void process_render(struct userdata *u, pa_usec_t now) {
 
         k = pa_usec_to_bytes_round_up(now + u->block_usec - u->timestamp, &u->source->sample_spec);
 
-        chunk.memblock = u->memchunk.memblock;
-        chunk.index = u->peek_index;
-        chunk.length = PA_MIN(u->memchunk.length - u->peek_index, k);
+        chunk = u->memchunk;
+        chunk.index += u->peek_index;
+        chunk.length = PA_MIN(chunk.length - u->peek_index, k);
 
-        pa_log_debug("posting %lu", (unsigned long) chunk.length);
+/*         pa_log_debug("posting %lu", (unsigned long) chunk.length); */
         pa_source_post(u->source, &chunk);
-        pa_memchunk_dump_to_file(&chunk, "sine");
 
         u->peek_index += chunk.length;
         while (u->peek_index >= u->memchunk.length)
@@ -165,9 +159,8 @@ static void thread_func(void *userdata) {
     pa_log_debug("Thread starting up");
 
     pa_thread_mq_install(&u->thread_mq);
-    pa_rtpoll_install(u->rtpoll);
 
-    u->timestamp = pa_rtclock_usec();
+    u->timestamp = pa_rtclock_now();
 
     for (;;) {
         int ret;
@@ -175,7 +168,7 @@ static void thread_func(void *userdata) {
         if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
             pa_usec_t now;
 
-            now = pa_rtclock_usec();
+            now = pa_rtclock_now();
 
             if (u->timestamp <= now)
                 process_render(u, now);
@@ -185,7 +178,7 @@ static void thread_func(void *userdata) {
             pa_rtpoll_set_timer_disabled(u->rtpoll);
 
         /* Hmm, nothing to do. Let's sleep */
-        if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
+        if ((ret = pa_rtpoll_run(u->rtpoll, true)) < 0)
             goto fail;
 
         if (ret == 0)
@@ -202,22 +195,12 @@ finish:
     pa_log_debug("Thread shutting down");
 }
 
-static void calc_sine(float *f, size_t l, double freq) {
-    size_t i;
-
-    l /= sizeof(float);
-
-    for (i = 0; i < l; i++)
-        f[i] = (float) sin((double) i/(double)l*M_PI*2*freq)/2;
-}
-
 int pa__init(pa_module*m) {
     struct userdata *u;
     pa_modargs *ma;
     pa_source_new_data data;
     uint32_t frequency;
     pa_sample_spec ss;
-    void *p;
 
     pa_assert(m);
 
@@ -248,14 +231,7 @@ int pa__init(pa_module*m) {
     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
 
     u->peek_index = 0;
-    pa_memchunk_reset(&u->memchunk);
-    u->memchunk.memblock = pa_memblock_new(m->core->mempool, (size_t) -1);
-    u->memchunk.length = pa_frame_align(pa_memblock_get_length(u->memchunk.memblock), &ss);
-
-    p = pa_memblock_acquire(u->memchunk.memblock);
-    calc_sine(p, u->memchunk.length,
-              (double) frequency * (double) pa_bytes_to_usec(u->memchunk.length, &ss) / (double) PA_USEC_PER_SEC);
-    pa_memblock_release(u->memchunk.memblock);
+    pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
 
     pa_source_new_data_init(&data);
     data.driver = __FILE__;
@@ -266,6 +242,12 @@ int pa__init(pa_module*m) {
     pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
     pa_source_new_data_set_sample_spec(&data, &ss);
 
+    if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+        pa_log("Invalid properties");
+        pa_source_new_data_done(&data);
+        goto fail;
+    }
+
     u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
     pa_source_new_data_done(&data);
 
@@ -278,13 +260,13 @@ int pa__init(pa_module*m) {
     u->source->update_requested_latency = source_update_requested_latency_cb;
     u->source->userdata = u;
 
+    u->block_usec = BLOCK_USEC;
+
     pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
     pa_source_set_rtpoll(u->source, u->rtpoll);
+    pa_source_set_fixed_latency(u->source, u->block_usec);
 
-    pa_source_set_latency_range(u->source, (pa_usec_t) -1, MAX_LATENCY_USEC);
-    u->block_usec = u->source->thread_info.max_latency;
-
-    if (!(u->thread = pa_thread_new(thread_func, u))) {
+    if (!(u->thread = pa_thread_new("sine-source", thread_func, u))) {
         pa_log("Failed to create thread.");
         goto fail;
     }
@@ -304,6 +286,15 @@ fail:
     return -1;
 }
 
+int pa__get_n_used(pa_module *m) {
+    struct userdata *u;
+
+    pa_assert(m);
+    pa_assert_se(u = m->userdata);
+
+    return pa_source_linked_by(u->source);
+}
+
 void pa__done(pa_module*m) {
     struct userdata *u;