«Convolution reverb and impulse responses» by zecraum
on 12 Jun'23 02:42 inBoilerplate code for creating and testing convolution reverb and impulse responses
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
// Posted by Paul Miller // https://listarc.cal.bham.ac.uk/lists/sc-users-2016/msg52187.html // code for building & testing IRs // setup ( s.waitForBoot({ { // init // free old buffers if (a.notNil, { if (a.buffer.notNil, { a.buffer.free; }); if (a.target.notNil, { a.target.free; }); if (a.irspectrum.notNil, { a.irspectrum.free; }); }); a = (); // settings dict //fft a.fftsize = 2048; // set target buffer for testing convolution with a.target= Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav"); // fill buffer using one of the following Examples: /* // ======= EX1 - create IR using dust & noise ====== */ // CHANGE THESE SETTINGS: a.bufferTime = 3; // time in secs for new IR a.densityMin = 50; // min/max density of reflections a.densityMax = 500; a.filterFreqMin = 100; // min/max freq for filter sweep a.filterFreqMax = 8000; if (a.buffer.notNil, { a.buffer.free; }); a.buffer = Buffer.alloc(s, s.sampleRate * a.bufferTime, 1); s.sync; // write into the buffer with a BufWr y = { var sourceVolEnv, filterFreqEnv, densityEnv, source, filterFunc, outSound; // CHANGE ENVELOPES, SOURCE & FILTER SETTINGS //sourceVolEnv = EnvGen.kr(Env([1, 0], [a.bufferTime], -3), doneAction: 2); //sourceVolEnv = EnvGen.kr(Env([0, 1], [a.bufferTime], 3), doneAction: 2); // reverse env sourceVolEnv = EnvGen.kr(Env([0.2, 1], [a.bufferTime], 6), doneAction: 2); // reverse env // filterFreqEnv = EnvGen.kr(Env([1, 0], [a.bufferTime], -2), // levelScale: (a.filterFreqMax - a.filterFreqMin), levelBias: a.filterFreqMin, doneAction: 0); filterFreqEnv = EnvGen.kr(Env([0, 1], [a.bufferTime], 6), levelScale: (a.filterFreqMax - a.filterFreqMin), levelBias: a.filterFreqMin, doneAction: 0); // reverse env // densityEnv = EnvGen.kr(Env([0, 1], [a.bufferTime], 2), // levelScale: (a.densityMax - a.densityMin), levelBias: a.densityMin, doneAction: 0); densityEnv = EnvGen.kr(Env([1, 0], [a.bufferTime], -2), levelScale: (a.densityMax - a.densityMin), levelBias: a.densityMin, doneAction: 0); // reverse env source = Dust.ar(densityEnv, 0.5) * WhiteNoise.ar; // source = Dust.ar(densityEnv, 0.5) * PinkNoise.ar; // source = Dust.ar(densityEnv, 0.5) * GrayNoise.ar; // filterFunc = {arg in, freq; LPF.ar(in, freq) }; // filterFunc = {arg in, freq; HPF.ar(in, freq) }; filterFunc = {arg in, freq; var allPass; //LPF.ar(in + AllpassC.ar(in, 0.1, 0.01, 0.5, mul: 0.75), freq); //LPF.ar(in + AllpassC.ar(in, 0.1, 0.1, 0.5, mul: 0.75), freq); //LPF.ar(in + AllpassC.ar(in, 0.01, XLine.kr(0.0001, 0.01, a.bufferTime), 0.2), freq); LPF.ar(in + AllpassC.ar(in * 0.5, 0.2, XLine.kr(0.001, 0.2, a.bufferTime), 0.5, mul: 0.5), freq); }; outSound = filterFunc.value(sourceVolEnv * source, filterFreqEnv) * 100 / densityEnv; // adjust volume with density // record buffer BufWr.ar(outSound, a.buffer, Phasor.ar(0, BufRateScale.kr(a.buffer), 0, BufFrames.kr(a.buffer)), loop: 0); outSound ! 2; }.play; // ======= /* // ======= EX2 - load file from disk - MONO ONLY ====== if (a.buffer.notNil, { a.buffer.free; }); a.buffer = Buffer.read(s, "/path/to/monoSoundFile.wav"); // e.g. //a.buffer = Buffer.read(s, "/Users/paul/TESTING/TestSamples/_impulses/SavedImpulse_20160831222540.aif"); // ======= */ /* // ======= EX3 - load file from disk & reverse it - MONO ONLY ====== if (a.buffer.notNil, { a.buffer.free; }); // a.buffer = Buffer.read(s, "/path/to/monoSoundFile.wav"); // e.g. // a.buffer = Buffer.read(s, "/Users/paul/TESTING/TestSamples/_impulses/LexiconHallMono1.wav"); a.buffer = Buffer.read(s, "/Users/paul/TESTING/TestSamples/_impulses/FactoryHallMono.wav"); s.sync; a.buffer.loadToFloatArray(action: { arg array; a.floatArray = array.reverse; }); s.sync; 2.wait; a.buffer.free; a.buffer = Buffer.loadCollection(s, a.floatArray); */ // ======= /* // ======= EX4 - build buffer from floatArray - taken from PartConv help file // synthesise the honourable 'Dan Stowell' impulse response a.ir = ([1] ++0.dup(100) ++ ((1, 0.99998 .. 0).collect{|f| f = f.squared.squared; f = if(f.coin){0}{f.squared}; f = if(0.5.coin){0-f}{f} } * 0.1) ).normalizeSum; a.ir = a.ir.reverse; // added for backwards reverb if (a.buffer.notNil, { a.buffer.free; }); a.buffer = Buffer.loadCollection(s, a.ir); // a.ir.plot; */ // ======= s.sync; "Setup Completed".postln; }.fork; }); ) ////////////////////////////////////////////////////// // prepare irspectrum buffer ( { s.sync; a.bufsize = PartConv.calcBufSize(a.fftsize, a.buffer); if (a.irspectrum.notNil, { a.irspectrum.free; }); a.irspectrum = Buffer.alloc(s, a.bufsize, 1); a.irspectrum.preparePartConv(a.buffer, a.fftsize); s.sync; "Prepare Buffer Completed".postln; }.fork; ) ////////////////////////////////////////////////////// // examples to use the IR - all are mono, but duplicated in left/right speaker (using ! 2) // convolve with target soundfile ( { var input, convLevel = 0.6; input = PlayBuf.ar(1, a.target.bufnum, loop:1); Out.ar(0, (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum, 0.5)) ! 2); }.play; ) // convolve with target soundfile adding input ( { var input, delayTime, inputDelay, inputLevel = 0.5, convLevel = 0.5; input = PlayBuf.ar(1, a.target, loop:1); Out.ar(0, ((inputLevel * input) + (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum, 0.5))) ! 2); }.play; ) // convolve with target soundfile adding delayed input ( { var input, delayTime, inputDelay, inputLevel = 0.5, convLevel = 0.5; input = PlayBuf.ar(1, a.target, loop:1); delayTime = BufFrames.ir(a.buffer.bufnum) / BufSampleRate.ir(a.buffer.bufnum); inputDelay = DelayC.ar(input * inputLevel, delayTime, delayTime); Out.ar(0, (inputDelay + (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum, 0.5))) ! 2); }.play; ) // convolve with live input ( { var input, convLevel = 0.8; input= SoundIn.ar(0); Out.ar(0, (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum)) ! 2); }.play; ) // convolve with live input adding input ( { var input, inputLevel = 0.7, convLevel = 0.7; input = SoundIn.ar(0); Out.ar(0, ((inputLevel * input) + (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum))) ! 2); }.play; ) // convolve with live input adding delayed input ( { var input, delayTime, delayedInput, inputLevel = 0.7, convLevel = 0.7; input = SoundIn.ar(0); delayTime = BufFrames.ir(a.buffer.bufnum) / BufSampleRate.ir(a.buffer.bufnum); delayedInput = DelayC.ar(input, delayTime, delayTime); Out.ar(0, ((inputLevel * delayedInput) + (convLevel * PartConv.ar(input, a.fftsize, a.irspectrum.bufnum))) ! 2); }.play; ) ////////////////////////////////////////////////////// // save ir buffer to disk ( a.savedBufferName = "/Users/paul/TESTING/TestSamples/_impulses/SavedImpulse_" // e.g. ++ Date.getDate.asSortableString ++ ".aif"; a.buffer.write(a.savedBufferName, headerFormat: "aiff", sampleFormat: "int24"); ("Saved buffer to disk. filename: " ++ a.savedBufferName).postln; ) ////////////////////////////////////////////////////// // free buffers when finished ( if (a.notNil, { if (a.buffer.notNil, { a.buffer.free; }); if (a.target.notNil, { a.target.free; }); if (a.irspectrum.notNil, { a.irspectrum.free; }); }); )
reception
comments