summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorXANTRONIX Development2022-03-06 20:12:20 -0500
committerXANTRONIX Development2022-03-06 20:12:20 -0500
commit99057a0f6f7e93db6f2dda2e6d8ffeec20e44307 (patch)
treef9f9dca79efa42bb9048f462e4abfb081a6bd0d1 /src
parentd408944a8c0ce626ff35c80b6b6e5fb2f7dc1fb9 (diff)
downloadxas-99057a0f6f7e93db6f2dda2e6d8ffeec20e44307.tar.gz
xas-99057a0f6f7e93db6f2dda2e6d8ffeec20e44307.tar.bz2
xas-99057a0f6f7e93db6f2dda2e6d8ffeec20e44307.zip
Implement xas_synth_set_type()
Implement xas_synth_set_type() to allow changing oscillator type during synthesis
Diffstat (limited to 'src')
-rw-r--r--src/synth.c40
1 files changed, 28 insertions, 12 deletions
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;
}