Voice Additive
by Ehsan Ziya
HTML
<script src="http://blog.chrislowis.co.uk/demos/polyphonic_synthesis/demo2/qwerty-hancock.js"></script>
<div id='keyboard'></div>
JavaScript
var context = new webkitAudioContext();
var gain = context.createGain();
gain.gain.value = 1;
gain.connect(context.destination);
function Voice(oscType, freq, oscNumber, pitchunison, panunison, attack, release) {
var now = context.currentTime;
this.oscillators = [];
this.release = release;
var that = this;
//VOICE GAIN
this.voicegain = context.createGain(); //create gain control for each voice
this.voicegain.connect(gain); //destination
for (var i = 0; i < oscNumber - 1; i++) {
this.osc = context.createOscillator();
that.oscillators.push(osc);
this.osc.type = oscType;
this.pan1 = context.createPanner(); // pan unison for each particle
this.pan1.panningModel = "equalpower"; // defaults to hrtf
// random pan level selection in the specified range between -1 and 1
this.panunisonval = ((Math.random() * panunison) - panunison / 2) * (panunison * 0.0001);
this.pan1.setPosition(this.panunisonval, 0, 0);
this.osc.frequency.value = freq;
//pitch unison random for each particle
this.detuneval = (Math.random() * pitchunison) - (pitchunison / 2);
this.osc.detune.value = this.detuneval;
this.osc.connect(this.pan1);
this.pan1.connect(this.voicegain);
this.osc.start(now);
}
//linear attack section
this.voicegain.gain.setValueAtTime(0,now);
this.voicegain.gain.linearRampToValueAtTime(1 / oscNumber * 2, now + attack);
// noteOff method
this.noteOff = function () {
//release and deleting the created nodes
that.oscillators.forEach(function () {
this.osc.stop(now + that.release + 0.5);
this.voicegain.gain.linearRampToValueAtTime(0, now + that.release);
});
};
}
var keyboard = qwertyHancock({
id: 'keyboard',
width: 600,
height: 150,
octaves: 3,
startNote: 'A3',
whiteNotesColour: 'white',
...