2 summary:: Overview of FFT UGens
4 related:: Classes/FFT, Classes/IFFT
8 SuperCollider implements a number of UGens supporting FFT based processing. The most basic of these are FFT and IFFT which convert data between the time and frequency domains:
10 chain = FFT(buffer, input)
15 link::Classes/FFT:: stores spectral data in a local buffer ( see link::Classes/Buffer:: ) in the following order: DC, nyquist, real 1f, imag 1f, real 2f, imag 2f, ... real (N-1)f, imag (N-1)f, where f is the frequency corresponding to the window size, and N is the window size / 2.
17 The buffer's size must correspond to a power of 2, and must also be a multiple of SC's block size. The window size is equivalent to the buffer size, and the window overlap defaults to 2. Both link::Classes/FFT:: and link::Classes/IFFT:: use a Sine window by default, the combination of which (i.e. raised sine, that is, sine squared) is a Hanning window.
19 section:: Phase Vocoder UGens and Spectral Processing
21 In between an FFT and an IFFT one can chain together a number of Phase Vocoder UGens (i.e. 'PV_...') to manipulate blocks of spectral data before reconversion. The process of buffering the appropriate amount of audio, windowing, conversion, overlap-add, etc. is handled for you automatically.
25 in = WhiteNoise.ar(0.8);
26 chain = FFT(LocalBuf(2048), in);
27 chain = PV_RandComb(chain, 0.95, Impulse.kr(0.4));
33 In order to expand PV UGens for a multichannel input signal, an appropriate array of buffers must be provided (not a multichannel buffer):
38 in = Ringz.ar(Impulse.ar([2, 3]), [700, 800], 0.1) * 5;
39 chain = FFT({ LocalBuf(2048) } ! 2, in);
40 chain = PV_RandComb(chain, 0.95, Impulse.kr(0.4));
47 PV Ugens write their output data emphasis:: in place ::, i.e. back into the same buffer from which they read. PV UGens which require two buffers write their data into the first buffer, usually called 'bufferA'.
51 { var inA, chainA, inB, chainB, chain;
52 inA = LFSaw.ar(MouseY.kr(100, 1000, 1), 0, 0.2);
53 inB = Ringz.ar(Impulse.ar(MouseX.kr(1, 100, 1)), 700, 0.5);
54 chainA = FFT(LocalBuf(2048), inA);
55 chainB = FFT(LocalBuf(2048), inB);
56 chain = PV_MagMul(chainA, chainB); // writes into bufferA
64 A similar example using a soundfile in an external buffer:
67 d = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
70 { var inA, chainA, inB, chainB, chain;
71 inA = LFSaw.ar(100, 0, 0.2);
72 inB = PlayBuf.ar(1, d, BufRateScale.kr(d), loop: 1);
73 chainA = FFT(LocalBuf(2048), inA);
74 chainB = FFT(LocalBuf(2048), inB);
75 chain = PV_MagMul(chainA, chainB); // writes into bufferA
83 Because each PV UGen overwrites the output of the previous one, it is necessary to copy the data to an additional buffer at the desired point in the chain in order to do parallel processing of input without using multiple FFT UGens. link::Classes/PV_Copy:: allows for this.
86 b = Buffer.alloc(s,2048,1); // use global buffers for plotting the data
87 c = Buffer.alloc(s,2048,1);
92 x = { var inA, chainA, chainB;
93 inA = LFClipNoise.ar(100);
95 chainB = PV_Copy(chainA, c);
96 IFFT(chainA) - IFFT(chainB); // cancels to zero so silent!
100 // IFFTed frames contain the same windowed output data
101 b.plot(\b, Rect(200, 430, 700, 300), nil, nil); c.plot(\c, Rect(200, 100, 700, 300), nil, nil);
105 Note that PV UGens convert as needed between cartesian (complex) and polar representations, therefore when using multiple PV UGens it may be impossible to know in which form the values will be at any given time. FFT produces complex output (see above), so while the following produces a reliable magnitude plot:
107 b = Buffer.alloc(s,1024); // use global buffers for plotting the data
108 a = { FFT(b, LFSaw.ar(4000)); 0.0 }.play;
110 b.getn(0, 1024, { arg buf;
112 z = buf.clump(2).flop;
113 z = [Signal.newFrom(z[0]), Signal.newFrom(z[1])];
114 x = Complex(z[0], z[1]);
115 {x.magnitude.plot}.defer
120 any Synth using PV UGens might not.
122 It is possible to manipulate the FFT data directly within a synth graph (if there doesn't already exist a PV UGen which will do what you want), using the methods pvcalc, pvcalc2, pvcollect. Here's an example which uses the link::Classes/SequenceableCollection:: methods clump and flop to rearrange the order of the spectral bins:
124 c = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
128 var in, numFrames=2048, chain, v;
129 in = PlayBuf.ar(1, c, loop: 1);
130 chain = FFT(LocalBuf(numFrames), in);
132 chain = chain.pvcalc(numFrames, {|mags, phases|
133 /* Play with the mags and phases, then return them */
134 [mags, phases].flop.clump(2).flop.flatten
137 Out.ar(0, 0.5 * IFFT(chain).dup);
143 section:: Multichannel Expansion with FFT UGens
145 Care must be taken when using multichannel expansion with FFT UGens, as they require separate buffers. Code such as this can be deceptive:
147 chain = FFT(bufnum, {WhiteNoise.ar(0.2)}.dup);
150 The above may seem to work, but does not. It does result in two FFT UGens, but as they both write to the same buffer, the second simply overwrites the data from the first, thus wasting CPU and accomplishing nothing.
152 When using multichannel expansion with FFT UGens it is necessary to ensure that each one writes to a different buffer. Here's an example of one way to do this:
155 SynthDef("help-multichannel FFT", { arg out=0; // bufnum is an array
157 in = [SinOsc.ar(200, 0, 0.2), WhiteNoise.ar(0.2)];
158 chain = FFT(LocalBuf([2048, 2048]), in); // each FFT has a different buffer
159 // now we can multichannel expand as normal
160 chain = PV_BrickWall(chain, SinOsc.kr(-0.1));
161 Out.ar(out, IFFT(chain));
165 // or using global buffers
167 b = {Buffer.alloc(s,2048,1)}.dup;
170 SynthDef("help-multichannel FFT", { arg out=0, bufnum= #[0, 1]; // bufnum is an array
172 in = [SinOsc.ar(200, 0, 0.2), WhiteNoise.ar(0.2)];
173 chain = FFT(bufnum, in); // each FFT has a different buffer
174 // now we can multichannel expand as normal
175 chain = PV_BrickWall(chain, SinOsc.kr(-0.1));
176 Out.ar(out, IFFT(chain));
177 }).play(s,[\bufnum, b]);
181 Note that dup on a UGen just makes a reference to that UGen, because UGen defines -copy to simply return the receiver. (See link::Classes/UGen:: for more detail.)
188 Code like code::IFFT(chain).dup:: is found throughout the PV help files , and is just a convenient way to copy a mono signal to stereo, without further computation.
190 See also link::Guides/Multichannel-Expansion::.
192 section:: PV and FFT UGens in the Standard Library
194 The following PV UGens are included in the standard SC distribution:
196 ## link::Classes/FFT:: || Fast Fourier Transform
197 ## link::Classes/IFFT:: || Inverse Fast Fourier Transform
198 ## link::Classes/PV_Add:: || complex addition
199 ## link::Classes/PV_BinScramble:: || scramble bins
200 ## link::Classes/PV_BinShift:: || shift and stretch bin position
201 ## link::Classes/PV_BinWipe:: || combine low and high bins from two inputs
202 ## link::Classes/PV_BrickWall:: || zero bins
203 ## link::Classes/PV_ConformalMap:: || complex plane attack
204 ## link::Classes/PV_Copy:: || copy an FFT buffer
205 ## link::Classes/PV_CopyPhase:: || copy magnitudes and phases
206 ## link::Classes/PV_Diffuser:: || random phase shifting
207 ## link::Classes/PV_HainsworthFoote:: || onset detection
208 ## link::Classes/PV_JensenAndersen:: || onset detection
209 ## link::Classes/PV_LocalMax:: || pass bins which are a local maximum
210 ## link::Classes/PV_MagAbove:: || pass bins above a threshold
211 ## link::Classes/PV_MagBelow:: || pass bins below a threshold
212 ## link::Classes/PV_MagClip:: || clip bins to a threshold
213 ## link::Classes/PV_MagFreeze:: || freeze magnitudes
214 ## link::Classes/PV_MagMul:: || multiply magnitudes
215 ## link::Classes/PV_MagDiv:: || division of magnitudes
216 ## link::Classes/PV_MagNoise:: || multiply magnitudes by noise
217 ## link::Classes/PV_MagShift:: || shift and stretch magnitude bin position
218 ## link::Classes/PV_MagSmear:: || average magnitudes across bins
219 ## link::Classes/PV_MagSquared:: || square magnitudes
220 ## link::Classes/PV_Max:: || maximum magnitude
221 ## link::Classes/PV_Min:: || minimum magnitude
222 ## link::Classes/PV_Mul:: || complex multiply
223 ## link::Classes/PV_PhaseShift:: || shift phase of all bins
224 ## link::Classes/PV_PhaseShift270:: || shift phase by 270 degrees
225 ## link::Classes/PV_PhaseShift90:: || shift phase by 90 degrees
226 ## link::Classes/PV_RandComb:: || pass random bins
227 ## link::Classes/PV_RandWipe:: || crossfade in random bin order
228 ## link::Classes/PV_RectComb:: || make gaps in spectrum
229 ## link::Classes/PV_RectComb2:: || make gaps in spectrum
230 ## link::Classes/UnpackFFT::, link::Classes/PackFFT::, link::Classes/Unpack1FFT:: || "unpacking" components used in pvcalc, pvcalc2, pvcollect (can also be used on their own)
232 For a full list of FFT UGens, see strong::UGens>FFT:: in the link::Browse#UGens>FFT:: page.