Mpg123Input::Mpg123Input(const QString &fileName) { handle=new Handle; QByteArray fName=QFile::encodeName(fileName); bool ok=false; int result; handle->mpg123 = mpg123_new(NULL, &result); if (handle->mpg123 && MPG123_OK==mpg123_open(handle->mpg123, fName.constData()) && MPG123_OK==mpg123_getformat(handle->mpg123, &handle->rate, &handle->channels, &handle->encoding) && MPG123_OK==mpg123_format_none(handle->mpg123) && MPG123_OK==mpg123_format(handle->mpg123, handle->rate, handle->channels, MPG123_ENC_FLOAT_32)) { mpg123_close(handle->mpg123); if (MPG123_OK==mpg123_open(handle->mpg123, fName.constData()) && MPG123_OK==mpg123_getformat(handle->mpg123, &handle->rate, &handle->channels, &handle->encoding)) { ok=true; } } if (!ok) { if (handle->mpg123) { mpg123_close(handle->mpg123); mpg123_delete(handle->mpg123); handle->mpg123 = NULL; } delete handle; handle = 0; } }
VALUE rb_mpg123_new(VALUE klass, VALUE filename, VALUE decideRate) { printf("Made it: 1\n"); int err = MPG123_OK; mpg123_handle *mh; printf("mh: "); printf("%d", mh); printf("\n"); VALUE mpg123; long rate; rate = decideRate; printf("rate: "); printf("%d", rate); printf("\n"); int channels, encoding; printf("Made it: 2\n"); printf("rate: "); printf("%d", rate); printf("\n"); Check_Type(filename, T_STRING); printf("Made it: 3\n"); printf("rate: "); printf("%d", rate); printf("\n"); if ((mh = mpg123_new(NULL, &err)) == NULL) { rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } printf("Made it: 4\n"); printf("rate: "); printf("%d", rate); printf("\n"); mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); printf("mpg123_open is: "); printf("%d", mpg123_open(mh, (char*) RSTRING_PTR(filename))); printf("\n"); printf("mh: "); printf("%d", mh); printf("\n"); printf("rate: "); printf("%d", rate); printf("\n"); printf("channels: "); printf("%d", channels); printf("\n"); printf("encoding: "); printf("%d", encoding); printf("\n"); printf("mpg123_getformat is: "); printf("%d", mpg123_getformat(mh, &rate, &channels, &encoding)); printf("\n"); printf("MPG123_OK is: "); printf("%d", MPG123_OK); printf("\n"); if (mpg123_open(mh, (char*) RSTRING_PTR(filename)) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { rb_raise(rb_eStandardError, "%s", mpg123_strerror(mh)); } printf("Made it: 5"); if (encoding != MPG123_ENC_FLOAT_32) { rb_raise(rb_eStandardError, "bad encoding"); } printf("Made it: 6"); mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); printf("Made it: 7"); return Data_Wrap_Struct(rb_cMpg123, 0, cleanup, mh); }
void * in_mp3_open(char *file) { mpg123_handle *m; int channels, encoding, r; long rate; channels = encoding = 0; rate = 0; m = mpg123_new(NULL, &r); assert(m != NULL); /* verbosity */ mpg123_param(m, MPG123_VERBOSE, 1, 0); r = mpg123_open(m, file); if (r != MPG123_OK) return NULL; assert(m != NULL); assert(mpg123_getformat(m, &rate, &channels, &encoding) == MPG123_OK); mpg123_format_none(m); mpg123_format(m, rate, channels, encoding); return m; }
int test_whence(const char* path, int scan_before) { int err = MPG123_OK; mpg123_handle* mh = NULL; off_t length, pos; mh = mpg123_new(NULL, &err ); if(mh == NULL) return -1; err = mpg123_open(mh, path ); if(err != MPG123_OK) return -1; if(scan_before) mpg123_scan(mh); pos = mpg123_seek( mh, 0, SEEK_END); if(pos < 0){ error1("seek failed: %s", mpg123_strerror(mh)); return -1; } pos = mpg123_tell(mh); length = mpg123_length(mh); /* Later: Read samples and compare different whence values with identical seek positions. */ mpg123_close(mh); mpg123_delete(mh); fprintf(stdout, "length %"OFF_P" vs. pos %"OFF_P"\n", length, pos); return (pos == length) ? 0 : -1; }
bool ofxSoundFile::mpg123Open(string path){ int err; mp3File = mpg123_new(NULL,&err); mpg123_param(mp3File, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); if(mpg123_open(mp3File,path.c_str())!=MPG123_OK){ ofLogError() << "ofxSoundFile: couldnt read " + path; return false; } int encoding; long int rate; mpg123_getformat(mp3File,&rate,&channels,&encoding); if(encoding!=MPG123_ENC_SIGNED_16 && encoding != MPG123_ENC_FLOAT_32){ ofLogError() << "ofxSoundFile: unsupported encoding"; return false; } mpg123_format_none(mp3File); mpg123_format(mp3File, rate, channels, encoding); samplerate = rate; mpg123_seek(mp3File,0,SEEK_END); samples = mpg123_tell(mp3File); mpg123_seek(mp3File,0,SEEK_SET); bitDepth = 16; //TODO:get real bitdepth;. }
void MusicPlayer::play(const char *filename) { #ifdef USE_OGG stb_vorbis_close(v); stb_vorbis_alloc t; t.alloc_buffer=this->buf; t.alloc_buffer_length_in_bytes=STB_BUFSIZE; // stb_vorbis has char* pointer even though it is not really writing to buffer v = stb_vorbis_open_filename((char *)filename, &error, &t); #endif #ifdef USE_MP3 mpg123_close(mh); error = mpg123_open(mh, filename); if (!error) { error = mpg123_getformat(mh, &rate, &channels, &encoding); rate = AUDIO_FREQUENCY; channels = 2; mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); } #endif }
void input_mpg123::open(std::string path) { int err; int encoding; handle = mpg123_new(NULL, &err); if (handle == NULL) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_open(handle, path.c_str())) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_getformat(handle, &_rate, &_channels, &encoding)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } read_metadata(); /* force 16 bit format */ encoding = MPG123_ENC_SIGNED_16; mpg123_format_none(handle); mpg123_format(handle, _rate, _channels, encoding); double seconds_left; if ((err = mpg123_position(handle, 0, 0, NULL, NULL, NULL, &seconds_left)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } this->_encoding = encoding; this->_length = seconds_left; }
MP3Decoder::MP3Decoder(const char* filename) { f = fopen(filename,"rb"); fseek(f,0,SEEK_END); size_t size = ftell(f); cbuf = (uchar*)malloc(size*24); size_t buffer_size = 0; size_t done = 0; rate = 0; channels = 0; encoding = 0; int err = MPG123_OK; err = mpg123_init(); if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL) { QMessageBox::critical(0,QString("MP3 decoding error!"),QString("Basic setup goes wrong: ")+QString(mpg123_plain_strerror(err))); cleanup(mh); return ; } /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */ if( mpg123_open(mh, filename) != MPG123_OK /* Peek into track and get first output format. */ || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { QMessageBox::critical(0,QString("MP3 decoding error!"),QString("Error decoding mp3 file: ")+QString(mpg123_strerror(mh))); cleanup(mh); return ; } if(encoding != MPG123_ENC_SIGNED_16 && encoding != MPG123_ENC_FLOAT_32) { /* Signed 16 is the default output format anyways; it would actually by only different if we forced it. So this check is here just for this explanation. */ cleanup(mh); QMessageBox::critical(0,QString("MP3 decoding error!"),"Bad encoding: 0x"+QString::number(encoding,16)); return; } /* Ensure that this output format will not change (it could, when we allow it). */ mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); buffer_size = mpg123_outblock(mh); cdone=0; buffer = (unsigned char*)malloc( buffer_size ); do { err = mpg123_read( mh, buffer, buffer_size, &done ); if(done<1)qDebug()<<"Error while reading mp3: "<<mpg123_errcode(mh); else memcpy(cbuf+cdone,buffer,done); cdone+=done; } while (err==MPG123_OK); cbuf[cdone-1]=0; if(err != MPG123_DONE){ fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n", err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) ); QMessageBox::critical(0,QString("MP3 decoding error!"),QString("MP3 Decoding ended prematurely: %1").arg(err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) )); return; } }
//------------------------------------------------------------ bool ofOpenALSoundPlayer_TimelineAdditions::mpg123ReadFile(string path,vector<short> & buffer,vector<float> & fftAuxBuffer){ int err = MPG123_OK; mpg123_handle * f = mpg123_new(NULL,&err); if(mpg123_open(f,path.c_str())!=MPG123_OK){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read " + path); return false; } int encoding; long int rate; mpg123_getformat(f,&rate,&channels,&encoding); if(encoding!=MPG123_ENC_SIGNED_16){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: unsupported encoding"); return false; } samplerate = rate; size_t done=0; size_t buffer_size = mpg123_outblock( f ); buffer.resize(buffer_size/2); while(mpg123_read(f,(unsigned char*)&buffer[buffer.size()-buffer_size/2],buffer_size,&done)!=MPG123_DONE){ buffer.resize(buffer.size()+buffer_size/2); }; buffer.resize(buffer.size()-(buffer_size/2-done/2)); mpg123_close(f); mpg123_delete(f); fftAuxBuffer.resize(buffer.size()); for(int i=0;i<(int)buffer.size();i++){ fftAuxBuffer[i] = float(buffer[i])/32565.f; } duration = float(buffer.size()/channels) / float(samplerate); return true; }
//------------------------------------------------------------ bool ofOpenALSoundPlayer::mpg123ReadFile(string path,vector<short> & buffer,vector<float> & fftAuxBuffer){ int err = MPG123_OK; mpg123_handle * f = mpg123_new(NULL,&err); if(mpg123_open(f,path.c_str())!=MPG123_OK){ ofLogError("ofOpenALSoundPlayer") << "mpg123ReadFile(): couldn't read \"" << path << "\""; return false; } mpg123_enc_enum encoding; long int rate; mpg123_getformat(f,&rate,&channels,(int*)&encoding); if(encoding!=MPG123_ENC_SIGNED_16){ ofLogError("ofOpenALSoundPlayer") << "mpg123ReadFile(): " << getMpg123EncodingString(encoding) << " encoding for \"" << path << "\"" << " unsupported, expecting MPG123_ENC_SIGNED_16"; return false; } samplerate = rate; size_t done=0; size_t buffer_size = mpg123_outblock( f ); buffer.resize(buffer_size/2); while(mpg123_read(f,(unsigned char*)&buffer[buffer.size()-buffer_size/2],buffer_size,&done)!=MPG123_DONE){ buffer.resize(buffer.size()+buffer_size/2); }; buffer.resize(buffer.size()-(buffer_size/2-done/2)); mpg123_close(f); mpg123_delete(f); fftAuxBuffer.resize(buffer.size()); for(int i=0;i<(int)buffer.size();i++){ fftAuxBuffer[i] = float(buffer[i])/32565.f; } duration = float(buffer.size()/channels) / float(samplerate); return true; }
VALUE rb_mpg123_new(VALUE klass, VALUE filename) { int err = MPG123_OK; mpg123_handle *mh; VALUE mpg123; long rate; int channels, encoding; Check_Type(filename, T_STRING); if ((mh = mpg123_new(NULL, &err)) == NULL) { rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); if (mpg123_open(mh, (char*) RSTRING_PTR(filename)) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { rb_raise(rb_eStandardError, "%s", mpg123_strerror(mh)); } if (encoding != MPG123_ENC_FLOAT_32) { rb_raise(rb_eStandardError, "bad encoding"); } mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); VALUE new_mpg123 = Data_Wrap_Struct(rb_cMpg123, 0, cleanup, mh); rb_iv_set(new_mpg123, "@file", filename); return new_mpg123; }
int main(int argc, char **argv) { mpg123_handle *m; int i; if(argc < 2) { fprintf(stderr, "\nI will give you the estimated and exact sample lengths of MPEG audio files.\n"); fprintf(stderr, "\nUsage: %s <mpeg audio file list>\n\n", argv[0]); return -1; } mpg123_init(); m = mpg123_new(NULL, NULL); mpg123_param(m, MPG123_RESYNC_LIMIT, -1, 0); /* New in library version 0.0.1 . */ for(i = 1; i < argc; ++i) { off_t a, b; mpg123_open(m, argv[i]); a = mpg123_length(m); mpg123_scan(m); b = mpg123_length(m); mpg123_close(m); printf("File %i: estimated %li vs. scanned %li\n", i, (long)a, (long)b); } mpg123_delete(m); mpg123_exit(); return 0; }
JNIEXPORT jlong JNICALL Java_de_mpg123_MPG123Decoder_checkFormat (JNIEnv *env, jobject self, jstring path_to_file) { // Error code int err = MPG123_OK; // Properties int channels; long rate; long num_samples; int encoding; size_t buffer_size; mpg123_handle* mh; // Create new mpg123 handle mh = mpg123_new(NULL, &err); __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "mpg123_new: %p", mh); if (err == MPG123_OK && mh != NULL) { // Get the utf-8 string const char *file_path = env->GetStringUTFChars(path_to_file, JNI_FALSE); __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Sound file path: %s", file_path); err = mpg123_open(mh, file_path); if (err == MPG123_OK) { err = mpg123_getformat(mh, &rate, &channels, &encoding); if (err == MPG123_OK) { return MPG123_OK; } else { __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Error: mpg123_getformat err: %i", err); __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Trouble with mpg123: %s", mpg123_strerror(mh)); return MPG123_ERR; } } else { __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Error: mpg123_open err: %i", err); return MPG123_ERR; } } __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Error: no proper initialization of mpg123lib."); return MPG123_ERR; }
void mpgplayer::run() { if(mpg123_init() != MPG123_OK) qDebug("Error initilizing mpg123"); const char **test = mpg123_supported_decoders(); int error; mpg123_handle *mh = mpg123_new(test[0],&error); if(!mpg123_feature(MPG123_FEATURE_DECODE_LAYER3)) { qDebug("You do not seem to have mp3 decoding support"); return; } mpg123_format_none(mh); if(mpg123_format(mh,samplerate,MPG123_STEREO,MPG123_ENC_SIGNED_16)!=MPG123_OK) qDebug("Error in initilizing format decoder"); qDebug(test[0]); mpg123_open(mh,"/home/eli/Projects/groove-evan/Animal.mp3"); net = TData; pa_simple *s; pa_sample_spec ss; ss.format = PA_SAMPLE_S16NE; ss.rate = samplerate; ss.channels = 2; s =pa_simple_new(NULL,"Groove",PA_STREAM_PLAYBACK ,NULL,"Music",&ss,NULL,NULL,NULL); unsigned char bytes[1024]; size_t bsize = 1024; size_t done = 0; bool stop = false; playing=true; while(!stop) { switch(net) { case TWait: usleep(100); break; case TData: if(mpg123_read(mh,bytes,bsize,&done)==MPG123_DONE) { net=TFinish; } pa_simple_write(s,bytes,done,&error); break; case TAbort: stop = true; break; case TFinish: pa_simple_drain(s,&error); stop = true; break; default: break; } } qDebug("Finsihed playback"); pa_simple_free(s); mpg123_exit(); }
void mp3_init(char *mp3_file_0, char *mp3_file_1) { int err; int channels, encoding; long rate; mpg123_init(); mh_0 = mpg123_new(NULL, &err); mp3_buffer_size = mpg123_outblock(mh_0); mp3_buffer = malloc(mp3_buffer_size * sizeof(unsigned char)); mpg123_open(mh_0, mp3_file_0); mpg123_getformat(mh_0, &rate, &channels, &encoding); printf("0: %s: rate: %ld channels: %d encoding: %d\n", mp3_file_0, rate, channels, encoding); memset(&mp3_format, 0, sizeof(mp3_format)); mp3_format.bits = mpg123_encsize(encoding) * 8; mp3_format.channels = channels; mp3_format.rate = rate; // -- Open driver -- // mp3_device_0 = ao_open_live(ao_default_driver_id(), &mp3_format, NULL); // no options if (mp3_device_0 == NULL) { fprintf(stderr, "Error opening device 0 .\n"); } mh_1 = mpg123_new(NULL, &err); mpg123_open(mh_1, mp3_file_1); mpg123_getformat(mh_1, &rate, &channels, &encoding); printf("1: %s: rate: %ld channels: %d encoding: %d\n", mp3_file_1, rate, channels, encoding); memset(&mp3_format, 0, sizeof(mp3_format)); mp3_format.bits = mpg123_encsize(encoding) * 8; mp3_format.channels = channels; mp3_format.rate = rate; // -- Open driver -- // mp3_device_1 = ao_open_live(ao_default_driver_id(), &mp3_format, NULL); // no options if (mp3_device_1 == NULL) { fprintf(stderr, "Error opening device 1.\n"); } }
JNIEXPORT jlong JNICALL Java_com_axelby_podax_player_MPG123_openFile (JNIEnv *env, jclass c, jstring filename) { int err = MPG123_OK; mpg123_handle *mh = mpg123_new(NULL, &err); if (err == MPG123_OK && mh != NULL) { MP3File* mp3 = mp3file_init(mh); const char* fileString = (*env)->GetStringUTFChars(env, filename, NULL); err = mpg123_open(mh, fileString); (*env)->ReleaseStringUTFChars(env, filename, fileString); if (err == MPG123_OK) { int encoding; if (mpg123_getformat(mh, &mp3->rate, &mp3->channels, &encoding) == MPG123_OK) { if(encoding == MPG123_ENC_SIGNED_16) { // Signed 16 is the default output format anyway; // it would actually by only different if we forced it. // So this check is here just for this explanation. // Ensure that this output format will not change // (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, mp3->rate, mp3->channels, encoding); mp3->buffer_size = mpg123_outblock(mh); mp3->buffer = (unsigned char*)malloc(mp3->buffer_size); mp3->num_samples = mpg123_length(mh); mp3->samples_per_frame = mpg123_spf(mh); mp3->secs_per_frame = mpg123_tpf(mh); if (mp3->num_samples == MPG123_ERR || mp3->samples_per_frame < 0) mp3->num_frames = 0; else mp3->num_frames = mp3->num_samples / mp3->samples_per_frame; if (mp3->num_samples == MPG123_ERR || mp3->samples_per_frame < 0 || mp3->secs_per_frame < 0) mp3->duration = 0; else mp3->duration = mp3->num_samples / mp3->samples_per_frame * mp3->secs_per_frame; return (jlong)mp3; } } } mp3file_delete(mp3); } else { __android_log_write(ANDROID_LOG_INFO, "podax-jni", mpg123_plain_strerror(err)); } return 0; }
bool Mp3::openFromFile(const std::string& filename) { stop(); if (myBuffer) delete [] myBuffer; if(myHandle) mpg123_close(myHandle); mpg123_param(myHandle, MPG123_RESYNC_LIMIT, -1, 0); #ifndef DEBUG mpg123_param(myHandle, MPG123_ADD_FLAGS, 32 , 0); #endif if (mpg123_open(myHandle, filename.c_str()) != MPG123_OK) { std::cerr << mpg123_strerror(myHandle) << std::endl; return false; } //This should improve myDuration calculation, but generates frankenstein streams¿? //Warning: Real sample count 9505152 differs from given gapless sample count -1152. Frankenstein stream if(mpg123_scan(myHandle) != MPG123_OK) { std::cerr << "Failed when scanning " << std::endl; return false; } long rate = 0; int channels = 0, encoding = 0; if (mpg123_getformat(myHandle, &rate, &channels, &encoding) != MPG123_OK) { std::cerr << "Failed to get format information for 464480e9ee6eb73bc2b768d7e3d7865aa432fc34quot;" << filename << "464480e9ee6eb73bc2b768d7e3d7865aa432fc34quot;" << std::endl; return false; } myDuration = sf::Time(sf::milliseconds(1 + 1000*mpg123_length(myHandle)/rate)); myBufferSize = mpg123_outblock(myHandle); myBuffer = new unsigned char[myBufferSize]; if (!myBuffer) { std::cerr << "Failed to reserve memory for decoding one frame for 464480e9ee6eb73bc2b768d7e3d7865aa432fc34quot;" << filename << "464480e9ee6eb73bc2b768d7e3d7865aa432fc34quot;" << std::endl; return false; } sf::SoundStream::initialize(channels, rate); return true; }
static inline jlong wrapped_Java_com_badlogic_gdx_audio_io_Mpg123Decoder_openFile (JNIEnv* env, jobject object, jstring obj_filename, char* filename) { //@line:111 mpg123_handle *mh = NULL; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; err = mpg123_init(); if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL || mpg123_open(mh, filename) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { fprintf( stderr, "Trouble with mpg123: %s\n", mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh) ); cleanup(mh); return 0; } if(encoding != MPG123_ENC_SIGNED_16) { // Signed 16 is the default output format anyways; it would actually by only different if we forced it. // So this check is here just for this explanation. cleanup(mh); return 0; } // Ensure that this output format will not change (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); size_t buffer_size = mpg123_outblock( mh ); unsigned char* buffer = (unsigned char*)malloc(buffer_size); size_t done = 0; int samples = 0; Mp3File* mp3 = new Mp3File(); mp3->handle = mh; mp3->channels = channels; mp3->rate = rate; mp3->buffer = buffer; mp3->buffer_size = buffer_size; int length = mpg123_length( mh ); if( length == MPG123_ERR ) mp3->length = 0; else mp3->length = length / rate; return (jlong)mp3; }
int GetMusicStatus() { char buff[MAX_PATH] = {0}; DWORD ret; _snprintf(buff, MAX_PATH, "%s\\\\%s", dir, fileInfo.cFileName); ret = mpg123_open(mh, buff); if(ret != MPG123_OK) { return 1; } mpg123_seek_frame(mh, lastFrame, SEEK_SET); return 0; }
int open_file() { if( mpg123_open(m, filename) == MPG123_OK && mpg123_getformat(m, NULL, &channels, NULL) == MPG123_OK ) { printf("Channels: %i\n", channels); return 0; } else { printf("Opening file failed: %s\n", mpg123_strerror(m)); return -1; } }
//------------------------------------------------------------ bool ofOpenALSoundPlayer::mpg123Stream(string path,vector<short> & buffer,vector<float> & fftAuxBuffer){ if(!mp3streamf){ int err = MPG123_OK; mp3streamf = mpg123_new(NULL,&err); if(mpg123_open(mp3streamf,path.c_str())!=MPG123_OK){ mpg123_close(mp3streamf); mpg123_delete(mp3streamf); ofLogError("ofOpenALSoundPlayer") << "mpg123Stream(): couldn't read \"" << path << "\""; return false; } long int rate; mpg123_getformat(mp3streamf,&rate,&channels,(int*)&stream_encoding); if(stream_encoding!=MPG123_ENC_SIGNED_16){ ofLogError("ofOpenALSoundPlayer") << "mpg123Stream(): " << getMpg123EncodingString(stream_encoding) << " encoding for \"" << path << "\"" << " unsupported, expecting MPG123_ENC_SIGNED_16"; return false; } samplerate = rate; mp3_buffer_size = mpg123_outblock( mp3streamf ); mpg123_seek(mp3streamf,0,SEEK_END); off_t samples = mpg123_tell(mp3streamf); duration = float(samples/channels) / float(samplerate); mpg123_seek(mp3streamf,0,SEEK_SET); } int curr_buffer_size = mp3_buffer_size; if(speed>1) curr_buffer_size *= (int)round(speed); buffer.resize(curr_buffer_size); fftAuxBuffer.resize(buffer.size()); size_t done=0; if(mpg123_read(mp3streamf,(unsigned char*)&buffer[0],curr_buffer_size*2,&done)==MPG123_DONE){ setPosition(0); buffer.resize(done/2); fftAuxBuffer.resize(done/2); if(!bLoop) stopThread(); stream_end = true; } for(int i=0;i<(int)buffer.size();i++){ fftAuxBuffer[i] = float(buffer[i])/32565.f; } return true; }
int main(int argc, char **argv) { int err; int ret = 0; mpg123_handle *mh; mpg123_id3v2 *id3; if(argc < 2) { printf("Gimme a MPEG file name...\n"); return 0; } mpg123_init(); mh = mpg123_new(NULL, &err); if(err != MPG123_OK) goto badend; mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_PLAIN_ID3TEXT, 0.); err = mpg123_open(mh, argv[1]); if(err != MPG123_OK) goto badend; err = mpg123_scan(mh); if(err != MPG123_OK) goto badend; err = mpg123_id3(mh, NULL, &id3); if(err != MPG123_OK) goto badend; if(id3 == NULL) { error("No ID3 data found."); goto badend; } print_field("artist", id3->artist); print_field("title", id3->title); print_field("album", id3->album); print_field("comment", id3->comment); goto end; badend: ret = -1; end: mpg123_delete(mh); mpg123_exit(); return ret; }
bool Mp3Stream::open(string path) { // open file int result = mpg123_open(mh, path.c_str()); if (result != MPG123_OK) { LOG4CXX_ERROR(narratorMP3StreamLog, "File " << path << " could not be opened"); return false; } // get rate, channels and encoding mpg123_getformat(mh, &mRate, &mChannels, &mEncoding); LOG4CXX_TRACE(narratorMP3StreamLog, "MP3 open with encoding " << mEncoding); isOpen = true; return true; }
/** * @name Reset delle variabili Decoder * * Questa funzione inizializza le variabili necessarie in fase di riproduzione per il decoder */ void resetMp3(char * song) { if (buffer!=NULL) {freeMp3();} int err; int channels, encoding; long rate; /* Inizialize */ mpg123_init(); mh = mpg123_new(NULL, &err); buffer_size = mpg123_outblock(mh); buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char)); /* open the file and get the decoding format */ mpg123_open(mh,song); mpg123_getformat(mh, &rate, &channels, &encoding); /* set the output format and open the output device */ int bits=(mpg123_encsize(encoding) * BITS); initAudioDev(bits,rate,channels); }
static void init_dev(void) { int channels, encoding; long rate; ao_sample_format format; if (dev) ao_close(dev); mpg123_open(mh, sound_file); mpg123_getformat(mh, &rate, &channels, &encoding); /* set the output format and open the output device */ format.bits = mpg123_encsize(encoding) * BITS; format.rate = rate; format.channels = channels; format.byte_format = AO_FMT_NATIVE; format.matrix = 0; dev = ao_open_live(driver, &format, NULL); }
int MP3Decoder_init(decoders_MP3Decoder *self, PyObject *args, PyObject *kwds) { char *filename; int error; self->handle = NULL; self->channels = 0; self->rate = 0; self->encoding = 0; self->closed = 0; self->audiotools_pcm = NULL; if (!PyArg_ParseTuple(args, "s", &filename)) return -1; if ((self->handle = mpg123_new(NULL, &error)) == NULL) { PyErr_SetString(PyExc_ValueError, "error initializing decoder"); return -1; } if ((error = mpg123_open(self->handle, filename)) != MPG123_OK) { PyErr_SetString(PyExc_ValueError, "error opening file"); return -1; } if ((error = mpg123_getformat(self->handle, &(self->rate), &(self->channels), &(self->encoding))) != MPG123_OK) { PyErr_SetString(PyExc_ValueError, "error getting file format"); return -1; } if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL) return -1; return 0; }
int Waveform::GetLengthOfMusicFileInMS(const wxString &filename) { mpg123_handle *mh; int err; int channels, encoding; long rate; mpg123_init(); mh = mpg123_new(NULL, &err); /* open the file and get the decoding format */ mpg123_open(mh, filename.c_str()); mpg123_getformat(mh, &rate, &channels, &encoding); int bits = mpg123_encsize(encoding); /* Get Track Size */ int trackSize = GetTrackSize(mh,bits,channels); mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); float seconds = (float)trackSize * ((float)1/(float)rate); return (int)(seconds * (float)1000); }
audioBuffer loadMp3File(const char* filename) { audioBuffer encBuffer; mpg123_handle *mh; unsigned char *buffer; size_t buffer_size; size_t done; int err; int channels, encoding; long rate; //inizialize mpg123 and its handler mpg123_init(); mh = mpg123_new(NULL, &err); buffer_size = mpg123_outblock(mh); buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char)); mpg123_open(mh, filename); mpg123_getformat(mh, &rate, &channels, &encoding); int m_bits = mpg123_encsize(encoding); int m_rate = rate; int m_channels = channels; //loads decoded file in memory for (int totalBtyes = 0 ; mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK ; ) { totalBtyes += done; } encBuffer.bufPointer = buffer; //deallocates mpg123 memory mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); return encBuffer; }
bool FileFormatMP3::Open(QString& filename) { _filePosition = 0; if( mpg123_open(_mpg123, filename.toStdString().c_str()) != MPG123_OK || mpg123_getformat(_mpg123, &_sampleRate, &_channels, &_encoding) != MPG123_OK ) { QMessageBox(QMessageBox::Critical, filename, QString("Could not open file."), QMessageBox::Ok); return false; } if(_encoding != MPG123_ENC_SIGNED_16 && _encoding != MPG123_ENC_FLOAT_32) { //cleanup(_mpg123); fprintf(stderr, "Bad MP3 encoding for file %s: 0x%x!\n", filename.toStdString().c_str(), _encoding); return false; } // TODO: Figure out if this is necessary. /* Ensure that this output format will not change (it could, when we allow it). */ //mpg123_format_none(mh); //mpg123_format(mh, rate, channels, encoding); return true; }
inline void MP3Decoder::openFile(const char *filename) { mpg123_handle *mh = NULL; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; err = mpg123_init(); if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL || mpg123_open(mh, filename) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { printf( "Trouble with mpg123: %s with file: %s\n", mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh), filename); this->cleanup(mh); return; } // Ensure that this output format will not change (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); size_t buffer_size = mpg123_length(mh) * channels;//mpg123_outblock( mh ); printf("buffer size: %ld\n", buffer_size); unsigned char* buffer = (unsigned char*)malloc(buffer_size); size_t done = 0; int samples = 0; mp3File = MP3File(); mp3File.handle = mh; mp3File.channels = channels; mp3File.rate = rate; mp3File.buffer = buffer; mp3File.buffer_size = buffer_size; int length = mpg123_length( mh ); if( length == MPG123_ERR ) mp3File.length = 0; else mp3File.length = length / rate; }