mpglib.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. mpglib: test program for libmpg123, in the style of the legacy mpglib test program
  3. copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1
  4. see COPYING and AUTHORS files in distribution or http://mpg123.org
  5. initially written by Thomas Orgis
  6. */
  7. #include <mpg123.h>
  8. /* unistd.h is not available under MSVC,
  9. io.h defines the read and write functions */
  10. #ifndef _MSC_VER
  11. #include <unistd.h>
  12. #else
  13. #include <io.h>
  14. #endif
  15. #ifdef _WIN32
  16. #include <fcntl.h>
  17. #endif
  18. #include <stdio.h>
  19. #define INBUFF 16384
  20. #define OUTBUFF 32768
  21. int main(int argc, char **argv)
  22. {
  23. size_t size;
  24. unsigned char buf[INBUFF]; /* input buffer */
  25. unsigned char out[OUTBUFF]; /* output buffer */
  26. ssize_t len;
  27. int ret;
  28. size_t in = 0, outc = 0;
  29. mpg123_handle *m;
  30. #ifdef _WIN32
  31. _setmode(_fileno(stdin),_O_BINARY);
  32. _setmode(_fileno(stdout),_O_BINARY);
  33. #endif
  34. mpg123_init();
  35. m = mpg123_new(argc > 1 ? argv[1] : NULL, &ret);
  36. if(m == NULL)
  37. {
  38. fprintf(stderr,"Unable to create mpg123 handle: %s\n", mpg123_plain_strerror(ret));
  39. return -1;
  40. }
  41. mpg123_param(m, MPG123_VERBOSE, 2, 0); /* Brabble a bit about the parsing/decoding. */
  42. /* Now mpg123 is being prepared for feeding. The main loop will read chunks from stdin and feed them to mpg123;
  43. then take decoded data as available to write to stdout. */
  44. mpg123_open_feed(m);
  45. if(m == NULL) return -1;
  46. fprintf(stderr, "Feed me some MPEG audio to stdin, I will decode to stdout.\n");
  47. while(1) /* Read and write until everything is through. */
  48. {
  49. len = read(0,buf,INBUFF);
  50. if(len <= 0)
  51. {
  52. fprintf(stderr, "input data end\n");
  53. break;
  54. }
  55. in += len;
  56. /* Feed input chunk and get first chunk of decoded audio. */
  57. ret = mpg123_decode(m,buf,len,out,OUTBUFF,&size);
  58. if(ret == MPG123_NEW_FORMAT)
  59. {
  60. long rate;
  61. int channels, enc;
  62. mpg123_getformat(m, &rate, &channels, &enc);
  63. fprintf(stderr, "New format: %li Hz, %i channels, encoding value %i\n", rate, channels, enc);
  64. }
  65. write(1,out,size);
  66. outc += size;
  67. while(ret != MPG123_ERR && ret != MPG123_NEED_MORE)
  68. { /* Get all decoded audio that is available now before feeding more input. */
  69. ret = mpg123_decode(m,NULL,0,out,OUTBUFF,&size);
  70. write(1,out,size);
  71. outc += size;
  72. }
  73. if(ret == MPG123_ERR){ fprintf(stderr, "some error: %s", mpg123_strerror(m)); break; }
  74. }
  75. fprintf(stderr, "%lu bytes in, %lu bytes out\n", (unsigned long)in, (unsigned long)outc);
  76. /* Done decoding, now just clean up and leave. */
  77. mpg123_delete(m);
  78. mpg123_exit();
  79. return 0;
  80. }