You could access this page securely.

API documentation for libmpg123, libout123, and libsyn123

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, NEWS.libout123, NEWS.libsyn123, and the overall NEWS file on libmpg123 versions and important changes between them.
Let me emphasize that the policy for the lib*123 family is to always stay backwards compatible -- only additions are planned (and it's not yet planned to change the plans;-).
mpg123_to_out123.c
Go to the documentation of this file.
1 /*
2  mpg123_to_wav.c
3 
4  copyright 2007-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
5  see COPYING and AUTHORS files in distribution or http://mpg123.org
6  initially written by Nicholas Humfrey
7 
8  The most complicated part is about the choices to make about output format,
9  and prepare for the unlikely case a bastard mp3 might file change it.
10 */
11 
12 #include <out123.h>
13 #include <mpg123.h>
14 #include <stdio.h>
15 #include <strings.h>
16 
17 void usage(const char *cmd)
18 {
19  printf("Usage: %s <input> [<driver> [<output> [encoding [buffersize]]]]\n"
20  , cmd);
21  printf( "\nPlay MPEG audio from intput file to output file/device using\n"
22  "specified out123 driver, sample encoding and buffer size optional.\n\n" );
23  exit(99);
24 }
25 
26 void cleanup(mpg123_handle *mh, out123_handle *ao)
27 {
28  out123_del(ao);
29  /* It's really to late for error checks here;-) */
30  mpg123_close(mh);
31  mpg123_delete(mh);
32  mpg123_exit();
33 }
34 
35 int main(int argc, char *argv[])
36 {
37  mpg123_handle *mh = NULL;
38  out123_handle *ao = NULL;
39  char *infile = NULL;
40  char *driver = NULL;
41  char *outfile = NULL;
42  unsigned char* buffer = NULL;
43  const char *encname;
44  size_t buffer_size = 0;
45  size_t done = 0;
46  int channels = 0;
47  int encoding = 0;
48  int framesize = 1;
49  long rate = 0;
50  int err = MPG123_OK;
51  off_t samples = 0;
52 
53  if(argc<2)
54  usage(argv[0]);
55 
56  infile = argv[1];
57  if(argc >= 3)
58  driver = argv[2];
59  if(argc >= 4)
60  outfile = argv[3];
61  printf("Input file: %s\n", infile);
62  printf("Output driver: %s\n", driver ? driver : "<nil> (default)");
63  printf("Output file: %s\n", outfile ? outfile : "<nil> (default)");
64 
65  err = mpg123_init();
66  if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL)
67  {
68  fprintf(stderr, "Basic setup goes wrong: %s", mpg123_plain_strerror(err));
69  cleanup(mh, ao);
70  return -1;
71  }
72 
73  ao = out123_new();
74  if(!ao)
75  {
76  fprintf(stderr, "Cannot create output handle.\n");
77  cleanup(mh, ao);
78  return -1;
79  }
80 
81  if(argc >= 5)
82  { /* Make mpg123 support the desired encoding only for all rates. */
83  const long *rates;
84  size_t rate_count;
85  size_t i;
86  int enc;
87  /* If that is zero, you'll get the error soon enough from mpg123. */
88  enc = out123_enc_byname(argv[4]);
90  mpg123_rates(&rates, &rate_count);
91  for(i=0; i<rate_count; ++i)
92  mpg123_format(mh, rates[i], MPG123_MONO|MPG123_STEREO, enc);
93  }
94 
95  /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */
96  if( mpg123_open(mh, infile) != MPG123_OK
97  /* Peek into track and get first output format. */
98  || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK )
99  {
100  fprintf( stderr, "Trouble with mpg123: %s\n", mpg123_strerror(mh) );
101  cleanup(mh, ao);
102  return -1;
103  }
104  if(out123_open(ao, driver, outfile) != OUT123_OK)
105  {
106  fprintf(stderr, "Trouble with out123: %s\n", out123_strerror(ao));
107  cleanup(mh, ao);
108  return -1;
109  }
110  /* It makes no sense for that to give an error now. */
111  out123_driver_info(ao, &driver, &outfile);
112  printf("Effective output driver: %s\n", driver ? driver : "<nil> (default)");
113  printf("Effective output file: %s\n", outfile ? outfile : "<nil> (default)");
114 
115  /* Ensure that this output format will not change
116  (it might, when we allow it). */
117  mpg123_format_none(mh);
118  mpg123_format(mh, rate, channels, encoding);
119 
120  encname = out123_enc_name(encoding);
121  printf( "Playing with %i channels and %li Hz, encoding %s.\n"
122  , channels, rate, encname ? encname : "???" );
123  if( out123_start(ao, rate, channels, encoding)
124  || out123_getformat(ao, NULL, NULL, NULL, &framesize) )
125  {
126  fprintf(stderr, "Cannot start output / get framesize: %s\n"
127  , out123_strerror(ao));
128  cleanup(mh, ao);
129  return -1;
130  }
131 
132  /* Buffer could be almost any size here, mpg123_outblock() is just some
133  recommendation. The size should be a multiple of the PCM frame size. */
134  buffer_size = argc >= 6 ? atol(argv[5]) : mpg123_outblock(mh);
135  buffer = malloc( buffer_size );
136 
137  do
138  {
139  size_t played;
140  err = mpg123_read( mh, buffer, buffer_size, &done );
141  played = out123_play(ao, buffer, done);
142  if(played != done)
143  {
144  fprintf(stderr
145  , "Warning: written less than gotten from libmpg123: %li != %li\n"
146  , (long)played, (long)done);
147  }
148  samples += played/framesize;
149  /* We are not in feeder mode, so MPG123_OK, MPG123_ERR and
150  MPG123_NEW_FORMAT are the only possibilities.
151  We do not handle a new format, MPG123_DONE is the end... so
152  abort on anything not MPG123_OK. */
153  } while (done && err==MPG123_OK);
154 
155  free(buffer);
156 
157  if(err != MPG123_DONE)
158  fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
159  err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
160 
161  printf("%li samples written.\n", (long)samples);
162  cleanup(mh, ao);
163  return 0;
164 }
struct mpg123_handle_struct mpg123_handle
Definition: mpg123.h:132
MPG123_EXPORT size_t out123_play(out123_handle *ao, void *buffer, size_t bytes)
struct out123_struct out123_handle
Definition: out123.h:93
MPG123_EXPORT int out123_enc_byname(const char *name)
MPG123_EXPORT void mpg123_rates(const long **list, size_t *number)
MPG123_EXPORT int out123_open(out123_handle *ao, const char *driver, const char *device)
MPG123_EXPORT void mpg123_exit(void)
MPG123_EXPORT size_t mpg123_outblock(mpg123_handle *mh)
MPG123_EXPORT int out123_driver_info(out123_handle *ao, char **driver, char **device)
MPG123_EXPORT const char * mpg123_strerror(mpg123_handle *mh)
MPG123_EXPORT int mpg123_close(mpg123_handle *mh)
MPG123_EXPORT void out123_del(out123_handle *ao)
MPG123_EXPORT const char * mpg123_plain_strerror(int errcode)
MPG123_EXPORT int mpg123_init(void)
MPG123_EXPORT const char * out123_strerror(out123_handle *ao)
MPG123_EXPORT int mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
MPG123_EXPORT int out123_getformat(out123_handle *ao, long *rate, int *channels, int *encoding, int *framesize)
MPG123_EXPORT void mpg123_delete(mpg123_handle *mh)
MPG123_EXPORT int out123_start(out123_handle *ao, long rate, int channels, int encoding)
MPG123_EXPORT int mpg123_read(mpg123_handle *mh, void *outmemory, size_t outmemsize, size_t *done)
MPG123_EXPORT int mpg123_format_none(mpg123_handle *mh)
MPG123_EXPORT mpg123_handle * mpg123_new(const char *decoder, int *error)
MPG123_EXPORT out123_handle * out123_new(void)
MPG123_EXPORT int mpg123_open(mpg123_handle *mh, const char *path)
MPG123_EXPORT int mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding)
MPG123_EXPORT const char * out123_enc_name(int encoding)
Hopefully valid HTML! Valid CSS!