Last active
November 4, 2016 09:43
-
-
Save naotokui/47ffb22c75440f42ae03d2ca9a3b6441 to your computer and use it in GitHub Desktop.
openFrameworks - get raw audio frame from audioPlayer using ofFmodSoundPlayer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ofxFmodSoundPlayer.h" | |
| FMOD_RESULT F_CALLBACK analyzerCallback(FMOD_DSP_STATE *dsp_state, | |
| float *inbuffer, float *outbuffer, | |
| unsigned int length, int inchannels, | |
| int outchannels) | |
| { | |
| // pass the data along | |
| memcpy(outbuffer, inbuffer, sizeof(float)* length * outchannels); | |
| ////////// Here you have acess the raw audio data from the sound file | |
| ////////// | |
| void *userdata = NULL; | |
| FMOD_DSP_GetUserData(dsp_state->instance, &userdata); | |
| ofxFmodSoundPlayer *player = (ofxFmodSoundPlayer *)userdata; | |
| // now you can access your instnce of your ofxFmodSondPlayer instance | |
| return FMOD_OK; | |
| } | |
| ofxFmodSoundPlayer::ofxFmodSoundPlayer(){ | |
| // FIXME: I'd love to setup the analyer callback here, but somehow FMOD doesnt allow me to do so... :-( | |
| } | |
| void ofxFmodSoundPlayer::play(){ | |
| ofFmodSoundPlayer::play(); | |
| // Setup - insert our own callback into FMOD graph | |
| FMOD_SYSTEM *system; | |
| FMOD_RESULT result = FMOD_Sound_GetSystemObject(sound, &system); | |
| ERRCHECK(result); | |
| FMOD_DSP_DESCRIPTION dsp_description; | |
| memset(&dsp_description, 0, sizeof(FMOD_DSP_DESCRIPTION)); | |
| strcpy(dsp_description.name, "Audio Analyzer DSP"); | |
| dsp_description.channels = 0; | |
| dsp_description.read = analyzerCallback; | |
| dsp_description.userdata = this; | |
| result = FMOD_System_CreateDSP(system, &dsp_description, &analyzer); | |
| ERRCHECK(result); | |
| // Create DSP | |
| result = FMOD_Channel_AddDSP(channel, analyzer, 0); | |
| ERRCHECK(result); | |
| } | |
| void ERRCHECK(FMOD_RESULT result){ | |
| if (result != FMOD_OK){ | |
| fprintf(stderr, "FMOD error! (%d) %s\n", result, FMOD_ErrorString(result)); | |
| exit(-1); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef ofxFmodSoundPlayer_h | |
| #define ofxFmodSoundPlayer_h | |
| #include "ofFmodSoundPlayer.h" | |
| class ofxFmodSoundPlayer : public ofFmodSoundPlayer | |
| { | |
| public: | |
| ofxFmodSoundPlayer(); | |
| void play(); | |
| private: | |
| FMOD_DSP *analyzer; | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment