Fabian Peter Hammerle пре 10 година
родитељ
комит
7c895adbb1
4 измењених фајлова са 45 додато и 3 уклоњено
  1. 9 0
      Bar.h
  2. 10 0
      Beat.h
  3. 22 3
      Loop.h
  4. 4 0
      main.cpp

+ 9 - 0
Bar.h

@@ -0,0 +1,9 @@
+#include "Beat.h"
+
+class Bar
+{
+public:
+    static const unsigned short beatsCount = 8;
+    Beat beats[beatsCount];
+};
+

+ 10 - 0
Beat.h

@@ -0,0 +1,10 @@
+#include <iostream>
+
+class Beat
+{
+    void trigger() 
+    {
+        std::cout << "\atick" << std::endl;
+    }
+};
+

+ 22 - 3
Loop.h

@@ -2,6 +2,8 @@
 #include <thread>
 #include <mutex>
 #include <chrono>
+#include <vector>
+#include "Bar.h"
 
 class Loop
 {
@@ -12,10 +14,16 @@ class Loop
     bool stopNext;
 
 public:
+
     bool enabled;
+    unsigned short currentBarIndex;
+    unsigned short currentBeatIndex;
+    std::vector<Bar> bars;
 
     Loop()
-        : bpm(60), stopNext(false), enabled(false), loopThread(&Loop::loop, this)
+        : bpm(60), stopNext(false), enabled(false), 
+          currentBarIndex(0), currentBeatIndex(0), lastBeatTime(0),
+          loopThread(&Loop::loop, this)
     {
     }
 
@@ -58,6 +66,7 @@ public:
     }
 
 private:
+
     std::chrono::milliseconds getMillisecondsSinceEpoch()
     {
         std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
@@ -68,13 +77,23 @@ private:
     void beat() 
     {
         std::cout << "\a" << lastBeatTime.count() << std::endl;
+        if(bars.size() > 0) {
+            if(currentBarIndex >= bars.size()) {
+                currentBarIndex = 0;
+            }
+        
+            std::cout << "trigger bar#" << currentBarIndex << std::endl;
+
+            currentBarIndex++;
+        }
+
+        lastBeatTime = getMillisecondsSinceEpoch();
     }
 
     void loop()
     {
         while(!stopNext) {
-            if((getMillisecondsSinceEpoch() - lastBeatTime) >= getBeatDuration()) {
-                lastBeatTime = getMillisecondsSinceEpoch();
+            if(enabled && (getMillisecondsSinceEpoch() - lastBeatTime) >= getBeatDuration()) {
                 beat();
             }
         }

+ 4 - 0
main.cpp

@@ -4,6 +4,10 @@
 int main() 
 {
     Loop loop;
+
+    loop.bars.push_back(Bar());
+    loop.bars.push_back(Bar());
+    
     loop.start();
 
     loop.setBpm(60);