cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-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.

            
              <div class="container">
  <header>Simple<b style="color: #FFF;">Metronome</b></header>

  <section class="metronome-container">
    <div class="counter"></div>
    
    <i class="fa fa-cog options-btn"></i>

    <div class="controls">
      <label>BPM: <span>
                    <i class="fa fa-minus bpm-minus"></i>
                    <input type="text" value="60" class="bpm-input" />
                    <i class="fa fa-plus bpm-plus"></i>
                  </span>
      </label>
      <label>
        Beat: <input type="text" value="4" class="ts-top" /></label>
      <div style="margin-bottom: 15px;">
        <input type="checkbox" id="timer-check" />
        <label for="timer-check"></label>
        
        Timer: <input type="text" value="60" class="timer" />
      </div>

      <button class="tap-btn">Tap</button>
      <button class="play-btn">Play</button>
    </div>
    
    <div class="options">
      <i class="fa fa-caret-down up"></i>
      <label>Off Beat Pitch: <input type="range" min="0" max="500" value="200" class="beat-range" /></label>
      <label>Accent Pitch: <input type="range" min="0" max="500" value="380" class="accent-range" /></label>
    </div>
  </section>
</div>

<footer>
  <ul>
    <li>Click circles to make an accent on that beat</li>
    <li>Best viewed in <b>Google Chrome</b></li>
    <li>Use options to change pitches of beats.</li>
  </ul>
</footer>

            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,600);
@import url(//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css);

body { 
  background: lighten(#F2F0D9, 5%); 
  font-family: 'Open Sans', sans-serif; 
}

input[type="range"] {
   -webkit-appearance:none !important;
    max-height: 3px;
    position: relative;
    top: -3px;
   background: #DDD;
 }

 input[type="range"]::-webkit-slider-thumb {
   -webkit-appearance:none !important;  
   width: 15px;
   height: 15px;
   position: relative;
   top: -1px;
   background: #222;
 }

 input[type="checkbox"] {
   display: none;
 }
  input[type="checkbox"] + label {
    display: inline-block !important;
    height: 15px;
    width: 15px;
    margin: 0px 4px 0px 0px !important;
    padding: 0px;
    position: relative;
    top: 4px;
    background: #FFF;
    cursor: pointer;
  
    &:before {
      content: "";
      display: inline-block;
      width: 4px;
      height: 8px;
      background: #F38630;
      opacity: 0;
      position: relative;
      top: -5px;
      right: -1px;
      transform: skewX(0deg);
      transition: all 0.24s;
    }
    &:after {
      content: "";
      display: inline-block;
      width: 4px;
      height: 15px;
      background: #F38630;
      opacity: 0;
      position: relative;
      top: -5px;
      right: -3px;
      transform: skewX(0deg);
      transition: all 0.24s;
    }
  }
input[type="checkbox"]:checked + label {
  &:before {
    transform: skewX(30deg);
    opacity: 1;
  }
  &:after {
    transform: skewX(-30deg);
    opacity: 1;
  }
}

.container { width: 100%; }

header {
  height: 65px;
  background: #879BAD;
  color: darken(#879BAD, 40%);
  font-size: 36px;
  font-weight: 400;
  text-align: center;
  padding: 5px;
}

.metronome-container {
  width: 75%;
  height: auto;
  position: relative;
  margin: 40px auto;
  background: lighten(#222, 5%);
  
  .options-btn {
    color: #FFF;
    font-size: 22px;
    position: absolute;
    top: 20px;
    right: 15px;
    transform: rotateZ(0deg);
    cursor: pointer;
    transition: transform 0.3s;
    
    &:hover {
      transform: rotateZ(60deg);
    }
  }
  
  .options-active {
    max-height: 1000px !important;
    padding: 15px;
    overflow: visible !important;
  }
  .options {
    max-height: 0px;
    background: #FFF;
    color: #333;
    overflow: hidden;
    transition: all 0.25s;
    position: absolute;
    top: 50px;
    right: 15px;
    
    .up {
      color: #FFF;
      font-size: 22px;
      position: absolute;
      top: -14px;
      right: 3px;
      z-index: 500;
      transform: rotateZ(180deg);
    }
    
    label {
      display: block;
      margin-top: 10px;
    }
  }
  
  .counter {
    width: 100%;
    padding-bottom: 10px;
    background: lighten(#333, 5%);
    text-align: center;
    
    .dot {
      width: 15px;
      height: 15px;
      display: inline-block;
      margin: 25px 10px 10px 10px;
      background: #FFF;
      border-radius: 50%;
      cursor: pointer;
      transition: all 0.3s;
    }
    .active { background: lighten(#44B8FF, 5%); }
  }
  
  .controls {
    color: #FFF;
    padding: 20px;
    text-align: center;
    
    label {
      display: block;
      margin-bottom: 18px;
      
      span {
        background: #FFF;
        color: #222;
        padding: 5px;
        
        i { 
          cursor: pointer; 
          transition: color 0.2s; 
        }
        i:hover { color: #FA6900; }
      }
    }
    
    input {
      width: 30px;
      border: 0px solid;
      color: #222;
      text-align: center;
      padding: 4px;
    }
    
    select {
      border: 0px;
      color: #222;
    }
    
    .play-btn {
      width: 45%;
      background: #D6E26D;
      border: 0px;
      border-radius: 5px;
      padding: 12px;
      color: darken(#D6E26D, 50%);
      transition: background 0.3s;
      
      &:hover { background: lighten(#D6E26D, 15%); }
    }
    
    .tap-btn {
      width: 45%;
      background: #888;
      border: 0px;
      border-radius: 5px;
      padding: 12px;
      color: lighten(#888, 45%);
      transition: background 0.3s;
      
      &:hover { background: lighten(#888, 5%); }
    }
  }
}

footer {
  width: 50%;
  height: auto;
  background: #FFF;
  color: #444;
  padding: 10px;
  margin: 50px auto;
}
            
          
!
            
              window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var timer, noteCount, counting, accentPitch = 380, offBeatPitch = 200;
var delta = 0;
var curTime = 0.0;

// Load up dots on pageload
$("document").ready(function() {
$(".ts-top").trigger("change");
$("header").fitText(1, { maxFontSize: "46px" });
});


/*
Scheduling Help by: https://www.html5rocks.com/en/tutorials/audio/scheduling/
*/
function schedule() {
while(curTime < context.currentTime + 0.1) {
  playNote(curTime);
  updateTime();
}
timer = window.setTimeout(schedule, 0.1);
}

function updateTime() {
curTime += 60.0 / parseInt($(".bpm-input").val(), 10);
noteCount++;
}

/* Play note on a delayed interval of t */
function playNote(t) {
    var note = context.createOscillator();

    if(noteCount == parseInt($(".ts-top").val(), 10) )
      noteCount = 0;

    if( $(".counter .dot").eq(noteCount).hasClass("active") )
      note.frequency.value = accentPitch;
    else
      note.frequency.value = offBeatPitch;

    note.connect(context.destination);

    note.start(t);
    note.stop(t + 0.05);

    $(".counter .dot").attr("style", "");

    $(".counter .dot").eq(noteCount).css({
      transform: "translateY(-10px)",
      background: "#F75454"
    });
}

function countDown() {
  var t = $(".timer");

  if( parseInt(t.val(), 10) > 0 && counting === true)
  {
      t.val( parseInt(t.val(), 10) - 1 );
      window.setTimeout(countDown, 1000);
  }
  else
  {
    $(".play-btn").click();
    t.val(60);
  }
}

/* Tap tempo */
$(".tap-btn").click(function() {
  var d = new Date();
  var temp = parseInt(d.getTime(), 10);

  $(".bpm-input").val( Math.ceil(60000 / (temp - delta)) );
  delta = temp;
});

/* Add or subtract bpm */
$(".bpm-minus, .bpm-plus").click(function() {
if($(this).hasClass("bpm-minus"))
  $(".bpm-input").val(parseInt($(".bpm-input").val(), 10) - 1 );
else
  $(".bpm-input").val(parseInt($(".bpm-input").val(), 10) + 1 );
});

/* Change pitches for tones in options */
$(".beat-range, .accent-range").change(function() {
  if($(this).hasClass("beat-range"))
    offBeatPitch = $(this).val();
  else
     accentPitch = $(this).val();
});

/* Activate dots for accents */
$(document).on("click", ".counter .dot", function() {
  $(this).toggleClass("active");
});

$(".options-btn").click(function() {
$(".options").toggleClass("options-active");
});

/* Add dots when time signature is changed */
$(".ts-top, .ts-bottom").on("change", function() {
  var _counter = $(".counter");
  _counter.html("");

  for(var i = 0; i < parseInt($(".ts-top").val(), 10); i++)
  {
    var temp = document.createElement("div");
    temp.className = "dot";

    if(i === 0)
      temp.className += " active";

    _counter.append( temp );
  }
});


/* Play and stop button */
$(".play-btn").click(function() {
if($(this).data("what") === "pause")
{
  // ====== Pause ====== //
  counting = false;
  window.clearInterval(timer);
  $(".counter .dot").attr("style", "");
  $(this).data("what", "play").attr("style","").text("Play");
}
else {
  // ====== Play ====== //
  
if( $("#timer-check").is(":checked") )
 {
   counting = true;
   countDown();
 }
  
  curTime = context.currentTime;
  noteCount = parseInt($(".ts-top").val(), 10);
  schedule();

  $(this).data("what", "pause").css({
    background: "#F75454",
    color: "#FFF"
  }).text("Stop");
}
});
            
          
!
999px
Loading ..................

Console