在Fmod Studio C ++中保存FFT频谱

时间:2015-05-17 14:46:33

标签: c++ audio signal-processing fmod spectrum

我正在尝试将频谱保存在我的FMOD_DSP_PARAMETER_FFT中但是我只收到满是零的频谱,如果你能看到我的错误我会同意,我认为我没有把DSP连接到频道或类似的东西,因为我没有在代码中找到错误。

我的代码现在是这样的:

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

我的fft参数 - >频谱总是一个零值数组...... 是否可以连接它而不修改正在播放的声音? 谢谢。

1 个答案:

答案 0 :(得分:1)

您的代码示例中存在一些突出问题。

  1. FFT DSP已被result = mydsp->setBypass(true);绕过,导致其无法处理。
  2. 主循环中没有调用System :: update。
  3. 主循环没有睡眠,所以它会尽可能快地旋转。
  4. 我认为您的主要问题可能是setBypass调用,请使用setBypass(false)。