From 99057a0f6f7e93db6f2dda2e6d8ffeec20e44307 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 6 Mar 2022 20:12:20 -0500 Subject: Implement xas_synth_set_type() Implement xas_synth_set_type() to allow changing oscillator type during synthesis --- include/xas/synth.h | 2 ++ src/synth.c | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/include/xas/synth.h b/include/xas/synth.h index 6902643..ca83c69 100644 --- a/include/xas/synth.h +++ b/include/xas/synth.h @@ -53,6 +53,8 @@ void xas_synth_set_gain(xas_synth *synth, float gain); void xas_synth_set_frequency(xas_synth *synth, size_t frequency); +void xas_synth_set_type(xas_synth *synth, enum xas_synth_type type); + void xas_synth_start(xas_synth *synth); void xas_synth_stop(xas_synth *synth); diff --git a/src/synth.c b/src/synth.c index 6057ce3..b01ebc8 100644 --- a/src/synth.c +++ b/src/synth.c @@ -117,6 +117,27 @@ static int set_gain(xas_synth *synth, float gain) { return 0; } +static int set_type(xas_synth *synth, enum xas_synth_type type) { + switch (type) { + case XAS_SYNTH_SINE: synth->sample = sample_sine; break; + case XAS_SYNTH_SQUARE: synth->sample = sample_square; break; + case XAS_SYNTH_TRIANGLE: synth->sample = sample_triangle; break; + case XAS_SYNTH_SAWTOOTH: synth->sample = sample_sawtooth; break; + + default: + errno = EINVAL; + + goto error_invalid_type; + } + + synth->type = type; + + return 0; + +error_invalid_type: + return -1; +} + xas_synth *xas_synth_new(xas_audio_format format, size_t buffer_size, enum xas_synth_type type) { @@ -132,7 +153,6 @@ xas_synth *xas_synth_new(xas_audio_format format, synth->obj.stream_new = (xas_object_stream_new_callback)xas_synth_stream_new; synth->obj.destroy = (xas_object_destroy_callback)xas_synth_destroy; - synth->type = type; synth->state = XAS_SYNTH_IDLE; synth->phase = 0.0f; synth->gain = XAS_SYNTH_DEFAULT_GAIN; @@ -143,21 +163,13 @@ xas_synth *xas_synth_new(xas_audio_format format, synth->format.sample_rate = format.sample_rate; synth->buffer_size = buffer_size; - switch (type) { - case XAS_SYNTH_SINE: synth->sample = sample_sine; break; - case XAS_SYNTH_SQUARE: synth->sample = sample_square; break; - case XAS_SYNTH_TRIANGLE: synth->sample = sample_triangle; break; - case XAS_SYNTH_SAWTOOTH: synth->sample = sample_sawtooth; break; - - default: - errno = EINVAL; - - goto error_invalid_type; + if (set_type(synth, type) < 0) { + goto error_set_type; } return synth; -error_invalid_type: +error_set_type: free(synth); error_malloc_synth: @@ -172,6 +184,10 @@ void xas_synth_set_gain(xas_synth *synth, float gain) { synth->gain = gain; } +void xas_synth_set_type(xas_synth *synth, enum xas_synth_type type) { + set_type(synth, type); +} + void xas_synth_set_frequency(xas_synth *synth, size_t frequency) { synth->frequency = frequency; } -- cgit v1.2.3