From 445aa21ce50c868a2477941cc4e9b686b4adee82 Mon Sep 17 00:00:00 2001 From: Eugene Petkevich Date: Tue, 17 May 2022 11:30:42 +0300 Subject: [PATCH] Rewrite Sampler as class --- extensions/classes/sampler.sc | 111 ++++++++++++++++++ startup.scd | 2 +- .../sampler.scd" | 95 --------------- .../test.scd" | 12 +- 4 files changed, 118 insertions(+), 102 deletions(-) create mode 100644 extensions/classes/sampler.sc delete mode 100644 "\320\245\321\215\320\267\320\270\321\204\321\203/sampler.scd" diff --git a/extensions/classes/sampler.sc b/extensions/classes/sampler.sc new file mode 100644 index 0000000..c7c2aa4 --- /dev/null +++ b/extensions/classes/sampler.sc @@ -0,0 +1,111 @@ +Sampler { + const freq, <>tunerSynthdefName, "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; -}; -) diff --git "a/\320\245\321\215\320\267\320\270\321\204\321\203/test.scd" "b/\320\245\321\215\320\267\320\270\321\204\321\203/test.scd" index e4e0a78..b3a16c4 100644 --- "a/\320\245\321\215\320\267\320\270\321\204\321\203/test.scd" +++ "b/\320\245\321\215\320\267\320\270\321\204\321\203/test.scd" @@ -1,9 +1,9 @@ ( ~push = ~pushFactory.value; -~se = ~samplerFactory.value; +~se = Sampler.new(\qaxi); ~pe = ~pedalFactory.value; ) - +Pxrand ( ~se.buf.loadToFloatArray(action: { |array| { @@ -29,13 +29,13 @@ ~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; }; -- 2.17.1