Problems with getting frequency spectrum data


(Karan Parikh) #1

I am trying to get frequency spectrum data using FMOD_DSP_TYPE_FFT, and then getting a float parameter FMOD_DSP_FFT_DOMINANT_FREQ. However, the dominant frequency is always being returned as zero.
There are no errors while compiling my code.

I tried with
Relevant Snippets of code

Initialization function ()
FMOD::DSP *dsp;
float ChanPitch;
result = system->createDSPByType(FMOD_DSP_TYPE_FFT, &dsp);
result = channel->addDSP(FMOD_DSP_PARAMETER_DATA_TYPE_FFT, dsp);
result = dsp->setBypass(false);

//callled every frame
loop function  ()
spec->getParameterFloat(FMOD_DSP_FFT_DOMINANT_FREQ, &ChanPitch, 0, 0);

**full code (using a modified version of the effects example**


int FMOD_Main()
{
    FMOD::System       *system        = 0;
    FMOD::Sound        *sound         = 0;
    FMOD::Channel      *channel       = 0;
    FMOD::ChannelGroup *mastergroup   = 0; 
    FMOD::DSP          *dsp     ;
    FMOD::DSP          *dspflange     = 0;
    FMOD_RESULT         result;

    unsigned int        version;
	float dfft=0 ;
    void               *extradriverdata = 0;

    Common_Init(&extradriverdata);

    /*
        Create a System object and initialize
    */
    result = FMOD::System_Create(&system);
    ERRCHECK(result);

    result = system->getVersion(&version);
    ERRCHECK(result);

    if (version < FMOD_VERSION)
    {
        Common_Fatal("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);
    }

    result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
    ERRCHECK(result);

    result = system->getMasterChannelGroup(&mastergroup);
    ERRCHECK(result);

    result = system->createSound(Common_MediaPath("drumloop.wav"), FMOD_DEFAULT, 0, &sound);
    ERRCHECK(result);

    result = system->playSound(sound, 0, false, &channel);
    ERRCHECK(result);

    /*
        Create some effects to play with
    */
    
    result = system->createDSPByType(FMOD_DSP_TYPE_FFT, &dsp);
  
    result = system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &dspflange);


    /*
        Add them to the master channel group.  Each time an effect is added (to position 0) it pushes the others down the list.
    */
   
    result = mastergroup->addDSP(0, dsp);

    result = mastergroup->addDSP(0, dspflange);
 
	result = mastergroup->addDSP(FMOD_DSP_PARAMETER_DATA_TYPE_FFT, dsp);

	

    /*
        By default, bypass all effects.  This means let the original signal go through without processing.
        It will sound 'dry' until effects are enabled by the user.
    */


    result = dsp->setBypass(false);

    result = dspflange->setBypass(true);
 

    /*
        Main loop
    */
    do
    { 
		/*
		FMOD_DSP_PARAMETER_FFT *fft= nullptr;
		dsp->getParameterData(FMOD_DSP_FFT_SPECTRUMDATA, (void **)fft, 0, 0, 0);
		for (int channel = 0; channel < 1; channel++)
			for (int bin = 0; 10; ++bin)
				float freqVal = fft->spectrum[channel][bin];
				
		// */
		
        Common_Update();
	

        if (Common_BtnPress(BTN_MORE))
        {
            bool paused;

            result = channel->getPaused(&paused);
           

            paused = !paused;

            result = channel->setPaused(paused);
          
        }

       
		dfft=dsp->getParameterFloat(FMOD_DSP_FFT_DOMINANT_FREQ, &dfft, 0, 0);
		

        if (Common_BtnPress(BTN_ACTION3))
        {
            bool bypass;

            result = dsp->getBypass(&bypass);
            

            bypass = !bypass;

			

            result = dsp->setBypass(bypass);
         
        }

        if (Common_BtnPress(BTN_ACTION4))
        {
            bool bypass;

            result = dspflange->getBypass(&bypass);
         

            bypass = !bypass;

            result = dspflange->setBypass(bypass);
       
        }

        result = system->update();
  

        {
            bool paused = 0;
            
            bool dspecho_bypass;
            bool dspflange_bypass;

           
            dsp      ->getBypass(&dspecho_bypass);
            dspflange    ->getBypass(&dspflange_bypass);

            if (channel)
            {
                result = channel->getPaused(&paused);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
              
                }
            }

            Common_Draw("==================================================");
            Common_Draw("Effects Example.");
            Common_Draw("Copyright (c) Firelight Technologies 2004-2017.");
            Common_Draw("==================================================");
            Common_Draw("");
            Common_Draw("Press %s to pause/unpause sound", Common_BtnStr(BTN_MORE));
            Common_Draw("Press %s to toggle dsplowpass effect", Common_BtnStr(BTN_ACTION1));
            Common_Draw("Press %s to toggle dsphighpass effect", Common_BtnStr(BTN_ACTION2));
            Common_Draw("Press %s to toggle dsp Spectrum e", Common_BtnStr(BTN_ACTION3));
            Common_Draw("Press %s to toggle dspflange effect", Common_BtnStr(BTN_ACTION4));
            Common_Draw("Press %s to quit", Common_BtnStr(BTN_QUIT));
            Common_Draw("");
			Common_Draw("%f",dfft);
            Common_Draw("%s : spectrum[%c] flans[%c] echo[%c] flange[%c]", 
                    paused              ? "Paused " : "Playing",
                  
                    dspecho_bypass      ? ' ' : 'x',
                    dspflange_bypass    ? ' ' : 'x');
        }

        Common_Sleep(50);
    } while (!Common_BtnPress(BTN_QUIT));

    /*
        Shut down
    */
 
    result = mastergroup->removeDSP(dsp);
 
    result = mastergroup->removeDSP(dspflange);

    

    result = dsp->release();
  
    result = dspflange->release();


    result = sound->release();

    result = system->close();

    result = system->release();

    Common_Close();

    return 0;
}

