Add rate control and pad response to sampler
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Sun, 8 May 2022 08:23:25 +0000 (11:23 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Sun, 8 May 2022 08:23:25 +0000 (11:23 +0300)
push-control.scd
sampler.scd
test.scd

index 0b695cb..cfd718a 100644 (file)
       });
     }, 86, 0),
 
-    // Keys
+    // Pads
     MIDIFunc.noteOn({ |...args|
       var note, row, col;
       note = args[1] - 36;
       col = note % 8;
       row = (note/ 8).asInteger;
-      push.hcPushKeyOn(row, col, *push.ppca(*args));
+      push.padDown(row, col, *push.ppca(*args));
     },
     (36..99), 0),
 
       note = args[1] - 36;
       col = note % 8;
       row = (note/ 8).asInteger;
-      push.hcPushKeyOff(row, col, *push.ppca(*args));
+      push.padUp(row, col, *push.ppca(*args));
     }, (36..99), 0),
 
     MIDIFunc.polytouch({ |...args|
       note = args[1] - 36;
       col = note % 8;
       row = (note/ 8).asInteger;
-      push.hcPushKeyPressure(row, col, *push.ppca(*args));
+      push.padPressure(row, col, *push.ppca(*args));
     }, (36..99), 0),
   ];
 
   push.pushOut.latency_(0);
   if (push.pushOut==nil, {"Cannot control push LEDs!"});
 
-  push.hcPushPadLed = {|zi, row, col, light=3, color=0, whiten=false, mode=0|
+  push.padLight = {|zi, row, col, light=3, color=0, whiten=false, mode=0|
     if ((col < 0) || (col > 7) || (row < 0) || (row > 7), {}, {
       zi.pushOut.noteOn(mode, 36+(row*8)+col, zi.pppc(color, light, whiten));
     });
index d4cddfa..a6fe1da 100644 (file)
@@ -4,17 +4,20 @@
   var sampler = Environment[
     \version -> "33.7.2.3",
     \maxDur -> maxDur,
+    \synthName -> \samplerPlay,
     \buf -> Buffer.alloc(s, s.sampleRate * maxDur, 1),
     \recsynthdef -> SynthDef(\samplerRecord, { |input, buf|
       var sig;
       sig = In.ar(input, 1);
       RecordBuf.ar(sig, buf, doneAction: Done.freeSelf, loop: 0);
     }).add,
-    \playsynthdef -> SynthDef(\samplerPlay, { |out = 0, buf = 0, startPos = 0, duration = 0.01|
-      var sig, env;
-      sig = PlayBuf.ar(1, buf, BufRateScale.ir(buf), startPos: startPos*BufSampleRate.ir(buf), doneAction: 2);
-      env = Env.new([0, 1, 1, 0], [0.01, duration-0.02, 0.01]);
+    \playsynthdef -> SynthDef(\samplerPlay, { |out = 0, buf = 0, startPos = 0, duration = 0.01, freq=220, amp=1|
+      var sig, env, rate;
+      rate = freq / 220;
+      sig = PlayBuf.ar(1, buf, BufRateScale.ir(buf)*rate, startPos: startPos*BufSampleRate.ir(buf), doneAction: 2);
+      env = Env.new([0, 1, 1, 0], [0.01, duration/rate-0.02, 0.01]);
       sig = sig * EnvGen.ar(env, 1, doneAction: 2);
+      sig = sig * amp;
       Out.ar(out, sig!2);
     }).add,
     \startRec -> { |zi|
         buf: zi.buf,
         startPos: zi.startPos,
         duration: zi.duration,
-      )
+      );
+    },
+    \makeSynthArgs -> { |zi|
+      [
+        \buf, zi.buf,
+        \startPos, zi.startPos,
+        \duration, zi.duration,
+      ];
     },
     \shiftPos -> { |zi, delta|
       var endPos = zi.startPos + zi.duration;
index 2f6a895..d4a472b 100644 (file)
--- a/test.scd
+++ b/test.scd
@@ -3,7 +3,10 @@
 ~se = ~samplerFactory.value;
 )
 
+(\freq: 220).play;
+
 ~se.makeSynthEvent
+~se.makeSynthArgs
 
 (
 ~push.recDown = { |zi|
   ~se.startPos.postln;
   ~se.duration.postln;
 };
+~push.padDown = { |zi, row, col, vel|
+  var note, amp;
+  ~push.padLight(row, col, 2, 1, false, 1);
+  note = Scale.major.degreeToFreq(col, 0.midicps, row+1);
+  amp = vel.linlin(1, 127, 0.01, 0.4);
+  Synth(~se.synthName, ~se.makeSynthArgs++[\freq, note]);
+};
+~push.padUp = { |zi, row, col, vel|
+  ~push.padLight(row, col, 0, 0, false, 1);
+};
 )
 
 //=============================================================================