# Get Spectrum and Get WaveData ?

**URL:** https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784
**Category:** Unity
**Created:** [January 6, 2015, 4:29pm UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784 "2015-01-06T16:29:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Footch](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Footch](https://qa.fmod.com/u/Footch)
#### Post date: [January 6, 2015, 4:29pm UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784/1 "2015-01-06T16:29:56Z")

</div>

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 for`getSpectrum’ and no extension method `getSpectrum' of type`FMOD.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](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](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!

---

<div class="post-metadata">

### Author: ![Footch](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Footch](https://qa.fmod.com/u/Footch)
#### Post date: [January 7, 2015, 12:16pm UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784/2 "2015-01-07T12:16:08Z")

</div>

> [@](#):
>
> 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 for `getSpectrum’ and no extension method `getSpectrum' of type `FMOD.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](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](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;
}

```

---

<div class="post-metadata">

### Author: ![wesmcouch](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@wesmcouch](https://qa.fmod.com/u/wesmcouch)
#### Post date: [May 2, 2016, 8:04pm UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784/3 "2016-05-02T20:04:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)
#### Post date: [January 9, 2015, 6:35am UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784/4 "2015-01-09T06:35:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Footch](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Footch](https://qa.fmod.com/u/Footch)
#### Post date: [January 9, 2015, 11:03pm UTC](https://qa.fmod.com/t/get-spectrum-and-get-wavedata/11784/5 "2015-01-09T23:03:52Z")

</div>

> [@](#):
>
> 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.

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.
