Browse Source

BeatSequence::expand() implementiert

Fabian Peter Hammerle 10 years ago
parent
commit
2e5e99eac3
4 changed files with 91 additions and 0 deletions
  1. 62 0
      BeatSequence.cpp
  2. 6 0
      BeatSequence.h
  3. 1 0
      tests/CMakeLists.txt
  4. 22 0
      tests/beat-sequence-expansion.cpp

+ 62 - 0
BeatSequence.cpp

@@ -0,0 +1,62 @@
+#include "BeatSequence.h"
+#include <sstream>
+
+namespace midi {
+
+BeatSequence::BeatSequence()
+    : BeatSequence(0)
+{
+}
+
+BeatSequence::BeatSequence(BeatIndex size)
+    : parent(size)
+{
+}
+
+void BeatSequence::expand(BeatIndex factor)
+{
+    if(factor < 1) {
+        throw "factor may not be less than 1."; 
+    }
+
+    resize(size() * factor);
+
+    for(signed int targetIndex = (size() - factor); targetIndex > 0; targetIndex -= factor)
+    {
+        BeatIndex sourceIndex = targetIndex / factor;
+        at(sourceIndex).swap(at(targetIndex));  
+    }
+}
+
+void BeatSequence::print(std::ostream& stream) const
+{
+    stream << "beat sequence:\n";
+
+    for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
+
+        std::stringstream beatLabelStream;
+        beatLabelStream << "beat #" << beatIndex << ": ";
+        std::string beatLabel = beatLabelStream.str();
+        stream << beatLabel;
+
+        std::string spacer(beatLabel.size(), ' ');
+
+        const MessageList& beat = at(beatIndex);
+        if(beat.size() == 0) {
+            stream << "no message" << "\n";
+        } else {
+            for(MessageList::const_iterator msg_it = beat.begin(); msg_it != beat.end(); msg_it++) {
+                if(msg_it != beat.begin()) {
+                    stream << spacer;
+                }
+                (*msg_it)->print(stream);
+            }
+        }
+
+    }
+
+    stream << "\n";
+    stream.flush();
+}
+
+} // namepsace

+ 6 - 0
BeatSequence.h

@@ -1,5 +1,6 @@
 #pragma once
 #pragma once
 #include <vector>
 #include <vector>
+#include <ostream>
 #include "MessageList.h"
 #include "MessageList.h"
 
 
 namespace midi {
 namespace midi {
@@ -10,6 +11,11 @@ class BeatSequence : public std::vector<MessageList>
 
 
 public:
 public:
     typedef unsigned int BeatIndex;
     typedef unsigned int BeatIndex;
+
+    BeatSequence();
+    BeatSequence(BeatIndex size);
+    void expand(BeatIndex factor);
+    void print(std::ostream& stream) const;
 };
 };
 
 
 } // namespace
 } // namespace

+ 1 - 0
tests/CMakeLists.txt

@@ -12,6 +12,7 @@ function(add_test name sources)
         target_link_libraries(${name} midi)
         target_link_libraries(${name} midi)
 endfunction(add_test)
 endfunction(add_test)
 
 
+add_test(beat-sequence-expansion beat-sequence-expansion.cpp)
 add_test(message-comparison message-comparison.cpp)
 add_test(message-comparison message-comparison.cpp)
 add_test(message-list-find message-list-find.cpp)
 add_test(message-list-find message-list-find.cpp)
 
 

+ 22 - 0
tests/beat-sequence-expansion.cpp

@@ -0,0 +1,22 @@
+#include "BeatSequence.h"
+#include <iostream>
+
+using namespace midi;
+
+int main()
+{
+    BeatSequence s(4);
+    for(BeatSequence::BeatIndex b = 0; b < s.size(); b++) {
+        s[b].push_back(std::make_shared<NoteOnMessage>(b, b * 10, 1));
+        s[b].push_back(std::make_shared<NoteOffMessage>(b, b * 10, 2));
+        s[b].push_back(std::make_shared<NoteOnMessage>(b, b * 10, 3));
+    }
+
+    s.print(std::cout);
+
+    s.expand(3);
+
+    s.print(std::cout);
+
+    return 0;
+}