Improve key synths and binds
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Sun, 18 Dec 2022 00:26:39 +0000 (01:26 +0100)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Sun, 18 Dec 2022 00:26:39 +0000 (01:26 +0100)
extensions/classes/midibind.sc
extensions/classes/midipedals.sc
extensions/classes/midisynths.sc
startup.scd

index e8d780c..9f2bcf2 100644 (file)
     }
   }
 
-  bindWarp {|param, warp=(\lin.asWarp), dict=nil|
-    warp = warp.asWarp;
+  bindWarp {|param, spec=\lin, dict=nil|
+    spec = spec.asSpec;
     ^{|v|
-      var newVal = warp.map(v);
+      var newVal = spec.map(v);
       this.set(param, newVal);
       "%: % = %\n".postf(this, param, newVal);
       dict !? {dict[param] = newVal;};
index 2b30ac3..5731d42 100644 (file)
@@ -2,7 +2,7 @@ MidiPedals : MidiHandler {
   var <>sustain;
   var <>express;
 
-  *new {|name='Pedals', midiNames=#["ReMOTE SL", ["ReMOTE SL MIDI 1", "ReMOTE SL MIDI 1"]], reveal=true|
+  *new {|name='Pedals', midiNames=#["ReMOTE SL", ["ReMOTE SL MIDI 1", "Port 1"]], reveal=true|
     ^super.newCopyArgs(name, midiNames, reveal).midiInit.controlInit;
   }
 
index 27e088e..1aa0783 100644 (file)
@@ -48,7 +48,7 @@ KeySynth : MidiSynth {
   var <>scale;
 
   *new {|midihandler, synthName=\default, bendRadius=1, panAcross=false, params=nil, amp=1.0, group=nil, onlyWhite=false, scale=nil|
-    ^super.newCopyArgs(midihandler, synthName, bendRadius, panAcross, params, amp, group).midiInit.keyInit(onlyWhite, scale);
+    ^super.newCopyArgs(midihandler, synthName, bendRadius, panAcross, params, amp, group).midiInit.keyInit(onlyWhite, scale).polyKeyInit;
   }
 
   keyInit {|ow=false, sc=nil|
@@ -56,7 +56,9 @@ KeySynth : MidiSynth {
     scale = sc ? Scale.chromatic();
 
     notes = Array.newClear(128);
+  }
 
+  polyKeyInit {
     midihandler.key_{|d, v, n|
       if (this.isWhiteKey(n) || onlyWhite.not) {
         if (onlyWhite) {
@@ -72,6 +74,8 @@ KeySynth : MidiSynth {
           notes[n].set(\gate, 0);
           notes[n] = nil;
         };
+      } {
+        // black keys in onlyWhite mode
       };
     };
 
@@ -93,7 +97,7 @@ KeySynth : MidiSynth {
   }
 
   midiKeyToFreq {|n|
-    "Playing %".format(scale.degreeToFreq(n-60, 60.midicps, 0).cpsmidi.round(0.001)).postln;
+    //"Playing %".format(scale.degreeToFreq(n-60, 60.midicps, 0).cpsmidi.round(0.001)).postln;
     ^scale.degreeToFreq(n-60, 60.midicps, 0) * bend.midiratio;
   }
 }
@@ -191,32 +195,52 @@ PadSynth : KeySynth {
 //=============================================================================
 
 // connect synth to midi-keyboard, play only one note at a time
-KeySynthMono : MidiSynth {
+KeySynthMono : KeySynth {
   var <note, <midinote;
+  var <>poly;
 
-  *new {|midihandler, synthName=\default, bendRadius=1, panAcross=false, params=nil, amp=1.0, group=nil|
-    ^super.newCopyArgs(midihandler, synthName, bendRadius, panAcross, params, amp, group).midiInit.keyInit;
+  *new {|midihandler, synthName=\default, bendRadius=1, panAcross=false, params=nil, amp=1.0, group=nil, onlyWhite=false, scale=nil, poly=false|
+    ^super.newCopyArgs(midihandler, synthName, bendRadius, panAcross, params, amp, group).midiInit.keyInit(onlyWhite, scale).monoKeyInit(poly);
   }
 
-  keyInit {
+  monoKeyInit {|aPoly=false|
+    poly = aPoly;
+
     note = nil;
     midinote = -1;
+    if (poly) {
+      this.params[\doneAction] = 0;
+      note = Synth(synthName, [\gate, 0], group);
+    };
 
     midihandler.key_{|d, v, n|
+      if (onlyWhite) {
+        n = 60 + this.distanceWhite(60, n);
+      };
       if (d) {
-        if (note.notNil) {
-          note.set(\gate, 0);
-        };
-        midinote = n;
-        note = Synth(synthName, params.value.asPairs++[
+        var newFreq = this.midiKeyToFreq(n);
+        var newParams = params.value.asPairs++[
           \gate, 1,
-          \freq, (n+bend).midicps,
+          \freq, newFreq,
           \amp, amp * v,
-        ]++this.makePanParam(n), group);
+        ]++this.makePanParam(n);
+        midinote = n;
+        if (note.notNil) {
+          if (poly) {
+            note.set(*newParams);
+          } {
+            note.set(\gate, 0);
+          };
+        };
+        if (poly.not) {
+          note = Synth(synthName, newParams, group);
+        };
       } {
         if (n == midinote) {
           note.set(\gate, 0);
-          note = nil;
+          if (poly.not) {
+            note = nil;
+          };
         };
       };
     };
@@ -231,8 +255,6 @@ KeySynthMono : MidiSynth {
 
   free {
     note.free;
-    midihandler.key = nil;
-    midihandler.bend = nil;
     super.free;
   }
 }
index 410d5b4..57574a3 100644 (file)
@@ -16,8 +16,11 @@ s.options.numInputBusChannels = 2;
 s.options.numOutputBusChannels = 4;
 
 // ==== Midi setup
-MIDIClient.init;
-MIDIIn.connectAll;
+~midiup = {
+  MIDIClient.init;
+  MIDIIn.connectAll;
+};
+~midiup.value;
 
 // ==== Scales