Get Spectrum and Get WaveData ?

Hello again, I switched from SquareTangle’s FMOD integration to the official integration and found some key differences. I was able to fix most of the problems but I am still having trouble with something. When I write channel.getSpectrum(left, 512, 0, FMOD.DSP_FFT_WINDOW.HAMMING); I get an FMOD.Channel' does not contain a definition forgetSpectrum’ and no extension method getSpectrum' of typeFMOD.Channel’ could be found error. I searched the forums and I found out that I need to add a DSP to the channel after I have played the sound in order to be able to get the spectrum and then I have to use getParameters to get the spectrum and wave data. But how exactly am I supposed to do that ? I am reusing this channel (meaning that I play different sounds on it) should I add a DSP every time after I play a new sound ? I want to get the current spectrum and wave data on every frame of the game.

This is the thread I’ve been reading: http://52.88.2.202/questions/question/dsp-getparameterdata-silent-crash-unity But I didn’t understand how to actually write the spectrum and wave data to a float array.

So far I have:

FMOD.DSP spectrumDSP;
		fmodSystem.createDSPByType(FMOD.DSP_TYPE.FFT, out spectrumDSP);
		spectrumDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWSIZE, 512);
		spectrumDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWTYPE, (int)FMOD.DSP_FFT_WINDOW.HAMMING);
		channel.addDSP(0, spectrumDSP);

I also have this:

System.IntPtr data;
		int dataLength = 0;
		FMOD.DSP_PARAMETER_FFT fftParam;
		spectrumDSP.getParameterData ((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out dataLength);

I’m getting the spectrum like in this thread:
http://52.88.2.202/questions/question/problem-getting-spectrum-in-newer-fmod-versions
However this will return an array of zeroes with the right amount of channels and the right size.

Thanks!

After a whole night of struggling I was able to finally get it to sort of work but I found out that with the new FMOD I have to have the channel playing with volume bigger than 0 for the spectrum to return anything else bigger than zero. I’m currently not being able to return the right values (they seem to be a lot smaller than before) but I’ll keep working on it!

I also need to add the DSP every time I want to get the spectrum:

private float[] GetSpectrum(int channelId){
	FMOD.DSP dsp;
	if (channelId== 0) {
		testChannel.addDSP (0, testFFT);
		dsp = testFFT;
	} else if (channelId== 1) {
		backwardChannel.addDSP (0, backFFT);
		dsp = backFFT;
	} else {
		forwardChannel.addDSP(0, forwardFFT);
		dsp = forwardFFT;
	}


	System.IntPtr data;
	uint dataLength;
	dsp.getParameterData ((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out dataLength);
	FMOD.DSP_PARAMETER_FFT fft = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(FMOD.DSP_PARAMETER_FFT));
	float[] spectrum = new float[fft.length/fft.numchannels];

	for(int i=0; i < spectrum.Length; i ++){
		if(fft.numchannels == 1) spectrum[i] = fft.spectrum[0][i]*GetSpectrumCurve(i, spectrum.Length);
		else if(fft.numchannels > 1){
			float avg = 0;
			for(int n=0; n < fft.numchannels; n ++){
				avg += fft.spectrum[n][i]*GetSpectrumCurve(i, spectrum.Length);
			}
			spectrum[i] = avg/fft.numchannels;
		}
	}
	return spectrum;
}

Hey do you by chance have the completed code file for this?

Use FMOD_CHANNELCONTROL_DSP_TAIL as the first argument to addDSP, this will put the FFT node before the filter so it will be unaffected by volume changes.

Also you need to for wait for audio data to pass through the FFT between calling addDSP and the time you call getParameterData, otherwise it will return all zero.

1 Like

Thanks, Nicholas! That’s a great tip! I managed to get the spectrum working but I’m still trying to find out the equivalent to getWaveData from the old FMOD. So far I’ve found that I can use Sound.lock but I am unsure how to use it exactly.