Browse Source

sequencer player beating

Fabian Peter Hammerle 10 years ago
parent
commit
301deee6a8
5 changed files with 177 additions and 0 deletions
  1. 15 0
      BeatSequence.h
  2. 89 0
      BeatSequencePlayer.cpp
  3. 43 0
      BeatSequencePlayer.h
  4. 3 0
      tests/CMakeLists.txt
  5. 27 0
      tests/beat-sequence-player.cpp

+ 15 - 0
BeatSequence.h

@@ -0,0 +1,15 @@
+#pragma once
+#include <vector>
+#include "MessageList.h"
+
+namespace midi {
+
+class BeatSequence : public std::vector<MessageList>
+{
+    typedef std::vector<MessageList> parent;
+
+public:    
+    typedef unsigned int BeatIndex;
+};
+
+} // namespace

+ 89 - 0
BeatSequencePlayer.cpp

@@ -0,0 +1,89 @@
+#include "BeatSequencePlayer.h"
+#include <iostream>
+
+namespace midi {
+
+BeatSequencePlayer::BeatSequencePlayer()
+    : BeatSequencePlayer(0)
+{
+}
+
+BeatSequencePlayer::BeatSequencePlayer(const BeatSequence* seq)
+    : looping(false), nextBeat(0), 
+      clock(&beat, (void*)this), sequence(seq)
+{
+}
+
+bool BeatSequencePlayer::getLooping()
+{
+    looping_mutex.lock();
+    bool l = looping;
+    looping_mutex.unlock();
+    return l;
+}
+
+void BeatSequencePlayer::setLooping(bool l)
+{
+    looping_mutex.lock();
+    looping = l;
+    looping_mutex.unlock();
+}
+
+BeatSequencePlayer::BeatIndex BeatSequencePlayer::getNextBeat()
+{
+    nextBeat_mutex.lock();
+    BeatIndex n = nextBeat;
+    nextBeat_mutex.unlock();
+    return n;
+}
+
+BeatSequencePlayer::Bpm BeatSequencePlayer::getBpm()
+{
+    return clock.getBpm();
+}
+
+void BeatSequencePlayer::setBpm(Bpm bpm)
+{
+    clock.setBpm(bpm);
+}
+
+void BeatSequencePlayer::setNextBeat(BeatIndex n)
+{
+    nextBeat_mutex.lock();
+    nextBeat = n;
+    nextBeat_mutex.unlock();
+}
+
+void BeatSequencePlayer::play()
+{
+    setLooping(false);
+    start();
+}
+
+void BeatSequencePlayer::loop()
+{
+    setLooping(true);
+    start();
+}
+
+void BeatSequencePlayer::start()
+{
+    clock.start();
+}
+
+void BeatSequencePlayer::stop()
+{
+    clock.stop();
+}
+
+bool BeatSequencePlayer::isPlaying()
+{
+    return clock.isRunning();
+}
+
+void BeatSequencePlayer::beat(void* data)
+{
+    std::cout << "beat" << std::endl;
+}
+
+} // namespace

+ 43 - 0
BeatSequencePlayer.h

@@ -0,0 +1,43 @@
+#pragma once
+#include <mutex>
+#include "Output.h"
+#include "BeatSequence.h"
+#include "CallbackClock.h"
+
+namespace midi {
+
+class BeatSequencePlayer
+{
+public:
+    typedef BeatSequence::BeatIndex BeatIndex;
+    typedef CallbackClock::bpm_type Bpm;
+
+private:
+    bool looping;
+    std::mutex looping_mutex;
+    BeatIndex nextBeat;
+    std::mutex nextBeat_mutex;
+    CallbackClock clock;
+
+public:
+    const BeatSequence* sequence;
+
+    BeatSequencePlayer();
+    BeatSequencePlayer(const BeatSequence* sequence);
+    Bpm getBpm();
+    void setBpm(Bpm bpm);
+    bool getLooping();
+    void setLooping(bool looping);
+    BeatIndex getNextBeat();
+    void setNextBeat(BeatIndex nextBeat);
+    void play();
+    void loop();
+    void start();
+    void stop();
+    bool isPlaying();
+
+private:
+    static void beat(void* data);
+};
+
+} // namespace

+ 3 - 0
tests/CMakeLists.txt

@@ -21,3 +21,6 @@ target_link_libraries(clock midi)
 
 add_executable(callback-clock callback-clock.cpp)
 target_link_libraries(callback-clock midi) 
+
+add_executable(beat-sequence-player beat-sequence-player.cpp)
+target_link_libraries(beat-sequence-player midi) 

+ 27 - 0
tests/beat-sequence-player.cpp

@@ -0,0 +1,27 @@
+#include "BeatSequencePlayer.h"
+
+using namespace std;
+using namespace midi;
+
+int main()
+{
+    BeatSequencePlayer p;
+    try {
+        p.start();
+    } catch(const char* ex) {
+        cout << ex << endl;
+    }
+    
+    while(true) {
+        BeatSequencePlayer::Bpm bpm;
+        cin >> bpm;
+        if(bpm == 0) {
+            p.stop();
+            break;
+        }
+        p.setBpm(bpm);
+    }
+
+    return 0;
+}
+