Thread: iterate through loop (timer/bpm)

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    iterate through loop (timer/bpm)

    Hi,

    I just signed up to these forums to help me with my new found hobby in c++

    I'm wanting to iterate 16 steps through a loop at a specific tempo/bpm. I know in the Arduino environment there is the millis() statement used with an if statement but I'm struggling with c++.

    At some point I want to develop audio dsp applications on ARM STM32.

    Do I have to set some kind of clock speed?

    for instance - 1 x 16 step sequence would include 96 pulses/cycles. (standard 24pulses per quarter note.)

    if the sequence was running at 120beats per minute then:

    120 * 96 = 11,520 pulses/cycles

    11,520 / 60s = 192 hz

    so I know how to sort of get my clock/tempo but how do I get a loop to run at this tempo and also execute some code at each step in the sequence?

    I've been reading BasicSynth, and a beginners guide to DSP as well as C++ and I'm currently doing a video course on c++ on udemy to help me, if anyone has any other recommendations in this field that would help it would be a great help.

    P

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    //#define BUSY_WAIT    // uncomment for busy wait
    
    #include <iostream>
    #include <chrono>
    #include <thread>
    
    void proc() {
        static int i = 0;
        std::cout << i++ << '\n';
    }
    
    int main() {
        using namespace std;
        using rate = chrono::duration<chrono::steady_clock::rep, ratio<1, 10>>;
    
        auto next = chrono::steady_clock::now() + rate{1};
    
        while (true) {
    
            proc();
    
    #ifdef BUSY_WAIT
            while (chrono::steady_clock::now() < next)
                ;
    #else
            this_thread::sleep_until(next);
    #endif
    
            next += rate{1};
        }
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need to iterate through an std::map...
    By Programmer_P in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2011, 11:46 AM
  2. For Loop won't iterate to next bit of code.
    By acidblue in forum C Programming
    Replies: 5
    Last Post: 04-25-2010, 02:25 PM
  3. ::iterate
    By Coding in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2008, 05:46 AM
  4. Timer/Loop on Listbox
    By silentkarma in forum Windows Programming
    Replies: 8
    Last Post: 09-13-2006, 09:03 PM
  5. Nested loop timer
    By lineb in forum C Programming
    Replies: 1
    Last Post: 03-05-2006, 04:31 PM

Tags for this Thread