2015-05-17 84 views
0

我試圖在我的FMOD_DSP_PARAMETER_FFT中保存頻譜,但我只接收充滿零的頻譜,如果您可以觀看我的錯誤,我會同意,我認爲我不是將DSP連接到通道或類似的東西,因爲我沒有在代碼中發現錯誤。在Fmod Studio C++中保存FFT頻譜

我的代碼現在是這樣的:

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

我fftparameter->頻譜始終爲零值的數組... 是更多鈔票來連接它沒有修改正在播放的聲音? 謝謝。

+0

您是否嘗試將playSound調用移動到您設置DSP通道之後? –

+0

仍然有相同的結果... – Custodius

回答

1

代碼示例中有幾個突出問題。

  1. FFT DSP已被繞過result = mydsp->setBypass(true);導致它不處理。
  2. 在主循環中沒有對System :: update的調用。
  3. 主循環沒有睡眠,所以它會盡快旋轉。

我認爲你的主要問題可能是setBypass調用,使用setBypass(false)。