From 9708b5b0e2b5de0e60fb6b0ef7925bc9375b85cf Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 17 Mar 2022 11:33:22 -0400 Subject: Implement xas_object_call_table Changes: * Implement xas_object_call_table as a static structure to allow creating a static dispatch table on a per-compilation-unit basis * Refactor xas_object to provide a single pointer to a xas_object_call_table * Refactor all object implementations to use xas_object_call_table --- src/bank.c | 20 +++++++++------- src/mixer.c | 16 +++++++------ src/object.c | 76 ++++++++++++++++++++++++++++++++++++++++++++---------------- src/synth.c | 22 ++++++++++++++---- src/vox.c | 14 +++++++---- 5 files changed, 102 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/bank.c b/src/bank.c index 474d4f5..43a0dff 100644 --- a/src/bank.c +++ b/src/bank.c @@ -151,6 +151,16 @@ static int set_entry(xas_bank_player *player, size_t index, size_t entry) { return xas_bank_player_set_entry(player, entry); } +static xas_object_call_table call_table = { + .start = (xas_object_start_callback)player_start, + .stop = (xas_object_stop_callback)player_stop, + .set_gain = (xas_object_set_gain_callback)set_gain, + .set_entry = (xas_object_set_entry_callback)set_entry, + .set_flags = (xas_object_set_flags_callback)xas_bank_player_set_flags, + .stream_new = (xas_object_stream_new_callback)xas_bank_player_stream_new, + .destroy = (xas_object_destroy_callback)xas_bank_player_destroy +}; + xas_bank_player *xas_bank_player_new(xas_bank *bank) { xas_bank_player *player; @@ -158,15 +168,7 @@ xas_bank_player *xas_bank_player_new(xas_bank *bank) { goto error_malloc_player; } - xas_object_init(&player->obj); - - player->obj.start = (xas_object_start_callback)player_start; - player->obj.stop = (xas_object_stop_callback)player_stop; - player->obj.set_gain = (xas_object_set_gain_callback)set_gain; - player->obj.set_entry = (xas_object_set_entry_callback)set_entry; - player->obj.set_flags = (xas_object_set_flags_callback)xas_bank_player_set_flags; - player->obj.stream_new = (xas_object_stream_new_callback)xas_bank_player_stream_new; - player->obj.destroy = (xas_object_destroy_callback)xas_bank_player_destroy; + player->obj.table = &call_table; player->bank = bank; player->status = XAS_BANK_PLAYER_STOPPED; diff --git a/src/mixer.c b/src/mixer.c index ef232e6..478670f 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -152,6 +152,14 @@ static int noop() { return 0; } +static xas_object_call_table call_table = { + .start = (xas_object_start_callback)noop, + .stop = (xas_object_stop_callback)noop, + .set_gain = (xas_object_set_gain_callback)set_gain, + .stream_new = (xas_object_stream_new_callback)xas_mixer_output, + .destroy = (xas_object_destroy_callback)xas_mixer_destroy +}; + xas_mixer *xas_mixer_new(xas_audio_format format, size_t buffer_size) { xas_mixer *mixer; @@ -173,13 +181,7 @@ xas_mixer *xas_mixer_new(xas_audio_format format, size_t buffer_size) { goto error_audio_stream_new_source; } - xas_object_init(&mixer->obj); - - mixer->obj.start = (xas_object_start_callback)noop; - mixer->obj.stop = (xas_object_stop_callback)noop; - mixer->obj.set_gain = (xas_object_set_gain_callback)set_gain; - mixer->obj.stream_new = (xas_object_stream_new_callback)xas_mixer_output; - mixer->obj.destroy = (xas_object_destroy_callback)xas_mixer_destroy; + mixer->obj.table = &call_table; mixer->inputs = NULL; mixer->last = NULL; diff --git a/src/object.c b/src/object.c index 357f115..6e3079b 100644 --- a/src/object.c +++ b/src/object.c @@ -3,82 +3,118 @@ #include -void xas_object_init(xas_object *object) { - memset(object, '\0', sizeof(xas_object)); -} - int xas_object_start(xas_object *object, size_t index) { - if (object->start == NULL) { + if (object->table->start == NULL) { errno = ENOSYS; return -1; } - return object->start(object, index); + return object->table->start(object, index); } int xas_object_stop(xas_object *object, size_t index) { - if (object->stop == NULL) { + if (object->table->stop == NULL) { errno = ENOSYS; return -1; } - return object->stop(object, index); + return object->table->stop(object, index); +} + +int xas_object_set_type(xas_object *object, int type) { + if (object->table->set_type == NULL) { + errno = ENOSYS; + + return -1; + } + + return object->table->set_type(object, type); } int xas_object_set_gain(xas_object *object, float gain) { - if (object->set_gain == NULL) { + if (object->table->set_gain == NULL) { errno = ENOSYS; return -1; } - return object->set_gain(object, gain); + return object->table->set_gain(object, gain); } int xas_object_set_entry(xas_object *object, size_t index, size_t entry) { - if (object->set_entry == NULL) { + if (object->table->set_entry == NULL) { errno = ENOSYS; return -1; } - return object->set_entry(object, index, entry); + return object->table->set_entry(object, index, entry); } int xas_object_set_freq(xas_object *object, size_t freq) { - if (object->set_freq == NULL) { + if (object->table->set_freq == NULL) { errno = ENOSYS; return -1; } - return object->set_freq(object, freq); + return object->table->set_freq(object, freq); } int xas_object_set_flags(xas_object *object, int flags) { - if (object->set_flags == NULL) { + if (object->table->set_flags == NULL) { + errno = ENOSYS; + + return -1; + } + + return object->table->set_flags(object, flags); +} + +int xas_object_set_point(xas_object *object, xas_object_coord point) { + if (object->table->set_point == NULL) { + errno = ENOSYS; + + return -1; + } + + return object->table->set_point(object, point); +} + +int xas_object_set_heading(xas_object *object, xas_object_coord heading) { + if (object->table->set_heading == NULL) { + errno = ENOSYS; + + return -1; + } + + return object->table->set_heading(object, heading); +} + +int xas_object_set_speed(xas_object *object, float speed) { + if (object->table->set_speed == NULL) { errno = ENOSYS; return -1; } - return object->set_flags(object, flags); + return object->table->set_speed(object, speed); } xas_audio_stream *xas_object_stream_new(xas_object *object) { - if (object->stream_new == NULL) { + if (object->table->stream_new == NULL) { errno = ENOSYS; return NULL; } - return object->stream_new(object); + return object->table->stream_new(object); } void xas_object_destroy(xas_object *object) { - if (object->destroy) { - object->destroy(object); + if (object->table->destroy) { + object->table->destroy(object); } } diff --git a/src/synth.c b/src/synth.c index 96956c5..d27b7a6 100644 --- a/src/synth.c +++ b/src/synth.c @@ -123,6 +123,12 @@ static int synth_stop(xas_synth *synth, ...) { return 0; } +static int set_freq(xas_synth *synth, size_t freq) { + xas_synth_set_frequency(synth, freq); + + return 0; +} + static int set_gain(xas_synth *synth, float gain) { xas_synth_set_gain(synth, gain); @@ -150,6 +156,16 @@ error_invalid_type: return -1; } +static xas_object_call_table call_table = { + .start = (xas_object_start_callback)synth_start, + .stop = (xas_object_stop_callback)synth_stop, + .set_type = (xas_object_set_type_callback)set_type, + .set_freq = (xas_object_set_freq_callback)set_freq, + .set_gain = (xas_object_set_gain_callback)set_gain, + .stream_new = (xas_object_stream_new_callback)xas_synth_stream_new, + .destroy = (xas_object_destroy_callback)xas_synth_destroy +}; + xas_synth *xas_synth_new(xas_audio_format format, size_t buffer_size, enum xas_synth_type type) { @@ -159,11 +175,7 @@ xas_synth *xas_synth_new(xas_audio_format format, goto error_malloc_synth; } - synth->obj.start = (xas_object_start_callback)synth_start; - synth->obj.stop = (xas_object_stop_callback)synth_stop; - synth->obj.set_gain = (xas_object_set_gain_callback)set_gain; - synth->obj.stream_new = (xas_object_stream_new_callback)xas_synth_stream_new; - synth->obj.destroy = (xas_object_destroy_callback)xas_synth_destroy; + synth->obj.table = &call_table; synth->state = XAS_SYNTH_IDLE; synth->phase = 0.0f; diff --git a/src/vox.c b/src/vox.c index 775aee9..421ac00 100644 --- a/src/vox.c +++ b/src/vox.c @@ -277,6 +277,14 @@ static int vox_generate(xas_vox *vox, ...) { return xas_vox_generate(vox); } +static xas_object_call_table call_table = { + .start = (xas_object_start_callback)vox_generate, + .stop = (xas_object_stop_callback)vox_stop, + .set_gain = (xas_object_set_gain_callback)set_gain, + .stream_new = (xas_object_stream_new_callback)xas_vox_stream_new, + .destroy = (xas_object_destroy_callback)xas_vox_destroy +}; + xas_vox *xas_vox_new_args(xas_audio_format format, size_t buffer_size, const char *text2wave_path, @@ -288,11 +296,7 @@ xas_vox *xas_vox_new_args(xas_audio_format format, goto error_malloc_vox; } - vox->obj.start = (xas_object_start_callback)vox_generate; - vox->obj.stop = (xas_object_stop_callback)vox_stop; - vox->obj.set_gain = (xas_object_set_gain_callback)set_gain; - vox->obj.stream_new = (xas_object_stream_new_callback)xas_vox_stream_new; - vox->obj.destroy = (xas_object_destroy_callback)xas_vox_destroy; + vox->obj.table = &call_table; vox->text2wave_path = text2wave_path; -- cgit v1.2.3