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;-).
mpglib.c
Go to the documentation of this file.
1 /*
2  mpglib: test program for libmpg123, in the style of the legacy mpglib test program
3 
4  This is example code only sensible to be considered in the public domain.
5  Initially written by Thomas Orgis.
6 */
7 
8 #include <mpg123.h>
9 
10 /* unistd.h is not available under MSVC,
11  io.h defines the read and write functions */
12 #ifndef _MSC_VER
13 #include <unistd.h>
14 #else
15 #include <io.h>
16 #endif
17 
18 #ifdef _WIN32
19 #include <fcntl.h>
20 #endif
21 
22 #include <stdio.h>
23 
24 #define INBUFF 16384
25 #define OUTBUFF 32768
28 int main(int argc, char **argv)
29 {
30  size_t size;
31  unsigned char buf[INBUFF]; /* input buffer */
32  unsigned char out[OUTBUFF]; /* output buffer */
33  ssize_t len;
34  int ret;
35  size_t in = 0, outc = 0;
36  mpg123_handle *m;
37 
38 #ifdef _WIN32
39 _setmode(_fileno(stdin),_O_BINARY);
40 _setmode(_fileno(stdout),_O_BINARY);
41 #endif
42 
43 #if MPG123_API_VERSION < 46
44  // Newer versions of the library don't need that anymore, but it is safe
45  // to have the no-op call present for compatibility with old versions.
46  mpg123_init();
47 #endif
48  m = mpg123_new(argc > 1 ? argv[1] : NULL, &ret);
49  if(m == NULL)
50  {
51  fprintf(stderr,"Unable to create mpg123 handle: %s\n", mpg123_plain_strerror(ret));
52  return -1;
53  }
54  mpg123_param(m, MPG123_VERBOSE, 2, 0); /* Brabble a bit about the parsing/decoding. */
55 
56  /* Now mpg123 is being prepared for feeding. The main loop will read chunks from stdin and feed them to mpg123;
57  then take decoded data as available to write to stdout. */
59  if(m == NULL) return -1;
60 
61  fprintf(stderr, "Feed me some MPEG audio to stdin, I will decode to stdout.\n");
62  while(1) /* Read and write until everything is through. */
63  {
64  len = read(0,buf,INBUFF);
65  if(len <= 0)
66  {
67  fprintf(stderr, "input data end\n");
68  break;
69  }
70  in += len;
71  /* Feed input chunk and get first chunk of decoded audio. */
72  ret = mpg123_decode(m,buf,len,out,OUTBUFF,&size);
73  if(ret == MPG123_NEW_FORMAT)
74  {
75  long rate;
76  int channels, enc;
77  mpg123_getformat(m, &rate, &channels, &enc);
78  fprintf(stderr, "New format: %li Hz, %i channels, encoding value %i\n", rate, channels, enc);
79  }
80  if(write(1,out,size) != size)
81  fprintf(stderr, "Output truncated.\n");
82  outc += size;
83  while(ret != MPG123_ERR && ret != MPG123_NEED_MORE)
84  { /* Get all decoded audio that is available now before feeding more input. */
85  ret = mpg123_decode(m,NULL,0,out,OUTBUFF,&size);
86  if(write(1,out,size) != size)
87  fprintf(stderr, "Output truncated.\n");
88  outc += size;
89  }
90  if(ret == MPG123_ERR){ fprintf(stderr, "some error: %s", mpg123_strerror(m)); break; }
91  }
92  fprintf(stderr, "%lu bytes in, %lu bytes out\n", (unsigned long)in, (unsigned long)outc);
93 
94  /* Done decoding, now just clean up and leave. */
95  mpg123_delete(m);
96  return 0;
97 }
MPG123_EXPORT const char * mpg123_strerror(mpg123_handle *mh)
MPG123_EXPORT const char * mpg123_plain_strerror(int errcode)
@ MPG123_NEED_MORE
Definition: mpg123.h:469
@ MPG123_ERR
Definition: mpg123.h:470
@ MPG123_NEW_FORMAT
Definition: mpg123.h:468
MPG123_EXPORT mpg123_handle * mpg123_new(const char *decoder, int *error)
MPG123_EXPORT void mpg123_delete(mpg123_handle *mh)
struct mpg123_handle_struct mpg123_handle
Definition: mpg123.h:164
MPG123_EXPORT int mpg123_param(mpg123_handle *mh, enum mpg123_parms type, long value, double fvalue)
MPG123_EXPORT int mpg123_init(void)
@ MPG123_VERBOSE
Definition: mpg123.h:228
MPG123_EXPORT int mpg123_open_feed(mpg123_handle *mh)
MPG123_EXPORT int mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize, void *outmemory, size_t outmemsize, size_t *done)
MPG123_EXPORT int mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding)
#define INBUFF
Definition: mpglib.c:24
#define OUTBUFF
Definition: mpglib.c:25
int main(int argc, char **argv)
Definition: mpglib.c:28
Hopefully valid HTML! Valid CSS!