summaryrefslogtreecommitdiffstats
path: root/src/synth_engine.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/synth_engine.c')
-rw-r--r--src/synth_engine.c210
1 files changed, 72 insertions, 138 deletions
diff --git a/src/synth_engine.c b/src/synth_engine.c
index 57ec29b..41756c7 100644
--- a/src/synth_engine.c
+++ b/src/synth_engine.c
@@ -2,6 +2,23 @@
#include "lowpass.h"
#include "filter.h"
+/* 1d convolution */
+void
+convole(float *signal, float *filter, size_t signal_size, size_t filter_size, float *out) {
+ for (size_t i = 0; i < filter_size + signal_size; i++) {
+ size_t kmin, kmax, k;
+ out[i] = 0;
+ /* find overlap */
+ kmin = (i >= filter_size - 1) ? i - (filter_size - 1) : 0;
+ kmax = (i < signal_size - 1) ? i : signal_size - 1;
+
+ /* Add the overlaping values */
+ for (k = kmin; k <= kmax; k++) {
+ out[i] += signal[k] * filter[i - k];
+ }
+ }
+}
+
float
adsr_amplitude(void *synthData, unsigned long long elapsed)
{
@@ -14,20 +31,20 @@ adsr_amplitude(void *synthData, unsigned long long elapsed)
float dLifeTime = (elapsed * (1.0 / (float)SAMPLE_RATE));
if (synth->n.noteOn != 0 && synth->n.noteOff == 0) {
- if (dLifeTime <= synth->adsr.a)
+ if (dLifeTime < synth->adsr.a)
dAmplitude = (dLifeTime / synth->adsr.a)*(dLifeTime / synth->adsr.a) * dStartAmplitude;
- if (dLifeTime > synth->adsr.a && dLifeTime <= ( synth->adsr.a + synth->adsr.d))
+ if (dLifeTime >= synth->adsr.a && dLifeTime <= ( synth->adsr.a + synth->adsr.d))
dAmplitude = ((dLifeTime - synth->adsr.a) / synth->adsr.d) * (synth->adsr.s - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (synth->adsr.a + synth->adsr.d))
dAmplitude = synth->adsr.s;
}
else { // Note is off
- if (dLifeTime <= synth->adsr.a)
+ if (dLifeTime < synth->adsr.a)
dReleaseAmplitude = (dLifeTime / synth->adsr.a)*(dLifeTime / synth->adsr.a) * dStartAmplitude;
- if (dLifeTime > synth->adsr.a && dLifeTime <= (synth->adsr.a + synth->adsr.d))
+ if (dLifeTime >= synth->adsr.a && dLifeTime <= (synth->adsr.a + synth->adsr.d))
dReleaseAmplitude = ((dLifeTime - synth->adsr.a) / synth->adsr.d) * (synth->adsr.s - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (synth->adsr.a + synth->adsr.d))
@@ -38,7 +55,7 @@ adsr_amplitude(void *synthData, unsigned long long elapsed)
if (synth->adsr.r < 0) {
dAmplitude = synth->adsr.s;
}
-}
+ }
// Amplitude should not be negative
if (dAmplitude <= 0.000)
dAmplitude = 0.0;
@@ -118,91 +135,45 @@ gen3(float f, unsigned long long phase, float x, unsigned int sample_rate)
/* + sqr_sample(0.2, f * x, 0.2 * x * x, phase, sample_rate); */
}
-
-
-/* 1d convolution */
-void
-convole(float *signal, float *filter, size_t signal_size, size_t filter_size, float *out) {
- for (size_t i = 0; i < filter_size + signal_size; i++) {
- size_t kmin, kmax, k;
- out[i] = 0;
- /* find overlap */
- kmin = (i >= filter_size - 1) ? i - (filter_size - 1) : 0;
- kmax = (i < signal_size - 1) ? i : signal_size - 1;
-
- /* Add the overlaping values */
- for (k = kmin; k <= kmax; k++) {
- out[i] += signal[k] * filter[i - k];
- }
- }
-}
-
-void
-low_pass_filter(float* signal, int length, float cutoff, float resonance, float* out) {
- float c = 1.0f / tanf(M_PI * cutoff); // calculate filter constant
- float a1 = 1.0f / (1.0f + resonance * c + c * c); // calculate filter coefficients
- float a2 = 2.0f * a1;
- float a3 = a1;
- float b1 = 2.0f * (1.0f - c * c) * a1;
- float b2 = (1.0f - resonance * c + c * c) * a1;
- float prev_input = 0.0f, prev_output = 0.0f; // initialize previous input and output to zero
- for (int i = 0; i < length; i++) {
- float input = signal[i];
- float output = a1 * input + a2 * prev_input + a3 * prev_output - (i >= 1 ? b1 * out[i-1] : 0.0f) - (i >= 2 ? b2 * out[i-2] : 0.0f);
- out[i] = output;
- prev_input = input;
- prev_output = output;
- }
+float
+clamp(float f)
+{
+ if (f <= -1) return -0.9999;
+ if (f >= 1) return 0.9999;
}
-
float
make_sample(unsigned long long phase, void *synthData, unsigned int sample_rate, int viz)
{
synth_t *synth = (synth_t*)synthData;
float sample = 0;
-
+ //LFO!
+ //if (synth->adsr.elapsed > SAMPLE_RATE / 2) synth->adsr.elapsed = 0;
+
int n = 1;
- if (1 /* !synth->filter */) {
- for (int i = 0; i < n; i++) {
- sample += (1.0 / n) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x, sample_rate);
- }
-
- if (!viz && synth->filter) {
- // ALLL THE FILTERS
- LowPass_Update(synth->resonance, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * synth->cutoff + 1, sample_rate);
- sample = LowPass_Filter(sample);
+ for (int i = 0; i < n; i++) {
+ sample += (1.0 / n) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x, sample_rate);
+ }
- update_bw_low_pass_filter(synth->fff, SAMPLE_RATE, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * synth->cutoff, synth->resonance);
- sample = bw_low_pass(synth->fff, sample);
- }
+ if (!viz && synth->filter) {
+ // ALLL THE FILTERS
+ LowPass_Update(synth->resonance, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * round(synth->cutoff) + 1, sample_rate);
+ sample = LowPass_Filter(sample);
- sample = synth->gain * adsr_amplitude(synth, synth->adsr.elapsed) *
- sample; //synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x);
-
- if (synth->clamp && sample >= 1) sample = 0.99;
- if (synth->clamp && sample <= -1) sample = -0.99;
- } else {
- // get sample array s[]
- int samples = 30;
- float s[samples];
-
- if (synth->adsr.elapsed < samples) {
- for (int i = 0; i < samples; i++) {
- s[i] = synth->gain * adsr_amplitude(synth, i) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, i, synth->x, sample_rate);
- }
- } else {
- for (int i = 0; i < samples; i++) {
- s[i] = synth->gain * adsr_amplitude(synth, synth->adsr.elapsed - 50 + i) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase - 50 + i, synth->x, sample_rate);
- }
- }
+ update_bw_low_pass_filter(synth->fff, SAMPLE_RATE, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * synth->cutoff, synth->resonance);
+ sample = bw_low_pass(synth->fff, sample);
+ }
- // process s[]
+ sample = synth->gain * adsr_amplitude(synth, synth->adsr.elapsed) * sample;
+
+ // band stop for high freqs
+ if (!viz)
+ sample = bw_band_stop(synth->fff2, sample);
- // return s[50]
- }
+
+ if (synth->clamp) sample = clamp(sample);
return sample;
}
@@ -221,70 +192,30 @@ sound_gen(const void *inputBuffer, void *outputBuffer,
(void) statusFlags;
(void) inputBuffer;
- if (1) {
- float s;
- for( unsigned long i=0; i<framesPerBuffer; i++ ) {
- //get_portaudio_frame(outputBuffer, synth);
- s = make_sample(synth->n.elapsed, synth, SAMPLE_RATE, 0);
- *out++ = s;
- *out++ = s;
- synth->adsr.elapsed++;
- synth->n.elapsed++;
- if (!synth->multi) {
- if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
- } else {
-
- }
- }
- } else {
- float s[FRAMES_PER_BUFFER * 5];
-
- if (synth->adsr.elapsed < framesPerBuffer * 5) {
- for (unsigned long long i = 0; i < framesPerBuffer * 5; i++) {
- s[i] = make_sample(i, synth, SAMPLE_RATE, 0);
- synth->adsr.elapsed++;
- synth->n.elapsed++;
- if (!synth->multi) {
- if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
- } else {
- }
- }
- } else {
- for (unsigned long long i = 0; i < framesPerBuffer * 5; i++) {
- s[i] = make_sample(synth->n.elapsed - framesPerBuffer * 2 + i, synth, SAMPLE_RATE, 0);
- synth->adsr.elapsed++;
- synth->n.elapsed++;
- if (!synth->multi) {
- if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
- } else {
- }
- }
+ float s;
+ for( unsigned long i=0; i<framesPerBuffer; i++ ) {
+ //get_portaudio_frame(outputBuffer, synth);
+ if (!synth->active) {
+ *out++ = 0.0f;
+ *out++ = 0.0f;
+ continue;
}
-
- // filter
-
- // output
-
- if (synth->adsr.elapsed < framesPerBuffer * 5) {
- for( unsigned long i=0; i<framesPerBuffer; i++ ) {
- *out++ = s[i];
- *out++ = s[i];
- }
- } else {
- for( unsigned long i=0; i<framesPerBuffer; i++ ) {
- *out++ = s[i + (2 * framesPerBuffer)];
- *out++ = s[i + (2 * framesPerBuffer)];
- }
+ if (adsr_amplitude(synth, synth->adsr.elapsed) == 0 && synth->n.noteOff != 0) {
+ printf("SYNTH OPFF\n");
+ synth->active = 0;
+ *out++ = 0.0f;
+ *out++ = 0.0f;
+ continue;
}
- synth->adsr.elapsed-= 4*framesPerBuffer;
-
+ s = make_sample(synth->n.elapsed, synth, SAMPLE_RATE, 0);
+ *out++ = s;
+ *out++ = s;
+ synth->adsr.elapsed++;
+ synth->n.elapsed++;
if (!synth->multi) {
- for (int i = 0; i < framesPerBuffer * 4; i++) {
- synth->n.elapsed--;
- if (synth->n.elapsed == 0) synth->n.elapsed = (int)(SAMPLE_RATE / synth->n.freq);
- }
+ if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
} else {
- synth->n.elapsed-= 4*framesPerBuffer;
+
}
}
@@ -305,7 +236,7 @@ init_synth(synth_t * synth)
synth->n.elapsed = 0;
- synth->adsr.a = 0.001;
+ synth->adsr.a = 0.0;
synth->adsr.d = 0.3;
synth->adsr.s = 0.7;
synth->adsr.r = 0.4;
@@ -317,7 +248,7 @@ init_synth(synth_t * synth)
synth->filter = 0;
synth->cutoff = 22000.0f;
synth->resonance = 1.0f;
- synth->clamp = 0;
+ synth->clamp = 1;
synth->gen[0] = gen0;
synth->gen[1] = gen1;
@@ -325,8 +256,11 @@ init_synth(synth_t * synth)
synth->gen[3] = gen3;
synth->geni = 0;
+ synth->active = 0;
+
synth->viz.sample_rate_divider = 1;
LowPass_Init();
synth->fff = create_bw_low_pass_filter(2, SAMPLE_RATE, 400);
+ synth->fff2 = create_bw_band_stop_filter(8, SAMPLE_RATE, 15000, 22000);
}