    function clamp(val, min, max) {
        return val < min ? min : val > max ? max : val;
    }

    function findBestLastBeat(type) {
        var reachThreshold = 50;
        var quanta = track.analysis[type];
        var longest = 0;
        var longestReach = 0;
        for (var i = quanta.length - 1; i >= 0; i--) {
            var q = quanta[i];
            var distanceToEnd = quanta.length - i;

            // if q is the last quanta, then we can never go past it
            // which limits our reach