Skip to content

Instantly share code, notes, and snippets.

@paavohuhtala
Created April 29, 2018 12:58
Show Gist options
  • Save paavohuhtala/9eea7ec0ac190679cd4bf304718731e7 to your computer and use it in GitHub Desktop.
Save paavohuhtala/9eea7ec0ac190679cd4bf304718731e7 to your computer and use it in GitHub Desktop.
let time = 0;
function getSine(freq, time) {
const period = 2;
const phase = freq * (time % period);
return phase - Math.floor(phase);
//return Math.sin(2 * Math.PI * freq * time) > 0.5 ? 1 : 0.0;
}
function loop(numFrames, outL, outR, sampleRate) {
const freq = 40;
const amp = 0.1;
for (let i = 0; i < numFrames; i++) {
let sample = getSine(freq, time) * (amp / 2.0);
sample += getSine(freq * 1.002, time) * (amp / 2.0);
sample += getSine(freq * 0.998, time) * (amp / 2.0);
sample += getSine(freq * 1.004, time) * (amp / 2.0);
sample += getSine(freq * 0.996, time) * (amp / 2.0);
outL[i] = sample;
outR[i] = sample;
time += 1 / sampleRate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment