Skip to content

Commit

Permalink
bugfix replace deprecated fmod system fft call with future compatible…
Browse files Browse the repository at this point in the history
… calls (#6639)

#changelog #sound
  • Loading branch information
ofTheo authored Jan 1, 2021
1 parent fe591d1 commit dc17c43
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 112 deletions.
120 changes: 55 additions & 65 deletions libs/openFrameworks/sound/ofFmodSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@


static bool bFmodInitialized_ = false;
static float fftValues_[8192]; //
static float fftInterpValues_[8192]; //
static float fftSpectrum_[8192]; // maximum #ofFmodSoundPlayer is 8192, in fmodex....
static float fftSpectrum_[8192]; // maximum #ofFmodSoundPlayer is 8192, in fmod....
static unsigned int buffersize = 1024;

static FMOD_DSP* fftDSP = NULL;

// --------------------- static vars
static FMOD_CHANNELGROUP * channelgroup;
Expand Down Expand Up @@ -49,6 +48,7 @@ float * ofFmodSoundGetSpectrum(int nBands){
// set to 0
for (int i = 0; i < 8192; i++){
fftInterpValues_[i] = 0;
fftSpectrum_[i] = 0;
}

// check what the user wants vs. what we can do:
Expand All @@ -61,66 +61,56 @@ float * ofFmodSoundGetSpectrum(int nBands){
return fftInterpValues_;
}

// FMOD needs pow2
int nBandsToGet = ofNextPow2(nBands);
if (nBandsToGet < 64) nBandsToGet = 64; // can't seem to get fft of 32, etc from fmodex

// get the fft
FMOD_System_GetSpectrum(sys, fftSpectrum_, nBandsToGet, 0, FMOD_DSP_FFT_WINDOW_HANNING);
// get the fft
// useful info here: https://www.parallelcube.com/2018/03/10/frequency-spectrum-using-fmod-and-ue4/
if( fftDSP == NULL ){
FMOD_System_CreateDSPByType(sys, FMOD_DSP_TYPE_FFT,&fftDSP);
FMOD_ChannelGroup_AddDSP(channelgroup,0,fftDSP);
FMOD_DSP_SetParameterInt(fftDSP, FMOD_DSP_FFT_WINDOWTYPE, FMOD_DSP_FFT_WINDOW_HANNING);
}

if( fftDSP != NULL ){
FMOD_DSP_PARAMETER_FFT *fft;
auto result = FMOD_DSP_GetParameterData(fftDSP, FMOD_DSP_FFT_SPECTRUMDATA, (void **)&fft, 0, 0, 0);
if( result == 0 ){

// Only read / display half of the buffer typically for analysis
// as the 2nd half is usually the same data reversed due to the nature of the way FFT works. ( comment from link above )
int length = fft->length/2;
if( length > 0 ){

std::vector <float> avgValCount;
avgValCount.assign(nBands, 0.0);

float normalizedBand = 0;
float normStep = 1.0 / (float)length;

for (int bin = 0; bin < length; bin++){
//should map 0 to nBands but accounting for lower frequency bands being more important
int logIndexBand = log10(1.0 + normalizedBand*9.0) * nBands;

//get both channels as that is what the old FMOD call did
for (int channel = 0; channel < fft->numchannels; channel++){
fftSpectrum_[logIndexBand] += fft->spectrum[channel][bin];
avgValCount[logIndexBand] += 1.0;
}

normalizedBand += normStep;
}

//average the remapped bands based on how many times we added to each bin
for(int i = 0; i < nBands; i++){
if( avgValCount[i] > 1.0 ){
fftSpectrum_[i] /= avgValCount[i];
}
}
}
}
}

// convert to db scale
for(int i = 0; i < nBandsToGet; i++){
fftValues_[i] = 10.0f * (float)log10(1 + fftSpectrum_[i]) * 2.0f;
}

// try to put all of the values (nBandsToGet) into (nBands)
// in a way which is accurate and preserves the data:
//

if (nBandsToGet == nBands){

for(int i = 0; i < nBandsToGet; i++){
fftInterpValues_[i] = fftValues_[i];
}

} else {

float step = (float)nBandsToGet / (float)nBands;
//float pos = 0;
// so for example, if nBands = 33, nBandsToGet = 64, step = 1.93f;
int currentBand = 0;

for(int i = 0; i < nBandsToGet; i++){

// if I am current band = 0, I care about (0+1) * step, my end pos
// if i > endPos, then split i with me and my neighbor

if (i >= ((currentBand+1)*step)){

// do some fractional thing here...
float fraction = ((currentBand+1)*step) - (i-1);
float one_m_fraction = 1 - fraction;
fftInterpValues_[currentBand] += fraction * fftValues_[i];
currentBand++;
// safety check:
if (currentBand >= nBands){
ofLogWarning("ofFmodSoundPlayer") << "ofFmodGetSpectrum(): currentBand >= nBands";
}

fftInterpValues_[currentBand] += one_m_fraction * fftValues_[i];

} else {
// do normal things
fftInterpValues_[currentBand] += fftValues_[i];
}
}

// because we added "step" amount per band, divide to get the mean:
for (int i = 0; i < nBands; i++){
fftInterpValues_[i] /= step;
if (fftInterpValues_[i] > 1)fftInterpValues_[i] = 1; // this seems "wrong"
}

for(int i = 0; i < nBands; i++){
fftInterpValues_[i] = 10.0f * (float)log10(1 + fftSpectrum_[i]) * 2.0f;
}

return fftInterpValues_;
Expand Down Expand Up @@ -213,8 +203,8 @@ bool ofFmodSoundPlayer::load(const std::filesystem::path& _fileName, bool stream
// [3] load sound

//choose if we want streaming
int fmodFlags = FMOD_SOFTWARE;
if(stream)fmodFlags = FMOD_SOFTWARE | FMOD_CREATESTREAM;
int fmodFlags = FMOD_DEFAULT;
if(stream)fmodFlags = FMOD_DEFAULT | FMOD_CREATESTREAM;

result = FMOD_System_CreateSound(sys, fileName.data(), fmodFlags, nullptr, &sound);

Expand Down Expand Up @@ -377,7 +367,7 @@ void ofFmodSoundPlayer::play(){
FMOD_Channel_Stop(channel);
}

FMOD_System_PlaySound(sys, FMOD_CHANNEL_FREE, sound, bPaused, &channel);
FMOD_System_PlaySound(sys, sound, channelgroup, bPaused, &channel);

FMOD_Channel_GetFrequency(channel, &internalFreq);
FMOD_Channel_SetVolume(channel,volume);
Expand All @@ -398,4 +388,4 @@ void ofFmodSoundPlayer::stop(){
FMOD_Channel_Stop(channel);
}

#endif //OF_SOUND_PLAYER_FMOD
#endif //OF_SOUND_PLAYER_FMOD
2 changes: 1 addition & 1 deletion libs/openFrameworks/sound/ofFmodSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ class ofFmodSoundPlayer : public ofBaseSoundPlayer {
FMOD_SOUND * sound;
};

#endif //OF_SOUND_PLAYER_FMOD
#endif //OF_SOUND_PLAYER_FMOD
2 changes: 1 addition & 1 deletion libs/openFrameworksCompiled/project/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ model {
"**/ofRtAudioSoundStream.cpp",
"**/glew/**/*",
"**/videoInput/**/*",
"**/fmodex/**/*",
"**/fmod/**/*",
"**/kiss/**/*",
"**/assimp/**/*",
"**/portaudio/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/openFrameworks/sound/ofRtAudioSoundS
# third party
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/glew/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/videoInput/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmodex/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmod/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/kiss/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/assimp/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/portaudio/%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/glew/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/boost/include/boost/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/videoInput/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/assimp/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmodex/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmod/%

################################################################################
# PLATFORM HEADER SEARCH PATHS
Expand Down
4 changes: 2 additions & 2 deletions libs/openFrameworksCompiled/project/ios/CoreOF.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HEADER_OFXIOS = "$(OF_PATH)/addons/ofxiOS/**"
HEADER_OFXACCELEROMETER = "$(OF_PATH)/addons/ofxAccelerometer/**"
HEADER_FREETYPE = "$(OF_PATH)/libs/freetype/include"
HEADER_FREETYPE2 = "$(OF_PATH)/libs/freetype/include/freetype2"
HEADER_FMODEX = "$(OF_PATH)/libs/fmodex/include"
HEADER_FMOD = "$(OF_PATH)/libs/fmod/include"
HEADER_GLEW = "$(OF_PATH)/libs/glew/include"
HEADER_FREEIMAGE = "$(OF_PATH)/libs/FreeImage/include"
HEADER_TESS2 = "$(OF_PATH)/libs/tess2/include"
Expand Down Expand Up @@ -34,7 +34,7 @@ MISC_FLAGS = "-ObjC"

OF_CORE_LIBS = $(MISC_FLAGS) $(LIB_BOOST) $(LIB_FREEIMAGE) $(LIB_FREETYPE) $(LIB_OPENSSL) $(LIB_TESS) $(LIB_CURL) $(LIB_URIPARSER) $(LIB_PUGIXML) $(LIB_OF)

OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_OFXIOS) $(HEADER_OFXACCELEROMETER) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMODEX) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_RTAUDIO) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)
OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_OFXIOS) $(HEADER_OFXACCELEROMETER) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMOD) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_RTAUDIO) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)

OF_CORE_FRAMEWORKS = -framework AudioToolbox -framework Accelerate -framework AVFoundation -framework CoreAudio -framework CoreGraphics -framework CoreLocation -framework CoreMotion -framework CoreMedia -framework CoreVideo -framework Foundation -framework GameController -framework GLKit -framework MapKit -framework OpenAL -framework OpenGLES -framework UIKit -framework Security -framework QuartzCore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/curl/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/uriparser/%

ifeq ($(USE_FMOD),0)
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmodex/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmod/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/openFrameworks/sound/ofFmodSoundPlayer.cpp
endif

Expand Down
8 changes: 4 additions & 4 deletions libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ MACOSX_DEPLOYMENT_TARGET = 10.9
HEADER_OF = "$(OF_PATH)/libs/openFrameworks/**"
HEADER_FREETYPE = "$(OF_PATH)/libs/freetype/include"
HEADER_FREETYPE2 = "$(OF_PATH)/libs/freetype/include/freetype2"
HEADER_FMODEX = "$(OF_PATH)/libs/fmodex/include"
HEADER_FMOD = "$(OF_PATH)/libs/fmod/include"
HEADER_GLEW = "$(OF_PATH)/libs/glew/include"
HEADER_FREEIMAGE = "$(OF_PATH)/libs/FreeImage/include"
HEADER_TESS2 = "$(OF_PATH)/libs/tess2/include"
Expand All @@ -23,7 +23,7 @@ HEADER_CURL = "$(OF_PATH)/libs/curl/include"
HEADER_URIPARSER = "$(OF_PATH)/libs/uriparser/include"
HEADER_PUGIXML = "$(OF_PATH)/libs/pugixml/include"

