# fft-\>numchannels Always comes back 0

**URL:** <https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843>\
**Category:** FMOD Engine\
**Created:** [December 1, 2016, 11:02pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843 "2016-12-01T23:02:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 1, 2016, 11:02pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/1 "2016-12-01T23:02:19Z")

</div>

Hey, I am currently trying to get the FFT parameterData, but following all examples I can find online, the fft response has a numchannels of 0 always.

Am I thinking about this wrong? Or is something up with my code maybe?

```
 // This is defined outside the loop
FMOD::DSP* dsp;
    system->createDSPByType(FMOD_DSP_TYPE::FMOD_DSP_TYPE_FFT, &dsp);
    channel->addDSP(FMOD_DSP_PARAMETER_DATA_TYPE_FFT, dsp);
    dsp->setActive(true);

    // This is in the loop
    FMOD_DSP_PARAMETER_FFT *fft; // FMOD_DSP_FFT_DOMINANT_FREQ 
    dsp->getParameterData(FMOD_DSP_FFT_SPECTRUMDATA, (void **)&fft, 0, 0, 0);
    
    Common_Draw("Channels %d", fft->numchannels);
    for (int channelz = 0; channelz < fft->numchannels; channelz++)
    		{
    			Common_Draw("count %f", channelz);
    			for (int bin = 0; bin < fft->length; bin++)
    			{
    				float val = fft->spectrum[channelz][bin];
    				Common_Draw("FFT %f", val);
    			}
    		}

```

I am using the record.cpp example for the low-level api.

Thank you for any feedback.

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 5, 2016, 4:34am UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/2 "2016-12-05T04:34:31Z")

</div>

Hi ,  
I put this code into the recording example and it seemed to work fine.

The key would be where to put your ‘outside the loop’ code though.  
The playsound only happens after a few updates, so it has to go after the playSound call so that channel is valid.

Are you checking error return values? I have a feeling you would be getting errors. (ie FMOD\_ERR\_INVALID\_HANDLE in addDSP).

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 5, 2016, 10:32pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/3 "2016-12-05T22:32:49Z")

</div>

> [@](#):
>
> Hi ,  
> I put this code into the recording example and it seemed to work fine.
> 
> The key would be where to put your ‘outside the loop’ code though.  
> The playsound only happens after a few updates, so it has to go after the playSound call so that channel is valid.
> 
> Are you checking error return values? I have a feeling you would be getting errors. (ie FMOD\_ERR\_INVALID\_HANDLE in addDSP).

Hey Brett, thanks for your feedback. Can you tell me where you put the code? I just tried moving it around, and i t seems to still be coming back with 0 channels on the fft.

The playSound is inside the loop, so confused on where to put things.

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 5, 2016, 10:41pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/4 "2016-12-05T22:41:01Z")

</div>

the playSound is inside the loop and is only called once, on a special condition (when the record cursor has travelled far enough.

You put it inside the if statement, right after the playsound call. (your ‘outside the loop’ code)

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 5, 2016, 11:22pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/5 "2016-12-05T23:22:30Z")

</div>

Where should I put the dps variable declaration? I tried having it in there, but then the code that grabs the fft cant access the variable. I tried declaring it up top but then it says the dsp var is not there when it hits the call right after playSound.

Thanks again, and sorry if this is some obvious thing…I just started in c++ in the last few months, still figuring it out.

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 5, 2016, 11:27pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/6 "2016-12-05T23:27:29Z")

</div>

Hi Scott,  
The issue you’re having is to do with ‘scope’ (you can google that in regards to C++). You need to make dsp accessible from both places, so just put it at the top of main if you want. Any time a variable is declared in {} braces the code in the level above it cannot access it.

Your second case should be the right one, what actual error did it give you?

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 5, 2016, 11:33pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/7 "2016-12-05T23:33:40Z")

</div>

error C4703: potentially uninitialized local pointer variable ‘dsp’ used

I tried putting FMOD::DSP\* dsp; at the very top outside of the int FMOD\_Main() and also inside. Outside up top had some read access violation, and inside had the error above.

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 5, 2016, 11:34pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/8 "2016-12-05T23:34:22Z")

</div>

I’m just using the record.cpp example also

Could you send me your record.cpp file so I can see how u did it?

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 5, 2016, 11:35pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/9 "2016-12-05T23:35:58Z")

</div>

thats more of a warning. Do you have warnings set as errors in your project settings.  
Just set dsp = 0, and put it in main, you don’t need to make it global.

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 5, 2016, 11:59pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/10 "2016-12-05T23:59:45Z")

</div>

Seem to be getting this error now:

Exception thrown: read access violation.

fft was 0xFFFFFFFFFFFFFFFB.

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 6, 2016, 12:02am UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/11 "2016-12-06T00:02:02Z")

</div>

oh yeah, inside your loop ie ‘// This is in the loop’ should be wrapped in a check to see if dsp is valid or not.

ie

if (dsp)  
{  
// your inside loop code  
}

as the first time through it will be using ‘0’ as your pointer value which is invalid. Looking at a debugger should reveal this.

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 6, 2016, 12:27am UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/12 "2016-12-06T00:27:34Z")

</div>

Thank you Brett! That did the trick.

I think I messed some other stuff up in the file, because I’m getting some weird unrelated errors now that its running. (I messed with it for a while).

Is there any way I can get you to [pastebin.com](http://pastebin.com) the contents of your record.cpp file so I don’t have to uninstall/reinstall? I cant seem to find the files anywhere except thru the api installer.

No worries if not, and thanks so much!

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 7, 2016, 2:50am UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/13 "2016-12-07T02:50:35Z")

</div>

Hi Scott, I put the modified record example here [http://pastebin.com/kC2NJ7xm](http://pastebin.com/kC2NJ7xm)

---

<div class="post-metadata">

**Author:** ![scott1](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@scott1](https://qa.fmod.com/u/scott1)\
**Post date:** [December 8, 2016, 9:44pm UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/14 "2016-12-08T21:44:27Z")

</div>

Hey Brett,

Using that code, the FFT will come back a for a few rounds, then it throws this error:

Exception thrown: read access violation.  
string was 0x2371A3C5840.

I tried to catch all the possible fails, but still not working, any ideas:

for (int channelz = 0; channelz \< fft-\>numchannels; channelz++)  
{  
int length = fft-\>length - 1;  
if(length != NULL){  
for (int bin = 0; bin \< length; bin++)  
//for (int bin = 0; bin \< 2; bin++)  
{  
float val = fft-\>spectrum[channelz][bin];  
if (!isnan(val)) {  
Common\_Draw(“FFT %f”, val);  
}

```
				}
			}
		}
```

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [December 9, 2016, 12:07am UTC](https://qa.fmod.com/t/fft-numchannels-always-comes-back-0/12843/15 "2016-12-09T00:07:04Z")

</div>

can you use the debugger to inspect the variables? I dont think length being null is a good check. For a start it should not be NULL but 0 (null is typically only used for pointers) and also if fft-\>length was 0 then length would be -1 anyway.  
The for loop wouldnt execute if length was 0 or -1 anyway.

Have you determined that’s why it has the problem? Where is the read, and what variable is it talking about?
