Skip to content

Instantly share code, notes, and snippets.

@caseyanderson
Last active February 3, 2023 21:48
Show Gist options
  • Save caseyanderson/1b73adf05f1f21b8e624b09c4e531c9f to your computer and use it in GitHub Desktop.
Save caseyanderson/1b73adf05f1f21b8e624b09c4e531c9f to your computer and use it in GitHub Desktop.
keyDownAction triggers a Phasor until a keyUpAction, can be used to store and share the duration of a recording into a buffer. Note: repeat triggers from keyDownAction are blocked
s.options.inDevice_("MacBook Pro Microphone");
s.options.outDevice_("MacBook Pro Speakers");
s.boot;
~buf = Buffer.alloc( s, s.sampleRate * 60, 1);
~dur = Bus.control(s, 1);
(
SynthDef(\record, {|amp = 0.9, buf, rate = 1, recManualBus, off = 0, trig = 0|
var env, sig, phase;
sig = SoundIn.ar(0, amp);
phase = Phasor.ar(trig, BufRateScale.kr(buf) * rate, 0, (BufFrames.kr(buf)));
BufWr.ar(sig, buf, phase);
Out.kr( recManualBus, phase );
FreeSelf.kr( off );
}).add;
SynthDef(\playRecording, {|amp = 0.9, buf, bufDur, trig = 0|
var env, play, sig;
env = EnvGen.kr(Env.asr(0.01, amp, 0.01), trig, doneAction: 2);
// play = PlayBuf.ar(1, buf, 1.0, trig);
play = LoopBuf.ar(1, buf, 1.0, trig, 0, 0, bufDur, 2);
sig = env * play;
Out.ar(0, Pan2.ar(sig));
}).add;
)
(
var keyDown = False;
w = Window.new("keyTrigRec", Rect(50, 50, 275, 275));
w.background_(Color.white);
w.view.keyDownAction = { arg view, char, modifiers, unicode, keycode;
if(keycode == 49, {
if( keyDown == False, {
"start recording".postln;
w.background_(Color.red);
// start the record synth
x = Synth(\record, [\trig, 1, \buf, ~buf, \recManualBus, ~dur.index ]);
// block repeated triggers from space bar
keyDown = True;
});
});
};
w.view.keyUpAction = { arg view, char, modifiers, unicode, keycode;
if(keycode == 49, {
"stop recording".postln;
w.background_(Color.green);
fork {
x.set(\off, 1); // stop recording
// check position of phasor
~dur.get({ |recDur|
~numFrames = recDur;
("numFrames recorded:" + ~numFrames).postln;
});
s.sync;
~dur.set(0.0); // reset bus for next recording
};
keyDown = False;
});
};
w.front;
)
// play and loop recording
y = Synth(\playRecording,[\buf, ~buf, \trig, 0, \bufDur, ~numFrames, \trig, 1 ]);
//
y.set(\trig, 0);
//
(
w.close;
w.free;
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment