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 /src | |
| parent | 8723f399b2e63693d0e90a1b0af87be865069e22 (diff) | |
| download | xas-4152e6845e71344142faf0911382d5a2b05ae1e4.tar.gz xas-4152e6845e71344142faf0911382d5a2b05ae1e4.tar.bz2 xas-4152e6845e71344142faf0911382d5a2b05ae1e4.zip | |
Allow setting synth gain
Diffstat (limited to 'src')
| -rw-r--r-- | src/synth.c | 25 | 
1 files changed, 18 insertions, 7 deletions
| 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;  } | 
 
    