diff options
author | XANTRONIX Development | 2022-02-28 13:13:12 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-02-28 13:13:12 -0500 |
commit | 4152e6845e71344142faf0911382d5a2b05ae1e4 (patch) | |
tree | a13b62afaec955c38911786c578b381157943d1d | |
parent | 8723f399b2e63693d0e90a1b0af87be865069e22 (diff) | |
download | xas-4152e6845e71344142faf0911382d5a2b05ae1e4.tar.gz xas-4152e6845e71344142faf0911382d5a2b05ae1e4.tar.bz2 xas-4152e6845e71344142faf0911382d5a2b05ae1e4.zip |
Allow setting synth gain
-rw-r--r-- | include/xas/synth.h | 7 | ||||
-rw-r--r-- | src/synth.c | 25 |
2 files changed, 24 insertions, 8 deletions
diff --git a/include/xas/synth.h b/include/xas/synth.h index 61f50bc..c13c53e 100644 --- a/include/xas/synth.h +++ b/include/xas/synth.h @@ -7,6 +7,7 @@ #include <xas/object.h> #include <xas/audio.h> +#define XAS_SYNTH_DEFAULT_GAIN 1.0f #define XAS_SYNTH_DEFAULT_FREQUENCY 2600 /* Hz */ enum xas_synth_type { @@ -36,7 +37,9 @@ struct _xas_synth { xas_synth_sample_callback sample; - float phase; + float phase, + gain; + size_t frequency; }; @@ -46,6 +49,8 @@ xas_synth *xas_synth_new(xas_audio_format format, void xas_synth_destroy(xas_synth *synth); +void xas_synth_set_gain(xas_synth *synth, float gain); + void xas_synth_set_frequency(xas_synth *synth, size_t frequency); void xas_synth_start(xas_synth *synth); diff --git a/src/synth.c b/src/synth.c index fddd2a0..76d718b 100644 --- a/src/synth.c +++ b/src/synth.c @@ -9,7 +9,7 @@ static int16_t sample_sine(xas_synth *synth) { static float tau = 2.0f * M_PI; if (synth->state == XAS_SYNTH_ACTIVE) { - ret = (int16_t)roundf((INT16_MAX >> 2) * sinf(synth->phase)); + ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * sinf(synth->phase)); synth->phase += tau / (synth->format.sample_rate / synth->frequency); @@ -29,8 +29,8 @@ static int16_t sample_square(xas_synth *synth) { if (synth->state == XAS_SYNTH_ACTIVE) { ret = (synth->phase - M_PI) > 0.0? - INT16_MAX / 4: - INT16_MIN / 4; + (int16_t)roundf(synth->gain * (INT16_MAX / 4)): + (int16_t)roundf(synth->gain * (INT16_MIN / 4)); synth->phase += tau / (synth->format.sample_rate / synth->frequency); @@ -51,7 +51,7 @@ static int16_t sample_triangle(xas_synth *synth) { if (synth->state == XAS_SYNTH_ACTIVE) { float v = 2.0f * fabs(2.0f * fabs(synth->phase - floorf(synth->phase + 0.5))) - 1.0f; - ret = (int16_t)roundf((INT16_MAX >> 2) * v); + ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * v); synth->phase += tau / (synth->format.sample_rate / synth->frequency); @@ -72,7 +72,7 @@ static int16_t sample_sawtooth(xas_synth *synth) { if (synth->state == XAS_SYNTH_ACTIVE) { float v = 2.0f * (synth->phase / tau - 0.5f); - ret = (int16_t)roundf((INT16_MAX >> 2) * v); + ret = (int16_t)roundf(synth->gain * (INT16_MAX >> 2) * v); synth->phase += tau / (synth->format.sample_rate / synth->frequency); @@ -111,6 +111,12 @@ static int synth_stop(xas_synth *synth) { return 0; } +static int set_gain(xas_synth *synth, float gain) { + xas_synth_set_gain(synth, gain); + + return 0; +} + xas_synth *xas_synth_new(xas_audio_format format, size_t buffer_size, enum xas_synth_type type) { @@ -120,8 +126,9 @@ 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.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->type = type; synth->state = XAS_SYNTH_IDLE; @@ -158,6 +165,10 @@ void xas_synth_destroy(xas_synth *synth) { free(synth); } +void xas_synth_set_gain(xas_synth *synth, float gain) { + synth->gain = gain; +} + void xas_synth_set_frequency(xas_synth *synth, size_t frequency) { synth->frequency = frequency; } |