diff options
Diffstat (limited to 'src/synth_engine.c')
-rw-r--r-- | src/synth_engine.c | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/src/synth_engine.c b/src/synth_engine.c index 9dbf2c5..5118c7d 100644 --- a/src/synth_engine.c +++ b/src/synth_engine.c @@ -5,6 +5,7 @@ #include "control.h" #include "sound.h" #include "wavetable.h" +#include "osc.h" #include <string.h> @@ -46,21 +47,6 @@ gen10(float f, unsigned long long phase, float x, unsigned int sample_rate) } float -gen1110(float f, unsigned long long phase, float x, unsigned int sample_rate) -{ -float amplitude = 0.5; // Set the initial amplitude to 0.5 -float period = 1.0 / f; // Calculate the period of the waveform -float angular_frequency = 2.0 * M_PI * f; // Calculate the angular frequency - -// Calculate the phase angle -float phase_angle = fmodf(angular_frequency * phase / sample_rate, 2.0 * M_PI); - -// Generate the sample value using the sine function -float sample = amplitude * sinf(phase_angle); - -return sample; -} -float gen110(float f, unsigned long long phase, float x, unsigned int sample_rate) { float a = fmodf(phase, sample_rate/f); @@ -105,13 +91,32 @@ gen3(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) { //return sqr_sample(1, f, .5, midi_note->elapsed, sample_rate); - return wvt_next(wvt_saw, f, sample_rate, &midi_note->wvt_index); + //return wvt_next(wvt_sound, f, sample_rate, &midi_note->wvt_index); + float sample = osc_sound(midi_note->wvt_index); + midi_note->wvt_index = osc_sound_next(f, midi_note->wvt_index); + return sample; /* return sawX_sample(0.7, f, 5, midi_note->elapsed, sample_rate) */ /* + sin_sample(0.3, 4.0/17.0*f, midi_note->elapsed, sample_rate); */ - /* return saw_sample(0.5, f * (1 + sqrt(5)) / 2, midi_note->elapsed, sample_rate) */ - /* + sin_sample(0.3, f * x, midi_note->elapsed, sample_rate) */ - /* + sqr_sample(0.2, f * x, 0.2 * x * x, midi_note->elapsed, sample_rate); */ +} + +float +gen4(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) +{ + return wvt_next(wvt_tri, f, sample_rate, &midi_note->wvt_index); +} + +float +gen5(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) +{ + return 0.8 * wvt_next(wvt_saw, f, sample_rate, &midi_note->wvt_index) + + .2 * sin_sample(1, f/2, midi_note->elapsed, sample_rate); +} + +float +gen6(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) +{ + return wvt_next(wvt_sqr, f, sample_rate, &midi_note->wvt_index); } float @@ -128,18 +133,21 @@ notes_active(synth_t *synth) if (!synth->midi_note[i].active) continue; - if (!adsr_amplitude(&synth->adsr, - (float)synth->midi_note[i].noteOn, - (float)synth->midi_note[i].noteOff, - (float)synth->midi_note[i].elapsed) + if (!fix_adsr(&synth->adsr, + (float)synth->midi_note[i].noteOn, + (float)synth->midi_note[i].noteOff, + synth->midi_note[i].elapsed, + synth->midi_note[i].noteOffSample) && synth->midi_note[i].noteOff != 0) { synth->midi_note[i].freq = 0; synth->midi_note[i].channel = -1; synth->midi_note[i].noteOn = -1; synth->midi_note[i].noteOff = -1; synth->midi_note[i].wvt_index = 0; + synth->midi_note[i].lfo_index = 0; synth->midi_note[i].velocity = -1; synth->midi_note[i].elapsed = -1; + synth->midi_note[i].noteOffSample = 0; synth->midi_note[i].active = 0; } else { flag++; @@ -149,7 +157,7 @@ notes_active(synth_t *synth) return flag; } -float cur_freq, next_freq; +float lfo_index; float make_sample(void *synthData, unsigned int sample_rate, int frame) @@ -174,34 +182,23 @@ make_sample(void *synthData, unsigned int sample_rate, int frame) if (!synth->midi_note[i].active) continue; - float adsr = adsr_amplitude(&synth->adsr, + float adsr = fix_adsr(&synth->adsr, synth->midi_note[i].noteOn, synth->midi_note[i].noteOff, - synth->midi_note[i].elapsed); + synth->midi_note[i].elapsed, + synth->midi_note[i].noteOffSample); float targ_freq = synth->midi_note[i].freq * cc_iget(&synth->cc_pitch, frame, FRAMES_PER_BUFFER); - float targ_freq_with_repitch = synth->midi_note[i].freq * cc_iget(&synth->cc_pitch, frame, FRAMES_PER_BUFFER); + targ_freq = targ_freq + targ_freq * + cc_iget(&synth->cc_lfo_amp, frame, FRAMES_PER_BUFFER) * + // (1.05946309436/24.0) * + (wvt_next(wvt_tri, (wvt_next(wvt_sound, 2 * cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), SAMPLE_RATE, &lfo_index) / 2.0 + 0.5) * cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), SAMPLE_RATE, &synth->midi_note[i].lfo_index) / 2.0 + 0.5); float synth_sample = synth->gen[synth->geni](targ_freq, &synth->midi_note[i], synth->x, sample_rate); - /* if (targ_freq != targ_freq_with_repitch) { */ - /* unsigned long long tmp = synth->midi_note[i].elapsed; */ - /* float next_sample = synth->gen[synth->geni](targ_freq_with_repitch, */ - /* tmp, */ - /* synth->x, */ - /* sample_rate); */ - /* while (synth_sample - next_sample > 0.001 && next_sample - synth_sample > 0.001) { */ - /* next_sample = synth->gen[synth->geni](targ_freq_with_repitch, */ - /* ++tmp, */ - /* synth->x, */ - /* sample_rate); */ - /* } */ - /* synth_sample = next_sample; */ - /* } */ - sample += 0.2 * rms * adsr * synth_sample; } @@ -265,8 +262,7 @@ get_frame(void *outputBuffer, synth_t *synth, int i) *out++ = 0.0f; add_to_viz(synth, 0.0f); - - synth->lfo.elapsed = 0; + lfo_index = 0; return; } @@ -275,6 +271,7 @@ get_frame(void *outputBuffer, synth_t *synth, int i) *out++ = 0.0f; *out++ = 0.0f; add_to_viz(synth, 0.0f); + lfo_index = 0; return; } @@ -410,7 +407,9 @@ init_synth(synth_t * synth) synth->cci = 0; CC(synth->cc_cutoff, "cutoff", 10, 5000, 30, 5000); CC(synth->cc_resonance, "resonance", 1, 10, .02, 1); - CC(synth->cc_lfo_freq, "lfo_freq", .01, 10, .02, .1); + //CC(synth->cc_lfo_freq, "lfo_freq", .01, 10, .02, 1); + CC(synth->cc_lfo_freq, "lfo_freq", 1, 1000, 2, 1); + CC(synth->cc_lfo_amp, "lfo_amp", 0, 1, .01, 0); CC(synth->cc_pitch, "pitch", -3, 4, 0.01, 1); synth->modi = 0; @@ -438,6 +437,8 @@ init_synth(synth_t * synth) synth->midi_note[i].velocity = -1; synth->midi_note[i].elapsed = -1; synth->midi_note[i].active = 0; + synth->midi_note[i].wvt_index = 0; + synth->midi_note[i].lfo_index = 0; synth->midi_note[i].adsr = &(synth->adsr); } @@ -453,7 +454,10 @@ init_synth(synth_t * synth) synth->gen[1] = gen1; synth->gen[2] = gen2; synth->gen[3] = gen3; - synth->geni = 2; + synth->gen[4] = gen4; + synth->gen[5] = gen5; + synth->gen[6] = gen6; + synth->geni = 3; synth->active = 0; |