Get tempo over time

Happy Holidays! I have been struggling to get the hosts tempo over time. My goal is to get the tempo and use it as a variable anywhere in my script. The issue is once the function is ran it gets the tempo only at that exact moment. If an end-user changes the tempo later the script isn’t aware of the tempo change. I want to be able to continually tell the script to get the tempo. So far I’ve tried several things but none have worked. Anyone knows how I can enhance the following scipt?

T1 = getTempo()

I’ve tried scripts such as the one below but nothing has worked.

defineParameter("TempoDetect", nil, 0, 0, 10, 1) --Fast loop for sun gradient

function calcModulation()
  local freq = 100
  local rate = getSamplingRate() / 32
  local TempoDetect = TempoDetect + freq / rate
  
  if TempoDetect >= 10 then
      TempoDetect = TempoDetect - 10
      return getTempo(T1)
  end
end

Anyone know what I am doing incorrectly? No matter what I have tried I either end up with nil value for T1 or I end up only being able to get the tempo once.

Hi Chris,

Is it for an animation?

Something like this could probably work:

defineParameter("TempoDetect", nil, 0, 0, 10, 1) --Fast loop for sun gradient
assert(getTempo() ~= -1) --Check if host provides tempo information

local rate = getSamplingRate() / 32
local phase = 0

function calcModulation()
  if isPlaying() then
    _, phase = math.modf(getBeatTime())
  else
    local freq = getTempo() / 60
    phase = phase + freq / rate
  end

  if phase >= 1 then
    phase = phase - 1
  end

  TempoDetect = 10 * phase

end

Ooops! That comment was just an old comment I forgot to delete lol.

No it isn’t for an animation. I am getting the tempo and then putting it into a table of note times as a synchronized option. So basically the user can choose to adjust the time in increments of milliseconds or synchronize to note values. When they choose to synchronize to note values I want to get the tempo and convert and calculate the note time value from the tempo. The issue I had is that if the tempo changes then the values in the table are not longer accurate. This is why I want the tempo to occasionally check the tempo to ensure accuracy. Your example doesn’t seem to work for me either. I’ll send you a PM.