int fm_player_open(fm_player_t *pl, fm_player_config_t *config) { pl->config = *config; if (strcmp(config->driver, "pifm") == 0) { float f = atof(config->dev); if (f < 1) f = 102.4; printf("Player audio driver: pifm\n"); printf("Player sample rate: %d Hz\n", config->rate); printf("Player FM fequency: %f Hz\n", f); config->channels = 1; fm_setup_fm(); fm_setup_dma(f); } else { 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->mh = mpg123_new(NULL, NULL); mpg123_format_none(pl->mh); mpg123_format(pl->mh, config->rate, config->channels, config->encoding); 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; }
size_t play_stream(void *buffer, size_t size, size_t nmemb, void *userp) { int err; off_t frame_offset; unsigned char *audio; size_t done; ao_sample_format format; int channels, encoding; long rate; mpg123_feed(mh, (const unsigned char*) buffer, size * nmemb); do { err = mpg123_decode_frame(mh, &frame_offset, &audio, &done); switch(err) { case MPG123_NEW_FORMAT: mpg123_getformat(mh, &rate, &channels, &encoding); 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(ao_default_driver_id(), &format, NULL); break; case MPG123_OK: ao_play(dev, audio, done); break; case MPG123_NEED_MORE: break; default: break; } } while(done > 0); return size * nmemb; }
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"); } }
/** * @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 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; }
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); }
size_t write_callback(char *delivered_data, size_t size, size_t nmemb, void *user_data) { if (!playback_pause) { int err; off_t frame_offset; unsigned char *audio; size_t done = 1; ao_sample_format format; int channels, encoding; long rate; mpg123_feed(mh, delivered_data, size * nmemb); while (done > 0) { err = mpg123_decode_frame(mh, &frame_offset, &audio, &done); switch (err) { case MPG123_NEW_FORMAT: mpg123_getformat(mh, &rate, &channels, &encoding); format.bits = mpg123_encsize(encoding) * 8; format.rate = rate; format.channels = channels; format.byte_format = AO_FMT_NATIVE; format.matrix = 0; device = ao_open_live(ao_default_driver_id(), &format, NULL); break; case MPG123_OK: ao_play(device, audio, done); break; default: break; } } return size * nmemb; } return CURL_WRITEFUNC_PAUSE; }
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; }
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; }
// Open Media file and return elapsed time in millseconds int Waveform::OpenfileMediaFile(const wxString &filename, wxString& error) { mpg123_handle *mh = NULL; int err; size_t buffer_size; int channels, encoding; long rate; err = mpg123_init(); if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL) { error = wxString::Format("Basic setup goes wrong: %s", mpg123_plain_strerror(err)); if (mh != NULL) { cleanup(mh); } return -1; } /* open the file and get the decoding format */ if( mpg123_open(mh, filename.c_str()) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { error = wxString::Format("Trouble with mpg123: %s", mpg123_strerror(mh)); cleanup(mh); return -1; } if( encoding != MPG123_ENC_SIGNED_16 ) { error = "Encoding unsupported. Must be signed 16 bit."; cleanup(mh); return -2; } /* set the output format and open the output device */ m_bits = mpg123_encsize(encoding); m_rate = rate; m_channels = channels; /* Get Track Size */ mMediaTrackSize = GetTrackSize(mh,m_bits,m_channels); buffer_size = mpg123_outblock(mh); int size = (mMediaTrackSize+buffer_size)*m_bits*m_channels; char * trackData = (char*)malloc(size); LoadTrackData(mh,trackData, size); // Split data into left and right and normalize -1 to 1 m_left_data = (float*)malloc(sizeof(float)*mMediaTrackSize); if( m_channels == 2 ) { m_right_data = (float*)malloc(sizeof(float)*mMediaTrackSize); SplitTrackDataAndNormalize((signed short*)trackData,mMediaTrackSize,m_left_data,m_right_data); } else if( m_channels == 1 ) { NormalizeMonoTrackData((signed short*)trackData,mMediaTrackSize,m_left_data); } else { error = "More than 2 audio channels is not supported yet."; } views.clear(); float samplesPerLine = GetSamplesPerLineFromZoomLevel(mZoomLevel); WaveView wv(mZoomLevel,samplesPerLine,m_left_data,mMediaTrackSize); views.push_back(wv); mCurrentWaveView = 0; mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); free(trackData); float seconds = (float)mMediaTrackSize * ((float)1/(float)rate); mAudioIsLoaded = true; return (int)(seconds * (float)1000); }
static int stream_loop(mpg123_handle *mh, int socket) { ao_device *dev; ao_sample_format format; size_t bytes_decoded; int status; unsigned char out_buffer[OUT_BUF_SIZE]; int channels; int encoding; long rate; /* listen to socket */ if (mpg123_open_fd(mh, socket) == MPG123_ERR) { fprintf(stderr, "%s\n", mpg123_strerror(mh)); return 1; } status = MPG123_OK; /* set some standard format */ memset(&format, 0, sizeof(ao_sample_format)); format.rate = 44100; format.channels = 2; format.byte_format = AO_FMT_LITTLE; format.bits = 16; dev = ao_open_live(ao_default_driver_id(), &format, NULL); if (!dev) { fprintf(stderr, "ao_open_live failed()"); return 1; } do { if (g_paused) { fprintf(stderr, "paused\n"); sleep(1); continue; } status = mpg123_read(mh, out_buffer, OUT_BUF_SIZE, &bytes_decoded); if (status != 0) { fprintf(stderr, "status [%d] - %s\n", status, mpg123_plain_strerror(status)); } if (status == MPG123_ERR || status == MPG123_DONE) { break; } else { if (status == MPG123_NEW_FORMAT) { status = mpg123_getformat(mh, &rate, &channels, &encoding); fprintf(stderr, "new format:\n\trate: %ld\n\tchannels:%d\n\tencoding: %d\n", rate, channels, encoding); format.rate = rate; format.channels = channels; format.byte_format = AO_FMT_LITTLE; format.bits = mpg123_encsize(encoding) * 8; /* close and reopen again for new format */ ao_close(dev); dev = ao_open_live(ao_default_driver_id(), &format, NULL); if (!dev) { fprintf(stderr, "ao_open_live failed\n"); break; } } if (dev) { ao_play(dev, (char*)out_buffer, bytes_decoded); } } } while (g_go_on && !g_stop); if (dev) { fprintf(stderr, "stderr close ao device\n"); ao_close(dev); } return status; }
void VoxPlayer::aoplay(const std::string& path) { m_isplaying = true; ao_device* ad = NULL; mpg123_handle* mh = NULL; unsigned char* buf = NULL; size_t bufsz = 0; size_t done = 0; int driver = ao_default_driver_id(); ao_sample_format asf; int channels = 0; int encoding = 0; long rate = 0; if(!(mh = mpg123_new(NULL, NULL))) { ERROR("mpg123_new() failed"); goto error_success; } bufsz = mpg123_outblock(mh); buf = (unsigned char*)malloc(bufsz * sizeof(unsigned char)); if(mpg123_open(mh, path.c_str()) != MPG123_OK) { ERROR("mpg123_open() failed"); goto error_success; } mpg123_volume(mh, 1.4); if(mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { ERROR("mpg123_getformat() failed"); goto error_success; } asf.bits = mpg123_encsize(encoding) * 8; // BITS asf.rate = rate; asf.channels = channels; asf.byte_format = AO_FMT_NATIVE; asf.matrix = 0; if(!(ad = ao_open_live(driver, &asf, NULL))) { ERROR("ao_open_live() failed"); goto error_success; } while(!g_main.isExit() && !m_interrupt) { int ret = mpg123_read(mh, buf, bufsz, &done); DEBUG("ret=%d, done=%d", ret, done); if(ret == MPG123_OK || ret == MPG123_DONE || ret == MPG123_NEED_MORE) { if(!ao_play(ad, (char*)buf, done)) { ERROR("ao_play() failed"); break; } if(ret == MPG123_DONE) break; continue; } break; } DEBUG("play done"); error_success: if(buf) { free(buf); buf = NULL; } if(ad) { ao_close(ad); ad = NULL; } if(mh) { mpg123_close(mh); mpg123_delete(mh); mh = NULL; } m_isplaying = false; }