Add simple sampler functionality
authorEugene Petkevich <nasedil.genio.code@gmail.com>
Tue, 3 May 2022 22:44:11 +0000 (01:44 +0300)
committerEugene Petkevich <nasedil.genio.code@gmail.com>
Tue, 3 May 2022 22:44:11 +0000 (01:44 +0300)
sampler.scd

index cf7d547..43deabc 100644 (file)
@@ -5,9 +5,76 @@ s.boot;
 (
 ~se = Environment[
   \version -> "33.7.2.2",
-  \buf -> nil,
+  \buf -> Buffer.alloc(s, s.sampleRate * 10.0, 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, doneAction: 2);
+    env = Env.new([0, 1, 1, 0], [0.01, duration-0.02, 0.01]);
+    sig = sig * EnvGen.ar(env, 1, doneAction: 2);
+    Out.ar(out, sig!2);
+  }).add,
+  \startRec -> { |zi|
+    zi.recSynth = Synth(\samplerRecord, [\input, s.inputBus, \buf, zi.buf], addAction: \addToTail);
+    zi.endPos = s.sampleRate * 10;
+    zi.startTime = thisThread.seconds;
+  },
+  \startPos -> 0,
+  \duration -> 0,
+  \stopRec -> { |zi|
+    zi.duration = thisThread.seconds - zi.startTime;
+    zi.recSynth.free;
+  },
+  \playSample -> { |zi|
+    var playSynth;
+    playSynth = Synth(\samplerPlay,
+      [\out, 0, \buf, zi.buf, \startPos, zi.startPos, \duration, zi.duration],
+      addAction: \addToHead);
+  }
 ];
 
 ~se.know = true;
 )
 
+~se.startRec();
+~se.stopRec();
+
+~se.buf.plot;
+~se.buf.getToFloatArray(wait:0.01, action: {arg array; a = array; { a[0..100].plot }.defer; "done".postln });
+
+~se.playSample();
+~se.duration = 2.5;
+
+(
+SynthDef(\test, { |amp = 1.0|
+  Out.ar(0, SoundIn.ar(1!2, amp.lag(0.01)));
+}).add;
+)
+
+a = Synth(\test);
+a.free;
+
+MIDIIn.connectAll;
+(
+MIDIdef.cc(\pollcc, { |...args|
+  //"cc".postln;
+  //args.postln;
+});
+
+MIDIdef.noteOn(\pollnoteon, { |...args|
+  "noteon".postln;
+  args.postln;
+});
+)
+
+(
+MIDIdef.cc(\tafe, { |...args|
+  var amp = args[0].linlin(0, 127, 0.1, 20);
+  a.set(\amp, amp);
+  amp.postln;
+}, 1);
+)
\ No newline at end of file