FMOD_RESULT F_CALLBACK pcmreadcallback(FMOD_SOUND *sound, void *data, unsigned int datalen) { FMOD_RESULT result; unsigned int read; EnterCriticalSection(&decodecrit); if (!decodesound) { return FMOD_ERR_FILE_EOF; } result = FMOD_Sound_ReadData(decodesound, data, datalen, &read); if (result == FMOD_ERR_FILE_EOF) /* Handle looping. */ { FMOD_Sound_SeekData(decodesound, 0); datalen -= read; result = FMOD_Sound_ReadData(decodesound, (char *)data + read, datalen, &read); } LeaveCriticalSection(&decodecrit); return FMOD_OK; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_RESULT result; 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; } result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, 0); ERRCHECK(result); result = FMOD_System_CreateStream(system, "../media/wave.mp3", FMOD_OPENONLY | FMOD_ACCURATETIME, 0, &sound); ERRCHECK(result); printf("===============================================================================\n"); printf("Offline Decoding Example. Copyright (c) Firelight Technologies 2004-2014.\n"); printf("===============================================================================\n"); printf("\n"); printf("This program will open wave.mp3 and decode it into wave.raw using the\n"); printf("FMOD_Sound_ReadData function.\n"); printf("\n"); /* Decode the sound and write it to a .raw file. */ { void *data; unsigned int length = 0, read; unsigned int bytesread; FILE *outfp; #define CHUNKSIZE 4096 result = FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_PCMBYTES); ERRCHECK(result); outfp = fopen("output.raw", "wb"); if (!outfp) { printf("Error! Could not open output.raw output file.\n"); return 0; } data = malloc(CHUNKSIZE); if (!data) { printf("Error! Failed to allocate %d bytes.\n", CHUNKSIZE); return 0; } bytesread = 0; do { result = FMOD_Sound_ReadData(sound, (char *)data, CHUNKSIZE, &read); fwrite((char *)data, read, 1, outfp); bytesread += read; printf("writing %d bytes of %d to output.raw\r", bytesread, length); } while (result == FMOD_OK && read == CHUNKSIZE); /* Loop terminates when either 1. the read function returns an error. (ie FMOD_ERR_FILE_EOF etc). 2. the amount requested was different to the amount returned. (somehow got an EOF without the file error, maybe a non stream file format like mod/s3m/xm/it/midi). If 'bytesread' is bigger than 'length' then it just means that FMOD miscalculated the size, but this will not usually happen if FMOD_ACCURATETIME is used. (this will give the correct length for VBR formats) */ printf("\n"); if (outfp) { fclose(outfp); } } printf("\n"); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); 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; }