2017-09-01 50 views
1

我正在尝试创建一个“每分钟节拍”(BPM)计算器,与您可以找到的一个相同(现在)为here。但出于某种原因,当我在测试歌曲的该链接上使用BPM计算器时,它会在7个按键中的实际值85.94之内获得1 BPM,并且从那里得到更准确的结果,在实际BPM的0.05内结束,而我的(基本上是同一编码的)Vue.js版本,它开始高得多(182 - > 126 - > 110)并从那里下降,但即使在60个按键后它仍然偏离了〜2 BPM,并且在一首完整的歌曲,它仍然约0.37 BPM。Vue.js计时计算不匹配普通JavaScript版本

下面的代码为the plain-JavaScript version at that link

var count = 0; 
var msecsFirst = 0; 
var msecsPrevious = 0; 

function ResetCount() 
    { 
    count = 0; 
    document.TAP_DISPLAY.T_AVG.value = ""; 
    document.TAP_DISPLAY.T_TAP.value = ""; 
    document.TAP_DISPLAY.T_RESET.blur(); 
    } 

function TapForBPM(e) 
    { 
    document.TAP_DISPLAY.T_WAIT.blur(); 
    timeSeconds = new Date; 
    msecs = timeSeconds.getTime(); 
    if ((msecs - msecsPrevious) > 1000 * document.TAP_DISPLAY.T_WAIT.value) 
    { 
    count = 0; 
    } 

    if (count == 0) 
    { 
    document.TAP_DISPLAY.T_AVG.value = "First Beat"; 
    document.TAP_DISPLAY.T_TAP.value = "First Beat"; 
    msecsFirst = msecs; 
    count = 1; 
    } 
    else 
    { 
    bpmAvg = 60000 * count/(msecs - msecsFirst); 
    document.TAP_DISPLAY.T_AVG.value = Math.round(bpmAvg * 100)/100; 
    document.TAP_DISPLAY.T_WHOLE.value = Math.round(bpmAvg); 
    count++; 
    document.TAP_DISPLAY.T_TAP.value = count; 
    } 
    msecsPrevious = msecs; 
    return true; 
    } 
document.onkeypress = TapForBPM; 

// End --> 

这是我的版本:

computed: { 
    tappedOutBpm: function() { 
     let totalElapsedSeconds = (this.timeOfLastBpmKeypress - this.timeOfFirstBpmKeypress)/1000.0 
     let bpm = (this.numberOfTapsForBpm/totalElapsedSeconds) * 60.0 
     return Math.round(100*bpm)/100; 
    }, 
}, 
methods: { 
    tapForBPM: function() { 
     let now = new Date; 
     now = now.getTime(); 
     // let now = window.performance.now() 
     if (this.timeOfFirstBpmKeypress === 0 || now - this.timeOfLastBpmKeypress > 5000) { 
      this.timeOfFirstBpmKeypress = now 
      this.timeOfLastBpmKeypress = now 
      this.numberOfTapsForBpm = 1 
     } else { 
      this.timeOfLastBpmKeypress = now 
      this.numberOfTapsForBpm++ 
     } 
    } 
} 

回答

0

我想通了通过两者的我们的代码步进。

的问题是,我是抽头数尽快设置为1当用户敲击的键第一次,而实际上它不是水龙头我想算,但是击败和第一拍不需要一个点击,但是两个:该节拍的开始和结束。所以我应该做的是将变量重命名为numberOfTappedOutBeats,并将其设置为0,而不是1