//------------------------------------------------------------ bool ofOpenALSoundPlayer_TimelineAdditions::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); ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read " + path); return false; } long int rate; mpg123_getformat(mp3streamf,&rate,&channels,&stream_encoding); if(stream_encoding!=MPG123_ENC_SIGNED_16){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: unsupported encoding"); 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; }
static uint8_t * ReadMP3(Sound_t * Sound, const uint8_t * InData, size_t FileSize){ mpg123_handle *mh; if(mpg123_init() != MPG123_OK || (mh = mpg123_new(NULL, NULL)) == NULL){ mpg123_exit(); return NULL; } long rate; int channels, encoding; unsigned samples; size_t OutSize; uint8_t * OutData; if(mpg123_format_none(mh) != MPG123_OK || mpg123_format(mh, 44100, MPG123_MONO | MPG123_STEREO, MPG123_ENC_SIGNED_16) != MPG123_OK || mpg123_open_feed(mh) != MPG123_OK || mpg123_feed(mh, InData, FileSize) != MPG123_OK || mpg123_set_filesize(mh, FileSize) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK || (samples = mpg123_length(mh)) == 0 || (OutData = (uint8_t*) malloc(OutSize = samples * channels * 2)) == NULL ){ mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); return NULL; } size_t decoded; mpg123_read(mh, OutData, OutSize, &decoded); mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); if(decoded != OutSize){ free(OutData); return NULL; } Sound->Channels = channels; Sound->SamplingRate = rate; Sound->BitDepth = 16; Sound->Duration = samples; Sound->Data = OutData; return OutData; }
int fm_player_open(fm_player_t *pl, fm_player_config_t *config) { pl->config = *config; pl->mh = mpg123_new(NULL, NULL); mpg123_format_none(pl->mh); mpg123_format(pl->mh, config->rate, config->channels, config->encoding); ao_sample_format ao_fmt; ao_fmt.rate = config->rate; ao_fmt.channels = config->channels; ao_fmt.bits = mpg123_encsize(config->encoding) * 8; ao_fmt.byte_format = AO_FMT_NATIVE; ao_fmt.matrix = 0; int driver = ao_driver_id(config->driver); if (driver == -1) { return -1; } ao_info *driver_info = ao_driver_info(driver); printf("Player audio driver: %s\n", driver_info->name); printf("Player sample rate: %d Hz\n", pl->config.rate); ao_option *options = NULL; if (config->dev[0] != '\0') { ao_append_option(&options, "dev", config->dev); } pl->dev = ao_open_live(driver, &ao_fmt, options); ao_free_options(options); if (pl->dev == NULL) return -1; pl->curl = curl_easy_init(); curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, download_callback); curl_easy_setopt(pl->curl, CURLOPT_WRITEDATA, pl); pl->tid_ack = 0; pthread_mutex_init(&pl->mutex_status, NULL); pthread_cond_init(&pl->cond_play, NULL); pl->status = FM_PLAYER_STOP; return 0; }
Mp3Stream::Mp3Stream() { LOG4CXX_INFO(narratorMP3StreamLog, "Initializing mp3stream"); mError = 0; mEncoding = 0; mChannels = 0; mRate = 0; isOpen = false; openTmpFile = false; mTmpFile = ""; scaleNegative = powf(2, 16); scalePositive = scaleNegative - 1; mpg123_init(); mh = mpg123_new(NULL, &mError); mFrameSize = mpg123_outblock(mh); }
/** * @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); }
int mp3Init(struct playerHandles *ph){ pthread_mutex_init(&dechandle_lock,NULL); mpg123_init(); h.m = mpg123_new(NULL, NULL); if(h.m == NULL){ fprintf(stderr,"Unable to create mpg123 handle\n"); return -1; } //mpg123_param(h.m, MPG123_VERBOSE, 0, 0); mpg123_param(h.m, MPG123_RESYNC_LIMIT, -1, 0); mpg123_param(h.m, MPG123_FLAGS, MPG123_QUIET, 0); //mpg123_open_feed(h.m); mpg123_open_fd(h.m,fileno(ph->ffd)); if(h.m == NULL) return -1; return 0; }
Mp3::Mp3() : myHandle (nullptr), myBufferSize(0), myBuffer (nullptr) { int err = MPG123_OK; if ((err = mpg123_init()) != MPG123_OK) { std::cerr << mpg123_plain_strerror(err) << std::endl; return; } myHandle = mpg123_new(NULL, &err); if (!myHandle) { std::cerr << "Unable to create mpg123 handle: " << mpg123_plain_strerror(err) << std::endl; return; } }
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; }
bool Mp3Decoder::openFile( FILE*& file ) { _handle = mpg123_new(NULL, NULL); if(_handle == NULL) { printf("mpg123_new failed\n"); return false; } _file = file; int res = mpg123_open_fd(_handle, _file->_file); if(res != MPG123_OK) { printf("mpg123_open_handle failed\n"); return false; } return afterOpen(); }
JNIEXPORT jlong JNICALL Java_com_axelby_mp3decoders_MPG123_openStream (JNIEnv *env, jclass c) { // init mpg123 handle int err = MPG123_OK; mpg123_handle *mh = mpg123_new(NULL, &err); if (err != MPG123_OK) { __android_log_print(ANDROID_LOG_INFO, "podax-jni", "mpg123_new error: %s", mpg123_plain_strerror(err)); return 0; } // set handle up as stream err = mpg123_open_feed(mh); if (err != MPG123_OK) { __android_log_print(ANDROID_LOG_INFO, "podax-jni", "mpg123_open_feed error: %s", mpg123_plain_strerror(err)); return 0; } MP3File* stream = mp3file_init(mh); return (jlong)stream; }
Mpg123Decoder::Mpg123Decoder() : handle(nullptr, mpg123_delete) { music_type = "mp3"; err = mpg123_init(); if (err != MPG123_OK) { error_message = "mpg123: " + std::string(mpg123_plain_strerror(err)); return; } handle.reset(mpg123_new(nullptr, &err)); mpg123_replace_reader_handle(handle.get(), custom_read, custom_seek, custom_close); if (!handle) { error_message = "mpg123: " + std::string(mpg123_plain_strerror(err)); return; } init = true; }
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); }
void open_sound(void) { int err; ao_initialize(); driver = ao_default_driver_id(); mpg123_init(); mh = mpg123_new(NULL, &err); buffer_size = mpg123_outblock(mh); buffer = malloc(buffer_size * sizeof(char)); GError *error = NULL; gchar *str; if ((str = g_key_file_get_string(keyfile, "preferences", "soundfile", &error))) { if (str[0]) { sound_file = str; init_dev(); } else g_free(str); } }
void *play_url(void *urll) { char *url = (char *) urll; ao_initialize(); mpg123_init(); mh = mpg123_new(NULL, NULL); mpg123_open_feed(mh); CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, play_stream); curl_easy_setopt(curl, CURLOPT_URL, url); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) log_err("curl_easy_perform failed: %s", curl_easy_strerror(res)); curl_easy_cleanup(curl); return NULL; }
static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec) { GstMpg123AudioDec *mpg123_decoder; int error; mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); error = 0; mpg123_decoder->handle = mpg123_new (NULL, &error); mpg123_decoder->has_next_audioinfo = FALSE; mpg123_decoder->frame_offset = 0; /* Initially, the mpg123 handle comes with a set of default formats supported. This clears this set. This is necessary, since only one format shall be supported (see set_format for more). */ mpg123_format_none (mpg123_decoder->handle); mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); /* Built-in mpg123 support for gapless decoding is disabled for now, since it does not work well with seeking */ mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; essential for MP3 radio streams */ mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Sets the resync limit to the end of the stream (e.g. don't give up prematurely) */ /* Open in feed mode (= encoded data is fed manually into the handle). */ error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { GstElement *element = GST_ELEMENT (dec); GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Error opening mpg123 feed: %s", mpg123_plain_strerror (error))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; return FALSE; } GST_DEBUG_OBJECT (dec, "mpg123 decoder started"); return TRUE; }
bool MPG123Decoder::open(FileReader &reader) { if(!inited) { if (!IsMPG123Present()) return false; if(mpg123_init() != MPG123_OK) return false; inited = true; } Reader = std::move(reader); { MPG123 = mpg123_new(NULL, NULL); if(mpg123_replace_reader_handle(MPG123, file_read, file_lseek, NULL) == MPG123_OK && mpg123_open_handle(MPG123, this) == MPG123_OK) { int enc, channels; long srate; if(mpg123_getformat(MPG123, &srate, &channels, &enc) == MPG123_OK) { if((channels == 1 || channels == 2) && srate > 0 && mpg123_format_none(MPG123) == MPG123_OK && mpg123_format(MPG123, srate, channels, MPG123_ENC_SIGNED_16) == MPG123_OK) { // All OK Done = false; return true; } } mpg123_close(MPG123); } mpg123_delete(MPG123); MPG123 = 0; } reader = std::move(Reader); // need to give it back. return false; }
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; }
int in_mp3_init(plugin_obj *obj) { int r; NEW_PLUGIN_OBJ(obj); assert(obj != NULL); if (mpg123_init() != MPG123_OK) return REPLAY_ERR; obj->data = mpg123_new(NULL, &r); if (obj->data == NULL) return REPLAY_ERR; if (mpg123_open_feed(obj->data) != MPG123_OK) return REPLAY_ERR; assert(obj->data != NULL); return REPLAY_OK; }
void UMP3Decoder::Init(const uint8*& Buffer, const uint8* BufferEnd) { int encoding; int channels; long samplerate; mpg123_init(); Handle = mpg123_new(NULL, &ErrorHandle); BlockBufferSize = mpg123_outblock(Handle); mpg123_open_feed(Handle); mpg123_feed(Handle, Buffer, BufferEnd - Buffer); mpg123_getformat(Handle, &samplerate, &channels, &encoding); uint32 bytesPerSample = mpg123_encsize(encoding); BitsPerSample = bytesPerSample * 8; Channels = channels; Samplerate = samplerate; UE_LOG(MP3ImporterLog, Display, TEXT("Initialized: Samplerate: %u, Channels: %u"), Samplerate, Channels); }
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; }
QString InternetFormatSupport( const char* address ) { loadCURL(); if ( !CURLloaded ) return ""; mpg123_handle *mus = mpg123_new(NULL, NULL); mpg123_open_feed( mus ); URL_FILE *f = url_fopen( address ); if ( !f ) { unloadCURL(); return ""; } char *data; int _DATA_BUFF; if ( !getDataBuff( f, url_fread, _DATA_BUFF, &data ) ) { url_fclose(f); unloadCURL(); return ""; } int bread = url_fread(data+10, 1, _DATA_BUFF-10, f); mpg123_decode( mus, (const unsigned char*)data, bread, 0,0,0 ); bool loaded = getMusInfo( mus, 0,0,0,0,0, -1, "" ); mpg123_close(mus); mpg123_delete(mus); delete[] data; url_fclose(f); unloadCURL(); if ( loaded ) return plugName; else return ""; }
bool Mp3Decoder::open( Downloader* downloader ) { _handle = mpg123_new(NULL, NULL); if(_handle == NULL) { printf("mpg123_new failed\n"); return false; } mpg123_replace_reader_handle(_handle, g_read, NULL, NULL); int res = mpg123_param(_handle, MPG123_FLAGS, MPG123_FUZZY | MPG123_SEEKBUFFER | MPG123_GAPLESS, 0); assert(res == MPG123_OK); res = mpg123_open_handle(_handle, downloader); if(res != MPG123_OK) { printf("mpg123_open_handle failed\n"); return false; } _downloader = downloader; return afterOpen(); }
/** * TODO */ JNIEXPORT jlong JNICALL Java_de_mpg123_MPG123Decoder_open (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; // Create new mpg123 handle m_Mp3Decoder.m_mh = mpg123_new(NULL, &err); __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "mpg123_new: %p", m_Mp3Decoder.m_mh); if (err == MPG123_OK && m_Mp3Decoder.m_mh != NULL) { // Get the utf-8 string const char *file_path = env->GetStringUTFChars(path_to_file, JNI_FALSE); // Create new sound file and assign it to private Mp3Decoder field SoundFile *sound_file = new SoundFile(file_path); __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Sound file path: %s", sound_file->file_path); err = mpg123_open(m_Mp3Decoder.m_mh, sound_file->file_path); // The jni string can now be released //env->ReleaseStringUTFChars(path_to_file, file_path); if (err == MPG123_OK) { err = mpg123_getformat(m_Mp3Decoder.m_mh, &rate, &channels, &encoding); if (err == MPG123_OK) { // Reset internal format table and only allow specific encodings mpg123_format_none(m_Mp3Decoder.m_mh); // TODO: remove this: Force 32 bit float encoding //mp3->encoding = MPG123_ENC_FLOAT_32; encoding = MPG123_ENC_SIGNED_16; // Set fixed format mpg123_format(m_Mp3Decoder.m_mh, rate, channels, encoding); // Store the maximum buffer size that is possible // The buffer will be needed in the reading/decoding step buffer_size = mpg123_outblock(m_Mp3Decoder.m_mh); // Store number of samples of one channel of current track num_samples = mpg123_length(m_Mp3Decoder.m_mh); // TODO: if sound file init fails, what to do? // Everything was properly loaded with mpg123. Initialize sound file sound_file->init(channels, rate, num_samples, encoding, buffer_size); //__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "return sound file pointer: %p", // sound_file); return (jlong) sound_file; } else { __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Error: mpg123_getformat err: %i", err); __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Trouble with mpg123: %s", mpg123_strerror(m_Mp3Decoder.m_mh)); } } else { __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Error: mpg123_open err: %i", err); } delete sound_file; } __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "Error: no proper initialization of mpg123lib."); return 0; }
int main(int argc, char *argv[]) { SNDFILE* sndfile = NULL; SF_INFO sfinfo; mpg123_handle *mh = NULL; unsigned char* buffer = NULL; size_t buffer_size = 0; size_t done = 0; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; off_t samples = 0; if (argc!=3) usage(); printf( "Input file: %s\n", argv[1]); printf( "Output file: %s\n", argv[2]); err = mpg123_init(); if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */ || mpg123_open(mh, argv[1]) != MPG123_OK /* Peek into track and get first output format. */ || 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 -1; } 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); fprintf(stderr, "Bad encoding: 0x%x!\n", encoding); return -2; } /* 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 could be almost any size here, mpg123_outblock() is just some recommendation. Important, especially for sndfile writing, is that the size is a multiple of sample size. */ buffer_size = mpg123_outblock( mh ); buffer = malloc( buffer_size ); bzero(&sfinfo, sizeof(sfinfo) ); sfinfo.samplerate = rate; sfinfo.channels = channels; sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_PCM_16; printf("Creating 16bit WAV with %i channels and %liHz.\n", channels, rate); sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo); if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; } do { err = mpg123_read( mh, buffer, buffer_size, &done ); sf_write_short( sndfile, (short*)buffer, done/sizeof(short) ); samples += done/sizeof(short); /* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities. We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */ } while (err==MPG123_OK); if(err != MPG123_DONE) fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n", err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) ); sf_close( sndfile ); samples /= channels; printf("%li samples written.\n", (long)samples); cleanup(mh); return 0; }
int main(int argc, char **argv) { int i, result; mpg123_handle* m; #if defined(WANT_WIN32_UNICODE) win32_cmdline_utf8(&argc,&argv); #endif progname = argv[0]; while ((result = getlopt(argc, argv, opts))) switch (result) { case GLO_UNKNOWN: fprintf (stderr, "%s: Unknown option \"%s\".\n", progname, loptarg); usage(1); case GLO_NOARG: fprintf (stderr, "%s: Missing argument for option \"%s\".\n", progname, loptarg); usage(1); } #ifdef WIN32 fprintf(stderr, "WARNING: This tool is not yet adapted to run on Windows (file I/O, unicode arguments)!\n"); #endif if(loptind >= argc) usage(1); mpg123_init(); m = mpg123_new(NULL, NULL); mpg123_param(m, MPG123_ADD_FLAGS, MPG123_PICTURE, 0.); for(i=loptind; i < argc; ++i) { mpg123_id3v1 *v1; mpg123_id3v2 *v2; int meta; if(mpg123_open(m, argv[i]) != MPG123_OK) { fprintf(stderr, "Cannot open %s: %s\n", argv[i], mpg123_strerror(m)); continue; } mpg123_scan(m); meta = mpg123_meta_check(m); if(meta & MPG123_ID3 && mpg123_id3(m, &v1, &v2) == MPG123_OK) { printf("FILE: %s\n", argv[i]); printf("\n==== ID3v1 ====\n"); if(v1 != NULL) print_v1(v1); printf("\n==== ID3v2 ====\n"); if(v2 != NULL) print_v2(v2); printf("\n==== ID3v2 Raw frames ====\n"); if(v2 != NULL) { print_raw_v2(v2); if(param.store_pics) store_pictures(argv[i], v2); } } else printf("Nothing found for %s.\n", argv[i]); mpg123_close(m); } mpg123_delete(m); mpg123_exit(); if(errors) error1("Encountered %i errors along the way.", errors); return errors != 0; #if defined(WANT_WIN32_UNICODE) win32_cmdline_utf8_free(argc,argv); #endif }
int mp3decode_reg(struct xlplayer *xlplayer) { static pthread_once_t once_control = PTHREAD_ONCE_INIT; struct mp3decode_vars *self; struct chapter *chapter; int fd, rv; long rate; int channels, encoding; int src_error; pthread_once(&once_control, decoder_library_init); if (!decoder_library_ok) { fprintf(stderr, "mp3decode_reg: decoder library is not ok\n"); goto rej; } if (!(self = xlplayer->dec_data = calloc(1, sizeof (struct mp3decode_vars)))) { fprintf(stderr, "mp3decode_reg: malloc failure\n"); goto rej; } if (!(self->mh = mpg123_new(NULL, NULL))) { fprintf(stderr, "mp3decode_reg: handle not okay"); goto rej_; } #ifdef MPG123_AUTO_RESAMPLE if (mpg123_param(self->mh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.0) != MPG123_OK) { fprintf(stderr, "mpgdecode_reg: failed to turn off auto resampling\n"); goto rej_; } #endif if (mpg123_param(self->mh, MPG123_ADD_FLAGS, MPG123_FORCE_STEREO, 0.0) != MPG123_OK) { fprintf(stderr, "mpgdecode_reg: failed to set flags"); goto rej_; } if (mpg123_format_none(self->mh) != MPG123_OK) { fprintf(stderr, "mp3decode_reg: failed to clear output formats"); goto rej_; } /* all the permitted mp3 sample rates are enabled * forced stereo is in effect so no need to add mono formats */ mpg123_format(self->mh, 48000, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 44100, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 32000, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 24000, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 22050, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 16000, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 12000, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 11025, MPG123_STEREO, MPG123_ENC_FLOAT_32); mpg123_format(self->mh, 8000, MPG123_STEREO, MPG123_ENC_FLOAT_32); if (!(self->fp = fopen(xlplayer->pathname, "r"))) { fprintf(stderr, "mp3decode_reg: failed to open %s\n", xlplayer->pathname); goto rej_; } mp3_tag_read(&self->taginfo, self->fp); lseek(fd = fileno(self->fp), 0, SEEK_SET); if ((rv = mpg123_open_fd(self->mh, fd)) != MPG123_OK) { fprintf(stderr, "mp3decode_reg: mpg123_open_fd failed with return value %d\n", rv); goto rej__; } if (mpg123_getformat(self->mh, &rate, &channels, &encoding) != MPG123_OK || channels != 2) { fprintf(stderr, "mp3decode_reg: mpg123_getformat returned unexpected value\n"); goto rej___; } if (rate != xlplayer->samplerate) { fprintf(stderr, "mp3decode_reg: configuring resampler\n"); xlplayer->src_state = src_new(xlplayer->rsqual, channels, &src_error); if (src_error) { fprintf(stderr, "mp3decode_reg: src_new reports %s\n", src_strerror(src_error)); goto rej___; } xlplayer->src_data.src_ratio = (double)xlplayer->samplerate / (double)rate; xlplayer->src_data.end_of_input = 0; size_t output_frames = (size_t)(xlplayer->src_data.src_ratio * 1.1 * 1152); xlplayer->src_data.output_frames = (long)output_frames; if (!(xlplayer->src_data.data_out = malloc(output_frames * 2 * sizeof (float)))) { fprintf(stderr, "mp3decode_reg: malloc failure\n"); goto rej____; } self->resample = TRUE; } xlplayer->dec_init = mp3decode_init; xlplayer->dec_play = mp3decode_play; xlplayer->dec_eject = mp3decode_eject; if ((chapter = mp3_tag_chapter_scan(&self->taginfo, xlplayer->play_progress_ms + 70))) { self->current_chapter = chapter; xlplayer_set_dynamic_metadata(xlplayer, dynamic_metadata_form[chapter->title.encoding], chapter->artist.text, chapter->title.text, chapter->album.text, 0); } if (xlplayer->seek_s) if (mpg123_seek(self->mh, (off_t)rate * xlplayer->seek_s, SEEK_SET) < 0) { fprintf(stderr, "mp3decode_init: seek failed\n"); mp3decode_eject(xlplayer); xlplayer->playmode = PM_STOPPED; xlplayer->command = CMD_COMPLETE; } return ACCEPTED; rej____: xlplayer->src_state = src_delete(xlplayer->src_state); rej___: mpg123_delete(self->mh); rej__: mp3_tag_cleanup(&self->taginfo); fclose(self->fp); rej_: free(self); rej: return REJECTED; }
int main(int argc, char *argv[]) { SNDFILE* sndfile = NULL; SF_INFO sfinfo; mpg123_handle *mh = NULL; unsigned char* buffer = NULL; size_t buffer_size = 0; size_t done = 0; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; off_t samples = 0; if (argc<3) usage(); printf( "Input file: %s\n", argv[1]); printf( "Output file: %s\n", argv[2]); err = mpg123_init(); if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL) { fprintf(stderr, "Basic setup goes wrong: %s", mpg123_plain_strerror(err)); cleanup(mh); return -1; } /* Simple hack to enable floating point output. */ if(argc >= 4 && !strcmp(argv[3], "f32")) mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */ if( mpg123_open(mh, argv[1]) != MPG123_OK /* Peek into track and get first output format. */ || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { fprintf( stderr, "Trouble with mpg123: %s\n", mpg123_strerror(mh) ); cleanup(mh); return -1; } 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); fprintf(stderr, "Bad encoding: 0x%x!\n", encoding); return -2; } /* Ensure that this output format will not change (it could, when we allow it). */ mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); bzero(&sfinfo, sizeof(sfinfo) ); sfinfo.samplerate = rate; sfinfo.channels = channels; sfinfo.format = SF_FORMAT_WAV|(encoding == MPG123_ENC_SIGNED_16 ? SF_FORMAT_PCM_16 : SF_FORMAT_FLOAT); printf("Creating WAV with %i channels and %liHz.\n", channels, rate); sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo); if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; } /* Buffer could be almost any size here, mpg123_outblock() is just some recommendation. Important, especially for sndfile writing, is that the size is a multiple of sample size. */ buffer_size = argc >= 5 ? atol(argv[4]) : mpg123_outblock(mh); buffer = malloc( buffer_size ); do { sf_count_t more_samples; err = mpg123_read( mh, buffer, buffer_size, &done ); more_samples = encoding == MPG123_ENC_SIGNED_16 ? sf_write_short(sndfile, (short*)buffer, done/sizeof(short)) : sf_write_float(sndfile, (float*)buffer, done/sizeof(float)); if(more_samples < 0 || more_samples*mpg123_encsize(encoding) != done) { fprintf(stderr, "Warning: Written number of samples does not match the byte count we got from libmpg123: %li != %li\n", (long)(more_samples*mpg123_encsize(encoding)), (long)done); } samples += more_samples; /* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities. We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */ } while (err==MPG123_OK); if(err != MPG123_DONE) fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n", err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) ); sf_close( sndfile ); samples /= channels; printf("%li samples written.\n", (long)samples); cleanup(mh); return 0; }
/* This initializes libmpg123 and prepares the handle, including funky * parameters. */ static int preinit(sh_audio_t *sh) { int err, flag; struct ad_mpg123_context *con; /* Assumption: You always call preinit + init + uninit, on every file. * But you stop at preinit in case it fails. * If that is not true, one must ensure not to call mpg123_init / exit * twice in a row. */ if (mpg123_init() != MPG123_OK) return 0; sh->context = malloc(sizeof(struct ad_mpg123_context)); con = sh->context; /* Auto-choice of optimized decoder (first argument NULL). */ con->handle = mpg123_new(NULL, &err); if (!con->handle) goto bad_end; /* Guessing here: Default value triggers forced upmix of mono to stereo. */ flag = fakemono == 0 ? MPG123_FORCE_STEREO : fakemono == 1 ? MPG123_MONO_LEFT : fakemono == 2 ? MPG123_MONO_RIGHT : 0; if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK) goto bad_end; /* Basic settings. * Don't spill messages, enable better resync with non-seekable streams. * Give both flags individually without error checking to keep going with * old libmpg123. Generally, it is not fatal if the flags are not * honored */ mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0); /* Do not bail out on malformed streams at all. * MPlayer does not handle a decoder throwing the towel on crappy input. */ mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0); /* Open decisions: Configure libmpg123 to force encoding (or stay open about * library builds that support only float or int32 output), (de)configure * gapless decoding (won't work with seeking in MPlayer, though). * Don't forget to eventually enable ReplayGain/RVA support, too. * Let's try to run with the default for now. */ /* That would produce floating point output. * You can get 32 and 24 bit ints, even 8 bit via format matrix. */ /* mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); */ /* Example for RVA choice (available since libmpg123 1.0.0): mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */ #ifdef AD_MPG123_FRAMEWISE /* Prevent funky automatic resampling. * This way, we can be sure that one frame will never produce * more than 1152 stereo samples. */ mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.); #else /* Older mpg123 is vulnerable to concatenated streams when gapless cutting * is enabled (will only play the jingle of a badly constructed radio * stream). The versions using framewise decoding are fine with that. */ mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.); #endif return 1; bad_end: if (!con->handle) mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", mpg123_plain_strerror(err)); else mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", mpg123_strerror(con->handle)); if (con->handle) mpg123_delete(con->handle); mpg123_exit(); free(sh->context); sh->context = NULL; return 0; }
int _tmain(int argc, TCHAR **argv) { unsigned char buf[INBUFF]; unsigned char *audio; FILE *in; mpg123_handle *m; int ret, state; size_t inc, outc; off_t len, num; size_t bytes; off_t inoffset; size_t nrates; const long *rates; size_t i; inc = outc = 0; nrates = 0; rates = NULL; if(argc < 3) { fprintf(stderr,"Please supply in and out filenames\n"); return -1; } mpg123_init(); m = mpg123_new(NULL, &ret); if(m == NULL) { fprintf(stderr,"Unable to create mpg123 handle: %s\n", mpg123_plain_strerror(ret)); return -1; } mpg123_param(m, MPG123_VERBOSE, 4, 0); ret = mpg123_param(m, MPG123_FLAGS, MPG123_FUZZY | MPG123_SEEKBUFFER | MPG123_GAPLESS, 0); if(ret != MPG123_OK) { fprintf(stderr,"Unable to set library options: %s\n", mpg123_plain_strerror(ret)); return -1; } // Let the seek index auto-grow and contain an entry for every frame ret = mpg123_param(m, MPG123_INDEX_SIZE, -1, 0); if(ret != MPG123_OK) { fprintf(stderr,"Unable to set index size: %s\n", mpg123_plain_strerror(ret)); return -1; } // Use float output formats only ret = mpg123_format_none(m); if(ret != MPG123_OK) { fprintf(stderr,"Unable to disable all output formats: %s\n", mpg123_plain_strerror(ret)); return -1; } mpg123_rates(&rates, &nrates); for(i=0; i<nrates; i++) { ret = mpg123_format(m, rates[i], MPG123_MONO | MPG123_STEREO, MPG123_ENC_FLOAT_32); if(ret != MPG123_OK) { fprintf(stderr,"Unable to set float output formats: %s\n", mpg123_plain_strerror(ret)); return -1; } } ret = mpg123_open_feed(m); if(ret != MPG123_OK) { fprintf(stderr,"Unable open feed: %s\n", mpg123_plain_strerror(ret)); return -1; } in = _tfopen(argv[1], __T("rb")); if(in == NULL) { _ftprintf(stderr,__T("Unable to open input file %s\n"), argv[1]); return -1; } out = _tfopen(argv[2], __T("wb")); if(out == NULL) { _ftprintf(stderr,__T("Unable to open output file %s\n"), argv[2]); return -1; } while(ret = mpg123_feedseek(m, 95000, SEEK_SET, &inoffset) == MPG123_NEED_MORE) { len = fread(buf, sizeof(unsigned char), INBUFF, in); if(len <= 0) break; inc += len; state = mpg123_feed(m, buf, len); if(state == MPG123_ERR) { fprintf(stderr, "Error: %s", mpg123_strerror(m)); return -1; } } fseek(in, inoffset, SEEK_SET); while(1) { len = fread(buf, sizeof(unsigned char), INBUFF, in); if(len <= 0) break; inc += len; ret = mpg123_feed(m, buf, len); while(ret != MPG123_ERR && ret != MPG123_NEED_MORE) { ret = mpg123_decode_frame(m, &num, &audio, &bytes); if(ret == MPG123_NEW_FORMAT) { mpg123_getformat(m, &rate, &channels, &enc); initwavformat(); initwav(); fprintf(stderr, "New format: %li Hz, %i channels, encoding value %i\n", rate, channels, enc); } fwrite(audio, sizeof(unsigned char), bytes, out); outc += bytes; } if(ret == MPG123_ERR) { fprintf(stderr, "Error: %s", mpg123_strerror(m)); break; } } fprintf(stderr, "Finished read %lu, decoded %lu\n", (unsigned long)inc, (unsigned long)outc); closewav(); fclose(out); fclose(in); mpg123_delete(m); mpg123_exit(); return 0; }
int mpeg_open(char *path) { int rv = -1; int err = 0; int res = 0; int channels = 0; int encoding = 0; long rate = 0; if(handle != NULL) { goto mpeg_open_exit; } handle = calloc(1, sizeof(mpeg_handle_t)); if(handle == NULL) { goto mpeg_open_exit; } handle->mh = mpg123_new(NULL, &err); if(handle->mh == NULL) { goto mpeg_open_exit; } mpg123_param(handle->mh, MPG123_FLAGS, MPG123_QUIET, 0.0); handle->driver = ao_default_driver_id(); handle->bufsize = mpg123_outblock(handle->mh) / BUF_DIVISOR; handle->buf = (unsigned char*) malloc(handle->bufsize); if(handle->buf == NULL) { goto mpeg_open_exit; } res = mpg123_open(handle->mh, path); if(res != MPG123_OK) { goto mpeg_open_exit; } res = mpg123_getformat(handle->mh, &rate, &channels, &encoding); if(res != MPG123_OK) { goto mpeg_open_exit; } handle->format.bits = mpg123_encsize(encoding) * 8; handle->format.rate = rate; handle->format.channels = channels; handle->format.byte_format = AO_FMT_NATIVE; handle->format.matrix = 0; handle->device = ao_open_live(handle->driver, &(handle->format), NULL); if(handle->device == NULL) { goto mpeg_open_exit; } else { /* Return value to indicate success */ rv = 0; } mpeg_open_exit: if(rv == -1) { if(handle != NULL) { if(handle->buf != NULL) { free(handle->buf); handle->buf = NULL; } if(handle->device != NULL) { ao_close(handle->device); handle->device = NULL; } if(handle->mh != NULL) { mpg123_close(handle->mh); mpg123_delete(handle->mh); handle->mh = NULL; } free(handle); handle = NULL; } } return rv; }