GML Dynamic Music Sequencer Timing Troubles

Discussion in 'Programming' started by deciduous, Jul 12, 2018.

Tags:
  1. deciduous

    deciduous Member

    Joined:
    Jul 6, 2016
    Posts:
    16
    Hello friends!

    Yesterday I started a fun new project that involves some tricky high precision timing, and I'm having some trouble finding a way to keep it running at a fixed rate.

    I'm creating a beat sequencer that uses a BPM value to find a rate at which to move through 16 steps every bar. If my BPM is set to 120 then it should sequentially trigger each step every 125 milliseconds. Then I have boolean arrays of audio samples that each have 16 indexes. If an element is set to true and its index matches the step number when it is triggered, the audio sample will be played. Here is a video demonstrating what I'm talking about:



    So far this works in terms of execution, but not in terms of optimization. I'd love to find some way to make it essentially framerate independent, because it will occasionally lag which is a problem if you want something to stay precisely on beat.

    Here is the code for managing the timing.

    Code:
    //create event
    bpm = 120;
    step_time = (1 / ((bpm / 60) * 4)) * 1000; //in milliseconds
    
    start_time = current_time;
    
    trigger_step = false;
    
    enum states
    {
        PLAYING,
        PAUSED,
        STOPPED
    }
    
    current_state = states.STOPPED;
    
    
    Code:
    //step event
    switch(current_state)
    {
        case states.PLAYING:
            if(current_time - start_time >= step_time)
            {
                trigger_step = true;
                start_time = current_time;
            }
            else
                trigger_step = false;
              
            break;
        case states.PAUSED:
            break;
        case states.STOPPED:
            break;
    }
    
    
    I've seen delta_time come up in a lot of my google searches, but either I can't implement it in such a way that works for what I'm trying to do, or I don't quite understand how to use it in this case (this is my guess).

    Does anyone have any suggestions or ideas?

    Thanks very much in advance!
     
    Last edited: Jul 12, 2018
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,547
    You should look into audio queues for this.

    Delta time can be used to compensate for existing delays between steps, but it can't be used to act between steps.
     
  3. deciduous

    deciduous Member

    Joined:
    Jul 6, 2016
    Posts:
    16
    Shoot I should have mentioned that I also want gameplay events to trigger on certain beats, so I think I'll have to keep everything tied to the step events in that case, no?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,547
    The gameplay events can work on delta time. It's the part playing the samples that requires action between steps and thus can't work on delta time.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice