Moisés Gabriel Cachay Tello
Creator, destructor.
@xpktro - LimaJS
Envolvente
(A[DS]R + VCA)
Filtro de paso
de banda (VCF)
Output
Oscilador (VCO)
const context = new AudioContext();
const oscillator = context.createOscillator();
oscillator.type = 'sawtooth';
oscillator.frequency.value = 440;
oscillator.connect(context.destination);
oscillator.start();
// Evitar la fatiga auditiva:
oscillator.stop(context.currentTime + 1);
const NOTES = {
1: 60,
2: 62,
3: 64,
4: 65,
5: 67,
6: 69,
7: 71,
8: 72
};
window.onkeydown = function(event) {
oscillator.frequency.value = piano2Hz(NOTES[event.key] || 60);
log('Oscillator frequency: ' + oscillator.frequency.value + 'Hz');
log('Key: ' + event.key);
};
function piano2Hz(keyNumber) {
// wikipedia.org/wiki/MIDI_tuning_standard#Frequency_values
return Math.pow(2, (keyNumber - 69) / 12) * 440;
}
const filter = context.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = 10000;
filter.Q.value = 0;
oscillator.connect(filter);
filter.connect(context.destination);
document.getElementById('cutoff').oninput = function(event) {
filter.frequency.value = (event.target.value / 100) * 10000;
};
document.getElementById('resonance').oninput = function() {
filter.Q.value = (event.target.value / 100) * 20;
};
<input id="cutoff" type="range" min="0" max="100" value="100">
<input id="resonance" type="range" min="0" max="100" value="0">
const amplitude = context.createGain();
amplitude.gain.value = 0;
oscillator.connect(filter);
filter.connect(amplitude);
amplitude.connect(context.destination);
document.onkeydown = function(event) {
oscillator.frequency.value = piano2Hz(NOTES[event.key] || 60);
amplitude.gain.cancelScheduledValues(0);
amplitude.gain.linearRampToValueAtTime(1, context.currentTime + attack());
}
document.onkeyup = function(event) {
amplitude.gain.cancelScheduledValues(0);
amplitude.gain.setValueAtTime(amplitude.gain.value, context.currentTime);
amplitude.gain.linearRampToValueAtTime(0, context.currentTime + release());
};
<input id="attack" type="range" min="0" max="100" value="0">
<input id="release" type="range" min="0" max="100" value="0">
By Moisés Gabriel Cachay Tello