void whitgl_sound_play(int id, float adjust) { int index = -1; int i; for(i=0; i<num_sounds; i++) { if(sounds[i].id == id) { index = i; continue; } } if(index == -1) { WHITGL_LOG("ERR Cannot find sound %d", id); return; } FMOD_RESULT result = FMOD_System_PlaySound(fmodSystem, FMOD_CHANNEL_FREE, sounds[index].sound, true, &sounds[index].channel); _whitgl_sound_errcheck("FMOD_System_PlaySound", result); float defaultFrequency; result = FMOD_Sound_GetDefaults(sounds[index].sound, &defaultFrequency, NULL, NULL, NULL); _whitgl_sound_errcheck("FMOD_Sound_GetDefaults", result); result = FMOD_Channel_SetFrequency(sounds[index].channel, defaultFrequency*adjust); _whitgl_sound_errcheck("FMOD_Channel_SetFrequency", result); result = FMOD_Channel_SetPaused(sounds[index].channel, false); _whitgl_sound_errcheck("FMOD_Channel_SetPaused", result); }
INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priority) { FMOD_SOUND *sound; FMOD_CHANNEL *chan; INT32 i; float frequency; sound = (FMOD_SOUND *)S_sfx[id].data; I_Assert(sound != NULL); FMR(FMOD_System_PlaySound(fsys, FMOD_CHANNEL_FREE, sound, true, &chan)); if (sep == 0) sep = 1; FMR(FMOD_Channel_SetVolume(chan, (vol / 255.0) * (sfx_volume / 31.0))); FMR(FMOD_Channel_SetPan(chan, (sep - 128) / 127.0)); FMR(FMOD_Sound_GetDefaults(sound, &frequency, NULL, NULL, NULL)); FMR(FMOD_Channel_SetFrequency(chan, (pitch / 128.0) * frequency)); FMR(FMOD_Channel_SetPriority(chan, priority)); //UNREFERENCED_PARAMETER(priority); //FMR(FMOD_Channel_SetPriority(chan, 1 + ((0xff-vol)>>1))); // automatic priority 1 - 128 based on volume (priority 0 is music) FMR(FMOD_Channel_GetIndex(chan, &i)); FMR(FMOD_Channel_SetPaused(chan, false)); return i; }
boolean I_SetSongSpeed(float speed) { FMOD_RESULT e; float frequency; if (!music_stream) return false; if (speed > 250.0f) speed = 250.0f; //limit speed up to 250x #ifdef HAVE_LIBGME // Try to set GME speed if (gme) { gme_set_tempo(gme, speed); return true; } #endif // Try to set Mod/Midi speed e = FMOD_Sound_SetMusicSpeed(music_stream, speed); if (e == FMOD_ERR_FORMAT) { // Just change pitch instead for Ogg/etc. FMR(FMOD_Sound_GetDefaults(music_stream, &frequency, NULL, NULL, NULL)); FMR_MUSIC(FMOD_Channel_SetFrequency(music_channel, speed*frequency)); } else FMR_MUSIC(e); return true; }
// seems to never be called on an invalid channel (calls I_SoundIsPlaying first?) // so I'm not gonna worry about it. void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) { FMOD_CHANNEL *chan; FMOD_SOUND *sound; float frequency; FMR(FMOD_System_GetChannel(fsys, handle, &chan)); FMR(FMOD_Channel_SetVolume(chan, (vol / 255.0) * (sfx_volume / 31.0))); FMR(FMOD_Channel_SetPan(chan, (sep - 128) / 127.0)); FMR(FMOD_Channel_GetCurrentSound(chan, &sound)); FMR(FMOD_Sound_GetDefaults(sound, &frequency, NULL, NULL, NULL)); FMR(FMOD_Channel_SetFrequency(chan, (pitch / 128.0) * frequency)); //FMR(FMOD_Channel_SetPriority(chan, 1 + ((0xff-vol)>>1))); // automatic priority 1 - 128 based on volume (priority 0 is music) }
void UAudioCaptureMic::close() { if(_sound) { if(_fp) { // Write back the wav header now that we know its length. int channels, bits; float rate; FMOD_Sound_GetFormat(_sound, 0, 0, &channels, &bits); FMOD_Sound_GetDefaults(_sound, &rate, 0, 0, 0); UWav::writeWavHeader(_fp, _dataLength, rate, channels, bits); fclose(_fp); _fp = 0; if(_encodeToMp3) { #ifdef BUILT_WITH_LAME // Encode to mp3 UMp3Encoder mp3Encoder; // Rename the wav file std::string tempFileName = _fileName; tempFileName.append("TMP"); UFile::rename(_fileName, tempFileName); if(mp3Encoder.encode(tempFileName, _fileName) == 0) { //Erase the wav file UFile::erase(tempFileName); } #endif } } FMOD_RESULT result; result = FMOD_Sound_Release(_sound); UASSERT_MSG(result==FMOD_OK, FMOD_ErrorString(result)); _sound = 0; } if(_fp) { fclose(_fp); _fp = 0; } }
static void open_all_sounds(FMOD_SYSTEM *system, FMOD_SOUND **fmod_sounds) { FMOD_RESULT result; int i; for (i = 0; i < num_sounds; i++) { float freq, vol, pan; int prio; debugmsg("opening sound file %s\n", sounds[i].file); result = FMOD_System_CreateSound(system, (char *)sounds[i].file, FMOD_SOFTWARE, 0, &fmod_sounds[i]); if (result != FMOD_OK) { fprintf(stderr, "Unable to open sound file \"%s\"\n", sounds[i].file); exit(1); } result = FMOD_Sound_SetMode(fmod_sounds[i], FMOD_LOOP_OFF); ERRCHECK(result); result = FMOD_Sound_GetDefaults(fmod_sounds[i], &freq, &vol, &pan, &prio); ERRCHECK(result); /* override defaults (except freq) as requested */ if (sounds[i].vol != USE_DEFAULT) { debugmsg("setting vol value on %s to %f\n", sounds[i].file, sounds[i].vol); vol = sounds[i].vol; } if (sounds[i].pan != USE_DEFAULT) { debugmsg("setting pan value on %s to %f\n", sounds[i].file, sounds[i].pan); pan = sounds[i].pan; } if (sounds[i].prio != USE_DEFAULT_PRIO) { debugmsg("setting prio value on %s to %d\n", sounds[i].file, sounds[i].prio); prio = sounds[i].prio; } result = FMOD_Sound_SetDefaults(fmod_sounds[i], freq, vol, pan, prio); ERRCHECK(result); } }
/* [ [DESCRIPTION] Writes out the contents of a record buffer to a file. [PARAMETERS] [RETURN_VALUE] void [REMARKS] ] */ void WriteWavHeader(FILE *fp, FMOD_SOUND *sound, int length) { int channels, bits; float rate; if (!sound) { return; } fseek(fp, 0, SEEK_SET); FMOD_Sound_GetFormat (sound, 0, 0, &channels, &bits); FMOD_Sound_GetDefaults(sound, &rate, 0, 0, 0); { #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__) #pragma pack(1) #endif /* WAV Structures */ typedef struct { signed char id[4]; int size; } RiffChunk; struct { RiffChunk chunk __PACKED; unsigned short wFormatTag __PACKED; /* format type */ unsigned short nChannels __PACKED; /* number of channels (i.e. mono, stereo...) */ unsigned int nSamplesPerSec __PACKED; /* sample rate */ unsigned int nAvgBytesPerSec __PACKED; /* for buffer estimation */ unsigned short nBlockAlign __PACKED; /* block size of data */ unsigned short wBitsPerSample __PACKED; /* number of bits per sample of mono data */ } FmtChunk = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits } __PACKED; struct { RiffChunk chunk; } DataChunk = { {{'d','a','t','a'}, length } }; struct { RiffChunk chunk; signed char rifftype[4]; } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + length }, {'W','A','V','E'} }; #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__) #pragma pack() #endif /* Write out the WAV header. */ fwrite(&WavHeader, sizeof(WavHeader), 1, fp); fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp); fwrite(&DataChunk, sizeof(DataChunk), 1, fp); } }
/* [ [DESCRIPTION] Writes out the contents of a record buffer to a file. [PARAMETERS] [RETURN_VALUE] void [REMARKS] ] */ void SaveToWav(FMOD_SOUND *sound) { FILE *fp; int channels, bits; float rate; void *ptr1, *ptr2; unsigned int lenbytes, len1, len2; if (!sound) { return; } FMOD_Sound_GetFormat (sound, 0, 0, &channels, &bits); FMOD_Sound_GetDefaults(sound, &rate, 0, 0, 0); FMOD_Sound_GetLength (sound, &lenbytes, FMOD_TIMEUNIT_PCMBYTES); { #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__) #pragma pack(1) #endif /* WAV Structures */ typedef struct { signed char id[4]; int size; } RiffChunk; struct { RiffChunk chunk __PACKED; unsigned short wFormatTag __PACKED; /* format type */ unsigned short nChannels __PACKED; /* number of channels (i.e. mono, stereo...) */ unsigned int nSamplesPerSec __PACKED; /* sample rate */ unsigned int nAvgBytesPerSec __PACKED; /* for buffer estimation */ unsigned short nBlockAlign __PACKED; /* block size of data */ unsigned short wBitsPerSample __PACKED; /* number of bits per sample of mono data */ } __PACKED FmtChunk = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits }; struct { RiffChunk chunk; } DataChunk = { {{'d','a','t','a'}, lenbytes } }; struct { RiffChunk chunk; signed char rifftype[4]; } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} }; #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__) #pragma pack() #endif fp = fopen("record.wav", "wb"); /* Write out the WAV header. */ fwrite(&WavHeader, sizeof(WavHeader), 1, fp); fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp); fwrite(&DataChunk, sizeof(DataChunk), 1, fp); /* Lock the sound to get access to the raw data. */ FMOD_Sound_Lock(sound, 0, lenbytes, &ptr1, &ptr2, &len1, &len2); /* Write it to disk. */ fwrite(ptr1, len1, 1, fp); /* Unlock the sound to allow FMOD to use it again. */ FMOD_Sound_Unlock(sound, ptr1, ptr2, len1, len2); fclose(fp); } }
bool UAudioCaptureMic::init() { this->close(); bool ok = UAudioCapture::init(); if(ok) { std::string::size_type loc; if(_fileName.size()) { loc = _fileName.find( ".mp3", 0 ); if( loc != std::string::npos ) { #ifdef BUILT_WITH_LAME _encodeToMp3 = true; #else _fileName.append(".wav"); UERROR("Cannot write to a mp3, saving to a wav instead (%s)", _fileName.c_str()); #endif } _fp = fopen(_fileName.c_str(), "wb"); } FMOD_RESULT result; FMOD_BOOL isRecording = false; result = UAudioSystem::isRecording(_driver, &isRecording); UASSERT_MSG(result==FMOD_OK, FMOD_ErrorString(result)); if(isRecording) { result = UAudioSystem::recordStop(_driver); UASSERT_MSG(result==FMOD_OK, FMOD_ErrorString(result)); } _dataLength = 0; _soundLength = 0; _lastRecordPos = 0; FMOD_CREATESOUNDEXINFO exinfo; memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); exinfo.numchannels = channels(); if(bytesPerSample() == 1) { exinfo.format = FMOD_SOUND_FORMAT_PCM8; } else if(bytesPerSample() == 2) { exinfo.format = FMOD_SOUND_FORMAT_PCM16; } else if(bytesPerSample() == 3) { exinfo.format = FMOD_SOUND_FORMAT_PCM24; } else if(bytesPerSample() == 4) { exinfo.format = FMOD_SOUND_FORMAT_PCM32; } exinfo.defaultfrequency = (int)fs(); exinfo.length = exinfo.defaultfrequency * bytesPerSample() * exinfo.numchannels * 2; // 2 -> pour deux secondes result = UAudioSystem::createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &_sound); UASSERT_MSG(result==FMOD_OK, FMOD_ErrorString(result)); if(_fp) { int channels, bits; float rate; FMOD_Sound_GetFormat(_sound, 0, 0, &channels, &bits); FMOD_Sound_GetDefaults(_sound, &rate, 0, 0, 0); UWav::writeWavHeader(_fp, _dataLength, rate, channels, bits); // Write out the wav header. La longueur sera de 0 puisqu'elle est incunnue pour l'instant. } result = FMOD_Sound_GetLength(_sound, &_soundLength, FMOD_TIMEUNIT_PCM); UASSERT_MSG(result==FMOD_OK, FMOD_ErrorString(result)); } return ok; }
/* [ [DESCRIPTION] Writes out the contents of a record buffer to a file. [PARAMETERS] [RETURN_VALUE] void [REMARKS] ] */ void SaveToWav(FMOD_SOUND *sound) { FILE *fp; int channels, bits; float rate; void *ptr1, *ptr2; unsigned int lenbytes, len1, len2; FMOD_SOUND_FORMAT format; int count = 0; if (!sound) { return; } FMOD_Sound_GetFormat (sound, 0, &format, &channels, &bits); FMOD_Sound_GetDefaults(sound, &rate, 0, 0, 0); FMOD_Sound_GetLength (sound, &lenbytes, FMOD_TIMEUNIT_PCMBYTES); { /* WAV Structures */ typedef struct { signed char id[4]; int size; } RiffChunk; struct { RiffChunk chunk __PACKED; unsigned short wFormatTag __PACKED; /* format type */ unsigned short nChannels __PACKED; /* number of channels (i.e. mono, stereo...) */ unsigned int nSamplesPerSec __PACKED; /* sample rate */ unsigned int nAvgBytesPerSec __PACKED; /* for buffer estimation */ unsigned short nBlockAlign __PACKED; /* block size of data */ unsigned short wBitsPerSample __PACKED; /* number of bits per sample of mono data */ } __PACKED FmtChunk = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits }; if (format == FMOD_SOUND_FORMAT_PCMFLOAT) { FmtChunk.wFormatTag = 3; } struct { RiffChunk chunk; } DataChunk = { {{'d','a','t','a'}, lenbytes } }; struct { RiffChunk chunk; signed char rifftype[4]; } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} }; #ifdef __BIG_ENDIAN__ /* Do some endian swapping */ FmtChunk.chunk.size = SWAPENDIAN_DWORD(FmtChunk.chunk.size); FmtChunk.wFormatTag = SWAPENDIAN_WORD(FmtChunk.wFormatTag); FmtChunk.nChannels = SWAPENDIAN_WORD(FmtChunk.nChannels); FmtChunk.nSamplesPerSec = SWAPENDIAN_DWORD(FmtChunk.nSamplesPerSec); FmtChunk.nAvgBytesPerSec = SWAPENDIAN_DWORD(FmtChunk.nAvgBytesPerSec); FmtChunk.nBlockAlign = SWAPENDIAN_WORD(FmtChunk.nBlockAlign); FmtChunk.wBitsPerSample = SWAPENDIAN_WORD(FmtChunk.wBitsPerSample); DataChunk.chunk.size = SWAPENDIAN_DWORD(DataChunk.chunk.size); WavHeader.chunk.size = SWAPENDIAN_DWORD(WavHeader.chunk.size); #endif fp = fopen("record.wav", "wb"); /* Write out the WAV header. */ fwrite(&WavHeader, sizeof(WavHeader), 1, fp); fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp); fwrite(&DataChunk, sizeof(DataChunk), 1, fp); /* Lock the sound to get access to the raw data. */ FMOD_Sound_Lock(sound, 0, lenbytes, &ptr1, &ptr2, &len1, &len2); #ifdef __BIG_ENDIAN__ /* Write it to disk. */ if (format == FMOD_SOUND_FORMAT_PCM16) { signed short *wptr = (signed short *)ptr1; for (count = 0; count < len1 >> 1; count++) { wptr[count] = SWAPENDIAN_WORD(wptr[count]); } } else if (format == FMOD_SOUND_FORMAT_PCMFLOAT) { float *fptr = (float *)ptr1; for (count = 0; count < len1 >> 2; count++) { SWAPENDIAN_FLOAT(fptr[count]); } } #endif fwrite(ptr1, len1, 1, fp); /* Unlock the sound to allow FMOD to use it again. */ FMOD_Sound_Unlock(sound, ptr1, ptr2, len1, len2); fclose(fp); } }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key; FMOD_CREATESOUNDEXINFO createsoundexinfo; unsigned int version, decodesound_lengthbytes = 0; int decodesound_channels; float decodesound_rate; /* 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; } result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, 0); ERRCHECK(result); InitializeCriticalSection(&decodecrit); /* First create the 'decoder sound'. Note it is a stream that does not initially read any data, because FMOD_OPENONLY has been specified. We could use createSound instead of createStream but that would allocate memory for the whole sound which is a waste. */ result = FMOD_System_CreateStream(system, "../media/wave.mp3", FMOD_OPENONLY | FMOD_LOOP_NORMAL | FMOD_LOWMEM | FMOD_CREATESTREAM, 0, &decodesound); ERRCHECK(result); result = FMOD_Sound_GetLength(decodesound, &decodesound_lengthbytes, FMOD_TIMEUNIT_PCMBYTES); ERRCHECK(result); result = FMOD_Sound_GetFormat(decodesound, 0, 0, &decodesound_channels, 0); ERRCHECK(result); result = FMOD_Sound_GetDefaults(decodesound, &decodesound_rate, 0, 0, 0); ERRCHECK(result); /* Now create a user created PCM stream that we will feed data into, and play. */ memset(&createsoundexinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); createsoundexinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); /* required. */ createsoundexinfo.decodebuffersize = 44100; /* Chunk size of stream update in samples. This will be the amount of data passed to the user callback. */ createsoundexinfo.numchannels = decodesound_channels; /* Number of channels in the sound. */ createsoundexinfo.length = decodesound_lengthbytes; /* Length of PCM data in bytes of whole song. -1 = infinite. */ createsoundexinfo.defaultfrequency = (int)decodesound_rate; /* Default playback rate of sound. */ createsoundexinfo.format = FMOD_SOUND_FORMAT_PCM16; /* Data format of sound. */ createsoundexinfo.pcmreadcallback = pcmreadcallback; /* User callback for reading. */ createsoundexinfo.pcmsetposcallback = pcmsetposcallback; /* User callback for seeking. */ result = FMOD_System_CreateStream(system, 0, FMOD_2D | FMOD_OPENUSER | FMOD_LOOP_NORMAL, &createsoundexinfo, &sound); ERRCHECK(result); printf("============================================================================\n"); printf("Manual Decode example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("============================================================================\n"); printf("Sound played here decoded in realtime by the user with a 'decoder sound' \n"); printf("The mp3 is created as a stream opened with FMOD_OPENONLY. This is the \n"); printf("'decoder sound'. The playback sound is a 16bit PCM FMOD_OPENUSER created \n"); printf("sound with a pcm read callback. When the callback happens, we call readData\n"); printf("on the decoder sound and use the pcmreadcallback data pointer as the parameter.\n"); printf("============================================================================\n"); printf("\n"); printf("Press space to pause, Esc to quit\n"); printf("Press '<' to rewind 1 second.\n"); printf("Press '>' to fast forward 1 second.\n"); printf("\n"); /* Play the sound. */ result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &channel); ERRCHECK(result); /* Main loop. */ do { if (_kbhit()) { key = _getch(); switch (key) { case ' ' : { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); break; } case '<' : { unsigned int position; FMOD_Channel_GetPosition(channel, &position, FMOD_TIMEUNIT_MS); if (position >= 1000) { position -= 1000; } FMOD_Channel_SetPosition(channel, position, FMOD_TIMEUNIT_MS); break; } case '>' : { unsigned int position; FMOD_Channel_GetPosition(channel, &position, FMOD_TIMEUNIT_MS); position += 1000; FMOD_Channel_SetPosition(channel, position, FMOD_TIMEUNIT_MS); break; } } } FMOD_System_Update(system); if (channel) { unsigned int ms; unsigned int lenms; int playing; int paused; FMOD_Channel_IsPlaying(channel, &playing); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Channel_GetPaused(channel, &paused); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Sound_GetLength(sound, &lenms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped"); } Sleep(20); } while (key != 27); printf("\n"); EnterCriticalSection(&decodecrit); { /* Remove the sound - wait! it might be still in use! Instead of releasing the decode sound first we could release it last, but this protection is here to make the issue obvious. */ result = FMOD_Sound_Release(decodesound); ERRCHECK(result); decodesound = 0; /* This will make the read callback fail from now on. */ } LeaveCriticalSection(&decodecrit); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); DeleteCriticalSection(&decodecrit); return 0; }
float JiwokFMODWrapper::GetBPM(const char *filename) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_RESULT result; unsigned int version; 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; } result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, 0); ERRCHECK(result); result = FMOD_System_CreateStream(system,filename, FMOD_OPENONLY | FMOD_ACCURATETIME, 0, &sound); // ERRCHECK(result); unsigned int length = 0; int channels = 0, bits = 0; float frequency = 0; float volume = 0, pan = 0; int priority = 0; FMOD_SOUND_TYPE stype; FMOD_SOUND_FORMAT format; result = FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_PCMBYTES); ERRCHECK(result); result = FMOD_Sound_GetDefaults(sound, &frequency, &volume, &pan, &priority); ERRCHECK(result); result = FMOD_Sound_GetFormat(sound, &stype, &format, &channels, &bits); ERRCHECK(result); printf("result is %d",result); soundtouch::BPMDetect *bpm = new soundtouch::BPMDetect(channels,frequency); //void *data; //unsigned int read; unsigned int bytesread; #define CHUNKSIZE 32768 //4096 // #define CHUNKSIZE 4096 bytesread = 0; soundtouch::SAMPLETYPE* samples = new soundtouch::SAMPLETYPE[CHUNKSIZE]; int sbytes = bits / 8; const unsigned int NUMSAMPLES = CHUNKSIZE; unsigned int bytes = NUMSAMPLES * sbytes; unsigned int readbytes = 0; do { readbytes = 0; if(sbytes == 2) { long int data[32768]; result = FMOD_Sound_ReadData(sound, data, bytes, &readbytes ); if(!result == FMOD_OK) break; for ( unsigned int i = 0; i < readbytes/sbytes; ++i ) samples[i] = (float) data[i] / 32768; } else if(sbytes == 1) { long int data[32768]; result = FMOD_Sound_ReadData(sound, data, bytes, &readbytes ); if(!result == FMOD_OK) break; for ( unsigned int i = 0; i < (readbytes); ++i ) samples[i] = (float) data[i] / 128; } bpm->inputSamples(samples, (readbytes /sbytes)/ channels ); bytesread += readbytes; } while (result == FMOD_OK && readbytes == CHUNKSIZE*2); result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); float bpmg = bpm->getBpm(); float bpm1 = bpmg; if ( bpmg < 1 ) return 0.; while ( bpmg > 190 ) bpmg /= 2.; while ( bpmg < 50 ) bpmg *= 2.; printf("bpmg bpmg is %f bpm %f",bpmg,bpm1); return bpmg; }