Rewrite Sampler as class
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Tue, 17 May 2022 08:30:42 +0000 (11:30 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Tue, 17 May 2022 08:30:42 +0000 (11:30 +0300)
extensions/classes/sampler.sc [new file with mode: 0644]
startup.scd
Хэзифу/sampler.scd [deleted file]
Хэзифу/test.scd

diff --git a/extensions/classes/sampler.sc b/extensions/classes/sampler.sc
new file mode 100644 (file)
index 0000000..c7c2aa4
--- /dev/null
@@ -0,0 +1,111 @@
+Sampler {
+  const <version = "33.7.4.2";
+  var <name, <server, <maxDur, <>freq, <>tunerSynthdefName, <synthdefName, <recsynthdefName;
+  var <buf, <recsynthdef, <playsynthdef, <startPos, <duration, <window;
+  var plotter, recSynth, playsTuner, tunerSynth, startTime, playSynth;
+
+  *new { | name=\default, server=nil, maxDur=10, freq=220, tunerSynthdefName=\default, synthdefName=\samplerPlay, recsynthdefName=\samplerRecord |
+    server = server ? Server.default;
+    ^super.newCopyArgs(name, server, maxDur, freq, tunerSynthdefName, synthdefName, recsynthdefName).prInit;
+  }
+
+  prInit {
+    version.postln;
+    name.postln;
+    server.postln;
+    maxDur.postln;
+    freq.postln;
+
+    startPos = 0;
+    duration = 0;
+    buf = Buffer.alloc(server, server.sampleRate * maxDur, 1);
+    recsynthdef = SynthDef(recsynthdefName, {| input, buf |
+      var sig;
+      sig = In.ar(input, 1);
+      RecordBuf.ar(sig, buf, doneAction: Done.freeSelf, loop: 0);
+    }).add;
+    playsynthdef = SynthDef(synthdefName, {| out=0, buf=0, startPos=0, duration=0.01, freq=220, amp=1, gate=1, pan=0|
+      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]);
+      // 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);
+      sig = Pan2.ar(sig, pan);
+      Out.ar(out, sig);
+    }).add;
+    window = Window.new(
+      "Sampler",
+      Rect(Window.screenBounds.width-600,Window.screenBounds.height-180,500,100);
+    );
+    plotter = Plotter.new(name, Rect(0, 0, window.bounds.width, window.bounds.height), window);
+
+    window.front;
+    ("Сэмплер «"++name++"» готов к использованию 😊").postln;
+  }
+
+  startRec { | playTuner=false |
+    startPos = 0.0;
+    recSynth = Synth(\samplerRecord, [
+      \input, server.inputBus,
+      \buf, buf
+    ], addAction: \addToTail);
+    startTime = thisThread.seconds;
+    playsTuner = playTuner;
+    if (playsTuner, {
+      tunerSynth = Synth(\default, [freq: freq]);
+    });
+  }
+
+  stopRec {
+    duration = min(thisThread.seconds - startTime, 10);
+    recSynth.free;
+    if (playsTuner, {
+      tunerSynth.set(\gate, 0);
+      playsTuner = false;
+    });
+    buf.loadToFloatArray(startPos*buf.sampleRate, duration*buf.sampleRate, action: { |array|
+      { plotter.value = array; }.defer;
+    });
+  }
+
+  playSample { |out=0|
+    playSynth = Synth(synthdefName,
+      [\out, out, \buf, buf, \startPos, startPos, \duration, duration],
+      addAction: \addToHead);
+  }
+
+  stopPlay {
+    playSynth.set(\gate, 0);
+  }
+
+  makeSynthBind {
+    ^Pbind(
+      \instrument, synthdefName,
+      \buf, buf,
+      \startPos, startPos,
+      \duration, duration,
+    );
+  }
+
+  makeSynthArgs {
+    ^[
+      \buf, buf,
+      \startPos, startPos,
+      \duration, duration,
+    ];
+  }
+
+  shiftPos { |delta=0.1|
+    var endPos = startPos + duration;
+    startPos = (startPos + delta).clip(0, endPos);
+    duration = endPos - startPos;
+    buf.loadToFloatArray(startPos*buf.sampleRate, duration*buf.sampleRate, action: { |array|
+      if (array.size == 0, {array = [0]});
+      { plotter.value = array; }.defer;
+    });
+  }
+
+}
\ No newline at end of file
index 829242f..8379526 100644 (file)
@@ -8,7 +8,7 @@ s.options.numAudioBusChannels = 32 * 1024;
 s.options.numControlBusChannels = 256 * 1024;
 s.options.numInputBusChannels = 16;
 s.options.numOutputBusChannels = 16;
