From 7cd2ef47ccf7730ada9a61deacfce6ea03b5516d Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 18 Apr 2023 23:33:12 +0300 Subject: Add adsr peak --- src/synth_engine.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/synth_engine.c') diff --git a/src/synth_engine.c b/src/synth_engine.c index 41756c7..fc0a53d 100644 --- a/src/synth_engine.c +++ b/src/synth_engine.c @@ -19,6 +19,14 @@ convole(float *signal, float *filter, size_t signal_size, size_t filter_size, fl } } +float +clamp(float f) +{ + if (f <= -1) return -0.9999; + if (f >= 1) return 0.9999; + return f; +} + float adsr_amplitude(void *synthData, unsigned long long elapsed) { @@ -26,7 +34,7 @@ adsr_amplitude(void *synthData, unsigned long long elapsed) float dAmplitude = 0.0; float dReleaseAmplitude = 0.0; - float dStartAmplitude = 1.0; + float dStartAmplitude = synth->adsr.peak; float dLifeTime = (elapsed * (1.0 / (float)SAMPLE_RATE)); @@ -60,7 +68,7 @@ adsr_amplitude(void *synthData, unsigned long long elapsed) if (dAmplitude <= 0.000) dAmplitude = 0.0; - return dAmplitude; + return clamp(dAmplitude); } @@ -128,18 +136,11 @@ gen2(float f, unsigned long long phase, float x, unsigned int sample_rate) float gen3(float f, unsigned long long phase, float x, unsigned int sample_rate) { - return sawX_sample(0.7, f, 5, phase, sample_rate) - + sin_sample(0.3, 4.0/17.0*f, phase, sample_rate); - /* return sawX_sample(0.5, f * (1 + sqrt(5)) / 2, 5, phase, sample_rate) */ - /* + sin_sample(0.3, f * x, phase, sample_rate) */ - /* + sqr_sample(0.2, f * x, 0.2 * x * x, phase, sample_rate); */ -} - -float -clamp(float f) -{ - if (f <= -1) return -0.9999; - if (f >= 1) return 0.9999; + /* return sawX_sample(0.7, f, 5, phase, sample_rate) */ + /* + sin_sample(0.3, 4.0/17.0*f, phase, sample_rate); */ + return saw_sample(0.5, f * (1 + sqrt(5)) / 2, phase, sample_rate) + + sin_sample(0.3, f * x, phase, sample_rate) + + sqr_sample(0.2, f * x, 0.2 * x * x, phase, sample_rate); } float @@ -237,6 +238,7 @@ init_synth(synth_t * synth) synth->adsr.a = 0.0; + synth->adsr.peak = 1.0f; synth->adsr.d = 0.3; synth->adsr.s = 0.7; synth->adsr.r = 0.4; -- cgit v1.2.3