Przeglądaj źródła

BeatSequence::reduceToNeighbour() implementiert

Fabian Peter Hammerle 10 lat temu
rodzic
commit
e60b2ae162
3 zmienionych plików z 29 dodań i 8 usunięć
  1. 23 0
      BeatSequence.cpp
  2. 4 0
      BeatSequence.h
  3. 2 8
      tests/beat-sequence-reducation.cpp

+ 23 - 0
BeatSequence.cpp

@@ -95,6 +95,29 @@ void BeatSequence::print(std::ostream& stream) const
     stream << "\n";
     stream.flush();
 }
+    
+void BeatSequence::reduceToNeighbour(BeatIndex factor)
+{
+    reduce(factor, moveReducationConflictToNextNeighbour, 0);
+}
+    
+void BeatSequence::moveReducationConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data)
+{
+    BeatSequence::BeatIndex previousIndex = beatIndex / factor * factor;
+
+    BeatSequence::BeatIndex targetIndex;
+    if((beatIndex - previousIndex) * 2 <= factor) {
+        targetIndex = previousIndex;
+    } else {
+        targetIndex = previousIndex + factor;
+        if(targetIndex >= sequence.size()) {
+            targetIndex = 0;
+        }
+    }
+    
+    MessageList& targetBeat = sequence[targetIndex];
+    targetBeat.splice(targetBeat.end(), sequence[beatIndex]);
+}
 
 UnresolvedBeatSequenceReducationConflict::UnresolvedBeatSequenceReducationConflict(BeatSequence::BeatIndex beatIndex)
     : parent("unresolved beat sequence reducation conflict"), beatIndex(beatIndex)

+ 4 - 0
BeatSequence.h

@@ -19,7 +19,11 @@ public:
     void expand(BeatIndex factor);
     void reduce(BeatIndex factor);
     void reduce(BeatIndex factor, ReducationConflictCallback conflictCallback, void* conflictCallbackData);
+    void reduceToNeighbour(BeatIndex factor);
     void print(std::ostream& stream) const;
+
+private:
+    static void moveReducationConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data); 
 };
 
 class UnresolvedBeatSequenceReducationConflict : public std::runtime_error

+ 2 - 8
tests/beat-sequence-reducation.cpp

@@ -3,15 +3,9 @@
 
 using namespace midi;
 
-void conflict(BeatSequence& sequence, BeatSequence::BeatIndex factor, BeatSequence::BeatIndex beatIndex, void* data)
-{
-    std::cout << beatIndex << " " << data << std::endl;
-    sequence[beatIndex].clear();
-}
-
 int main()
 {
-    BeatSequence s(13);
+    BeatSequence s(15);
     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));
@@ -19,7 +13,7 @@ int main()
 
     s.print(std::cout);
 
-    s.reduce(3, conflict, (void*)1234);
+    s.reduceToNeighbour(3);
     s.print(std::cout);
 
     return 0;