summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXANTRONIX Development2022-01-31 13:03:45 -0500
committerXANTRONIX Development2022-01-31 13:03:45 -0500
commit300f39e19369d44d3217bf1122d1feeaef4a0f5d (patch)
tree20beb967288817c26d976c8cbe462847aa63b859
parentd8e3d3fc0fb6e4f88f3e3c789575e38a74a01299 (diff)
downloadxas-300f39e19369d44d3217bf1122d1feeaef4a0f5d.tar.gz
xas-300f39e19369d44d3217bf1122d1feeaef4a0f5d.tar.bz2
xas-300f39e19369d44d3217bf1122d1feeaef4a0f5d.zip
I really don't like the fact that this compiles
-rw-r--r--examples/test.c64
-rw-r--r--include/xas/synth.h14
-rw-r--r--src/Makefile4
-rw-r--r--src/synth.c46
4 files changed, 100 insertions, 28 deletions
diff --git a/examples/test.c b/examples/test.c
index ebc32fe..a56da4b 100644
--- a/examples/test.c
+++ b/examples/test.c
@@ -6,17 +6,18 @@
#include <fcntl.h>
#include <math.h>
+#include <xas/synth.h>
#include <xas/audio.h>
#include <xas/riff.h>
-#define SYNTH_STATUS_ON (1 << 0)
+#define SYNTH_STATUS_CLEAR 0
+#define SYNTH_STATUS_ON (1 << 0)
typedef struct _synth_sine {
int flags;
float phase;
- size_t sample_rate,
- frequency;
+ size_t frequency;
} synth_sine;
static void usage(int argc, char **argv, const char *message, ...) {
@@ -36,31 +37,36 @@ static void usage(int argc, char **argv, const char *message, ...) {
exit(EX_USAGE);
}
-static ssize_t synth_sine_fill(synth_sine *synth,
- int16_t *samples,
- size_t count,
- xas_audio_stream *stream) {
- size_t i;
+static int16_t sine_sample(xas_synth *synth, synth_sine *sine) {
+ int16_t ret;
- for (i=0; i<count; i++) {
- if (synth->flags & SYNTH_STATUS_ON) {
- samples[i] = (int16_t)roundf((INT16_MAX >> 2) * sinf(synth->phase));
+ if (sine->flags & SYNTH_STATUS_ON) {
+ ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase));
- synth->phase += (2.0f * M_PI) / (stream->sample_rate / synth->frequency);
- } else {
- samples[i] = 0;
- }
+ sine->phase += (2.0f * M_PI) / (synth->sample_rate / sine->frequency);
+ } else {
+ ret = 0;
}
- return count;
+ return ret;
+}
+
+static void sine_destroy(xas_synth *synth, synth_sine *sine) {
+ return;
}
int main(int argc, char **argv) {
xas_audio_stream *stream;
+ xas_synth *synth;
+
+ synth_sine sine = {
+ .flags = SYNTH_STATUS_CLEAR,
+ .phase = 0.0f,
+ .frequency = 220
+ };
- int duration_s = 60,
- frequency = 220,
- sample_rate = 44100;
+ size_t sample_rate = 44100,
+ duration_s = 60;
int i;
@@ -76,14 +82,19 @@ int main(int argc, char **argv) {
goto error_riff_file_open;
}
- for (i=0; i < duration_s * frequency; i++) {
- float phase;
+ if ((synth = xas_synth_new(sample_rate,
+ (xas_synth_callback_sample)sine_sample,
+ (xas_synth_callback_destroy)sine_destroy,
+ &sine)) == NULL) {
+ goto error_synth_new;
+ }
- for (phase = 0.0; phase < 2 * M_PI; phase += (2 * M_PI) / (sample_rate / frequency)) {
- int16_t value = (int16_t)round((INT16_MAX >> 2) * sin(phase));
+ for (i=0; i < duration_s * sample_rate; i++) {
+ int16_t samples[1] = {
+ xas_synth_sample(synth)
+ };
- xas_audio_stream_write(stream, &value, 1);
- }
+ xas_synth_fill(synth, samples, 1);
}
xas_audio_stream_flush(stream);
@@ -91,6 +102,9 @@ int main(int argc, char **argv) {
return EX_OK;
+error_synth_new:
+ xas_audio_stream_destroy(stream);
+
error_riff_file_open:
return EX_OSERR;
}
diff --git a/include/xas/synth.h b/include/xas/synth.h
index 4d0fbd0..014d330 100644
--- a/include/xas/synth.h
+++ b/include/xas/synth.h
@@ -2,6 +2,9 @@
#define _XAS_SYNTH_H
#include <stdint.h>
+#include <sys/types.h>
+
+#include <xas/audio.h>
typedef struct _xas_synth xas_synth;
@@ -10,15 +13,24 @@ typedef int16_t (*xas_synth_callback_sample)(xas_synth *synth, void *ctx);
typedef void (*xas_synth_callback_destroy)(xas_synth *synth, void *ctx);
struct _xas_synth {
+ size_t sample_rate;
+
xas_synth_callback_sample sample;
xas_synth_callback_destroy destroy;
void *ctx;
};
-xas_synth *xas_synth_new(xas_synth_callback_sample sample,
+xas_synth *xas_synth_new(size_t sample_rate,
+ xas_synth_callback_sample sample,
xas_synth_callback_destroy destroy,
void *ctx);
void xas_synth_destroy(xas_synth *synth);
+int16_t xas_synth_sample(xas_synth *synth);
+
+ssize_t xas_synth_fill(xas_synth *synth,
+ int16_t *samples,
+ size_t count);
+
#endif /* _XAS_SYNTH_H */
diff --git a/src/Makefile b/src/Makefile
index d638619..a8853be 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -7,9 +7,9 @@ CC = $(CROSS)cc
CFLAGS += -I$(INCLUDE_PATH)
LDFLAGS +=
-HEADERS = audio.h mixer.h riff.h
+HEADERS = audio.h mixer.h synth.h riff.h
-OBJS = audio.o mixer.o riff.o
+OBJS = audio.o mixer.o synth.o riff.o
VERSION_MAJOR = 0
VERSION_MINOR = 0.1
diff --git a/src/synth.c b/src/synth.c
new file mode 100644
index 0000000..96c6d83
--- /dev/null
+++ b/src/synth.c
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+
+#include <xas/synth.h>
+
+xas_synth *xas_synth_new(size_t sample_rate,
+ xas_synth_callback_sample sample,
+ xas_synth_callback_destroy destroy,
+ void *ctx) {
+ xas_synth *synth;
+
+ if ((synth = malloc(sizeof(*synth))) == NULL) {
+ goto error_malloc_synth;
+ }
+
+ synth->sample_rate = sample_rate;
+ synth->sample = sample;
+ synth->destroy = destroy;
+ synth->ctx = ctx;
+
+ return synth;
+
+error_malloc_synth:
+ return NULL;
+}
+
+void xas_synth_destroy(xas_synth *synth) {
+ if (synth->destroy) {
+ synth->destroy(synth, synth->ctx);
+ }
+}
+
+int16_t xas_synth_sample(xas_synth *synth) {
+ return synth->sample(synth, synth->ctx);
+}
+
+ssize_t xas_synth_fill(xas_synth *synth,
+ int16_t *samples,
+ size_t count) {
+ size_t i;
+
+ for (i=0; i<count; i++) {
+ samples[i] = synth->sample(synth, synth->ctx);
+ }
+
+ return count;
+}