Skip to content

Instantly share code, notes, and snippets.

@mazbox
Created July 17, 2020 16:11
Show Gist options
  • Save mazbox/0d015b9dc10cc1cfdda2d8c360015f12 to your computer and use it in GitHub Desktop.
Save mazbox/0d015b9dc10cc1cfdda2d8c360015f12 to your computer and use it in GitHub Desktop.
#include "LiveAudio.h"
#include <math.h>
#include <stdlib.h>
class MyLiveAudio : public LiveAudio {
public:
float random() {
return (rand() %10000) / 5000.f - 1.f;
}
int pos = 0;
float freq = 400;
float phase = 0;
float env = 0;
int beat = 0;
float decay = 0.999;
float freqDecay = 1;
float noiz = 0.f;
// 2 controls
// 1 - weirdness
// 2 - business
float frac(float f) {
return f - (int)f;
}
float business = 0.5;
float weirdness = 0.5f;
float getSample() {
pos++;
if(pos>7000) {
beat = (beat+1)%16;
int seed = 4236+ beat*123;//(1 + 13323*beat);
if(frac(seed*1.3495) < business) {
freq = 11 + 3 * (seed%12)*weirdness;
env = 0.1f + frac(seed * 0.844574)*0.2;
decay = 1.f - pow(0.1, 3 + 2*frac(seed*0.6));
freqDecay = 1.f + pow(0.1, 4 + 3*frac(seed*0.9));
if(frac(seed*193.99343)>0.5) {
freqDecay = 2.f - freqDecay;
}
phase = 0;
noiz = 0.5f * (seed & 1);
}
pos = 0;
}
env *= decay;
freq *= freqDecay;//1.00001;
phase += freq * 0.0002;
phase -= (int) phase;
float out = 0;
if(phase<0.5) out = phase * 4.f - 1.f;
else out = 1.f - (phase - 0.5f)*4.f;
out = noiz * random() + (1.f - noiz) *out;
//out *= noiz * random() + (1.f - noiz);
out = tanh(out*env*40.f);
return out * env;
}
void audioOut(float *samples, int length, int numChans) override {
for(int i = 0; i < length; i++) {
float out = getSample();
samples[i*2] = samples[i*2+1] = out;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment