<commit_before>
window.AudioContext = window.AudioContext || window.webkitAudioContext;

var context = new AudioContext();

// var compressor = context.createDynamicsCompressor();
// compressor.threshold.value = -50;
// compressor.knee.value = 40;
// compressor.ratio.value = 12;
// compressor.reduction.value = -20;
// compressor.attack.value = 20;
// compressor.release.value = 0.25;
// compressor.connect(context.destination)

var Audio = function(name, path){
  this.name = name
  this.path = path
  this.gain = context.createGain()
  this.gain.connect(context.destination)
  this.pattern = {}
}

Audio.prototype.loadSound = function(){
  var sound = this
  var getSound = new XMLHttpRequest();
  getSound.open("GET", sound.path, true);
  getSound.responseType = "arraybuffer";
  getSound.onload = function(){
    context.decodeAudioData(getSound.response, function(audioBuffer){
      sound.buffer = audioBuffer;
    });
  }
  getSound.send();
}


var allSounds = {
  tomHi: new Audio('tomHi', 'sounds/TOM-HI.wav'),
  tomLo: new Audio('tomLo', 'sounds/TOM-LO.wav'),
  clav: new Audio('clav', 'sounds/CLAV.wav'),
  hatOpen: new Audio('hatOpen', 'sounds/HAT-OPEN.wav'),
  hatClosed: new Audio('hatClosed', 'sounds/HAT-CLOSED.wav'),
  clap: new Audio('clap', 'sounds/CLAP.wav'),
  snare: new Audio('snare', 'sounds/SNARE.wav'),
  kickHi: new Audio('kickHi', 'sounds/KICK-HI.wav'),
  kickLo: new Audio('kickLo', 'sounds/KICK-LO.wav'),
}



function loadAllSounds(){
  for (sound in allSounds) {
    allSounds[sound].loadSound()
  }
}


function playSound(audio){
  var sound = context.createBufferSource();
  sound.buffer = audio.buffer;
  sound.connect(audio.gain);
  sound.start(0);
  $('.instrument_label.'+audio.name).animate( { backgroundColor: "red" }, 0 )
  .animate( { backgroundColor: "white" }, 100 );

}

function instrumentLabelClickListener(){
  $('.instrument_label').on('click', function(){
    var sound = $(this).html()
    for (audio in allSounds){
      var track = allSounds[audio];
      if (track.name === sound) {
        playSound(track)
      }
    }
  })
}


var tempo = 100;
var rhythmIndex = 1;
var playing = false;
var sixteenthNoteTime;
var timer;


function play(){
  if(!playing){
    loop();
    playing = true;
  }else{
    rhythmIndex = 1
  }
};

function loop(){
  sixteenthNoteTime = 60 / tempo / 4;
  timer = setTimeout(function(){
    playCurrentIndex()
    loop()
  }, sixteenthNoteTime*1000)
}

function playCurrentIndex(){
  for (audio in allSounds){
    var track = allSounds[audio];
    var pattern = track.pattern
    if (pattern[rhythmIndex]) {
      playSound(track)
    }
  }
  $('.'+rhythmIndex+'.active')
  .animate( { backgroundColor: "yellow" }, 0 )
  .animate( { backgroundColor: "red" }, 500 );
  movePlayhead(rhythmIndex)
  progressRhythm()
};

function progressRhythm(){
  if(rhythmIndex < 16){
    rhythmIndex++
  }else{
    rhythmIndex = 1
  }
}

function stop(){
  clearTimeout(timer)
  playing = false
  rhythmIndex = 1
  $('.bar').css('background-color','');
};




function playButtonListener(){
  $('button.play').on('click', function(){
    play();
  });
}

function stopButtonListener(){
  $('button.stop').on('click', function(){
    stop();
  });
};


function trashButtonListener(){
  $('.trash').on('click', function(){
    var txt;
    var prompt = confirm("Clear your current pattern?");
    if (prompt == true) {
      clearPads();
    }
  })
}

function clearPads(){
  for (audio in allSounds){
    allSounds[audio].pattern = {}
  }
  $('div.pad').css('background-color', '').removeClass('active');
  stop();
}

function padClickListener(){
  $('div.pad').on('click', function(){
    var sound = $(this).attr('id')
    var position = $(this).attr("value")

    if ($(this).hasClass('active')){
      $(this).removeClass('active')
      $(this).css('background-color', '')
      allSounds[sound].pattern[position] = false

    } else {
      $(this).addClass('active')
      $(this).css('background-color', 'red')
      allSounds[sound].pattern[position] = true
    }
  });
}


function tempoChangeListener(){
  $('input.bpm').on('change', function(){
    tempo = $(this).val()
  });
}


