fft->numchannels Always comes back 0


(Scott Hedstrom) #1

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.


(Brett Paterson) #2

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).


(Scott Hedstrom) #3

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.


(Brett Paterson) #4

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)


(Scott Hedstrom) #5

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.


(Brett Paterson) #6

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?


(Scott Hedstrom) #7

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.


(Scott Hedstrom) #8

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?


(Brett Paterson) #9

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.


(Scott Hedstrom) #10

Seem to be getting this error now:

Exception thrown: read access violation.

fft was 0xFFFFFFFFFFFFFFFB.


(Brett Paterson) #11

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.


(Scott Hedstrom) #12

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 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!


(Brett Paterson) #13

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


(Scott Hedstrom) #14

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);
}

				}
			}
		}

(Brett Paterson) #15

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?