-s.boot;
+
 MIDIClient.init;
 MIDIIn.connectAll;
 )
diff --git a/Хэзифу/sampler.scd b/Хэзифу/sampler.scd
deleted file mode 100644 (file)
index f185575..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-(
-~samplerFactory = {
-  var maxDur = 10.0;
-  var sampler = Environment[
-    \version -> "33.7.4.1",
-    \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, 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]);
-      // 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|
-      zi.startPos = 0.0;
-      zi.recSynth = Synth(\samplerRecord, [\input, s.inputBus, \buf, zi.buf], addAction: \addToTail);
-      zi.endPos = s.sampleRate * 10;
-      zi.startTime = thisThread.seconds;
-      zi.pleiPich = pleiPich;
-      if (pleiPich, {
-        zi.pichSint = Synth(\default, [freq: 220]);
-      });
-    },
-    \startPos -> 0,
-    \duration -> 0,
-    \stopRec -> { |zi|
-      zi.duration = min(thisThread.seconds - zi.startTime, 10);
-      zi.recSynth.free;
-      if (zi.pleiPich, {
-        zi.pichSint.set(\gate, 0);
-      });
-      zi.buf.loadToFloatArray(zi.startPos*zi.buf.sampleRate, zi.duration*zi.buf.sampleRate, action: { |array|
-        { zi.plotter.value = array; }.defer;
-      });
-    },
-    \playSample -> { |zi|
-      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|
-      Pbind(
-        \instrument, \samplerPlay,
-        \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;
-      zi.startPos = (zi.startPos + delta).clip(0, endPos);
-      zi.duration = endPos - zi.startPos;
-      zi.buf.loadToFloatArray(zi.startPos*zi.buf.sampleRate, zi.duration*zi.buf.sampleRate, action: { |array|
-        if (array.size == 0, {array = [0]});
-        { zi.plotter.value = array; }.defer;
-      });
-    },
-    \window -> Window.new(
-      "Sampler",
-      Rect(Window.screenBounds.width-600,Window.screenBounds.height-180,500,100)
-    )
-  ];
-  sampler.know = true;
-
-  sampler.plotter = Plotter.new("sampler", Rect(0, 0, sampler.window.bounds.width, sampler.window.bounds.height), sampler.window);
-
-  sampler.window.front;
-
-  "Сэмплер готов к использованию 😊".postln;
-
-  sampler;
-};
-)
index e4e0a78..b3a16c4 100644 (file)
@@ -1,9 +1,9 @@
 (
 ~push = ~pushFactory.value;
-~se = ~samplerFactory.value;
+~se = Sampler.new(\qaxi);
 ~pe = ~pedalFactory.value;
 )
-
+Pxrand
 (
 ~se.buf.loadToFloatArray(action: { |array|
   {
   ~se.startRec(true);
 };
 ~pe.suUp = { |zi|
-  ~se.stopRec();
+  ~se.stopRec;
 };
 ~push.playDown = { |zi|
-  ~se.playSample();
+  ~se.playSample;
 };
 ~push.playUp = { |zi|
-  ~se.stopPlay();
+  ~se.stopPlay;
 };
 ~push.hcPushTurnL = { |zi, delta|
   var mul = 0.01;
@@ -47,7 +47,7 @@
   var freq;
   ~push.padLight(row, col, 2, 1, false, 0);
   freq = Scale.major.degreeToFreq(col, 0.midicps, row+1);
-  ~notes[freq.cpsmidi.asInteger] = Synth(~se.synthName, ~se.makeSynthArgs++[\freq, freq, \amp, vel]);
+  ~notes[freq.cpsmidi.asInteger] = Synth(~se.synthdefName, ~se.makeSynthArgs++[\freq, freq, \amp, vel]);
   ~amps[freq.cpsmidi.asInteger] = vel;
   ("down "++vel).postln;
 };