Sampler — add second env and stop on playUp / use pressure to control amp and soften out
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Thu, 12 May 2022 20:59:15 +0000 (23:59 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Thu, 12 May 2022 20:59:15 +0000 (23:59 +0300)
sampler.scd
test.scd

index c341a08..87dbfe4 100644 (file)
       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, freq=220, amp=1|
-      var sig, env, rate;
+    \playsynthdef -> SynthDef(\samplerPlay, { |out = 0, buf = 0, startPos = 0, duration = 0.01, freq=220, amp=1, gate=1|
+      var sig, env, rate, susEnv;
       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;
+      // next env is to to nicely fade out synth on gate close
+      susEnv = Env.asr(0, 1, 0.01);
+      sig = sig * EnvGen.ar(env, doneAction: 2) * EnvGen.ar(susEnv, gate, doneAction: 2);
+      sig = sig * amp.lag(0.03);
       Out.ar(out, sig!2);
     }).add,
     \startRec -> { |zi, pleiPich=false|
       });
     },
     \playSample -> { |zi|
-      var playSynth;
       "playSample called".postln;
-      playSynth = Synth(\samplerPlay,
+      zi.playSynth = Synth(\samplerPlay,
         [\out, 0, \buf, zi.buf, \startPos, zi.startPos, \duration, zi.duration],
         addAction: \addToHead);
     },
+    \stopPlay -> { |zi|
+      zi.playSynth.postln;
+      zi.playSynth.set(\gate, 0);
+    },
     \makeSynthBind -> { |zi|
       "makeSynthBind called".postln;
       Pbind(
index 3a7353c..c4fc30b 100644 (file)
--- a/test.scd
+++ b/test.scd
@@ -9,6 +9,8 @@
 ~se.makeSynthEvent
 ~se.makeSynthArgs
 (
+~notes = Array.newClear(128);
+~amps = Array.newClear(128);
 ~pe.suDown = { |zi|
   ~se.startRec(true);
 };
@@ -18,6 +20,9 @@
 ~push.playDown = { |zi|
   ~se.playSample();
 };
+~push.playUp = { |zi|
+  ~se.stopPlay();
+};
 ~push.hcPushTurnL = { |zi, delta|
   var mul = 0.01;
   ~se.shiftPos(delta * mul);
   ~se.duration.postln;
 };
 ~push.padDown = { |zi, row, col, vel|
-  var note, amp;
+  var freq;
   ~push.padLight(row, col, 2, 1, false, 0);
-  note = Scale.major.degreeToFreq(col, 0.midicps, row+1);
-  Synth(~se.synthName, ~se.makeSynthArgs++[\freq, note, \amp, vel]);
+  freq = Scale.major.degreeToFreq(col, 0.midicps, row+1);
+  ~notes[freq.cpsmidi.asInteger] = Synth(~se.synthName, ~se.makeSynthArgs++[\freq, freq, \amp, vel]);
+  ~amps[freq.cpsmidi.asInteger] = vel;
+  ("down "++vel).postln;
 };
 ~push.padUp = { |zi, row, col, vel|
+  var freq;
   ~push.padLight(row, col, 0, 0, false, 1);
+  freq = Scale.major.degreeToFreq(col, 0.midicps, row+1);
+  ~notes[freq.cpsmidi.asInteger].set(\gate, 0);
+  ~amps[freq.cpsmidi.asInteger] = 0;
+};
+~push.padPressure = { |zi, row, col, vel|
+  var freq;
+  //~push.padLight(row, col, 0, 0, false, 1);
+  freq = Scale.major.degreeToFreq(col, 0.midicps, row+1);
+  if (vel > ~amps[freq.cpsmidi.asInteger], {
+    ~notes[freq.cpsmidi.asInteger].get(\amp, { |amp|
+      var maxRatio = 1.5;
+      ("initial "++vel).postln;
+      if ((vel/amp) > maxRatio, { vel = amp * maxRatio; });
+      ~notes[freq.cpsmidi.asInteger].set(\amp, vel);
+      ("new "++vel).postln;
+    });
+  });
+  ("pres "++vel).postln;
 };
 )