void SSVisualizer::UpdateUserLayer( const float deltaTime ) { // Perform non-simulation update logic here (Don't forget to set update order priority!) FMOD_System_Update(m_SoundSystem); //Get spectrum data PROFILE(AutoProfiler memAllocProfiler("VisualizerAllocation", Profiler::PROFILER_CATEGORY_STANDARD, true, true)); #ifdef USE_STACK_ALLOC float* leftSpectrum = (float*)m_Allocator->Allocate(sizeof(float) * SPECTRUM_SIZE); float* rightSpectrum = (float*)m_Allocator->Allocate(sizeof(float) * SPECTRUM_SIZE); Particle2* particles = (Particle2*)m_Allocator->Allocate(sizeof(Particle2) * SPECTRUM_SIZE); #else float* leftSpectrum = (float*)malloc(sizeof(float) * SPECTRUM_SIZE); float* rightSpectrum = (float*)malloc(sizeof(float) * SPECTRUM_SIZE); Particle2* particles = (Particle2*)malloc(sizeof(Particle2) * SPECTRUM_SIZE); #endif PROFILE(memAllocProfiler.Stop()); FMOD_Channel_GetSpectrum(m_Channel, leftSpectrum, SPECTRUM_SIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE); FMOD_Channel_GetSpectrum(m_Channel, rightSpectrum, SPECTRUM_SIZE, 1, FMOD_DSP_FFT_WINDOW_TRIANGLE); int res = 8; float maxH; for (int i = 0; i < SPECTRUM_SIZE / res; ++i) { float h = -0.5f + glm::max((rightSpectrum[i] + rightSpectrum[i]) * 5.0f - 0.04f, 0.0f) + 0.015f; maxH = glm::max(h + 0.5f, maxH); for (int k = 0; k < res; k++) { int index = i * res + k; particles[index].Pos = glm::vec4((index / (float)SPECTRUM_SIZE) * 2.0f - 1.0f, h, 0.1f, 1); particles[index].Color = m_Color; } } maxH = glm::max(maxH, 0.0f); m_Color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f) * maxH + glm::vec4(0.0f, 0.0f, 1.0f, 1.0f) * (1.0f - maxH); glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Particle2) * SPECTRUM_SIZE, particles); m_RenderProgram.Apply(); glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBindVertexArray(m_VAO); glDrawArrays(GL_LINE_STRIP, 0, SPECTRUM_SIZE); PROFILE(AutoProfiler memDellocProfiler("VisualizerDeallocation", Profiler::PROFILER_CATEGORY_STANDARD, true, true)); #ifdef USE_STACK_ALLOC m_Allocator->Unwind(m_Marker); #else free(leftSpectrum); free(rightSpectrum); free(particles); #endif PROFILE(memDellocProfiler.Stop()); }
void CubeAnim::update() { FMOD_System_GetChannel(system, 0, &canal); FMOD_Channel_GetSpectrum(canal, spectre, TAILLE_SPECTRE, 0, FMOD_DSP_FFT_WINDOW_RECT); for (size_t i = 0; i < _objects.size(); ++i) _objects[i]->translate(vec3(0, getEquation(i), 0)); }
void spectreSong (float spectum[512]) { FMOD_CHANNEL* channel; channel = getChannel (); FMOD_Channel_GetSpectrum(channel, spectum, 512, 0, FMOD_DSP_FFT_WINDOW_RECT); }
FMOD_RESULT bmx_FMOD_Channel_GetSpectrum(MAX_FMOD_CHANNEL *channel, BBArray * spectrumArray, int channelOffset, FMOD_DSP_FFT_WINDOW windowType) { int size = spectrumArray->scales[0]; float arr[size]; FMOD_RESULT res = FMOD_Channel_GetSpectrum(channel->channel, &arr[0], size, channelOffset, windowType); float *s=(float*)BBARRAYDATA( spectrumArray, spectrumArray->dims ); for (int i = 0; i < size; i ++) { s[i] = arr[i]; } return res; }
//FMOD Thread void getSpectrum(void *fm) { FMOD_SYSTEM *fmod = (FMOD_SYSTEM*)fm; FMOD_CHANNEL *channel = 0; FMOD_SOUND *sound = 0; FMOD_CREATESOUNDEXINFO exinfo; int numdrivers, selected=-1;//for stereo mix int count;//stereo mix char name[256]; //stereo mix float t[SPECTRUMSIZE/8]; int j = 0; int i = 0; float max=0; FMOD_RESULT result; static float spectrum[SPECTRUMSIZE]; result = FMOD_System_SetOutput(fmod, FMOD_OUTPUTTYPE_DSOUND); //output=directsound //get stereo mix result = FMOD_System_GetRecordNumDrivers(fmod, &numdrivers); for (count=0; count < numdrivers; count++) { result = FMOD_System_GetRecordDriverInfo(fmod, count, name, 256, 0); if(strncmp(name, "Stereo Mix", strlen("Stereo Mix"))==0) { selected=count; } } if(selected == -1) { printf("Couldn't find Stereo Mix!\n"); exit(-1); } //create a sound result = FMOD_System_SetSoftwareFormat(fmod, OUTPUTRATE, FMOD_SOUND_FORMAT_PCM16, 1, 0, FMOD_DSP_RESAMPLER_LINEAR); result = FMOD_System_Init(fmod, 100, FMOD_INIT_NORMAL, 0); memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); exinfo.numchannels = 1; exinfo.format = FMOD_SOUND_FORMAT_PCM16; exinfo.defaultfrequency = OUTPUTRATE; exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5; result = FMOD_System_CreateSound(fmod, 0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound); result = FMOD_System_RecordStart(fmod, selected, sound, TRUE); Sleep(200); result = FMOD_System_PlaySound(fmod, FMOD_CHANNEL_REUSE, sound, FALSE, &channel); result = FMOD_Channel_SetVolume(channel, 0); do { result = FMOD_Channel_GetSpectrum(channel, spectrum, SPECTRUMSIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE); for(i = 0;i<8000;i++) { t[j]+=(10.0f * (float)log10(spectrum[i]) * 2.0f); if(i<=1600) j=0; if(i>1600)j=1; if(i>3200)j=2; if(i>4800)j=3; if(i>6400)j=4; } max=0; WaitForSingleObject(spectrumMutex, INFINITE); for(i =0;i<5;i++) { t[i]=t[i]/1600+150; analyzedSpectrum[i] = t[i]; if(t[i] >max) {max=t[i];} } ReleaseMutex(spectrumMutex); FMOD_System_Update(fmod); Sleep(10); WaitForSingleObject(stopAppMutex, INFINITE); if(_stopApp==TRUE) {break;} ReleaseMutex(stopAppMutex); }while(1); _endthread(); }
// Updates the frequency spectrum to the current audio frequencies void SoundManager::update() { FMOD_Channel_GetSpectrum(mChannel, mSpectrum, mSpectrumSize, 0, FMOD_DSP_FFT_WINDOW_RECT); }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system = 0; FMOD_SOUND *sound = 0; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; FMOD_CREATESOUNDEXINFO exinfo; int key, driver, recorddriver, numdrivers, count, bin; unsigned int version; /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } /* System initialization */ printf("---------------------------------------------------------\n"); printf("Select OUTPUT type\n"); printf("---------------------------------------------------------\n"); printf("1 : DirectSound\n"); printf("2 : Windows Multimedia WaveOut\n"); printf("3 : ASIO\n"); printf("---------------------------------------------------------\n"); printf("Press a corresponding number or ESC to quit\n"); do { key = _getch(); } while (key != 27 && key < '1' && key > '5'); switch (key) { case '1' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_DSOUND); break; case '2' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_WINMM); break; case '3' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_ASIO); break; default : return 1; } ERRCHECK(result); /* Enumerate playback devices */ result = FMOD_System_GetNumDrivers(system, &numdrivers); ERRCHECK(result); printf("---------------------------------------------------------\n"); printf("Choose a PLAYBACK driver\n"); printf("---------------------------------------------------------\n"); for (count=0; count < numdrivers; count++) { char name[256]; result = FMOD_System_GetDriverInfo(system, count, name, 256, 0); ERRCHECK(result); printf("%d : %s\n", count + 1, name); } printf("---------------------------------------------------------\n"); printf("Press a corresponding number or ESC to quit\n"); do { key = _getch(); if (key == 27) { return 0; } driver = key - '1'; } while (driver < 0 || driver >= numdrivers); result = FMOD_System_SetDriver(system, driver); ERRCHECK(result); /* Enumerate record devices */ result = FMOD_System_GetRecordNumDrivers(system, &numdrivers); ERRCHECK(result); printf("---------------------------------------------------------\n"); printf("Choose a RECORD driver\n"); printf("---------------------------------------------------------\n"); for (count=0; count < numdrivers; count++) { char name[256]; result = FMOD_System_GetRecordDriverInfo(system, count, name, 256, 0); ERRCHECK(result); printf("%d : %s\n", count + 1, name); } printf("---------------------------------------------------------\n"); printf("Press a corresponding number or ESC to quit\n"); recorddriver = 0; do { key = _getch(); if (key == 27) { return 0; } recorddriver = key - '1'; } while (recorddriver < 0 || recorddriver >= numdrivers); printf("\n"); result = FMOD_System_SetSoftwareFormat(system, OUTPUTRATE, FMOD_SOUND_FORMAT_PCM16, 1, 0, FMOD_DSP_RESAMPLER_LINEAR); ERRCHECK(result); result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); /* Create a sound to record to. */ memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); exinfo.numchannels = 1; exinfo.format = FMOD_SOUND_FORMAT_PCM16; exinfo.defaultfrequency = OUTPUTRATE; exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5; result = FMOD_System_CreateSound(system, 0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound); ERRCHECK(result); /* Start the interface */ printf("=========================================================================\n"); printf("Pitch detection example. Copyright (c) Firelight Technologies 2004-2015.\n"); printf("=========================================================================\n"); printf("\n"); printf("Record something through the selected recording device and FMOD will\n"); printf("Determine the pitch. Sustain the tone for at least a second to get an\n"); printf("accurate reading.\n"); printf("Press 'Esc' to quit\n"); printf("\n"); result = FMOD_System_RecordStart(system, recorddriver, sound, TRUE); ERRCHECK(result); Sleep(200); /* Give it some time to record something */ result = FMOD_System_PlaySound(system, FMOD_CHANNEL_REUSE, sound, FALSE, &channel); ERRCHECK(result); /* Dont hear what is being recorded otherwise it will feedback. Spectrum analysis is done before volume scaling in the DSP chain */ result = FMOD_Channel_SetVolume(channel, 0); ERRCHECK(result); bin = 0; /* Main loop. */ do { static float spectrum[SPECTRUMSIZE]; float dominanthz = 0; float max; int dominantnote = 0; float binsize = BINSIZE; if (_kbhit()) { key = _getch(); } result = FMOD_Channel_GetSpectrum(channel, spectrum, SPECTRUMSIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE); ERRCHECK(result); max = 0; for (count = 0; count < SPECTRUMSIZE; count++) { if (spectrum[count] > 0.01f && spectrum[count] > max) { max = spectrum[count]; bin = count; } } dominanthz = (float)bin * BINSIZE; /* dominant frequency min */ dominantnote = 0; for (count = 0; count < 120; count++) { if (dominanthz >= notefreq[count] && dominanthz < notefreq[count + 1]) { /* which is it closer to. This note or the next note */ if (fabs(dominanthz - notefreq[count]) < fabs(dominanthz - notefreq[count+1])) { dominantnote = count; } else { dominantnote = count + 1; } break; } } printf("Detected rate : %7.1f -> %7.1f hz. Detected musical note. %-3s (%7.1f hz)\r", dominanthz, ((float)bin + 0.99f) * BINSIZE, note[dominantnote], notefreq[dominantnote]); FMOD_System_Update(system); Sleep(10); } while (key != 27); printf("\n"); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
//This analyzes the spectrum of the music FMODs own features void sound_system_c::get_spectrum(float *spectrumL, float *spectrumR) const { fmod_errorcheck(FMOD_Channel_GetSpectrum(channel, spectrumL, SPECTRUMSIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE)); fmod_errorcheck(FMOD_Channel_GetSpectrum(channel, spectrumR, SPECTRUMSIZE, 1, FMOD_DSP_FFT_WINDOW_TRIANGLE)); }