简体   繁体   中英

How to run a clock on its own thread

I'm trying to create a step sequencer in C++ that will eventually send out MIDI data. I created it by having a clock on its own thread that calculates the amount of time since the last beat, and if it is time for the next beat, it writes a piece of data to the console.

However, I find that no matter what I set the BPM to, I get messages at a rate that is obviously too slow. I can't seem to figure out why the timing on this thread is wrong, and it doesn't help that I'm not terribly familiar with how the std::chrono library works. Thoughts?

Code below:

#include <thread>
#include <mutex>
#include <chrono>
#include <vector>
#include <iostream>

class StepSequencer {
 public:
  StepSequencer();
  ~StepSequencer();
  void run();
  void setBeatsPerMinute(float bpm);
  void addNote(int noteValue, int beatIndex);
  void playNote(int beatIndex);
 protected:
  int mNumberOfBeatBins;
  int mSequencerPlayhead;
  float mBeatsPerMinute;
  float mSecondsPerBeat;
  std::vector<int> mBeatBins;
  std::mutex mMutex;
  std::thread mSequencerThread;
  bool mRunSequencerThread;
  std::chrono::time_point<std::chrono::system_clock> mLastBeatTime;
  std::chrono::time_point<std::chrono::system_clock> mCurrentTime;
};

#include "stdafx.h"
#include "StepSequencer.h"

StepSequencer::StepSequencer() {
  mNumberOfBeatBins = 16;
  for(int i = 0; i < 16; i++) {
    mBeatBins.push_back(0);
  }

  mBeatsPerMinute = 0;
  mSecondsPerBeat = 1;

  mLastBeatTime = std::chrono::system_clock::now();
  mCurrentTime = std::chrono::system_clock::now();

  mSequencerPlayhead = 0;

    mRunSequencerThread = false;
    mSequencerThread = std::thread(&StepSequencer::run, this);
}

StepSequencer::~StepSequencer() {
  if(mSequencerThread.joinable()) {
    mSequencerThread.join();
  }
}

void StepSequencer::run() {
  mRunSequencerThread = true;
  while(mRunSequencerThread) {
        mCurrentTime = std::chrono::system_clock::now();
    mMutex.lock();
        if (std::chrono::duration_cast<std::chrono::seconds>(mCurrentTime - mLastBeatTime).count() > mSecondsPerBeat) {
      mSequencerPlayhead++;
      mSequencerPlayhead = mSequencerPlayhead % mNumberOfBeatBins;
      playNote(mSequencerPlayhead);
            mLastBeatTime = std::chrono::system_clock::now();
    }
    mMutex.unlock();
    this_thread::sleep_for(std::chrono::milliseconds(1));
  }
} 

void StepSequencer::setBeatsPerMinute(float bpm) {
  mMutex.lock();
  mBeatsPerMinute = bpm;
  if(mBeatsPerMinute > 0) {
      mSecondsPerBeat = 60.0 / mBeatsPerMinute;
  }
  else {
    mSecondsPerBeat = 1;
  }
 mMutex.unlock();
}

void StepSequencer::addNote(int noteValue, int beatIndex) {
  mBeatBins[beatIndex] = noteValue;
}

void StepSequencer::playNote(int beatIndex) {
  std::cout << mBeatBins[beatIndex] << std::endl;
}

std::chrono::seconds has a representation of 'A signed integral type of at least 35 bits'. So you are going to get a value of count() which increments only once per second, giving the option of 60,30,20,15,12, etc. beats per minute.

Work in milliseconds or use a custom duration which is backed by a floating point value instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM