From d8e3d3fc0fb6e4f88f3e3c789575e38a74a01299 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 31 Jan 2022 10:53:09 -0500 Subject: Add examples/ --- examples/Makefile | 18 +++++++++++ examples/test.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/test.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..52755fd --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,18 @@ +include ../mk/build.mk + +CC = $(CROSS)cc + +INCLUDE_PATH = ../include + +CFLAGS += -I$(INCLUDE_PATH) +LDFLAGS += -L../src -lxas -lm + +EXAMPLES = test + +all: $(EXAMPLES) + +$(EXAMPLES): %: %.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + $(RM) -f $(EXAMPLES) diff --git a/examples/test.c b/examples/test.c new file mode 100644 index 0000000..ebc32fe --- /dev/null +++ b/examples/test.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define SYNTH_STATUS_ON (1 << 0) + +typedef struct _synth_sine { + int flags; + float phase; + + size_t sample_rate, + frequency; +} synth_sine; + +static void usage(int argc, char **argv, const char *message, ...) { + va_list args; + + va_start(args, message); + + if (message) { + vfprintf(stderr, message, args); + fputc('\n', stderr); + } + + va_end(args); + + fprintf(stderr, "usage: %s output.wav\n", argv[0]); + + 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; + + for (i=0; iflags & SYNTH_STATUS_ON) { + samples[i] = (int16_t)roundf((INT16_MAX >> 2) * sinf(synth->phase)); + + synth->phase += (2.0f * M_PI) / (stream->sample_rate / synth->frequency); + } else { + samples[i] = 0; + } + } + + return count; +} + +int main(int argc, char **argv) { + xas_audio_stream *stream; + + int duration_s = 60, + frequency = 220, + sample_rate = 44100; + + int i; + + if (argc != 2) { + usage(argc, argv, "No output file provided"); + } + + if ((stream = xas_riff_file_open(argv[1], + 2, + sample_rate, + 1, + O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { + goto error_riff_file_open; + } + + for (i=0; i < duration_s * frequency; i++) { + float phase; + + 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)); + + xas_audio_stream_write(stream, &value, 1); + } + } + + xas_audio_stream_flush(stream); + xas_audio_stream_destroy(stream); + + return EX_OK; + +error_riff_file_open: + return EX_OSERR; +} -- cgit v1.2.3