summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2023-04-18 23:33:12 +0300
committergramanas <anastasis.gramm2@gmail.com>2023-04-18 23:37:37 +0300
commit7cd2ef47ccf7730ada9a61deacfce6ea03b5516d (patch)
tree5fcb923d4143683450ca21af1ce7faca4a0960d8 /src
parent560356027650af8cd3dcc21888cdc3a76382ea5d (diff)
downloadsynth-project-7cd2ef47ccf7730ada9a61deacfce6ea03b5516d.tar.gz
synth-project-7cd2ef47ccf7730ada9a61deacfce6ea03b5516d.tar.bz2
synth-project-7cd2ef47ccf7730ada9a61deacfce6ea03b5516d.zip
Add adsr peak
Diffstat (limited to 'src')
-rw-r--r--src/synth_engine.c30
-rw-r--r--src/synth_engine.h1
-rw-r--r--src/synth_gui.c10
3 files changed, 23 insertions, 18 deletions
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
@@ -20,13 +20,21 @@ 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)
{
synth_t *synth = (synth_t*)synthData;
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;
diff --git a/src/synth_engine.h b/src/synth_engine.h
index 674a11e..a540126 100644
--- a/src/synth_engine.h
+++ b/src/synth_engine.h
@@ -23,6 +23,7 @@
typedef struct {
float a;
+ float peak;
float d;
float s;
float r;
diff --git a/src/synth_gui.c b/src/synth_gui.c
index f92c853..f5e02cf 100644
--- a/src/synth_gui.c
+++ b/src/synth_gui.c
@@ -54,13 +54,15 @@ draw_adsr_sliders(synth_t * synth, int x, int y, int width, int height, int offs
int count = 0;
snprintf(buf, sizeof buf, "%.3f", synth->adsr.a);
- synth->adsr.a = GuiSliderBar((Rectangle){ x, y + count++ * (height + offset), width, height }, "A: ", buf, synth->adsr.a , 0.0f, 2.0f);
+ synth->adsr.a = GuiSlider((Rectangle){ x, y + count++ * (height + offset), width, height }, "A: ", buf, synth->adsr.a , 0.0f, 2.0f);
+ snprintf(buf, sizeof buf, "%.3f", synth->adsr.peak);
+ synth->adsr.peak = GuiSlider((Rectangle){ x, y + count++ * (height + offset), width, height }, "P: ", buf, synth->adsr.peak , 0.0f, 1.0f);
snprintf(buf, sizeof buf, "%.3f", synth->adsr.d);
- synth->adsr.d = GuiSliderBar((Rectangle){ x, y + count++ * (height + offset), width, height }, "D: ", buf, synth->adsr.d , 0.001f, 2.0f);
+ synth->adsr.d = GuiSlider((Rectangle){ x, y + count++ * (height + offset), width, height }, "D: ", buf, synth->adsr.d , 0.001f, 2.0f);
snprintf(buf, sizeof buf, "%.3f", synth->adsr.s);
- synth->adsr.s = GuiSliderBar((Rectangle){ x, y + count++ * (height + offset), width, height }, "S: ", buf, synth->adsr.s , 0.0f, 1.0f);
+ synth->adsr.s = GuiSlider((Rectangle){ x, y + count++ * (height + offset), width, height }, "S: ", buf, synth->adsr.s , 0.0f, 1.0f);
snprintf(buf, sizeof buf, "%.3f", synth->adsr.r);
- synth->adsr.r = GuiSliderBar((Rectangle){ x, y + count++ * (height + offset), width, height }, "R: ", buf, synth->adsr.r , -0.001f, 3.0f);
+ synth->adsr.r = GuiSlider((Rectangle){ x, y + count++ * (height + offset), width, height }, "R: ", buf, synth->adsr.r , -0.001f, 3.0f);
snprintf(buf, sizeof buf, "%.3f", synth->cutoff);
synth->cutoff = GuiSliderBar((Rectangle){ x, y + count++ * (height + offset), width, height }, "fC: ", buf, synth->cutoff , 0.0f, 11000.0f);
snprintf(buf, sizeof buf, "%.3f", synth->resonance);