Skip to content

Instantly share code, notes, and snippets.

@dyfer
Last active June 8, 2019 00:32
Show Gist options
  • Save dyfer/7a670021d6fee8b45e055e853356db9a to your computer and use it in GitHub Desktop.
Save dyfer/7a670021d6fee8b45e055e853356db9a to your computer and use it in GitHub Desktop.
Tests for various convolution UGens in SuperCollider
// testing convolution behavior
// s.options.hardwareBufferSize = 2048; //optionaly test with hardware buffer size equeling kernel size...
// s.reboot;
( // allocate three buffers
~kernelSize = 2048;
~kernel = Buffer.alloc(s,~kernelSize);
~inOut = Buffer.alloc(s, ~kernelSize * 2, 2); //
~kernel.zero;
~inOut.zero;
)
~kernel.set(0, 1); //single impulse in the kernel
~kernel.plot;
//Convolution2
(
SynthDef("conv2Rec", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
conv = Convolution2.ar(input, kernel, trig, ~kernelSize, 1);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
Out.ar(out, [input, conv]);
}).add
)
x = Synth.new("conv2Rec",[\kernel, ~kernel, \buffer, ~inOut]);
~inOut.plot; //Convolution2 introduces delay
//Convolution3
(
SynthDef("conv3Rec", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
conv = Convolution3.ar(input, kernel, trig, ~kernelSize, 1);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
Out.ar(out, [input, conv]);
}).add
)
x = Synth.new("conv3Rec",[\kernel, ~kernel, \buffer, ~inOut, \trig, 1]);
~inOut.plot; //Convolution3 results in kernel being repeated (?)
~inOut.getToFloatArray(action: {|arr| a = arr});
a[..4].plot
a[0]
a[1]
//Convolution - less efficient
(
SynthDef("convRec", { |out, kernel, trig = 0|
var input, conv, kernelSig;
input = Impulse.ar(1);
kernelSig = PlayBuf.ar(1, kernel, BufRateScale.kr(kernel), 1, 0, 0);
// conv = Convolution.ar(input, kernelSig, ~kernelSize, 1);
conv = Convolution.ar(input, kernelSig, s.options.blockSize, 1);
// RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0);
Out.ar(out, [input, conv]);
}).add
)
//Convolution - less efficient
(
SynthDef("convRec", { |out, kernel, trig = 0|
var input, conv, kernelSig;
input = SinOsc.ar(200, 0, -12.dbamp);
kernelSig = PlayBuf.ar(1, kernel, BufRateScale.kr(kernel), 1, 0, 1);
// conv = Convolution.ar(input, kernelSig, ~kernelSize, 1);
conv = Convolution.ar(input, kernelSig, s.options.blockSize, 1);
// RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0);
Out.ar(out, [input, conv]);
}).add
)
x.free; x = Synth.new("convRec",[\kernel, ~kernel, \buffer, ~inOut, \trig, 1]);
~inOut.plot; //also has a delay
//partconv
(
//setup
~irbufsize = PartConv.calcBufSize(~kernelSize, ~kernel);
~irKernel = Buffer.alloc(s, ~irbufsize, 1);
~irKernel.preparePartConv(~kernel, ~kernelSize);
)
// ~irKernel.plot;
~kernel.plot;
(
SynthDef("partConvRec", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
conv = PartConv.ar(input, ~kernelSize, kernel, 1);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
Out.ar(out, [input, conv]);
}).add
)
x = Synth.new("partConvRec",[\kernel, ~irKernel, \buffer, ~inOut, \trig, 1]);
~inOut.plot; //delay
// also modified Don's convolution
(
SynthDef("partConv2Rec", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
conv = PartConv2.ar(input, ~kernelSize, kernel, 1);
RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
Out.ar(out, [input, conv]);
}).add
)
x = Synth.new("partConv2Rec",[\kernel, ~irKernel, \buffer, ~inOut, \trig, 1]);
~inOut.plot; //also delay
//sl kernel ispection
~slKernel = Buffer.read(s, "/Volumes/data/Dokumenty/2012-2013/dxSoundLab/kernels/SoundLabKernels113/96000/moderate/k_24_1.365w_18c_5b_0.0t/FIR_01.wav")
~slKernel.numFrames; //32768
//test multiple sl kernels using convolution 3
//Convolution3
(
SynthDef("conv3Rec", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
conv = Convolution3.ar(input, kernel, trig, 2048, 1);
// conv = Convolution3.ar(input, kernel, trig, ~slKernel.numFrames, 1);
// RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
Out.ar(out, [input, conv]);
}).add
)
x = Synth.new("conv3Rec",[\kernel, ~slKernel, \buffer, ~inOut, \trig, 1]); //400% CPU for 32768 kernel size
x.free;
s.options.hardwareBufferSize = 128; //optionaly test with hardware buffer size equeling kernel size...
s.options.memSize = 2**16;
s.options.sampleRate = 48000;
s.options.sampleRate = 44100;
s.options.numOutputBusChannels = 16;
s.options.numInputBusChannels = 16;
s.reboot;
// Server.killAll
//partconv SL kernel
//sl kernel ispection
~slKernel.free; ~slKernel = Buffer.read(s, "/Volumes/data/Dokumenty/2012-2013/dxSoundLab/kernels/SoundLabKernels117/44100/moderate_with_desk-realtime/m_24_1.365w_18c_5b_0.7t/FIR_03.wav"); //realtime
~slKernel.free; ~slKernel = Buffer.read(s, "/Volumes/data/Dokumenty/2012-2013/dxSoundLab/kernels/SoundLabKernels113/96000/moderate/k_24_1.365w_18c_5b_0.0t/FIR_01.wav")
~slKernel.numFrames; //32768
// ~slKernel.play
(
//setup
~kernel = ~slKernel;
~kernelSize = 256;
~irbufsize = PartConv.calcBufSize(~kernelSize, ~kernel);
~numpartitions= PartConv.calcNumPartitions(~kernelSize, ~kernel);
~numpartitions.postln;
~irKernel.free; ~irKernel = Buffer.alloc(s, ~irbufsize, 1);
~irKernel.preparePartConv(~kernel, ~kernelSize);
)
~irKernel.plot;
~kernel.plot;
(
SynthDef("partConvTest", { |out, kernel, trig = 0|
var input, conv;
input = Impulse.ar(1);
// input = SinOsc.ar(300, 0, -12.dbamp);
conv = PartConv.ar(input, ~kernelSize, kernel, 1);
// RecordBuf.ar([input, conv], \buffer.kr(0), loop: 0, doneAction: Done.freeSelf);
// Out.ar(out, [input, conv]* \amp.kr(1));
Out.ar(out, [conv] * \amp.kr(1));
//monitoring
Out.ar(\monOut.kr(0), [input * \srcAmp.kr(-12.dbamp), SoundIn.ar(\in.kr(0))]);
}).add
)
x.free; x = Synth.new("partConvTest",[\kernel, ~irKernel.bufnum, \buffer, ~inOut, \trig, 1]);
x.free;
x.free;
~num = 32;
~num
x.do(_.free); x = ~num.collect({Synth.new("partConvTest",[\kernel, ~irKernel, \buffer, ~inOut, \trig, 1, \amp, -24.dbamp])});
x.do(_.free); x = ~num.collect({Synth.new("partConvTest",[\kernel, ~irKernel, \buffer, ~inOut, \trig, 1, \amp, -36.dbamp/~num, \srcAmp, 1/~num, \out, 4, \in, 2, \monOut, 0])});
x.do(_.free);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment