From 718527b8f93ca893f996fdd1bf46063d1b5d3ecb Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 27 Feb 2022 21:29:32 -0500 Subject: Refactor src/synth.c Changes: * Implement xas_synth type to consolidate synthesiser implementation and different tone generator types * Refactor examples/ code to remove duplicate synthesiser code --- include/xas/synth.h | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'include/xas') diff --git a/include/xas/synth.h b/include/xas/synth.h index 20446cd..53f3343 100644 --- a/include/xas/synth.h +++ b/include/xas/synth.h @@ -6,23 +6,49 @@ #include -typedef struct _xas_synth xas_synth; +#define XAS_SYNTH_DEFAULT_FREQUENCY 2600 /* Hz */ -typedef int16_t (*xas_synth_callback_sample)(xas_synth *synth, void *ctx); +enum xas_synth_type { + XAS_SYNTH_SINE, + XAS_SYNTH_SQUARE, + XAS_SYNTH_TRIANGLE, + XAS_SYNTH_SAWTOOTH +}; -typedef void (*xas_synth_callback_cleanup)(xas_synth *synth, void *ctx); +enum xas_synth_state { + XAS_SYNTH_IDLE, + XAS_SYNTH_ACTIVE +}; + +typedef struct _xas_synth xas_synth; + +typedef int16_t (*xas_synth_sample_callback)(xas_synth *synth); struct _xas_synth { + enum xas_synth_type type; + enum xas_synth_state state; + xas_audio_format format; - xas_synth_callback_sample sample; - xas_synth_callback_cleanup cleanup; - void *ctx; + size_t buffer_size; + + xas_synth_sample_callback sample; + + float phase; + size_t frequency; }; -xas_audio_stream *xas_synth_new(xas_synth_callback_sample sample, - xas_synth_callback_cleanup cleanup, - xas_audio_format format, - size_t buffer_size, - void *ctx); +xas_synth *xas_synth_new(xas_audio_format format, + size_t buffer_size, + enum xas_synth_type type); + +void xas_synth_destroy(xas_synth *synth); + +void xas_synth_set_frequency(xas_synth *synth, size_t frequency); + +void xas_synth_start(xas_synth *synth); + +void xas_synth_stop(xas_synth *synth); + +xas_audio_stream *xas_synth_new_stream(xas_synth *synth); #endif /* _XAS_SYNTH_H */ -- cgit v1.2.3