-
-
Save stagas/090cdab57c887161674c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
var freq = [5, 8, 1, 9, 9, 5, 8, 1, 8, 5, 1] | |
var bass = [1, 0.1, 0.1, 1, 1.3, 1.6, 1.3, 1.5, 2, 1.6] | |
function note(n) { | |
return 440*Math.pow(2, (n-49)/12.0) | |
} | |
function sin(f, t) { | |
return Math.sin(2 * Math.PI * f * t) | |
} | |
function dsp(t) { | |
var fi = Math.floor(t*3) % freq.length | |
var s = 0.0 | |
s += sin(note(48+freq[fi]), t) | |
var r = 0.0 | |
for (var i = 0; i < 4; i++) { | |
r += sin(.5*220*freq[(fi+i)%freq.length], t) | |
} | |
var b = 0.0 | |
var bi = Math.floor(t*3*2) % bass.length | |
b += sin(bass[bi]*100, t) | |
return ( | |
s*0.2 | |
+ r*.02 | |
+ b*0.4 | |
) | |
} | |
*/ | |
var freq = [5, 8, 1, 9, 9, 5, 8, 1, 8, 5, 1] | |
var bass = [1, 0.1, 0.1, 1, 1.3, 1.6, 1.3, 1.5, 2, 1.6] | |
function note(n) { | |
return 440*Math.pow(2, (n-49)/12.0) | |
} | |
function sin(f, t) { | |
return Math.sin(2 * Math.PI * f * t) | |
} | |
function Oscillator(type, size, alias){ | |
if (!(this instanceof Oscillator)) return new Oscillator(type, size, alias); | |
this.pos = 0; | |
this.size = size || sampleRate; | |
this.coeff = this.size / sampleRate; | |
this.table = new Float32Array(this.size); | |
this.alias = alias === false ? false : true; | |
this.build(type); | |
} | |
Oscillator.prototype.build = function(type){ | |
switch (type) { | |
case 'sin': | |
var scale = 2 * Math.PI / this.size; | |
for (var i = 0; i < this.size; i++) { | |
this.table[i] = Math.sin(i * scale); | |
} | |
break; | |
case 'saw': | |
for (var i = 0; i < this.size; i++) { | |
var x = (i / this.size); | |
this.table[i] = +2.0 * (x - Math.round(x)); | |
} | |
break; | |
case 'ramp': | |
for (var i = 0; i < this.size; i++) { | |
var x = (i / this.size); | |
this.table[i] = -2.0 * (x - Math.round(x)); | |
} | |
break; | |
case 'tri': | |
for (var i = 0; i < this.size; i++) { | |
var x = (i / this.size) - 0.25; | |
this.table[i] = 1.0 - 4.0 * Math.abs(Math.round(x) - x); | |
} | |
break; | |
case 'sqr': | |
var half = this.size / 2; | |
for (var i = 0; i < this.size; i++) { | |
this.table[i] = i < half ? +1 : -1; | |
} | |
break; | |
} | |
}; | |
Oscillator.prototype.play = function(freq){ | |
this.pos += freq * this.coeff; | |
if (this.pos >= this.size) this.pos -= this.size; | |
this.index = this.pos | 0; | |
if (!this.alias) return this.table[this.index]; | |
this.alpha = this.pos - this.index; | |
this.next = this.table[this.index == this.size - 1 ? 0 : this.index + 1]; | |
this.curr = this.table[this.index]; | |
return this.curr + (this.next - this.curr) * this.alpha; | |
}; | |
var sin_s = Oscillator('sin'); | |
var sin_r = Oscillator('sin'); | |
var sin_b = Oscillator('sin'); | |
function dsp(t) { | |
var vibrato = sin(10, t) * 8; | |
var fi = Math.floor(t*3) % freq.length | |
var s = 0.0 | |
s += sin_s.play(note(48+freq[fi]) + vibrato) | |
var r = 0.0 | |
for (var i = 0; i < 4; i++) { | |
r += sin_r.play(.5*220*freq[(fi+i)%freq.length] + vibrato) | |
} | |
var b = 0.0 | |
var bi = Math.floor(t*3*2) % bass.length | |
b += sin_b.play(bass[bi]*100 + vibrato) | |
return ( | |
s*0.2 | |
+ r*.02 | |
+ b*0.4 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment