Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                        <div>
        <a class="play" onclick="this.innerText = play();">▶</a>
        <h1>S<span>ick</span>S<span>tep</span>S<span>equencer</span></h1>
        <span id="volumeBox" style="float: right; padding: 15px; font-size: 24px;">Volume <input id="setVolume" type="range" min="0.0" max=".20" step="0.01" value="0.15" style="height: 20px; width: 200px" onInput="mainGain.gain.value = event.target.value; document.getElementById('showVolume').innerText=Math.round(mainGain.gain.value*100*5);"><span id="showVolume" style="width: 50px">75</span>%</span>
      </div>

    <div class="controls controls--sequencer">
      <div>
        <span id="tempoBox"><span id="showTempo"></span>BPM <input id="setTempo" type="range" min="30.0" max="200.0" step="1" style="height: 20px; width: 200px" onInput="preset.tempo = event.target.value; document.getElementById('showTempo').innerText=preset.tempo;"></span>
      
        <span id="swingBox">Swing <span id="showSwing"></span>% <input id="setSwing" type="range" min="0" max="100" step="1" style="height: 20px; width: 100px" onInput="preset.swing = event.target.value; document.getElementById('showSwing').innerText=preset.swing;"></span>
      
        <span id="humanizeBox">Humanize-R <input id="setHumanizeR" type="range" min="0" max="100" step="1" style="height: 20px; width: 50px" onInput="preset.humanizeTime = event.target.value; document.getElementById('showHumanizeR').innerText=preset.humanizeTime;"><span id="showHumanizeR"></span>%</span>
      </div>
       
      <div>
        Tune
        <button id="transpose-minus" onClick="preset.transpose += -1; document.getElementById('showTranspose').innerText=preset.transpose;drawNotes();">-</button><span id="showTranspose"></span>oct.<button id="transpose-plus" onClick="preset.transpose += 1; document.getElementById('showTranspose').innerText=preset.transpose;drawNotes();">+</button>
        <button id="tune-minus" onClick="preset.tune += -1; document.getElementById('showTune').innerText=preset.tune;drawNotes();">-</button><span id="showTune">0</span>#<button id="tune-plus" onClick="preset.tune += 1; document.getElementById('showTune').innerText=preset.tune;drawNotes();">+</button>
        <button id="finetune-minus" onClick="preset.finetune += -10; document.getElementById('showFinetune').innerText=preset.finetune;">-</button><span id="showFinetune">0</span>%<button id="finetune-plus" onClick="preset.finetune += 10; document.getElementById('showFinetune').innerText=preset.finetune;">+</button>

        <span id="humanizeBox">Humanize-T <span id="showHumanizeT"></span>% <input id="setHumanizeT" type="range" min="0" max="100" step="1" value="0" style="height: 20px; width: 50px" onInput="preset.humanizeFinetune = event.target.value; document.getElementById('showHumanizeT').innerText=preset.humanizeFinetune;"></span>
      </div>
      <div>
        <span id="glideBox">Glide<input id="setGlide" type="range" min="0" max="0.1" step="0.001" style="height: 20px; width: 100px" onInput="preset.glideTime = event.target.value; document.getElementById('showGlide').innerText=preset.glideTime;"> <span id="showGlide"></span>s</span>
        
        <span id="accentBox">Accent vol<input id="setAccent" type="range" min="0" max="1" step="0.01" style="height: 20px; width: 100px" onInput="preset.accentDamping = event.target.value; document.getElementById('showAccent').innerText=preset.accentDamping;"> <span id="showAccent"></span></span>
        <span id="accentModBox">Accent cutoff<input id="setAccentMod" type="range" min="0" max="3" step="0.01" style="height: 20px; width: 100px" onInput="preset.cutoffModFactor = event.target.value; document.getElementById('showAccentMod').innerText=preset.cutoffModFactor;"> <span id="showAccentMod"></span></span>
      </div>
    </div>



<div class="matrix">
  <div class="line glides">
    <div class="cell label label-glide"><div class="label__text">Glide</div></div>
    <div class="cell glide" data-ncell="0"></div>
    <div class="cell glide" data-ncell="1"></div>
    <div class="cell glide" data-ncell="2"></div>
    <div class="cell glide" data-ncell="3"></div>
    <div class="cell glide" data-ncell="4"></div>
    <div class="cell glide" data-ncell="5"></div>
    <div class="cell glide" data-ncell="6"></div>
    <div class="cell glide" data-ncell="7"></div>
    <div class="cell glide" data-ncell="8"></div>
    <div class="cell glide" data-ncell="9"></div>
    <div class="cell glide" data-ncell="10"></div>
    <div class="cell glide" data-ncell="11"></div>
    <div class="cell glide" data-ncell="12"></div>
    <div class="cell glide" data-ncell="13"></div>
    <div class="cell glide" data-ncell="14"></div>
    <div class="cell glide" data-ncell="15"></div>
  </div>

  <div class="line accents">
    <div class="cell label label-accent"><div class="label__text">Accent</div></div>
    <div class="cell accent" data-ncell="0"></div>
    <div class="cell accent" data-ncell="1"></div>
    <div class="cell accent" data-ncell="2"></div>
    <div class="cell accent" data-ncell="3"></div>
    <div class="cell accent" data-ncell="4"></div>
    <div class="cell accent" data-ncell="5"></div>
    <div class="cell accent" data-ncell="6"></div>
    <div class="cell accent" data-ncell="7"></div>
    <div class="cell accent" data-ncell="8"></div>
    <div class="cell accent" data-ncell="9"></div>
    <div class="cell accent" data-ncell="10"></div>
    <div class="cell accent" data-ncell="11"></div>
    <div class="cell accent" data-ncell="12"></div>
    <div class="cell accent" data-ncell="13"></div>
    <div class="cell accent" data-ncell="14"></div>
    <div class="cell accent" data-ncell="15"></div>
  </div>
  
  <div class="line pads">
    <div class="cell label label-note"><div class="label__text">Note</div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="0"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="1"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="2"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="3"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="4"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="5"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="6"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="7"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="8"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="9"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="10"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="11"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="12"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="13"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="14"></div></div>
    <div class="cell pad"><div class="pad__note" data-ncell="15"></div></div>
  </div>  
</div>

<div class="controls controls--synth">
  <div class="unit">
    Waveform
    <ul>
      <li>
        <input class="js-osc1-waveform" type="radio" name="osc1-waveform" onClick="preset.osc1Type = 'sine';osc1.type = 'sine';">
        <label>sin</label>
      </li>
      <li>
        <input class="js-osc1-waveform" type="radio" name="osc1-waveform" onClick="preset.osc1Type = 'square';osc1.type = 'square'">
        <label>sqr</label>
      </li>
      <li>
        <input class="js-osc1-waveform" type="radio" name="osc1-waveform" onClick="preset.osc1Type = 'sawtooth';osc1.type = 'sawtooth'">
        <label>saw</label>
      </li>
      <li>
        <input class="js-osc1-waveform" type="radio" name="osc1-waveform" onClick="preset.osc1Type = 'triangle';osc1.type = 'triangle'"> 
        <label>tri</label>
      </li>
    </ul>  
  </div>

  <div class="unit">
    Filter
    <ul>
      <li>
        <input class="js-filter-type" type="radio" name="filter-type" onClick="preset.filterType = 'lowpass';filter.type = 'lowpass';">
        <label>low</label>
      </li>
      <li>
        <input class="js-filter-type" type="radio" name="filter-type" onClick="preset.filterType = 'highpass';filter.type = 'highpass';">
        <label>high</label>
      </li>
      <li>
        <input class="js-filter-type" type="radio" name="filter-type" onClick="preset.filterType = 'bandpass';filter.type = 'bandpass';">
        <label>band</label>
      </li>
      <li>
        <input class="js-filter-type" type="radio" name="filter-type" onClick="preset.filterType = 'notch';filter.type = 'notch';">
        <label>notch</label>
      </li>
    </ul>  
  </div>
  
  <div class="unit">
    Filter
    <ul>
      <li>
        <span id="swingBox">Cutoff <input id="setCutoff" type="range" min="20" max="4000" step="1" style="height: 20px; width: 150px" onInput="preset.filterFrequency = event.target.value;filter.frequency.value = preset.filterFrequency"></span> 
      </li>
      <li>
        <span id="swingBox">Reson. <input id="setQ" type="range" min="0" max="55" step="0.01" style="height: 20px; width: 150px" onInput="preset.filterResonance = event.target.value;filter.Q.value = preset.filterResonance"></span>
      </li>
    </ul>  
  </div>
  
  <div class="unit unit--left">
    <ol>
      <li>Click the upper left play button</li>
      <li>Tweak sequencer params at the top</li>
      <li>Play with synth params at the bottom</li>
      <li>Click on pads to toggle states</li>
      <li>Set notes height with mouse scrollwheel</li>
    </ol>  
  </div>  
</div>

<hr style="clear: both"/>

<button onclick="document.getElementById('export-zone').innerText=JSON.stringify(preset); ">Export</button>
<button onclick="importPresetButton()">Import</button>
<button onclick="preset = presetEmpty;updateSynth();drawUI();console.log(presetEmpty)">Clear</button><br>
<textarea name="" id="export-zone" cols="50" rows="3" placeholder="Press export button and copy text to save a preset, paste text and press import button to load one."></textarea>
<div id="export-zone"></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Special+Elite);

*, *:after, *:before {
  box-sizing: border-box;
}

$color-cell-base: #ccc;
$color-cell-active-1: #f84;
$color-cell-active-2: #f42;

$cell-base-height: 3vw;
$cell-note-size: 4.5vw;

body {
  font-family: 'Special Elite', sans-serif;
}

.matrix {
  padding-top: 1vw;
  background: #444;
}

.line {
  padding-bottom: 1vw;
  font-size: 0;
}

.cell {
  display: inline-block;
  position: relative;
  width: $cell-note-size;
  height: $cell-base-height;
  margin-left: 1vw;
  background-color: $color-cell-base;
  transition: background-color 0.2s;

  &:hover {
    background-color: darken($color-cell-base, 10%)
  }
  
  &.cell--active-1 {
    background-color: $color-cell-active-1;
      
    &:hover {
      background-color: darken($color-cell-active-1, 20%)
    }
  }
  
  &.cell--active-2 {
    background-color: $color-cell-active-2;
      
    &:hover {
      background-color: darken($color-cell-active-2, 20%) 
    }
  }
  
  &.label {
    width: 7vw;
    background-color: transparent;

    .label__text {
      position: absolute;
      width: 100%;
      height: 100%;
      font-size: 2vw;
      line-height: 3vw;
      text-align: right;
      color: rgba(255,255,255,0.25);
    }
    
    &.label-note {
      height: $cell-note-size;
    }
  }
}

.pads {
}

.pad {
  height: $cell-note-size;

  .pad__note {
    position: absolute;
    width: 100%;
    height: 100%;
    font-size: 2.5vw;
    line-height: 5vw;
    text-align: center;
  }
}

////////////////////////////////////////////////////

.controls {
	font-size:20px;
	font-weight:bold;
	padding: 10px;
  background-color: #eee;

  * {
      margin: 5px;
    }
}

.unit {
  float: left;
  
  &.unit--left {
    float: right;
  }
}

  
h1 {
  display: inline-block;
  height: 3rem;
  vertical-align: top;
  font-size: 3rem;
  line-height: 1.2;
  
  span {
    font-size: 1.4142rem;
//    line-height: 1.4142rem;
  }
}

.play {
  $size-play-button: 3rem;
  display: inline-block;
	background-color:#79bbff;
	color:#ffffff;
//	padding:8px 24px;
	text-decoration:none;
	text-shadow:1px 1px 6px #000000;
	width: $size-play-button;
  height: $size-play-button;
  line-height: $size-play-button;
	text-align: center;
}

.play:hover {
	background-color:#378de5;
}

.play:active {
	position:relative;
	top:1px; left: 1px;
}

#showTempo { 
	display: inline-block;
	width: 45px; 
}

#showSwing { 
	display: inline-block;
	width: 35px; 
}

* {
    -webkit-user-select: none;
}

body, html {
    margin: 0;
    height: 100%;
    position: relative;
}
body {
    overflow: auto;
}

ol {
  margin: 3vw;
}
              
            
!

JS

              
                var cl = function(txt){return console.log(txt)};

var nSteps = 16;                // Number of steps in the sequence
                                // Still hardcoded

//var mainVolume = 0.75;

var presetDefault = {"tempo":"91","swing":"0","humanizeTime":"27","humanizeTimeMax":0.025,"humanizeFinetune":"14","humanizeFinetuneMax":0.5,"transpose":0,"tune":0,"finetune":0,"glideTime":"0.081","accentDamping":"0.23","osc1Type":"sawtooth","filterType":"highpass","filterFrequency":"558","filterResonance":"27.1","cutoffModFactor":"1.4","modCutoffTime":"0.025","steps":{"steps":[{"note":69,"accent":1,"glide":1,"modulation":1013},{"note":84,"accent":0,"glide":0,"modulation":514},{"note":72,"accent":0,"glide":2,"modulation":496},{"note":-1,"accent":2,"glide":0,"modulation":344},{"note":76,"accent":1,"glide":0,"modulation":925},{"note":0,"accent":0,"glide":0,"modulation":191},{"note":69,"accent":0,"glide":1,"modulation":697},{"note":72,"accent":2,"glide":1,"modulation":325},{"note":-1,"accent":0,"glide":0,"modulation":1010},{"note":0,"accent":0,"glide":0,"modulation":919},{"note":64,"accent":1,"glide":1,"modulation":711},{"note":0,"accent":0,"glide":0,"modulation":498},{"note":69,"accent":2,"glide":2,"modulation":987},{"note":60,"accent":1,"glide":0,"modulation":951},{"note":57,"accent":2,"glide":0,"modulation":687},{"note":71,"accent":2,"glide":1,"modulation":532}]}};

var presetEmpty = {"tempo":"120","swing":"0","humanizeTime":"0","humanizeTimeMax":0.025,"humanizeFinetune":"0","humanizeFinetuneMax":0.5,"transpose":0,"tune":0,"finetune":0,"glideTime":"0.05","accentDamping":"0.3","osc1Type":"sawtooth","filterType":"lowpass","filterFrequency":"4000","filterResonance":"0","cutoffModFactor":"1.00","modCutoffTime":"0.025","steps":{"steps":[{"note":-1,"accent":0,"glide":0,"modulation":1013},{"note":-1,"accent":0,"glide":0,"modulation":514},{"note":-1,"accent":0,"glide":0,"modulation":496},{"note":-1,"accent":0,"glide":0,"modulation":344},{"note":-1,"accent":0,"glide":0,"modulation":925},{"note":-1,"accent":0,"glide":0,"modulation":191},{"note":-1,"accent":0,"glide":0,"modulation":697},{"note":-1,"accent":0,"glide":0,"modulation":325},{"note":-1,"accent":0,"glide":0,"modulation":1010},{"note":-1,"accent":0,"glide":0,"modulation":919},{"note":-1,"accent":0,"glide":0,"modulation":711},{"note":-1,"accent":0,"glide":0,"modulation":498},{"note":-1,"accent":0,"glide":0,"modulation":987},{"note":-1,"accent":0,"glide":0,"modulation":951},{"note":-1,"accent":0,"glide":0,"modulation":687},{"note":-1,"accent":0,"glide":0,"modulation":532}]}};

// All params object

var preset = {};



// Sync
var lookahead = 25.0;       // How frequently to call scheduling function 
                            //(in milliseconds)
var scheduleAheadTime = 0.1;// How far ahead to schedule audio (sec)
                            // This is calculated from lookahead, and overlaps 
                            // with next interval (in case the timer is late)

// Audio nodes

var audioContext,
    osc1,
    filter,
    osc1Gain,
    comp,
    mainVolume;


// Sequencer

var isPlaying = false;      // Are we currently playing?

var startTime;              // The start time of the entire sequence.
var current16thNote;        // What note is currently last scheduled?

var nextNoteTime = 0.0;     // when the next note is due.
var nextNoteLength = 0.0;   // length of "beep" (in seconds)
var timerID = 0;            // setInterval identifier.

var last16thNoteDrawn = -1; // the last "box" we drew on the screen
var notesInQueue = [];      // the notes that have been put into the web audio,
                            // and may or may not have played yet. {note, time}

var swingMaxValue = 1/12;   // Difference between 1/4 and 1/6
var semitoneRatio = Math.pow(2, (1/12));
var semitones = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];


// UI

var $accents = $('.accent'),
    $glides = $('.glide'),
    $pads = $('.pad'),
    $padNotes = $('.pad__note');

var $setVolume = $('#setVolume'),
    $showVolume = $('#showVolume'),

    $setTempo = $('#setTempo'),
    $showTempo = $('#showTempo'),
    $setSwing = $('#setSwing'),
    $showSwing = $('#showSwing'),
    $setHumanizeR = $('#setHumanize-r'),
    $showHumanizeR = $('#showHumanize-r'),

    $showTranspose = $('#showTranspose'),
    $showTune = $('#showTune'),

    $setHumanizeT = $('#setHumanizeT'),
    $showHumanizeT = $('#showHumanizeT'),
    $setGlide = $('#setGlide'),
    $showGlide = $('#showGlide'),
    $setAccent = $('#setAccent'),
    $showAccent = $('#showAccent'),
    $setAccentMod = $('#setAccentMod'),
    $showAccentMod = $('#showAccentMod');
      
    $setOsc1Type = $('.js-osc1-waveform');
    $setFilterType = $('.js-filter-type');
    $setCutoff = $('#setCutoff'),
    $setQ = $('#setQ'),



// First, let's shim the requestAnimationFrame API, with a setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  window.oRequestAnimationFrame ||
  window.msRequestAnimationFrame ||
  function( callback ){
    window.setTimeout(callback, 1000 / 60);
  };
})();

// OBJECTS

function Step(note, accent, glide, modulation) {
  this.note = note || 0;
  this.accent = accent || 0;
  this.glide = glide || 0;
  this.modulation = modulation || 0;
}

function Steps(steps) {
  this.steps = steps || [];
}

// Helpers

function noteToFrequency(note) {
  // Midi note for A4 (440 Hz) is 69
  return 440 * Math.pow(semitoneRatio, (note - 69));
}

function noteToName(note) {
  if (note === -1) {
    return '';
  }
  if (note === 0) {
    return '-';
  }

  var name = '';
  var octave = Math.floor(note / 12) - 1;
  var semitone = semitones[note - (octave + 1) * 12];
  name += semitone + '' + octave;
  return name;
}

function nameToNote(name) {
  if (name === '') {
    return -1;
  }
  if (name === '-') {
    return 0;
  }
  
  var endingNumbers = /\d+$/g;
  var octave = parseInt(endingNumbers.exec(name)) + 1;
  
  var startingLetters = /[A-G]#{0,1}/g;
//  var startingLetters = /[A-G]/g;
  var semitoneName = startingLetters.exec(name);
  var semitone = semitones.indexOf(semitoneName[0]);
  var note = octave * 12 + semitone;

//  console.log('name: ' + name + ' / semitoneName: ' + semitoneName + ' / semitone: ' + semitone + ' / octave: ' + octave + ' / note: ' + note);
  
  return note;
}

// Loop routine

