# Stop BufWr to freeze signal

Hey,

I would like to freeze an input signal that I record with BufWr. I am not capable of stop recording the BufWr function. I guess since the phase is moving continously? How would I stop recording so the buffer doesnt get rewritten every two seconds? Thank you

``````//Sources
Ndef(\x, { SinOsc.ar(357) * 0.1 });
Ndef(\x, { SoundIn.ar(0) * 1 });

~buf = Buffer.alloc(s, 2 * s.sampleRate);

//phase
Ndef(\phase, { Phasor.ar(0, BufRateScale.kr(~buf), 0, BufFrames.kr(~buf)) });

//Record into buffer
(
Ndef(\record, {
var phase = Ndef.ar(\phase);
var in = Ndef.ar(\x);
BufWr.ar(in, ~buf, phase)
})
)

//Freeze
(
Ndef(\Grain, {|rate = 1|
var trate, dur, sig1, sig2;
var phase = Ndef.ar(\phase);
var buflength = BufFrames.kr(~buf);
phase = phase - 2000 % buflength;
sig1 = TGrains.ar(1, Impulse.ar(10), ~buf, rate, phase, 0.3, 0, 0.3, 4);
sig1;
}).play;
)``````

I think you might want to set Phasorâ€™s rate to 0. That would stop your phase from changing, and make BufRd stuck writing and re-writing the same sample.

``````Ndef(\phase, {|play=1|
Phasor.ar(0, BufRateScale.kr(~buf)*play, 0, BufFrames.kr(~buf))
});

Ndef(\phase).set(\play,0) // stop
Ndef(\phase).set(\play,1) // go
``````

If you donâ€™t want to overwrite even that single sample (which I wouldnâ€™t), you could just stop Ndef(\record).
Or you could introduce a parameter to write on the buffer the same content it already has:

``````(
Ndef(\record, {|overwrite=1|
var phase = Ndef.ar(\phase);
var in = Ndef.ar(\x);
var old_in  = BufRd.ar(1,~buf,phase);
BufWr.ar(writing, ~buf, phase)
})
)
// overwrite=1  : write new content (normal)
Ndef(\record).set(\overwrite,1)
// overwrite=-1 : re-write old content (no change)
Ndef(\record).set(\overwrite,-1)
// -1 < overwrite  < 1 : mix old and new content (overdub)
Ndef(\record).set(\overwrite,0)
``````
2 Likes

FWIW this is exactly the solution Iâ€™m using in some of my code: instead of thinking that the only possible solution is to â€śstop writing,â€ť another solution is to switch the input between new or old data. (Many programming problems are solved by shifting perspective, rather than looking for another feature.)

hjh

3 Likes