//------------------------------------------------------------ bool ofxSoundFile::mpg123ReadFile(ofSoundBuffer & buffer){ size_t done=0; size_t block_size = mpg123_outblock( mp3File ); int err; if(buffer.size()==0){ buffer.resize(block_size); do{ err = mpg123_read(mp3File,(unsigned char*)&buffer[buffer.size()-block_size],block_size*4,&done); buffer.resize(buffer.size()+block_size); }while(err==MPG123_OK); buffer.resize(buffer.size()-(block_size-done/4)); if(err != MPG123_DONE){ ofLogError() << "Warning: Decoding ended prematurely because: " << (err == MPG123_ERR ? mpg123_strerror(mp3File) : mpg123_plain_strerror(err)); return false; } duration = float(buffer.size())/float(channels) / float(samplerate); }else{ err = mpg123_read(mp3File,(unsigned char*)&buffer[0],buffer.size()*sizeof(float),&done); if(err != MPG123_OK){ ofLogError() << "Warning: Error decoding mp3: " << (err == MPG123_ERR ? mpg123_strerror(mp3File) : mpg123_plain_strerror(err)); return false; } } return true; }
//-------------------------------------------------------------- bool ofxSoundFile::readTo(ofSoundBuffer & buffer, unsigned int _samples){ buffer.setNumChannels(channels); buffer.setSampleRate(samplerate); if(_samples!=0){ // will read the requested number of samples // clamp to the number of samples we actually have _samples = min(_samples,samples); buffer.resize(_samples*channels); } #ifdef OF_USING_SNDFILE else if (sndFile){ // will read entire file buffer.resize(samples); } #endif #ifdef OF_USING_LAD else if (audioDecoder) { // will read entire file buffer.resize(samples); } #endif #ifdef OF_USING_MPG123 else if(mp3File){ buffer.clear(); } #endif #ifdef OF_USING_SNDFILE if(sndFile) return sfReadFile(buffer); #elif defined( OF_USING_MPG123 ) if(mp3File) return mpg123ReadFile(buffer); #elif defined( OF_USING_LAD ) if(audioDecoder) return ladReadFile(buffer); #endif return false; }