Answered
0
1

Hi, I’m trying to save the Spectrum in my FMOD_DSP_PARAMETER_FFT but I’m only receiving the spectrum full of zeros, if you can watch my mistake I will agree.

FMOD::System     *system;
FMOD::Sound      *sound1;
FMOD::Channel    *channel = 0;
FMOD::ChannelGroup *mastergroup;
FMOD::ChannelControl *control;
FMOD::DSP         *mydsp, *dsphead, *dspchannelmixer;
FMOD::DSPConnection *conection;
FMOD_RESULT       result;
unsigned int      version;
result = FMOD::System_Create(&system);
result = system->getVersion(&version);

result = system->init(32, FMOD_INIT_NORMAL, NULL);
result = system->createSound("MySong.mp3",FMOD_DEFAULT, 0, &sound1);
result = sound1->setMode(FMOD_LOOP_NORMAL);
result = system->playSound(sound1, 0, true, &channel);

/*
Create the DSP effect.
*/
result = system->getMasterChannelGroup(&mastergroup);
result = system->createDSPByType(FMOD_DSP_TYPE_FFT, &mydsp);

result = system->getMasterChannelGroup(&mastergroup);
result = mastergroup->addDSP(0, mydsp);


result = mydsp->setBypass(true);
result = mydsp->setActive(true);

char s[256];
unsigned int len;
float freq[32];

float fft = 0;
std::vector<float> fftheights;
float m_spectrum_data[FFT_NUM_BINS];

while (1) { //program loop

unsigned int ms = 0;
unsigned int lenms = 0;
bool         playing = 0;
bool         paused = 0;
int          channelsplaying = 0;
  if (channel)
  {
    FMOD::Sound *currentsound = 0;
    result = channel->setPaused(false);
    result = channel->setMute(false);
    result = channel->isPlaying(&playing);
    result = channel->getPaused(&paused);
    result = channel->setVolume(0.5);
    result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
    channel->getCurrentSound(&currentsound);
    if (currentsound)
    {
      result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);


    }
  }

  system->getChannelsPlaying(&channelsplaying);


  FMOD_DSP_PARAMETER_FFT *fftparameter;
  float val;
  char s[256];
  unsigned int len;
  float *data = 0;
  float freq[32];
  int rate, chan, nyquist;
  int windowsize = 1024;

  result = system->getSoftwareFormat(&rate, 0, 0);


  result = mydsp->setParameterInt(FMOD_DSP_FFT_WINDOWTYPE, FMOD_DSP_FFT_WINDOW_TRIANGLE);

  result = mydsp->setParameterInt(FMOD_DSP_FFT_WINDOWSIZE, windowsize);

  result = mydsp->getParameterFloat(FMOD_DSP_FFT_DOMINANT_FREQ, &val, 0, 0);

  result = mydsp->getParameterData(FMOD_DSP_FFT_SPECTRUMDATA, (void **)&fftparameter, &len, s, 256);

  nyquist = windowsize / 2;

  for (chan = 0; chan < 2; chan++)
  {
    float average = 0.0f;
    float power = 0.0f;

    for (int i = 0; i < nyquist - 1; ++i)
    {
      float hz = i * (rate * 0.5f) / (nyquist - 1);
      int index = i + (16384 * chan);

      if (fftparameter->spectrum[chan][i] > 0.0001f) // arbitrary cutoff to filter out noise
      {
        average += data[index] * hz;
        power += data[index];
      }
    }

    if (power > 0.001f)
    {
      freq[chan] = average / power;
    }
    else
    {
      freq[chan] = 0;
    }
  }
  printf("\ndom freq = %d : %.02f %.02f\n", (int)val, freq[0], freq[1]);
}

My fftparameter->spectrum is always an array of zero values and I don’t know if I’m not making well the addition of DSPs to the channel…
Please, in case that you can correct this, can you do it without modify the sound that is playing??
Thank you.

  • You must to post comments
Best Answer
0
0
  1. Call system::update() in your program loop.
  2. Don’t bypass the FFT dsp.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.