cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <menu><input checked id="shownote" type="radio" name="i"><label for="shownote">Show notes</label><input id="showkeys" type="radio" name="i"><label for="showkeys">Play with keyboard</label></menu>

<div class="keyboard"></div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300);

* {
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: border-box;
}

$blue : #22475E;
$yellow : #4C4A30;
$red : #421D26;
$radius : 5px;

html, body { height: 100%; }
body {
  font-family: "Open Sans Condensed";
  animation: bg 30s linear infinite;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: $blue;
  padding: .25rem 1rem;
}

@keyframes bg {
  from, to { background: $blue; }
  33% { background: $yellow; }
  66% { background: $red; }
}

menu {  
  label {
    display: inline-block;
    font-size: 1.3rem;
    padding: .5rem 1rem;
    background: rgba(ivory, .3);
    cursor: pointer;
    transition: .5s;
    
    &:first-of-type { border-radius: $radius 0 0 $radius; }
    &:last-of-type { border-radius: 0 $radius $radius 0; }
  }
  
  input { 
    display: none;
    
    &:checked + label {
      background: rgba(ivory, .9);
      transition: 0;
    }
  }
  
  margin-bottom: 4rem;
}

.keyboard {
  width: 40rem;
  position: relative;
}

.key {
  display: inline-block;
  position: relative;
  width: 11.111%;
  padding-bottom: 35%;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.3);
  background: ivory;
  vertical-align: top;
  cursor: pointer;
  border-radius: $radius;
  transition: 1s;
  color: rgba(255,255,255, .3);
  
  &:after {
    display: block;
    font-family: "Open Sans Condensed";
    font-size: 1.5rem;
    line-height: 2;
    text-align: center;
    content: attr(data-note);
    
    .keybind & { content: attr(data-bind); }
    
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
  }
  
  &:hover {
    background: shade(ivory, 10%);
    transition: .2s;
  }
  
  
  &.active {
    animation: blacks 300ms;
  }
  
  &:focus { outline: 0; }
  
  &:first-child,
  &:last-child {
    opacity: 0;
    
    &.active {
      animation: secrets 300ms;
    }
  }
}



[data-note*="#"] {
  position: absolute;
  z-index: 100;
  width: 12.5%;
  background: #111;
  transform-origin: center top;
  transform: translateX(-50%) scale(.6);
  border-radius: 0 0 $radius*1.6 $radius*1.6;
  
  &:after {
    font-size: 2.5rem;
    top: inherit;
    bottom: 100%;
  }
  
  &:hover {
    background: tint(black, 20%);
  }
  
  &.active {
    animation: blacks 300ms;
  }
}

@keyframes secrets {
  from {
    opacity: .2;
    background: tint(black, 35%);
  }
}
@keyframes whites {
  from { 
    background: tint(black, 35%);
    color: ivory;
    text-shadow: 0 0 1rem, 0 0 2rem, 0 0 3rem;
  }
}
@keyframes blacks {
   from {
    background: tint(black, 35%);
    color: ivory;
    text-shadow: 0 0 1rem, 0 0 2rem, 0 0 3rem;  
  }
}
            
          
!
            
              var notes = ['B','c','c#','d','d#','e','f','f#','g','g#','a','a#','b','C'];
var keybind = ['S','D','R','F','T','G','H','U','J','I','K','O','L','º'];

$(function(){
  setup();
  
  
  $('input[type="radio"]').change(function(){
    var checked = $('input[type="radio"]:checked').attr("id");
    
    if(checked == "showkeys") {
      $("html").addClass("keybind");
    } else {
      $("html").removeClass("keybind");
    }
  });
  
  
  $(document).keydown(function(e){
    var pressed = String.fromCharCode(e.which);
    
    if(keybind.indexOf(pressed) !== -1){
      var index = keybind.indexOf(pressed);
         
      $('.key[data-note="' + notes[index] + '"]').mousedown();
      

      setTimeout(function(){
        $(".key").removeClass("active").unbind("mouseover");      
      }, 300);
    }
  });

  $(".key").mousedown(function(){
    var $this = $(this);
    var note = $(this).attr("data-note");
    var octave = $(this).attr("data-octave");
    
    play($this, note, octave);
    
    $(".key").mouseover(function(){
      var $this = $(this);
      var note = $(this).attr("data-note");
      var octave = $(this).attr("data-octave");

      play($this, note, octave);    
    });
    
    $(document).mouseup(function(){
      $(".key").unbind("mouseover");
    });
  });
});

function setup() {
    $.each(notes, function(i, note){
      var octave = 5;

      if(note === "B"){
        var octave = octave - 1;
      }
      if(note === "C"){
        var octave = octave + 1;
      }
    
      var $key = $('<button class="key" data-bind="' + keybind[i] + '" data-note=' + note + ' data-octave=' + octave + '></button>');
    
      $(".keyboard").append($key);
    });
}


function play($this, note, octave) {
  console.log(note + octave);
  
  $this.addClass("active");
  
  var play = beeplay().play(note + octave, 3/4);
}

/* beeplay.min.js --------- */
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a){window.beeplay=function(b){"use strict";var c=a("./modules/main");return c.prototype.isArray=a("./modules/isArray"),c.prototype.nn=a("./modules/nn"),c.prototype.pd=a("./modules/pd"),c.prototype.pn=a("./modules/pn"),c.prototype.play=a("./modules/play"),c.prototype.start=a("./modules/start"),c.prototype.put=a("./modules/put"),c.prototype.toJSON=a("./modules/toJSON"),new c(b)}},{"./modules/isArray":2,"./modules/main":3,"./modules/nn":4,"./modules/pd":5,"./modules/play":6,"./modules/pn":7,"./modules/put":8,"./modules/start":9,"./modules/toJSON":10}],2:[function(a,b){b.exports=function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},{}],3:[function(a,b){b.exports=function(a){a="object"==typeof a?a:{},this.bpm=a.bpm||120,this.sampleRate=a.sampleRate||44100,this.key=a.key||"C",this.time=a.time||"4/4",this.volume=a.volume||1,this.stack=[],this.currentTime=0;try{var b=window.AudioContext||window.webkitAudioContext||window.mozAudioContext||window.oAudioContext||window.msAudioContext;this.context=window.__audioContext__||new b,this.context.sampleRate=this.sampleRate,window.__audioContext__=this.context}catch(c){console.error(c.message)}return this}},{}],4:[function(a,b){b.exports=function(a){var b=["c","c#","d","d#","e","f","f#","g","g#","a","a#","b"],c=-1!==a.indexOf("#")?2:1,d=a.substring(0,c).toLowerCase(),e=Number(a.substring(c))+1;return b.indexOf(d)+12*e}},{}],5:[function(a,b){b.exports=function(a){var b=this.volume/7,c=["pp","p","mp","m","mf","f","ff"];a=a.toLowerCase();var d=(c.indexOf(a)+1)*b;return d}},{}],6:[function(a,b){b.exports=function(a,b,c){return a=this.isArray(a)?a:[a],c=c||"m",this.put(a,b,c),this.start(a,b,c),this}},{}],7:[function(a,b){b.exports=function(a){if(null===a)return-1;var b=this.nn(a),c=this.sampleRate/100,d=b-69,e=Math.abs(d);if(69===b)return c;if(d>0)for(;e--;)c*=Math.pow(2,1/12);else for(;e--;)c/=Math.pow(2,1/12);return c}},{}],8:[function(a,b){b.exports=function(a,b,c){this.stack.push({notes:a,length:b,dynamics:c})}},{}],9:[function(a,b){b.exports=function(a,b,c){var d=this.context,e=this.sampleRate,f=this.bpm,g=this;return a.forEach(function(a){var h=d.createBuffer(1,e,e),i=h.getChannelData(0),j=g.pn(a);if(-1!==j){for(var k=0;60/f*b*e>k;k++)i[k]=Math.sin(2*Math.PI*j*(k/e));var l=d.createGain();l.gain.value=g.pd(c),l.connect(d.destination);var m=d.createBufferSource();m.buffer=h,m.connect(l),m.start(g.currentTime)}}),this.currentTime+=60/f*b,this.time}},{}],10:[function(a,b){b.exports=function(){var a={key:this.key,bpm:this.bpm,frequency:this.frequency,time:this.time,notes:JSON.stringify(this.stack)};return JSON.stringify(a)}},{}]},{},[1,2,3,4,5,6,7,8,9,10]);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console