Created
July 17, 2017 04:41
-
-
Save chooblarin/5597a1db253a77adf6758fe86d67f87f to your computer and use it in GitHub Desktop.
Beat Detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BeatDetector { | |
constructor(holdTime, decayRate, minLevel) { | |
this.holdTime = holdTime // the number of frames to hold a beat | |
this.decayRate = decayRate | |
this.minLevel = minLevel // a volume less than this is no beat | |
this.cutOff = 0.0 | |
this.time = 0 | |
} | |
detect(level) { | |
const val = level || 0.0 | |
if (this.minLevel < val && this.cutOff < val) { | |
this.cutOff = val * 1.1 | |
this.time = 0 | |
return true | |
} else { | |
if (this.time <= this.holdTime) { | |
this.time += 1 | |
} else { | |
const decayed = this.cutOff * this.decayRate | |
this.cutOff = Math.max(decayed, this.minLevel) | |
} | |
return false | |
} | |
} | |
} | |
export default BeatDetector |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment