Add pad led controls
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Mon, 6 Jun 2022 17:57:05 +0000 (20:57 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Mon, 6 Jun 2022 17:57:05 +0000 (20:57 +0300)
extensions/classes/midipush.sc
Хэзифу/test.scd

index 218640a..33a97a5 100644 (file)
@@ -5,7 +5,7 @@
 // https://github.com/Ableton/push-interface/blob/master/doc/AbletonPush2MIDIDisplayInterface.asc
 
 MidiPush : MidiHandler {
-  classvar buttonControls;
+  classvar buttonControls, padColors, padModes;
   var <>but, <>press, <>pad, <>bend, <>rotLeft, <>rotRight, <>rot, <>butup, <>butlo;
   var <>touchBend, <>touchRotLeft, <>touchRotRight, <>touchRot;
   var <tempo=1, <syncTempo=false, prTickPlayer;
@@ -65,6 +65,46 @@ MidiPush : MidiHandler {
       114 -> \volume,
       115 -> \panSend,
     ];
+
+    padColors = IdentityDictionary[
+      \white -> 0,
+      \red -> 1,
+      \amber -> 2,
+      \yellow -> 3,
+      \lime -> 4,
+      \green -> 5,
+      \spring -> 6,
+      \turq -> 7,
+      \cyan -> 8,
+      \sky -> 9,
+      \ocean -> 10,
+      \blue -> 11,
+      \orchid -> 12,
+      \magenta -> 13,
+      \pink -> 14
+    ];
+
+    padModes = IdentityDictionary[
+      \instant -> 0,
+      \change24th -> 1,
+      \change16th -> 2,
+      \change8th -> 3,
+      \change4th -> 4,
+      \changeBeat -> 4,
+      \change2th -> 5,
+      \pulse24th -> 6,
+      \pulse16th -> 7,
+      \pulse8th -> 8,
+      \pulse4th -> 9,
+      \pulseBeat -> 9,
+      \pulse2th -> 10,
+      \blink24th -> 11,
+      \blink16th -> 12,
+      \blink8th -> 13,
+      \blink4th -> 14,
+      \blinkBeat -> 14,
+      \blink2th -> 15,
+    ];
   }
 
   controlInit {
@@ -193,31 +233,22 @@ MidiPush : MidiHandler {
     "Џяпюӈи готов к использованию 😊".postln;
   }
 
-  tempo_ { |v=1|
-    {
-      tempo = v;
-      midiout.start;
-      1.do({
-        24.do({
-          midiout.midiClock;
-          (1.0/tempo/24).wait;
-        });
-      });
-      midiout.stop;
-    }.fork();
-  }
-
   syncTempo_{|sync=true|
+    if (syncTempo == sync) {^this};
+    syncTempo = sync;
     if (sync) {
       prTickPlayer = {
         midiout.start;
         true.while{
           midiout.midiClock;
           (1.0/24).wait;
+          thisThread.clock.beats.postln;
         }
       }.fork(TempoClock.default);
     } {
       prTickPlayer.stop;
+      prTickPlayer = nil;
+      midiout.stop;
     };
   }
 
@@ -232,6 +263,44 @@ MidiPush : MidiHandler {
     };
   }
 
+  calcColor {|c=0, l=3, m=false|
+    var color;
+    c = padColors[c] ? c;
+    if (c > 60) {^c};
+    if (c == 0, {
+      color = l;
+    },{
+      if (l == 0, {
+        color = 0;
+      }, {
+        if (m, {
+          color = c*4;
+        }, {
+          color = c*4 + 4 - l;
+        });
+      });
+    });
+    ^color;
+  }
+
+  // 3 arguments version only works for the colors 0..14
+  // c is color number, l is lightness from 0 to 3, m is whether blended color should be used
+  // for colors 61..127 use one argument with color number
+  ledPad {|row, col, light=3, color=0, mix=false, mode=1|
+    mode = padModes[mode] ? mode;
+    if ((col < 0) || (col > 7) || (row < 0) || (row > 7), {}, {
+      midiout.noteOn(mode, 36+(row*8)+col, this.calcColor(color, light, mix));
+    });
+  }
+
+  ledPadOff {
+    for (0, 7, {|i|
+      for (0, 7, {|j|
+        this.ledPad(i, j, 0);
+      });
+    });
+  }
+
 }
 
 // test
@@ -250,8 +319,8 @@ MidiPush : MidiHandler {
   ~col = (~col + delta).clip(0, 127);
   ~col.postln;
   (0..127).do({|i|
-    ~push.midiout.noteOn(0, i, ~col);
-    ~push.midiout.control(0, i, ~col);
+    ~push.midiout.noteOn(1, i, ~col);
+    ~push.midiout.control(1, i, ~col);
   });
 };
 )
index 0bffc00..725331a 100644 (file)
@@ -1,3 +1,36 @@
+//=============================================================================
+// test midi
+//=============================================================================
+(
+~kedi = MidiRemote25SL();
+~push = MidiPush();
+)
+
+(
+Pdef(\test, Pbind(*[
+  dur: 1,
+  db: -17,
+])).quant_(1).play;
+)
+
+(
+TempoClock.play({|beats, time, clock|
+  ~push.syncTempo_(false);
+  [beats, time, clock].postln;
+}, 1);
+)
+
+(
+~kedi.tempo_{|v|
+  TempoClock.tempo = v / 60;
+  v.postln;
+};
+)
+
+
+//=============================================================================
+
+
 (
 ~push = ~pushFactory.value;
 ~se = Sampler(\qaxi);