diff options
author | XANTRONIX Development | 2022-01-31 10:53:09 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-01-31 10:53:09 -0500 |
commit | d8e3d3fc0fb6e4f88f3e3c789575e38a74a01299 (patch) | |
tree | 1dfecdcb3f273df1933a5a1e2eda179eef9c97fa /examples | |
parent | 6a280b4d4555000071356e754afe1b686a5bb434 (diff) | |
download | xas-d8e3d3fc0fb6e4f88f3e3c789575e38a74a01299.tar.gz xas-d8e3d3fc0fb6e4f88f3e3c789575e38a74a01299.tar.bz2 xas-d8e3d3fc0fb6e4f88f3e3c789575e38a74a01299.zip |
Add examples/
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 18 | ||||
-rw-r--r-- | examples/test.c | 96 |
2 files changed, 114 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <sysexits.h> +#include <fcntl.h> +#include <math.h> + +#include <xas/audio.h> +#include <xas/riff.h> + +#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; i<count; i++) { + if (synth->flags & 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; +} |