function gainChangeListener(){<commit_msg>fixed clearPads() background color bug<commit_after>
window.AudioContext = window.AudioContext || window.webkitAudioContext;

var context = new AudioContext();

// var compressor = context.createDynamicsCompressor();
// compressor.threshold.value = -50;
// compressor.knee.value = 40;
// compressor.ratio.value = 12;
// compressor.reduction.value = -20;
// compressor.attack.value = 20;
// compressor.release.value = 0.25;
// compressor.connect(context.destination)

var Audio = function(name, path){
  this.name = name
  this.path = path
  this.gain = context.createGain()
  this.gain.connect(context.destination)
  this.pattern = {}
}

Audio.prototype.loadSound = function(){
  var sound = this
  var getSound = new XMLHttpRequest();
  getSound.open("GET", sound.path, true);
  getSound.responseType = "arraybuffer";
  getSound.onload = function(){
    context.decodeAudioData(getSound.response, function(audioBuffer){
      sound.buffer = audioBuffer;
    });
  }
  getSound.send();
}


var allSounds = {
  tomHi: new Audio('tomHi', 'sounds/TOM-HI.wav'),
  tomLo: new Audio('tomLo', 'sounds/TOM-LO.wav'),
  clav: new Audio('clav', 'sounds/CLAV.wav'),
  hatOpen: new Audio('hatOpen', 'sounds/HAT-OPEN.wav'),
  hatClosed: new Audio('hatClosed', 'sounds/HAT-CLOSED.wav'),
  clap: new Audio('clap', 'sounds/CLAP.wav'),
  snare: new Audio('snare', 'sounds/SNARE.wav'),
  kickHi: new Audio('kickHi', 'sounds/KICK-HI.wav'),
  kickLo: new Audio('kickLo', 'sounds/KICK-LO.wav'),
}



function loadAllSounds(){
  for (sound in allSounds) {
    allSounds[sound].loadSound()
  }
}


function playSound(audio){
  var sound = context.createBufferSource();
  sound.buffer = audio.buffer;
  sound.connect(audio.gain);
  sound.start(0);
  $('.instrument_label.'+audio.name).animate( { backgroundColor: "red" }, 0 )
  .animate( { backgroundColor: "white" }, 100 );

}

function instrumentLabelClickListener(){
  $('.instrument_label').on('click', function(){
    var sound = $(this).html()
    for (audio in allSounds){
      var track = allSounds[audio];
      if (track.name === sound) {
        playSound(track)
      }
    }
  })
}


var tempo = 100;
var rhythmIndex = 1;
var playing = false;
var sixteenthNoteTime;
var timer;


function play(){
  if(!playing){
    loop();
    playing = true;
  }else{
    rhythmIndex = 1
  }
};

function loop(){
  sixteenthNoteTime = 60 / tempo / 4;
  timer = setTimeout(function(){
    playCurrentIndex()
    loop()
  }, sixteenthNoteTime*1000)
}

function playCurrentIndex(){
  for (audio in allSounds){
    var track = allSounds[audio];
    var pattern = track.pattern
    if (pattern[rhythmIndex]) {
      playSound(track)
    }
  }
  $('.'+rhythmIndex+'.active')
  .animate( { backgroundColor: "yellow" }, 0 )
  .animate( { backgroundColor: "red" }, 500 );
  movePlayhead(rhythmIndex)
  progressRhythm()
};

function progressRhythm(){
  if(rhythmIndex < 16){
    rhythmIndex++
  }else{
    rhythmIndex = 1
  }
}

function stop(){
  clearTimeout(timer)
  playing = false
  rhythmIndex = 1
  $('.bar').css('background-color','');
};




function playButtonListener(){
  $('button.play').on('click', function(){
    play();
  });
}

function stopButtonListener(){
  $('button.stop').on('click', function(){
    stop();
  });
};


function trashButtonListener(){
  $('.trash').on('click', function(){
    var txt;
    var prompt = confirm("Clear your current pattern?");
    if (prompt == true) {
      clearPads();
    }
  })
}

function clearPads(){
  for (audio in allSounds){
    allSounds[audio].pattern = {}
  }
  $('div.pad').css('background-color', 'white').removeClass('active');
  stop();
}

function padClickListener(){
  $('div.pad').on('click', function(){
    var sound = $(this).attr('id')
    var position = $(this).attr("value")

    if ($(this).hasClass('active')){
      $(this).removeClass('active')
      $(this).css('background-color', '')
      allSounds[sound].pattern[position] = false

    } else {
      $(this).addClass('active')
      $(this).css('background-color', 'red')
      allSounds[sound].pattern[position] = true
    }
  });
}


function tempoChangeListener(){
  $('input.bpm').on('change', function(){
    tempo = $(this).val()
  });
}


function gainChangeListener(){
{'fraction_non_alphanumeric': 0.17994310099573257, 'fraction_numerical': 0.009009009009009009, 'mean_word_length': 3.7213206491326245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '456005', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2768, 'n_words': 559}