summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/xas/synth.h2
-rw-r--r--src/synth.c40
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;
}