More MIDI control, oscillator drift

main
Thor 1 year ago
parent b8afdc9f8b
commit 740c317707
  1. 28
      main.cpp
  2. 4
      oscillator.cpp
  3. 6
      oscillator.h

@ -14,7 +14,7 @@ using namespace std;
#define SAMPLE_RATE 48000
#define NUM_OSCS 7
float noteToFreq(int note) {
float noteToFreq(float note) {
return 440 * pow(2, (note - 69) / 12.0);
}
@ -43,12 +43,11 @@ static int paCallback(
value += state->oscs[j].tick();
}
value /= NUM_OSCS;
state->filter.setFrequency(0.21 + 0.20 * cos(M_PI * state->time));
value = state->filter.tick(value);
value = 0.25 * sin(M_PI_2 * clamp(value, -1, 1));
value = 0.25 * clamp(value, -1, 1);
*out++ = value;
*out++ = value;
state->time += 1.0 / SAMPLE_RATE;
state->time += 0.5 / SAMPLE_RATE;
}
return 0;
@ -70,14 +69,28 @@ static void ptCallback(PtTimestamp timestamp, void* userData) {
if(vel > 0) {
int note = (msg >> 8) & 0x7F;
float centreFreq = noteToFreq(note);
float spreadCoeff = 0.004;
float spreadCoeff = 0.04;
for(int osc = 0, pos = -3; pos <= 3; ++osc, ++pos) {
float freq = centreFreq * (1 + pos * spreadCoeff);
float freq = noteToFreq(note + spreadCoeff * pos);
state->oscs[osc].phaseStep = (freq * 2 * M_PI) / SAMPLE_RATE;
}
state->time = 0;
}
} else if(cmd == 0xB) {
int ctl = (msg >> 8) & 0x7F;
if(ctl == 0x01) {
int val = (msg >> 16) & 0x7F;
state->filter.setFrequency(val / 256.0);
/*
float flutterSpread = 0.001 * val / 127.0;
for(int osc = 0; i < NUM_OSCS; ++i) {
state->oscs[i].flutterSpread = flutterSpread;
}
*/
}
}
cout << "MIDI:";
@ -110,8 +123,7 @@ void handlePAError(PaError err) {
int main(void) {
static State state;
for(int i = 0; i < 7; ++i) {
float freq = 440 + i;
state.oscs[i].phaseStep = (freq * 2 * M_PI) / SAMPLE_RATE;
state.oscs[i].phaseStep = (440 * 2 * M_PI) / SAMPLE_RATE;
state.oscs[i].phase = PIx2 * (float) rand() / RAND_MAX;
}
state.filter.setMode(SVFx2::MODE_LP);

@ -4,5 +4,7 @@ Oscillator::Oscillator() {
mode = MODE_SAW;
phase = 0;
phaseStep = (440 * 2 * M_PI) / 44100;
value = 0;
value = 0;
flutterSpread = 0.005;
flutter = 0;
}

@ -14,6 +14,9 @@ public:
float phase; // current waveform phase angle (radians)
float phaseStep; // phase angle step per sample
float value; // current amplitude value
float flutterSpread;
float flutterSpeed;
float flutter;
Oscillator();
@ -60,7 +63,8 @@ public:
value -= polyBlep(fmod(t + 0.5, 1.0)); // Layer output of Poly BLEP on top (flop)
}
phase += phaseStep;
flutter += 0.01 * ((float) rand() / RAND_MAX - 0.5) - 0.00001 * flutter;
phase += phaseStep * (1.0 + flutterSpread * flutter);
if(phase >= PIx2) { // wrap if phase angle >=360º
phase -= PIx2;
}

Loading…
Cancel
Save