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;-).
00001
00002
00003
00004
00005
00006
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
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
00049 || mpg123_open(mh, argv[1]) != MPG123_OK
00050
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 {
00061
00062 cleanup(mh);
00063 fprintf(stderr, "Bad encoding: 0x%x!\n", encoding);
00064 return -2;
00065 }
00066
00067 mpg123_format_none(mh);
00068 mpg123_format(mh, rate, channels, encoding);
00069
00070
00071
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
00090
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 }