LIB_FMODEX = "$(OF_PATH)/libs/fmodex/lib/osx/libfmodex.dylib"
LIB_FMOD = "$(OF_PATH)/libs/fmod/lib/osx/libfmod.dylib"
LIB_GLFW = "$(OF_PATH)/libs/glfw/lib/osx/glfw3.a"
LIB_FREEIMAGE = "$(OF_PATH)/libs/FreeImage/lib/osx/freeimage.a"
LIB_FREETYPE = "$(OF_PATH)/libs/freetype/lib/osx/freetype.a"
Expand All @@ -42,10 +42,10 @@ LIB_PUGIXML = "$(OF_PATH)/libs/pugixml/lib/osx/pugixml.a"
LIB_OF = "$(OF_PATH)/libs/openFrameworksCompiled/lib/osx/openFrameworks.a"
LIB_OF_DEBUG = "$(OF_PATH)/libs/openFrameworksCompiled/lib/osx/openFrameworksDebug.a"

OF_CORE_LIBS = $(LIB_TESS) $(LIB_GLEW) $(LIB_CAIRO1) $(LIB_CAIRO2) $(LIB_CAIRO3) $(LIB_FMODEX) $(LIB_RTAUDIO) $(LIB_GLFW) $(LIB_FREEIMAGE) $(LIB_FREETYPE) $(LIB_BOOST_FS) $(LIB_BOOST_SYSTEM) $(LIB_CURL) $(LIB_URIPARSER) $(LIB_PUGIXML)
OF_CORE_LIBS = $(LIB_TESS) $(LIB_GLEW) $(LIB_CAIRO1) $(LIB_CAIRO2) $(LIB_CAIRO3) $(LIB_FMOD) $(LIB_RTAUDIO) $(LIB_GLFW) $(LIB_FREEIMAGE) $(LIB_FREETYPE) $(LIB_BOOST_FS) $(LIB_BOOST_SYSTEM) $(LIB_CURL) $(LIB_URIPARSER) $(LIB_PUGIXML)


OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMODEX) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_CAIRO) $(HEADER_RTAUDIO) $(HEADER_GLFW) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)
OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMOD) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_CAIRO) $(HEADER_RTAUDIO) $(HEADER_GLFW) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)


OF_CORE_FRAMEWORKS = -framework Accelerate -framework AGL -framework AppKit -framework ApplicationServices -framework AudioToolbox -framework AVFoundation -framework Cocoa -framework CoreAudio -framework CoreFoundation -framework CoreMedia -framework CoreServices -framework CoreVideo -framework IOKit -framework OpenGL -framework QuartzCore -framework QTKit -framework Security
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/videoInput/%


ifeq ($(USE_FMOD),0)
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmodex/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/fmod/%
PLATFORM_CORE_EXCLUSIONS += $(OF_LIBS_PATH)/openFrameworks/sound/ofFmodSoundPlayer.cpp
PLATFORM_CFLAGS += -DUSE_FMOD=0
endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Module{
}else if(platform==="android"){
libsexceptions = [
"glfw",
"fmodex",
"fmod",
"glew",
"kiss",
"rtAudio",
Expand Down
4 changes: 2 additions & 2 deletions libs/openFrameworksCompiled/project/tvOS/CoreOF.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ HEADER_OFXMULTITOUCH = "$(OF_PATH)/addons/ofxMultiTouch/**"
HEADER_OFXACCELEROMETER = "$(OF_PATH)/addons/ofxAccelerometer/**"
HEADER_FREETYPE = "$(OF_PATH)/libs/freetype/include"
HEADER_FREETYPE2 = "$(OF_PATH)/libs/freetype/include/freetype2"
HEADER_FMODEX = "$(OF_PATH)/libs/fmodex/include"
HEADER_FMOD = "$(OF_PATH)/libs/fmod/include"
HEADER_GLEW = "$(OF_PATH)/libs/glew/include"
HEADER_FREEIMAGE = "$(OF_PATH)/libs/FreeImage/include"
HEADER_TESS2 = "$(OF_PATH)/libs/tess2/include"
Expand Down Expand Up @@ -33,7 +33,7 @@ MISC_FLAGS = "-ObjC"

OF_CORE_LIBS = $(MISC_FLAGS) $(LIB_BOOST) $(LIB_FREEIMAGE) $(LIB_FREETYPE) $(LIB_OPENSSL) $(LIB_TESS) $(LIB_CURL) $(LIB_URIPARSER) $(LIB_PUGIXML)

OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_OFXIOS) $(HEADER_OFXACCELEROMETER) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMODEX) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_RTAUDIO) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)
OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_OFXIOS) $(HEADER_OFXACCELEROMETER) $(HEADER_BOOST) $(HEADER_UTF8) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMOD) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_RTAUDIO) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) $(HEADER_URIPARSER) $(HEADER_PUGIXML)

OF_CORE_FRAMEWORKS = -framework AudioToolbox -framework Accelerate -framework AVFoundation -framework CoreAudio -framework CoreGraphics -framework CoreLocation -framework CoreMotion -framework CoreMedia -framework CoreVideo -framework Foundation -framework GameController -framework GLKit -framework MapKit -framework OpenAL -framework OpenGLES -framework UIKit -framework Security -framework QuartzCore

Expand Down
Loading

0 comments on commit dc17c43

Please sign in to comment.