/*
* First, patch the gate output of your keyboard into the EG’s gate input, this will trigger the voltage envelope each time you hit a key.
* Next patch from the EG’s output into the VCA’s CV input. Now when you hit a key you will hear articulated and dynamic notes according to your Envelope Generator settings.
*/
// set up our Audio Context
var audioContext = new(window.AudioContext || window.webkitAudioContext);
// set up an oscillator, gain, and master gain
var vco = audioContext.createOscillator(),
vca = audioContext.createGain(),
master = audioContext.createGain(),
a = d = r = 0.1,
s = 1,
egMode = 1,
velocity = 1;
// keep track of active keys
var activeKeys = {};
// setup controls
var waveType = document.querySelector('.wavetype'),
masterGain = document.querySelector('.master-gain'),
masterGainDisplay = document.querySelector('.master-gain-display'),
Attack = document.querySelector('.eg-attack'),
Decay = document.querySelector('.eg-decay'),
Sustain = document.querySelector('.eg-sustain'),
Release = document.querySelector('.eg-release'),
egHigh = document.querySelector('.egs-high'),
egMedium = document.querySelector('.egs-medium'),
egLow = document.querySelector('.egs-low'),
attackDisplay = document.querySelector('.ega-display'),
decayDisplay = document.querySelector('.egd-display'),
sustainDisplay = document.querySelector('.egs-display'),
releaseDisplay = document.querySelector('.egr-display');
// connect all modules to master
vco.connect(vca);
vca.connect(master);
master.connect(audioContext.destination);
//set params
vca.gain.value = 0;
master.gain.value = 1;
// start the vco
vco.start(0);
// Controls
waveType.onchange = function() {
changeWaveType(waveType.value);
}
Attack.oninput = function() {
changeAttack(Attack.value);
}
Decay.oninput = function() {
changeDecay(Decay.value);
}
Sustain.oninput = function() {
changeSustain(Sustain.value);
}
Release.oninput = function() {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.