Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Accurately" calculate BPM (Beats Per Minute)?

Asked by 9 years ago

I may not have much to say (and this question may be broad-ish), but can you accurately calculate BPM in RBX.lua? Doing some "deep" web search was not helpful as there were not many tutorials on calculating BPM. Whilst one site may have a tutorial, tempotap.com seems to have on what I was looking for. It works, but it is not very accurate on Lua

--Javascript code taken from tempotap.com
--[[
    var startTime = null;
        var currentBeats = 0;



        /*
        $(window).keypress(function(event) {
            var spaceBarKey = 32;
            var lowerXKey = 120;
            var upperXKey = 88;

            var result = true;

            var keyCode = event.which;

            //if (!(event.which == 115 && event.ctrlKey)) return true;
                //alert(keyCode);

            if (keyCode == spaceBarKey) {
                handleNewBeat();
                result = false;
            } else if ((keyCode == lowerXKey) || (keyCode == upperXKey)) {
                handleBeat();
                result = false;
            }

            if (!result) {
                event.preventDefault();
            }
            return result;
        });
        */

        function handleNewBeat() {
            if (currentBeats == 0) {
                startTime = new Date();
            }
            currentBeats++;
            updateBpm();
        }

        function handleReset() {
            startTime = null;
            currentBeats = 0;
            updateBpm();
        }

        function updateBpm() {
            var value = ' ';
            var title = 'BPM';
            if (currentBeats > 1) {
                var now = new Date();
                var miliseconds = now.getTime() - startTime.getTime();
                var minutes = miliseconds / 60000.0;
                var bpm = (currentBeats - 1) / minutes;
                value = bpm.toFixed(2);
                title = 'BPM (' + currentBeats.toString() + ' beats)';
            } else if (currentBeats == 1) {
                title = 'BPM (1 beat)';
            }
            $('#divBpm').html(value); 
            $('#divBpmTitle').html(title);
        }

        shortcut.add(" ",function() {
            handleNewBeat();
        });
        shortcut.add("x",function() {
            handleReset();
        });
        shortcut.add("Shift+x",function() {
            handleReset();
        });
]]

--Javascript code 'translated' into Lua, but it is not accurate enough

local mouse = game.Players.LocalPlayer:GetMouse()

local starttime = nil
local currentBeat = 0

function handleNewBeat()
    if currentBeat == 0 then
        starttime = time()
    end
    currentBeat = currentBeat + 1 --original Javascript code is 'currentBeat++'
    updateBPM()
end

function updateBPM()
    if currentBeat > 1 then
        now = tick()
        miliseconds = now % 100000
        minute = miliseconds % 60
        bpm = (currentBeat - 1) * minute --/ 4 --I have done different operators, but none seem to be accurate either
        --script.Parent.Text = "BPM: "..bpm
        print("BPM: ", bpm)
    end
end

mouse.KeyDown:connect(function(key)
    if key == "t" then
        handleNewBeat()
    end
end)

1 answer

Log in to vote
0
Answered by 9 years ago

(It's milliseconds, by the way)

Change line 87 to use tick instead of time -- these are two different types of time and should not be combined (tick is real time seconds, time is game seconds; game seconds can go slower than real seconds in some circumstances)

Then change line 96-99 to be:

minutes = (now - starttime) / 60 --tick() is measured in seconds, divide by 60 seconds in a minute
bpm = currentBeat / minutes --# beats / minutes == "beats per minute"

Notably, you were using "%" when I think you needed to use "/".

0
I thought "time()" was mostly using your pc's time (or so I read it from somewhere). This seems accurate enough. Anyways, thank you! CMVProduction 25 — 9y
Ad

Answer this question