diff options
-rw-r--r-- | include/xas/object.h | 48 | ||||
-rw-r--r-- | src/bank.c | 20 | ||||
-rw-r--r-- | src/mixer.c | 16 | ||||
-rw-r--r-- | src/object.c | 76 | ||||
-rw-r--r-- | src/synth.c | 22 | ||||
-rw-r--r-- | src/vox.c | 14 |
6 files changed, 140 insertions, 56 deletions
diff --git a/include/xas/object.h b/include/xas/object.h index 4d5b754..64d645b 100644 --- a/include/xas/object.h +++ b/include/xas/object.h @@ -3,12 +3,18 @@ #include <xas/audio.h> +typedef struct _xas_object_coord { + float x, y, z; +} xas_object_coord; + typedef struct _xas_object xas_object; typedef int (*xas_object_start_callback)(xas_object *object, size_t index); typedef int (*xas_object_stop_callback)(xas_object *object, size_t index); +typedef int (*xas_object_set_type_callback)(xas_object *object, int type); + typedef int (*xas_object_set_gain_callback)(xas_object *object, float gain); typedef int (*xas_object_set_freq_callback)(xas_object *object, size_t freq); @@ -19,27 +25,43 @@ typedef int (*xas_object_set_entry_callback)(xas_object *object, typedef int (*xas_object_set_flags_callback)(xas_object *object, int flags); +typedef int (*xas_object_set_point_callback)(xas_object *object, + xas_object_coord point); + +typedef int (*xas_object_set_heading_callback)(xas_object *object, + xas_object_coord heading); + +typedef int (*xas_object_set_speed_callback)(xas_object *object, float speed); + typedef xas_audio_stream *(*xas_object_stream_new_callback)(xas_object *object); typedef void (*xas_object_destroy_callback)(xas_object *object); +typedef struct _xas_object_call_table { + xas_object_start_callback start; + xas_object_stop_callback stop; + xas_object_set_type_callback set_type; + xas_object_set_gain_callback set_gain; + xas_object_set_entry_callback set_entry; + xas_object_set_freq_callback set_freq; + xas_object_set_flags_callback set_flags; + xas_object_set_point_callback set_point; + xas_object_set_heading_callback set_heading; + xas_object_set_speed_callback set_speed; + xas_object_stream_new_callback stream_new; + xas_object_destroy_callback destroy; +} xas_object_call_table; + struct _xas_object { - xas_object_start_callback start; - xas_object_stop_callback stop; - xas_object_set_gain_callback set_gain; - xas_object_set_entry_callback set_entry; - xas_object_set_freq_callback set_freq; - xas_object_set_flags_callback set_flags; - xas_object_stream_new_callback stream_new; - xas_object_destroy_callback destroy; + xas_object_call_table *table; }; -void xas_object_init(xas_object *object); - int xas_object_start(xas_object *object, size_t index); int xas_object_stop(xas_object *object, size_t index); +int xas_object_set_type(xas_object *object, int type); + int xas_object_set_gain(xas_object *object, float gain); int xas_object_set_entry(xas_object *object, size_t index, size_t entry); @@ -48,6 +70,12 @@ int xas_object_set_freq(xas_object *object, size_t freq); int xas_object_set_flags(xas_object *object, int flags); +int xas_object_set_point(xas_object *object, xas_object_coord point); + +int xas_object_set_heading(xas_object *object, xas_object_coord heading); + +int xas_object_set_speed(xas_object *object, float speed); + xas_audio_stream *xas_object_stream_new(xas_object *object); void xas_object_destroy(xas_object *object); @@ -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 <xas/object.h> -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; @@ -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; |