From 718527b8f93ca893f996fdd1bf46063d1b5d3ecb Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 27 Feb 2022 21:29:32 -0500 Subject: Refactor src/synth.c Changes: * Implement xas_synth type to consolidate synthesiser implementation and different tone generator types * Refactor examples/ code to remove duplicate synthesiser code --- examples/say.c | 82 +++++++++++++++++++++++++----------------------------- examples/spatial.c | 50 +++++++++++---------------------- examples/test.c | 73 +++++++++++++++++++----------------------------- 3 files changed, 82 insertions(+), 123 deletions(-) (limited to 'examples') diff --git a/examples/say.c b/examples/say.c index 92856f2..fa16242 100644 --- a/examples/say.c +++ b/examples/say.c @@ -40,41 +40,15 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(EX_USAGE); } -static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { - static float tau = 2.0f * M_PI; - - int16_t ret; - - if (sine->flags & SYNTH_STATUS_ON) { - ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase)); - - sine->phase += tau / (synth->format.sample_rate / sine->frequency); - - if (sine->phase > tau) { - sine->phase -= tau; - } - } else { - ret = 0; - } - - return ret; -} - -static void sine_cleanup(xas_synth *synth, synth_sine *sine) { - return; -} - int main(int argc, char **argv) { - synth_sine sine_channels[2] = { - { SYNTH_STATUS_ON, 0.0f, 220 }, - { SYNTH_STATUS_ON, 0.0f, 420 }, - }; - xas_mixer *mixer; xas_bank *bank; xas_vox *vox; + xas_synth *sine_l, + *sine_r; + xas_bank_player *player; xas_audio_stream *voice, @@ -97,6 +71,18 @@ int main(int argc, char **argv) { usage(argc, argv, "No output file provided"); } + if ((sine_l = xas_synth_new(format, + buffer_size, + XAS_SYNTH_SINE)) == NULL) { + goto error_synth_new_l; + } + + if ((sine_r = xas_synth_new(format, + buffer_size, + XAS_SYNTH_SINE)) == NULL) { + goto error_synth_new_r; + } + if ((vox = xas_vox_new(format, buffer_size, "/usr/bin/text2wave")) == NULL) { @@ -125,20 +111,12 @@ int main(int argc, char **argv) { goto error_bank_player_stream_new; } - if ((synth_l = xas_synth_new((xas_synth_callback_sample)sine_sample, - (xas_synth_callback_cleanup)sine_cleanup, - format, - buffer_size, - &sine_channels[0])) == NULL) { - goto error_synth_new_l; + if ((synth_l = xas_synth_new_stream(sine_l)) == NULL) { + goto error_synth_new_stream_l; } - if ((synth_r = xas_synth_new((xas_synth_callback_sample)sine_sample, - (xas_synth_callback_cleanup)sine_cleanup, - format, - buffer_size, - &sine_channels[1])) == NULL) { - goto error_synth_new_r; + if ((synth_r = xas_synth_new_stream(sine_r)) == NULL) { + goto error_synth_new_stream_r; } if ((mixer = xas_mixer_new(format, buffer_size)) == NULL) { @@ -157,10 +135,18 @@ int main(int argc, char **argv) { goto error_mixer_input_add; } + xas_synth_set_frequency(sine_l, 220); + xas_synth_start(sine_l); + + xas_synth_set_frequency(sine_l, 420); + xas_synth_start(sine_r); + /* * Time to fill the sample bank, meow! */ - xas_vox_say(vox, "I want to eat your soul. You don't understand. I really want to eat your soul.\n"); + xas_vox_say(vox, "I want to eat your soul."); + xas_vox_say(vox, "You don't understand."); + xas_vox_say(vox, "I really want to eat your soul."); xas_vox_generate(vox); @@ -192,6 +178,8 @@ int main(int argc, char **argv) { xas_mixer_destroy(mixer); xas_audio_stream_destroy(synth_r); xas_audio_stream_destroy(synth_l); + xas_synth_destroy(sine_r); + xas_synth_destroy(sine_l); xas_audio_stream_destroy(player_stream); xas_audio_stream_destroy(voice); xas_audio_stream_destroy(output); @@ -210,10 +198,10 @@ error_mixer_input_add: error_mixer_new: xas_audio_stream_destroy(synth_r); -error_synth_new_r: +error_synth_new_stream_r: xas_audio_stream_destroy(synth_l); -error_synth_new_l: +error_synth_new_stream_l: xas_audio_stream_destroy(player_stream); error_bank_player_stream_new: @@ -232,5 +220,11 @@ error_bank_new: xas_vox_destroy(vox); error_vox_new: + xas_synth_destroy(sine_r); + +error_synth_new_r: + xas_synth_destroy(sine_l); + +error_synth_new_l: return EX_OSERR; } diff --git a/examples/spatial.c b/examples/spatial.c index ec606d6..c5c658c 100644 --- a/examples/spatial.c +++ b/examples/spatial.c @@ -39,32 +39,11 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(EX_USAGE); } -static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { - int16_t ret; - static float tau = 2.0f * M_PI; - - if (sine->flags & SYNTH_STATUS_ON) { - ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase)); - - sine->phase += tau / (synth->format.sample_rate / sine->frequency); - - if (sine->phase > tau) { - sine->phase -= tau; - } - } else { - ret = 0; - } - - return ret; -} - -static void sine_cleanup(xas_synth *synth, synth_sine *sine) { - return; -} - int main(int argc, char **argv) { xas_spatial_scene *scene; + xas_synth *sine; + xas_audio_stream *synth, *voice, *output, @@ -72,11 +51,6 @@ int main(int argc, char **argv) { xas_vox *vox; - synth_sine sine_channels[2] = { - { SYNTH_STATUS_ON, 0.0f, 2600 }, - { SYNTH_STATUS_ON, 0.0f, 420 }, - }; - xas_audio_format format = { .channels = XAS_AUDIO_STEREO, .sample_size = XAS_AUDIO_PCM_16_BIT, @@ -102,14 +76,16 @@ int main(int argc, char **argv) { goto error_riff_new_file; } - if ((synth = xas_synth_new((xas_synth_callback_sample)sine_sample, - (xas_synth_callback_cleanup)sine_cleanup, - format, - buffer_size, - &sine_channels[0])) == NULL) { + if ((sine = xas_synth_new(format, + buffer_size, + XAS_SYNTH_SINE)) == NULL) { goto error_synth_new; } + if ((synth = xas_synth_new_stream(sine)) == NULL) { + goto error_synth_new_stream; + } + if ((vox = xas_vox_new(format, buffer_size, "/usr/bin/text2wave")) == NULL) { @@ -136,7 +112,7 @@ int main(int argc, char **argv) { if (xas_spatial_scene_add_object(scene, (xas_spatial_coord){ 5.2, 0.0, 0.0 }, synth, - &sine_channels[0]) == NULL) { + sine) == NULL) { goto error_spatial_scene_add_object; } @@ -147,6 +123,9 @@ int main(int argc, char **argv) { goto error_spatial_scene_add_object; } + xas_synth_set_frequency(sine, 320); + xas_synth_start(sine); + xas_vox_sayf(vox, "I want to eat your soul.\n"); xas_vox_sayf(vox, "You don't understand.\n"); xas_vox_sayf(vox, "I really want to eat your soul.\n"); @@ -196,6 +175,9 @@ error_vox_stream_new: error_vox_new: xas_audio_stream_destroy(synth); +error_synth_new_stream: + xas_synth_destroy(sine); + error_synth_new: xas_audio_stream_destroy(wave); diff --git a/examples/test.c b/examples/test.c index 1fbd8c4..272e32d 100644 --- a/examples/test.c +++ b/examples/test.c @@ -11,16 +11,6 @@ #include #include -#define SYNTH_STATUS_CLEAR 0 -#define SYNTH_STATUS_ON (1 << 0) - -typedef struct _synth_sine { - int flags; - float phase; - - size_t frequency; -} synth_sine; - static void usage(int argc, char **argv, const char *message, ...) { va_list args; @@ -38,29 +28,6 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(EX_USAGE); } -static int16_t sine_sample(xas_synth *synth, synth_sine *sine) { - int16_t ret; - static float tau = 2.0f * M_PI; - - if (sine->flags & SYNTH_STATUS_ON) { - ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(sine->phase)); - - sine->phase += tau / (synth->format.sample_rate / sine->frequency); - - if (sine->phase > tau) { - sine->phase -= tau; - } - } else { - ret = 0; - } - - return ret; -} - -static void sine_cleanup(xas_synth *synth, synth_sine *sine) { - return; -} - int main(int argc, char **argv) { xas_mixer *mixer; @@ -68,10 +35,8 @@ int main(int argc, char **argv) { *synth_r, *wave; - synth_sine sine_channels[2] = { - { SYNTH_STATUS_ON, 0.0f, 220 }, - { SYNTH_STATUS_ON, 0.0f, 420 }, - }; + xas_synth *sine_l, + *sine_r; xas_audio_format format = { .channels = XAS_AUDIO_STEREO, @@ -93,22 +58,26 @@ int main(int argc, char **argv) { goto error_riff_new_file; } - if ((synth_l = xas_synth_new((xas_synth_callback_sample)sine_sample, - (xas_synth_callback_cleanup)sine_cleanup, - format, + if ((sine_l = xas_synth_new(format, buffer_size, - &sine_channels[0])) == NULL) { + XAS_SYNTH_SINE)) == NULL) { goto error_synth_new_l; } - if ((synth_r = xas_synth_new((xas_synth_callback_sample)sine_sample, - (xas_synth_callback_cleanup)sine_cleanup, - format, + if ((synth_l = xas_synth_new_stream(sine_l)) == NULL) { + goto error_synth_new_stream_l; + } + + if ((sine_r = xas_synth_new(format, buffer_size, - &sine_channels[1])) == NULL) { + XAS_SYNTH_SINE)) == NULL) { goto error_synth_new_r; } + if ((synth_r = xas_synth_new_stream(sine_r)) == NULL) { + goto error_synth_new_stream_r; + } + if ((mixer = xas_mixer_new(format, buffer_size)) == NULL) { goto error_mixer_new; } @@ -121,6 +90,12 @@ int main(int argc, char **argv) { goto error_mixer_input_add; } + xas_synth_set_frequency(sine_l, 2600); + xas_synth_start(sine_l); + + xas_synth_set_frequency(sine_r, 420); + xas_synth_start(sine_r); + for (i=0; i