mpg123 logo
download : svn :: features :: sf.net project - bug tracker :: news archive
libmpg123 API :: testing :: benchmarking :: faq :: links :: contact
Note: This API doc is automatically generated from the current development version that you can get via Subversion or as a daily snapshot from http://mpg123.org/snapshot. There may be differences (additions) compared to the latest stable release. See NEWS.libmpg123 and the overall NEWS file on libmpg123 versions and important changes between them.
Let me emphasize that the policy for libmpg123 is to always stay backwards compatible -- only additions are planned (and it's not yet planned to change the plans;-).

mpg123_to_wav.c

Go to the documentation of this file.
00001 /*
00002         mpg123_to_wav.c
00003 
00004         copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1
00005         see COPYING and AUTHORS files in distribution or http://mpg123.org
00006         initially written by Nicholas Humfrey
00007 */
00008 
00009 #include <stdio.h>
00010 #include <strings.h>
00011 #include <mpg123.h>
00012 #include <sndfile.h>
00013 
00014 
00015 void usage()
00016 {
00017         printf("Usage: mpg123_to_wav <input> <output>\n");
00018         exit(99);
00019 }
00020 
00021 void cleanup(mpg123_handle *mh)
00022 {
00023         /* It's really to late for error checks here;-) */
00024         mpg123_close(mh);
00025         mpg123_delete(mh);
00026         mpg123_exit();
00027 }
00028 
00029 int main(int argc, char *argv[])
00030 {
00031         SNDFILE* sndfile = NULL;
00032         SF_INFO sfinfo;
00033         mpg123_handle *mh = NULL;
00034         unsigned char* buffer = NULL;
00035         size_t buffer_size = 0;
00036         size_t done = 0;
00037         int  channels = 0, encoding = 0;
00038         long rate = 0;
00039         int  err  = MPG123_OK;
00040         off_t samples = 0;
00041 
00042         if (argc!=3) usage();
00043         printf( "Input file: %s\n", argv[1]);
00044         printf( "Output file: %s\n", argv[2]);
00045         
00046         err = mpg123_init();
00047         if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL
00048             /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */
00049             || mpg123_open(mh, argv[1]) != MPG123_OK
00050             /* Peek into track and get first output format. */
00051             || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK )
00052         {
00053                 fprintf( stderr, "Trouble with mpg123: %s\n",
00054                          mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh) );
00055                 cleanup(mh);
00056                 return -1;
00057         }
00058 
00059         if(encoding != MPG123_ENC_SIGNED_16)
00060         { /* Signed 16 is the default output format anyways; it would actually by only different if we forced it.
00061              So this check is here just for this explanation. */
00062                 cleanup(mh);
00063                 fprintf(stderr, "Bad encoding: 0x%x!\n", encoding);
00064                 return -2;
00065         }
00066         /* Ensure that this output format will not change (it could, when we allow it). */
00067         mpg123_format_none(mh);
00068         mpg123_format(mh, rate, channels, encoding);
00069 
00070         /* Buffer could be almost any size here, mpg123_outblock() is just some recommendation.
00071            Important, especially for sndfile writing, is that the size is a multiple of sample size. */
00072         buffer_size = mpg123_outblock( mh );
00073         buffer = malloc( buffer_size );
00074 
00075         bzero(&sfinfo, sizeof(sfinfo) );
00076         sfinfo.samplerate = rate;
00077         sfinfo.channels = channels;
00078         sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_PCM_16;
00079         printf("Creating 16bit WAV with %i channels and %liHz.\n", channels, rate);
00080 
00081         sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo);
00082         if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; }
00083 
00084         do
00085         {
00086                 err = mpg123_read( mh, buffer, buffer_size, &done );
00087                 sf_write_short( sndfile, (short*)buffer, done/sizeof(short) );
00088                 samples += done/sizeof(short);
00089                 /* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities.
00090                    We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */
00091         } while (err==MPG123_OK);
00092 
00093         if(err != MPG123_DONE)
00094         fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
00095                  err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
00096 
00097         sf_close( sndfile );
00098 
00099         samples /= channels;
00100         printf("%li samples written.\n", (long)samples);
00101         cleanup(mh);
00102         return 0;
00103 }

Generated on Mon Dec 29 23:37:35 2008 for libmpg123 by  doxygen 1.5.4