2017-09-01 51 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