Hello !
You can skip this : I guess this will be the first of several topics regarding FFT, as I just started using it and understanding it, and it’s difficult to me, as I didn’t study math in high school and topic is really abstract. Excuse me if I’m confused about some concepts. If you’re in the same position as I am, you might find this video useful to get started : 3blue1brown : But what is the Fourier Transform? A visual introduction.
My question is : how do I get the result of a Fourier analysis off a complete sound file ?
Using the documentation, I came up with this code :
(
var filePath = Platform.userHomeDir ++ "/SC/AudioFiles/celloMono.wav";
var buffer = Buffer.read(s, filePath);
var synth;
~fftBuffer = Buffer.alloc(s, 2048, 1);
synth = {
var audioFile = PlayBuf.ar(1, buffer, BufRateScale.kr(buffer), loop: 1);
var chain = FFT(~fftBuffer, buffer);
}.play;
CmdPeriod.doOnce({
buffer.free;
~fftBuffer.free;
synth.free;
});
)
(
~fftBuffer.getToFloatArray(action: { |array|
array.postln;
});
)
Alas, I only get an array full of zeros when posting the content of the buffer…
By looping the sample ( loop: 1
), I then got a real result (which is different every time…). This made me understand that the FFT()
is constantly rewriting the buffer, and I get an array full of zeros because I call it after my sample has stopped playing.
So this is perfect for real time FFT, but the tool seems to be difficult to use for ‘NRT analysis’.
What I understood so far : I should change the FFT()
winsize parameter so that it matches my file number of frames. But, as it also has to be a power of 2, I should first calculate the next power of two after the number of frames in my audio file, and then add some zeros at the end of the audio file buffer so that it matches this number. After that, I should loop the sample so every time I access the array, the FFT has been done on the full file duration.
This seems really unappropriate. Does anyone see what I missed ?