diff options
author | XANTRONIX Development | 2022-01-31 20:47:58 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-01-31 20:47:58 -0500 |
commit | 59d4694efd295217e15c90ca5636c4bdcfecf222 (patch) | |
tree | 2a1c78bab9707b67877c0c133815e20355f00393 /examples | |
parent | 09e1154a36f692e9ad2cee5f47fc66459f2568a2 (diff) | |
download | xas-59d4694efd295217e15c90ca5636c4bdcfecf222.tar.gz xas-59d4694efd295217e15c90ca5636c4bdcfecf222.tar.bz2 xas-59d4694efd295217e15c90ca5636c4bdcfecf222.zip |
This is getting annoyingly close to working...
Diffstat (limited to 'examples')
-rw-r--r-- | examples/test.c | 114 |
1 files changed, 83 insertions, 31 deletions
diff --git a/examples/test.c b/examples/test.c index e1da192..49937aa 100644 --- a/examples/test.c +++ b/examples/test.c @@ -8,6 +8,7 @@ #include <xas/synth.h> #include <xas/audio.h> +#include <xas/mixer.h> #include <xas/riff.h> #define SYNTH_STATUS_CLEAR 0 @@ -51,61 +52,112 @@ static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { return ret; } -static void sine_destroy(xas_synth *synth, synth_sine *sine) { +static void sine_cleanup(xas_synth *synth, synth_sine *sine) { return; } int main(int argc, char **argv) { - xas_audio_stream *stream; - xas_synth *synth; + xas_mixer *mixer; - synth_sine sine = { - .flags = SYNTH_STATUS_ON, - .phase = 0.0f, - .frequency = 220 + xas_audio_stream *synth_l, + *synth_r, + *wave; + + synth_sine sine_channels[2] = { + { SYNTH_STATUS_ON, 0.0f, 220 }, + { SYNTH_STATUS_ON, 0.0f, 420 }, }; size_t sample_rate = 44100, - duration_s = 60; - - int i; + duration_s = 60, + 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) { + if ((wave = xas_riff_file_open(argv[1], + XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + XAS_AUDIO_STREAM_STEREO, + O_WRONLY | O_CREAT | O_TRUNC)) == NULL) { goto error_riff_file_open; } - 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; + if ((synth_l = xas_synth_new(XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + sample_rate, + (xas_synth_callback_sample)sine_sample, + (xas_synth_callback_cleanup)sine_cleanup, + &sine_channels[0])) == NULL) { + goto error_synth_new_l; } - for (i=0; i < duration_s * sample_rate; i++) { - int16_t samples[1] = { - xas_synth_sample(synth) - }; + if ((synth_r = xas_synth_new(XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + sample_rate, + (xas_synth_callback_sample)sine_sample, + (xas_synth_callback_cleanup)sine_cleanup, + &sine_channels[1])) == NULL) { + goto error_synth_new_r; + } + + if ((mixer = xas_mixer_new(XAS_AUDIO_STREAM_PCM_16_BIT, + sample_rate, + XAS_AUDIO_STREAM_STEREO, + sample_rate)) == NULL) { + goto error_mixer_new; + } - xas_audio_stream_write(stream, - samples, - 1); + if (xas_mixer_input_add(mixer, synth_l, 0.3, -1.0) == NULL) { + goto error_mixer_input_add; } - xas_audio_stream_flush(stream); - xas_audio_stream_destroy(stream); + if (xas_mixer_input_add(mixer, synth_r, 0.3, 1.0) == NULL) { + goto error_mixer_input_add; + } + + printf("Good here\n"); + + for (i=0; i<duration_s; i++) { + int16_t *buf; + ssize_t readlen; + + printf("one second\n"); + + if ((readlen = xas_audio_stream_read(mixer->output, + (void **)&buf, + sample_rate)) < 0) { + goto error_audio_stream_read; + } + + if (xas_audio_stream_write(wave, buf, readlen) < 0) { + goto error_audio_stream_write; + } + } + + xas_audio_stream_flush(wave); + + xas_mixer_destroy(mixer); + xas_audio_stream_destroy(synth_r); + xas_audio_stream_destroy(synth_l); + xas_audio_stream_destroy(wave); return EX_OK; -error_synth_new: - xas_audio_stream_destroy(stream); +error_audio_stream_write: +error_audio_stream_read: +error_mixer_input_add: + xas_mixer_destroy(mixer); + +error_mixer_new: + xas_audio_stream_destroy(synth_r); + +error_synth_new_r: + xas_audio_stream_destroy(synth_l); + +error_synth_new_l: + xas_audio_stream_destroy(wave); error_riff_file_open: return EX_OSERR; |