(Brett Paterson) #2

Looks like you overwrite the frequency value with the return code (which is FMOD_OK = 0)

dfft=dsp->getParameterFloat(FMOD_DSP_FFT_DOMINANT_FREQ, &dfft, 0, 0);


(Karan Parikh) #3

Thanks, Brett,

I think it seems to be working now

/*==============================================================================
Effects Example
Copyright ©, Firelight Technologies Pty, Ltd 2004-2017.

This example shows how to apply some of the built in software effects to sounds
by applying them to the master channel group. All software sounds played here
would be filtered in the same way. To filter per channel, and not have other
channels affected, simply apply the same functions to the FMOD::Channel instead
of the FMOD::ChannelGroup.
==============================================================================*/
#include “fmod.hpp”
#include “common.h”

int FMOD_Main()
{
FMOD::System *system = 0;
FMOD::Sound *sound = 0;
FMOD::Channel *channel = 0;
FMOD::ChannelGroup *mastergroup = 0;
FMOD::DSP *dsp;
FMOD::DSP *dspflange = 0;
FMOD_RESULT result;

unsigned int        version;
float dfft;
void               *extradriverdata = 0;

Common_Init(&extradriverdata);

/*
Create a System object and initialize
*/
result = FMOD::System_Create(&system);
ERRCHECK(result);



result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
ERRCHECK(result);

result = system->getMasterChannelGroup(&mastergroup);
ERRCHECK(result);

result = system->createSound(Common_MediaPath("drumloop.wav"), FMOD_DEFAULT, 0, &sound);
ERRCHECK(result);

result = system->playSound(sound, 0, false, &channel);
ERRCHECK(result);

/*
Create some effects to play with
*/

result = system->createDSPByType(FMOD_DSP_TYPE_FFT, &dsp);
ERRCHECK(result);

result = system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &dspflange);
ERRCHECK(result);

/*
Add them to the master channel group.  Each time an effect is added (to position 0) it pushes the others down the list.
*/

result = mastergroup->addDSP(0, dsp);
ERRCHECK(result);


result = mastergroup->addDSP(0, dspflange);

//result = mastergroup->addDSP(FMOD_DSP_PARAMETER_DATA_TYPE_FFT, dsp);



/*
By default, bypass all effects.  This means let the original signal go through without processing.
It will sound 'dry' until effects are enabled by the user.
*/


result = dsp->setBypass(false);
//result = dsp->setActive(true);

result = dspflange->setBypass(true);


/*
Main loop
*/
do
{
	/*
	FMOD_DSP_PARAMETER_FFT *fft= nullptr;
	dsp->getParameterData(FMOD_DSP_FFT_SPECTRUMDATA, (void **)fft, 0, 0, 0);
	for (int channel = 0; channel < 1; channel++)
	for (int bin = 0; 10; ++bin)
	float freqVal = fft->spectrum[channel][bin];

	// */

	Common_Update();


	


	result = dsp->getParameterFloat(FMOD_DSP_FFT_DOMINANT_FREQ, &dfft, 0, 0);


	if (Common_BtnPress(BTN_ACTION3))
	{
		bool bypass;

		result = dsp->getBypass(&bypass);


		bypass = !bypass;



		result = dsp->setBypass(bypass);

	}

	

	result = system->update();


	{
		bool paused = 0;

		bool dspecho_bypass;
		bool dspflange_bypass;


		dsp->getBypass(&dspecho_bypass);
		dspflange->getBypass(&dspflange_bypass);

		if (channel)
		{
			result = channel->getPaused(&paused);
			if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
			{

			}
		}

		
		Common_Draw("%f", dfft);
	
	}

	Common_Sleep(50);
} while (!Common_BtnPress(BTN_QUIT));

/*
Shut down
*/

result = mastergroup->removeDSP(dsp);

result = mastergroup->removeDSP(dspflange);



result = dsp->release();

result = dspflange->release();


result = sound->release();

result = system->close();

result = system->release();

Common_Close();

return 0;

}