123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <chrono>
- #include <vector>
- #include "Bar.h"
- class Loop
- {
- std::thread loopThread;
- std::chrono::milliseconds lastBeatTime;
- unsigned short bpm;
- std::mutex bpmLock;
- bool stopNext;
- public:
- bool enabled;
- unsigned short currentBarIndex;
- unsigned short currentBeatIndex;
- std::vector<Bar> bars;
- Loop()
- : loopThread(&Loop::loop, this),
- bpm(60), stopNext(false), enabled(false),
- currentBarIndex(0), currentBeatIndex(0),
- lastBeatTime(0), bars(1)
- {
- }
- ~Loop()
- {
- stopNext = true;
- loopThread.join();
- }
- void setBpm(unsigned short b)
- {
- bpmLock.lock();
- bpm = b;
- bpmLock.unlock();
- }
- unsigned short getBpm()
- {
- bpmLock.lock();
- unsigned short r = bpm;
- bpmLock.unlock();
- return r;
- }
- std::chrono::milliseconds getBeatDuration()
- {
- return std::chrono::milliseconds(
- (long long)((double)60 * 1000 / bpm)
- );
- }
- void start()
- {
- enabled = true;
- }
- void stop()
- {
- enabled = false;
- }
- private:
- std::chrono::milliseconds getMillisecondsSinceEpoch()
- {
- std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
- std::chrono::system_clock::duration duration = now.time_since_epoch();
- return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
- }
- void beat()
- {
- // std::cout << "\a" << lastBeatTime.count() << std::endl;
- if(bars.size() > 0) {
- if(currentBarIndex >= bars.size()) {
- currentBarIndex = 0;
- }
- if(currentBeatIndex >= bars[currentBarIndex].beatsCount) {
- currentBeatIndex = 0;
- currentBarIndex++;
- if(currentBarIndex >= bars.size()) {
- currentBarIndex = 0;
- }
- }
- std::cout << "trigger bar#" << currentBarIndex << " beat#" << currentBeatIndex << std::endl;
- bars[currentBarIndex].beats[currentBeatIndex].trigger();
- currentBeatIndex++;
- }
- lastBeatTime = getMillisecondsSinceEpoch();
- }
- void loop()
- {
- while(!stopNext) {
- if(enabled && (getMillisecondsSinceEpoch() - lastBeatTime) >= getBeatDuration()) {
- beat();
- }
- }
- }
- };
|