summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-01-12 19:48:44 +0100
committerLennart Poettering <lennart@poettering.net>2009-01-12 19:48:44 +0100
commitb8e6aae08ee21d839ce8ebac6505a78c5ddcd8f5 (patch)
tree14c1054691e6836d1d0b2272a1fe8d69db558095
parent949de8156efbab1a2fb103ec414eaf983d7fa1c0 (diff)
add new API function pa_memchunk_sine()
Ease generation of sine signals. Try to make the repeatable sine memblock fit into a single mempool slot.
-rw-r--r--src/pulsecore/sample-util.c34
-rw-r--r--src/pulsecore/sample-util.h2
2 files changed, 36 insertions, 0 deletions
diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c
index bce30926..3be08efd 100644
--- a/src/pulsecore/sample-util.c
+++ b/src/pulsecore/sample-util.c
@@ -39,6 +39,7 @@
#include <pulsecore/core-error.h>
#include <pulsecore/macro.h>
#include <pulsecore/g711.h>
+#include <pulsecore/core-util.h>
#include "sample-util.h"
#include "endianmacros.h"
@@ -1015,3 +1016,36 @@ void pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn) {
fclose(f);
}
+
+static void calc_sine(float *f, size_t l, double freq) {
+ size_t i;
+
+ l /= sizeof(float);
+
+ for (i = 0; i < l; i++)
+ *(f++) = (float) 0.5f * sin((double) i*M_PI*2*freq / (double) l);
+}
+
+void pa_memchunk_sine(pa_memchunk *c, pa_mempool *pool, unsigned rate, unsigned freq) {
+ size_t l;
+ unsigned gcd, n;
+ void *p;
+
+ pa_memchunk_reset(c);
+
+ gcd = pa_gcd(rate, freq);
+ n = rate / gcd;
+
+ l = pa_mempool_block_size_max(pool) / sizeof(float);
+
+ l /= n;
+ if (l <= 0) l = 1;
+ l *= n;
+
+ c->length = l * sizeof(float);
+ c->memblock = pa_memblock_new(pool, c->length);
+
+ p = pa_memblock_acquire(c->memblock);
+ calc_sine(p, c->length, freq * l / rate);
+ pa_memblock_release(c->memblock);
+}
diff --git a/src/pulsecore/sample-util.h b/src/pulsecore/sample-util.h
index 36d19e48..2230aaf0 100644
--- a/src/pulsecore/sample-util.h
+++ b/src/pulsecore/sample-util.h
@@ -83,4 +83,6 @@ size_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec);
void pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn);
+void pa_memchunk_sine(pa_memchunk *c, pa_mempool *pool, unsigned rate, unsigned freq);
+
#endif