From bc57ce02e1d21187aa9ff939d93776c6fbf623dc Mon Sep 17 00:00:00 2001
From: rorist <rorist@0xcafe.ch>
Date: Fri, 18 Aug 2023 12:39:34 +0200
Subject: [PATCH] Fixed bpm and don't use sleep for the seq

---
 __init__.py | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/__init__.py b/__init__.py
index f3c04ff..c3ee53a 100755
--- a/__init__.py
+++ b/__init__.py
@@ -40,6 +40,8 @@ class EndlessSequencer(Application):
         #        'close.wav' ]
         self.current_sample = None
         self.current_note = 0
+        self.startTime = time.ticks_ms()
+        self.bpm = 120
         #self.sample_names = ['kick.wav', 'snare.wav', 'cowbell.wav', 'hihat.wav', 'silence.wav']
         #self.app_path = '/flash/sys/apps/seq/'
 
@@ -135,6 +137,7 @@ class EndlessSequencer(Application):
                         print('  seq {}'.format(self.sample_names[sample_id]))
                     self.notes.append(sample_id)
                     self.play_sample(sample_id)
+                    time.sleep(0.2)
 
         # Light petal
         for i in range(10):
@@ -162,25 +165,35 @@ class EndlessSequencer(Application):
         if len(self.notes) > 0 and not ct.petals[0].pressed:
             self.play_seq()
 
+    def next_bpm(self):
+        # taken from binarybrain code
+        beatTime = 60.0 * 1000.0 / self.bpm 
+        curRelTime = time.ticks_diff(time.ticks_ms(), self.startTime)
+        return curRelTime / beatTime > 1
+
     def play_sample(self, i):
         self.current_sample = i
         if i < len(self.samples):
             if self.DEBUG and i in self.sample_names:
                 print (self.sample_names[i])
             self.samples[i].signals.trigger.start()
-            time.sleep(0.2)
 
     def play_seq(self):
         if self.DEBUG:
             print("PLAY SEQ")
 
-        if self.current_note == len(self.notes): self.current_note = 0
+        if self.next_bpm():
+            self.startTime = time.ticks_ms()
+            if self.current_note == len(self.notes): self.current_note = 0
 
-        if self.DEBUG:
-            print("  play note {}".format(i))
-        self.play_sample(self.notes[self.current_note])
+            if self.DEBUG:
+                print("  play note {}".format(i))
 
-        self.current_note += 1
+            self.play_sample(self.notes[self.current_note])
+            self.current_note += 1
+        else:
+            if self.DEBUG:
+                print("  wait")
 
 if __name__ == "__main__":
     import st3m.run
-- 
GitLab