# Thread: How do I implement a Tempo function in my sequencer?

1. ## How do I implement a Tempo function in my sequencer?

I know that there are many talented programmers here and I really need help with a problem I have. This is the story:

I am programming a midi sequencer. It can record and play back the music I record but I need a smart way to play back my recording at different Tempo's. I want the sequencer to handle Tempo's from 1 to 300 BPM (Beats Per Minute) with 2 decimals. And it must be able to handle tempo changes as well.

For example: The song begin at 120 BPM, after 10 sec it will change to 140 BPM (or whatever) and back to 80 BPM after 50 sec.

The theory behind recording and playing back midi is very simple. When midi comes in to the sequencer they get a timestamp that is a main timer telling when they arrived, midi and timer is saved together. To play back midi the saved timestamp is compared to the main timer and when the time matches the sequencer sends out the saved midi again. But this is at the same tempo they where recorded at so...

The timestamp values must in some way be recalculated based on the tempo I select in my sequencer but how? Maybe I must use a different timestamp format or whatever, help please...

2. Just create a variable and just use it as a coefficient for the current time.

3. Originally posted by Polymorphic OOP
Just create a variable and just use it as a coefficient for the current time.
Can you please give me an example what you mean?

4. TimeYouUse = GetCurrentTime() * SomeRatio;

2.0 for twice as fast, 0.5 for half, etc.

5. Originally posted by Polymorphic OOP
TimeYouUse = GetCurrentTime() * SomeRatio;

2.0 for twice as fast, 0.5 for half, etc.
Ok, I see, seems to be a easy way of doing it. But what about changes in tempo at different locations in my song? Every timstamp is from the beginning of the song, not the time between events. So I suppose I can't just recalculate every timestamp when I have many tempo changes in my song?

Example: I have a tempo change from 120 to 140 BPM 60 sec into the song. The time to get there is different depending on what tempo changes I have before this happens. If I have a fast tempo I get there fast, and a slow tempo takes me longer to get there. What mathematics is involved in this?

6. Wouldn't it make more sense to run two timer's? One upon which you base your tempo, and the other that is used as 'real time' to give length of song, etc. Rather than use maths to work out how far you were into things based upon your tempo timer.

7. Originally posted by Azuth
Wouldn't it make more sense to run two timer's? One upon which you base your tempo, and the other that is used as 'real time' to give length of song, etc. Rather than use maths to work out how far you were into things based upon your tempo timer.
I'm not sure I follow you here. Please explain with a good example

8. This code will probably be considered very ugly by the more experienced programmers, and it may have no relevance to your issue (I don't know much about your code), but what I meant was, don't limit yourself to just one timer.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

long millisec (int i) //Counts in parts of a second
{
clock_t millisec;
millisec = clock () / (CLOCKS_PER_SEC/i) ;
return millisec;
}

void gotoxy(int x, int y) /* Used for positioning text in console */
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main (int argc, char* argv[])
{

while (1)
{
gotoxy (0,0);
printf ("Counting in 100'ths of a second: %Ld\n",millisec(100));
printf ("Another counter, going twice as fast: %Ld\n",millisec(200));
}

return 0;
}```
I hope I got that right, seemed to be working right by my watch.