4 ms·
A little ADSR envelope and one smidgen of FM synthesis helps... var data = [], sampleRateHz = 44100, lengthInSeconds = 8, sampleCount = sampleRateHz
by pacaro 14y ago
A little ADSR envelope and one smidgen of FM synthesis helps...
var data = [],
sampleRateHz = 44100,
lengthInSeconds = 8,
sampleCount = sampleRateHz * lengthInSeconds,
//Notes sequence, play with it ;)
notes = [87, 0, 87, 0, 85, 0, 82, 0, 80, 0, 82, 82, 80, 0, 82, 0, 85, 0, 78, 0, 78, 78],
noteLength = sampleCount / notes.length,
//Base Frequency based on the notes
baseFreq = function(index) {
var r = 2 * Math.PI * 220.0 * Math.pow(2,(notes[index]-69)/12.0) / sampleRateHz;
return r;
},
envelope = function(offset, attack, decay, sustain, release) {
if (offset <= attack) {
return offset / attack
}
offset -= attack;
if (offset <= decay) {
var prop = offset / decay;
return 1 + prop * (sustain - 1);
}
offset -= decay;
var susLen = 1 - (attack + decay + release);
if (offset <= susLen) {
return sustain;
}
offset -= susLen;
return sustain * (1 - offset / release);
};
//Fill up the sound data!!
for(var j = 0; j < sampleCount; j++) {
var currNote = Math.round(j/noteLength);
var currFreq = baseFreq(currNote);
var sample = currFreq * j + 0.5 * Math.sin(currFreq * j * 1.21);
var offset = (j % noteLength) / noteLength;
var amp = envelope(offset, 0.05, 0.01, 0.5, 0.75);
data[j] = 128 + Math.round(96 * amp * Math.sin(sample));
}
- kushsolitary 14y agothis helped a LOT! I created a fork just to test and the sound was definitely awesome! http://cssdeck.com/labs/5atlvhph/0 http://cssdeck.com/labs/5atlvhph/0