Multiscale Spectral Loss

I am trying to calculate the multiscale spectral loss between two FFT spectrums, yet I fail miserably.
I am not sure that .pvcalc2 is the best function here, but none of the PV_ Ugens seem to be able to do the job.

~mel_win_sizes = [128, 256, 1024, 2048];

(
x = {
	var src, sines, mssl, inFFT, targetFFT;
	src = SoundIn.ar(0);
	sines =	Mix(8.collect{
		var freq = 50.exprand(4950);
		var amp = 0.1.rrand(1).ampdb.linlin(-65, 1.0, 0.0, 1.0);
		SinOsc.ar(freq) * AmpComp.kr(freq) * amp * 0.1});	
	inFFT = FFT(LocalBuf(2048), src, 0.25, 1);
	targetFFT = FFT(LocalBuf(2048), sines, 0.25, 1);
	mssl = inFFT.pvcalc2(targetFFT, {| magnitudes1, phases1, magnitudes2, phases2 |
		4.collect{|n|
			var magDiff, frobDiff, logAbsDist, inFrobNorm;
			
			magDiff = magnitudes1[0..(~mel_win_sizes[n] - 1)]  - magnitudes2[0..(~mel_win_sizes[n] - 1)];
			frobDiff = magDiff.squared.sum.sqrt;
			logAbsDist = magDiff.abs.sum.log;
			inFrobNorm = magnitudes1.squared.sum.sqrt;
			(frobDiff / inFrobNorm) + logAbsDist;
		}
	}.sum);
	
	mssl.poll;
	[sines, src]
}.play;
)

I have struggled a bit with .pvcalc et simila, and I believe that it’s probably easier to make your own UGen if you need to go very specific with FFT.
You don’t specify what happens in your code, but I assume that you are trying to calculate the multiscale spectral loss between your incoming audio input and the sines signal you create. I think that the issue here is that inFFT.pvcalc2 only happens once at the creation of the Synth and you are stuck with the data you get there.
It is not complicated to make your own custom FFT Ugen that does that!

My two cents!

Best,
Stefano

Thankyou @StefanoCatena ! Sorry for not providing much detail on the actual code, but you’re right, I am trying to calculate the loss between two audio sources—an incoming sound and a target sound (sines).
I didn’t realize that .pvcalc2 only happens once!
I haven’t tried making UGens before but since I am sending the data to Python, I might route the audio there and perform the calculation there as well.