function nextNote() {
  // Advance current note and time by a 16th note...
  var secondsPerBeat = 60.0 / preset.tempo;    // Notice this picks up the CURRENT 
                                        // tempo value to calculate beat length.
//  nextNoteTime += 0.25 * secondsPerBeat;    

  // Add beat length to last beat time
  // and apply swing
  var humanTime = preset.humanizeTimeMax * preset.humanizeTime * (Math.random() * 2 - 1) / 100;  
  if (current16thNote % 2) {
    nextNoteTime += (0.25 - swingMaxValue * preset.swing / 100) * secondsPerBeat + humanTime;
    nextNoteLength = (0.25 + swingMaxValue * preset.swing / 100) * secondsPerBeat - humanTime;
  } else {
    nextNoteTime += (0.25 + swingMaxValue * preset.swing / 100) * secondsPerBeat + humanTime;
    nextNoteLength = (0.25 - swingMaxValue * preset.swing / 100) * secondsPerBeat - humanTime;
  }

  current16thNote++;    // Advance the beat number, wrap to zero
  if (current16thNote === nSteps) {
    current16thNote = 0;
  }
}

function scheduleNote( beatNumber, time ) {
  // push the note on the queue, even if we're not playing.
  notesInQueue.push( { note: beatNumber, time: time } ); 

  var note = preset.steps.steps[beatNumber].note;
  var glide = preset.steps.steps[beatNumber].glide;
  var accent = preset.steps.steps[beatNumber].accent;

  if (note > 0) {
    // create an oscillator
//    var osc = audioContext.createOscillator(); 
//    osc.connect(audioContext.destination);
//    osc.type = "square";
//    var baseFrequency = noteToFrequency(sequence[beatNumber] + preset.transpose * 12 + preset.tune); 
//    var fineTuneMax = baseFrequency * (semitoneRatio - 1);
//    osc.frequency.value = baseFrequency + preset.finetune * fineTuneMax / 100;

//    osc.start(time);
//    osc.stop(time + nextNoteLength);

    // Freq.
    var baseFrequency = noteToFrequency(note + preset.transpose * 12 + preset.tune);
    var fineTuneMax = baseFrequency * (semitoneRatio - 1);
    var humanDetuneMax = preset.humanizeFinetune * preset.humanizeFinetuneMax * fineTuneMax * (Math.random() * 2 - 1) / 100;
    var modGlideTime = 0;
    var velocity;
    
    var cutoffMod;
    var cutoff = preset.filterFrequency;
    
    if (glide === 2) {
      modGlideTime = preset.glideTime;
    } else if (glide === 1) {
      modGlideTime = preset.glideTime / 2;  // TODO ? Glide factor
    }
    
    switch (accent) {
      case 0:  // Low strength (or silent)
        velocity = (1 - preset.accentDamping);
        cutoffMod = 0;
        break;
      case 1:  // Mid strength
        velocity = (1 - preset.accentDamping / 2);
        cutoffMod = 0.5;
        break;
      case 2:  // Full strength
        velocity = 1.0;
        cutoffMod = 1;
        break;
    }

    cutoff = cutoff * Math.pow(semitoneRatio, 12 * cutoffMod * preset.cutoffModFactor);
    filter.frequency.setTargetAtTime(cutoff, time, preset.modCutoffTime);
    
    osc1.frequency.setTargetAtTime(baseFrequency + preset.finetune * fineTuneMax / 100 + humanDetuneMax, time, modGlideTime);
    osc1Gain.gain.setTargetAtTime(velocity, time, 0.001, /*env1Attack*/0.001);
    
  } else if (note === 0){
    osc1Gain.gain.setTargetAtTime(0.0, time, /*env1Release*/0.001);
  }
}

function scheduler() {
  // while there are notes that will need to play before the next interval, 
  // schedule them and advance the pointer.
  while (nextNoteTime < audioContext.currentTime + scheduleAheadTime ) {
    scheduleNote( current16thNote, nextNoteTime );
    nextNote();
  }
  timerID = window.setTimeout( scheduler, lookahead );
}

// Stop and go

function play() {
  isPlaying = !isPlaying;

  if (isPlaying) { // start playing
//    osc1Gain.gain.value = 1.0;
    current16thNote = 0;
    nextNoteTime = audioContext.currentTime;
    scheduler();    // kick off scheduling
    return "⬛";
  } else {
    window.clearTimeout( timerID );
    osc1Gain.gain.cancelScheduledValues(0); 
    osc1Gain.gain.value = 0.0;
    return "▶";
  }
}


// Webaudio nodes' params direct setting

function updateSynth() {
  osc1.type = preset.osc1Type;
  
  filter.type = preset.filterType;
  filter.frequency.value = preset.filterFrequency;
  filter.Q.value = preset.filterResonance;
}


// DRAWING UI

function drawUI() {
  updateControls();
  drawNotes();
  drawCells();
}

function updateControls() {

//  preset.blah = $setVolume.val;
  $setTempo.val(preset.tempo);
  $showTempo.text(preset.tempo);
  $setSwing.val(preset.swing);
  $showSwing.text(preset.swing);
  $setHumanizeR.val(preset.humanizeTime);
  $showHumanizeR.text(preset.humanizeTime);

  $showTranspose.text(preset.transpose);
  $showTune.text(preset.tune);

  $setHumanizeT.val(preset.humanizeFinetune);
  $showHumanizeT.text(preset.humanizeFinetune);
  $setGlide.val(preset.glideTime);
  $showGlide.text(preset.glideTime);
  $setAccent.val(preset.accentDamping);
  $showAccent.text(preset.accentDamping);
  $setAccentMod.val(preset.cutoffModFactor);
  $showAccentMod.text(preset.cutoffModFactor);

  switch (preset.osc1Type) {
    case 'sine':
      $setOsc1Type[0].checked = true;
      break;
    case 'square':
      $setOsc1Type[1].checked = true;
      break;
    case 'sawtooth':
      $setOsc1Type[2].checked = true;
      break;
    case 'triangle':
      $setOsc1Type[3].checked = true;
      break;
  }
  
  switch (preset.filterType) {
    case 'lowpass':
      $setFilterType[0].checked = true;
      break;
    case 'highpass':
      $setFilterType[1].checked = true;
      break;
    case 'band':
      $setFilterType[2].checked = true;
      break;
    case 'notch':
      $setFilterType[3].checked = true;
      break;
  }
  
  $setCutoff.val(preset.filterFrequency);
  $setQ.val(preset.filterResonance); 
}


// UI DRAWING

function drawNotes() {
  $padNotes.each(function(i) {
    var note = preset.steps.steps[i].note;
    if (note > 0) {
      note += preset.transpose * 12 + preset.tune
    }
    $(this).text(noteToName(note));
  }); 
}

function drawCells() {  // Accent and glide pads
  $accents.removeClass('cell--active-1 cell--active-2');
  $glides.removeClass('cell--active-1 cell--active-2');

  for (var i = 0; i < nSteps; i++) {

    var glide = preset.steps.steps[i].glide;
    var accent = preset.steps.steps[i].accent;
    $($glides[i]).addClass('cell--active-' + glide);
    $($accents[i]).addClass('cell--active-' + accent);
  }
}

// Legacy UI drawings

function draw() {
  var currentNote = last16thNoteDrawn; 
  var currentTime = audioContext.currentTime;

  while (notesInQueue.length && notesInQueue[0].time < currentTime) {
    currentNote = notesInQueue[0].note;
    notesInQueue.splice(0,1);   // remove note from queue
  }

  // We only need to draw if the note has moved.
  if (last16thNoteDrawn != currentNote) {
    for (var i=0; i<nSteps; i++) {
      var color = ( currentNote == i ) ? ((currentNote%4 === 0)?"#444":"#aaa") : "#ccc";
      $pads[i].style.backgroundColor = color;
    }
    last16thNoteDrawn = currentNote;
  }

  // set up to draw again
  requestAnimFrame(draw);
}

