From: Eugene Petkevich Date: Thu, 12 May 2022 20:59:15 +0000 (+0300) Subject: Sampler — add second env and stop on playUp / use pressure to control amp and soften out X-Git-Url: https://git.zuelum.org/?a=commitdiff_plain;h=7599a5da7bd5031e814789053ac3bf35cc35ba00;p=scprojects.git Sampler — add second env and stop on playUp / use pressure to control amp and soften out --- diff --git a/sampler.scd b/sampler.scd index c341a08..87dbfe4 100644 --- a/sampler.scd +++ b/sampler.scd @@ -14,13 +14,15 @@ 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| @@ -45,12 +47,15 @@ }); }, \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( diff --git a/test.scd b/test.scd index 3a7353c..c4fc30b 100644 --- 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); @@ -25,13 +30,34 @@ ~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; }; )