How do I implement a Tempo function in my sequencer?

This is a discussion on How do I implement a Tempo function in my sequencer? within the C Programming forums, part of the General Programming Boards category; I know that there are many talented programmers here and I really need help with a problem I have. This ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    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...

    You cantīt teach an old dog new tricks.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Just create a variable and just use it as a coefficient for the current time.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    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?
    You cantīt teach an old dog new tricks.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    TimeYouUse = GetCurrentTime() * SomeRatio;

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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    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?
    You cantīt teach an old dog new tricks.

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    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
    You cantīt teach an old dog new tricks.

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21