Add white key only relative mono synth
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Mon, 20 Jun 2022 14:17:40 +0000 (17:17 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Mon, 20 Jun 2022 14:18:00 +0000 (17:18 +0300)
extensions/classes/midisynths.sc
Хэзифу/test.scd

index e291b3d..4550cb6 100644 (file)
@@ -23,6 +23,17 @@ MidiSynth {
     };
   }
 
+  distanceWhite {|start, end|
+    var res = 0;
+    res = ((end-start) / 12).asInteger * 7;
+    if (end >= start) {
+      res = res + ((end-start) % 12).collect{|i| i}.count{|i| Scale.major.degrees.includesEqual((start+i+1) % 12)};
+    } {
+      res = res - ((start-end) % 12).collect{|i| i}.count{|i| Scale.major.degrees.includesEqual((start-i-1) % 12)};
+    };
+    ^res;
+  }
+
 }
 
 //=============================================================================
@@ -42,7 +53,7 @@ KeySynth : MidiSynth {
       if (d) {
         notes[n] = Synth(synthName, params.value.asPairs++[
           \gate, 1,
-          \freq, n.midicps,
+          \freq, (n+bend).midicps,
           \amp, amp * v,
         ]++this.makePanParam(n), group);
       } {
@@ -108,7 +119,7 @@ PadSynth : KeySynth {
         midihandler.ledPad(row, col, 2, 1, 1);
         notes[midinote] = Synth(synthName, params.value.asPairs++[
           \gate, 1,
-          \freq, freq,
+          \freq, freq * bend.midiratio,
           \amp, amp * v,
         ]++this.makePanParam(midinote), group);
         amps[midinote] = amp * v;
@@ -181,7 +192,7 @@ KeySynthMono : MidiSynth {
         midinote = n;
         note = Synth(synthName, params.value.asPairs++[
           \gate, 1,
-          \freq, n.midicps,
+          \freq, (n+bend).midicps,
           \amp, amp * v,
         ]++this.makePanParam(n), group);
       } {
@@ -212,41 +223,51 @@ KeySynthMono : MidiSynth {
 
 // connect synth to midi-keyboard, play only one note at a time, relative keys
 KeySynthRelative : KeySynthMono {
-  var <>centerKey, <>intervals, keyNote;
+  var <>centerKey, <>intervals, <>onlyWhite, keyNote;
 
-  *new {|midihandler, synthName=\default, bendRadius=1, panAcross=false, params=nil, amp=1.0, group=nil, centerKey=60, intervals=#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]|
+  *new {|midihandler, synthName=\default, bendRadius=1, panAcross=false, params=nil, amp=1.0, group=nil, centerKey=60, intervals=#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], onlyWhite=true|
     ^super.newCopyArgs(
       midihandler, synthName, bendRadius, panAcross, params, amp, group
-    ).midiInit.keyInit.relInit(centerKey, intervals);
+    ).midiInit.keyInit.relInit(centerKey, intervals, onlyWhite);
   }
 
-  relInit {|c, i|
+  relInit {|c, i, ow|
     centerKey = c;
     intervals = i;
+    onlyWhite = ow;
     keyNote = -1;
     midinote = 60;
     midihandler.key_{|d, v, n|
       if (d) {
-        if (note.notNil) {
-          note.set(\gate, 0);
-        };
+        var distance;
         keyNote = n;
-        midinote = case
-        { (n-centerKey) == 0 } {
-          midinote;
-        }
-        { (n-centerKey) > 0 } {
-          midinote + intervals[n-centerKey-1];
-        }
-        { (n-centerKey) < 0 } {
-          midinote - intervals[centerKey-n-1];
+        if (onlyWhite) {
+          distance = this.distanceWhite(centerKey, n);
+        } {
+          distance = n - centerKey;
+        };
+        [distance, intervals.size].postln;
+        if (distance.abs <= intervals.size) {
+          midinote = case
+          { distance == 0 } {
+            midinote;
+          }
+          { distance > 0 } {
+            midinote + intervals[distance-1];
+          }
+          { distance < 0 } {
+            midinote - intervals[-1-distance];
+          };
+          if (note.notNil) {
+            note.set(\gate, 0);
+          };
+          midinote.postln;
+          note = Synth(synthName, params.value.asPairs++[
+            \gate, 1,
+            \freq, (midinote+bend).midicps,
+            \amp, amp * v,
+          ]++this.makePanParam(midinote), group);
         };
-        midinote.postln;
-        note = Synth(synthName, params.value.asPairs++[
-          \gate, 1,
-          \freq, midinote.midicps,
-          \amp, amp * v,
-        ]++this.makePanParam(midinote), group);
       } {
         if (n == keyNote) {
           note.set(\gate, 0);
index 9b3b78a..3e93037 100644 (file)
@@ -138,4 +138,5 @@ s.waitForBoot{
 ~kedi = MidiRemote25SL();
 
 ~monokeys = KeySynthRelative(~kedi);
-~monokeys.intervals = #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; // all white
+~monokeys.synthName = \dyti;
+~monokeys.intervals = #[5];