//=============================================================================

function init(){ 
  
  preset = presetDefault;
  updateControls();
  
  // NOTE: THIS RELIES ON THE MONKEYPATCH LIBRARY BEING LOADED FROM
  // Http://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js
  audioContext = new AudioContext();

  // create an oscillator
  osc1 = audioContext.createOscillator();
  osc1Gain = audioContext.createGain();
  filter = audioContext.createBiquadFilter();
  comp = audioContext.createDynamicsCompressor();
  mainGain = audioContext.createGain();
  
  osc1.connect(filter);
  filter.connect(osc1Gain)
  osc1Gain.connect(comp);
  comp.connect(mainGain);
  mainGain.connect(audioContext.destination);

  osc1Gain.gain.value = 0.0; // Prevents osc's to play before hitting start!
  mainGain.gain.value = .15;

  osc1.start();

  drawUI();
  updateSynth();

 
// console.log('==========================================================================');
  
  // if we wanted to load audio files, etc., this is where we should do it.

  requestAnimFrame(draw);    // start the drawing loop.
}

/* UI wirings */

// $pads.on('click', cycleActivesPads);
$glides.on('click', cycleActivesGlide);
$accents.on('click', cycleActivesAccent);

$padNotes.on('click', function(e) {
  var $cell = $(e.target);
  var n = $cell.attr('data-ncell');
  if (n >= preset.steps.steps.length) return;
  
  var note = preset.steps.steps[n].note;

  if (note === -1) {
    note = 0;
  } else if (note === 0) {
    note = 69;  // TODO : keep track of last note
  } else {
    note = -1;
  }
  
  preset.steps.steps[n].note = note;
  drawNotes();
});

$padNotes.on('mousewheel DOMMouseScroll', function(e){
  var $cell = $(e.target);
  var n = $cell.attr('data-ncell');
  if (n >= preset.steps.steps.length) return;
  
  var note = preset.steps.steps[n].note;
          console.log(note);

  if (note === 0 || note === -1) return;
  
  
  if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
    // scroll up
    if (note < 127) note ++;
  }
  else {
    // scroll down
    if (note > 12) note --;
  }
  
  preset.steps.steps[n].note = note;
  drawNotes();
});


function cycleActivesGlide(e) {
  var $cell = $(e.target);
  var n = $cell.attr('data-ncell');
  if (n >= preset.steps.steps.length) return;
  
  var glide = preset.steps.steps[n].glide;
  glide += 1;
  if (glide > 2) {
    glide = 0;
  }
  preset.steps.steps[n].glide = glide;
  
  switch (glide) { 
    case 0:
      $cell.removeClass('cell--active-2');
      break;
    case 1:
      $cell.addClass('cell--active-1');
      break;
    case 2:
      $cell.removeClass('cell--active-1');
      $cell.addClass('cell--active-2');
      break;
  }

//  console.log(n + ':' + glide); 
}

// Bouh!
function cycleActivesAccent(e) {
  var $cell = $(e.target);
  var n = $cell.attr('data-ncell'); 
  if (n >= preset.steps.steps.length) return;
  
  var accent = preset.steps.steps[n].accent;
  accent += 1;
  if (accent > 2) {
    accent = 0;
  }
  preset.steps.steps[n].accent = accent;
  
  switch (accent) { 
    case 0:
      $cell.removeClass('cell--active-2');
      break;
    case 1:
      $cell.addClass('cell--active-1');
      break;
    case 2:
      $cell.removeClass('cell--active-1'); 
      $cell.addClass('cell--active-2');
      break;
  }
  
//  console.log(n + ':' + preset.steps.steps[n].accent); 
}

function importPresetButton() {
  preset=JSON.parse(document.getElementById('export-zone').value);

  updateSynth();

  drawUI();

  console.log(preset); 
}



window.addEventListener("load", init );


              
            
!
999px

Console