thenuke321

Untitled

Mar 15th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. int playMp3(string thePath)
  2. {
  3.     mpg123_handle *mh;
  4.     unsigned char *buffer;
  5.     size_t buffer_size;
  6.     size_t done;
  7.     int err;
  8.  
  9.     int driver;
  10.     ao_device *dev;
  11.  
  12.     ao_sample_format format;
  13.     int channels, encoding;
  14.     long rate;
  15.  
  16.     /* initializations */
  17.     ao_initialize();
  18.     driver = ao_default_driver_id();
  19.     mpg123_init();
  20.     mh = mpg123_new(NULL, &err);
  21.     buffer_size = mpg123_outblock(mh);
  22.     buffer = (unsigned char*)malloc(buffer_size * sizeof(unsigned char));
  23.  
  24.     /* open the file and get the decoding format */
  25.     mpg123_open(mh, thePath.c_str());
  26.     mpg123_getformat(mh, &rate, &channels, &encoding);
  27.  
  28.     /* set the output format and open the output device */
  29.     format.bits = mpg123_encsize(encoding) * BITS;
  30.     format.rate = rate;
  31.     format.channels = channels;
  32.     format.byte_format = AO_FMT_NATIVE;
  33.     format.matrix = 0;
  34.     dev = ao_open_live(driver, &format, NULL);
  35.     char *workAround = new char[buffer_size * sizeof(unsigned char)];
  36.     /* decode and play */
  37.     while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
  38.     {
  39.         // Shift from unsigned to signed!
  40.         for (auto i = 0; i < buffer_size / 2; i++){workAround[i] = static_cast<char>(buffer[i]);}
  41.         // This was the original ao system
  42.         //ao_play(dev, buffer, done);
  43.         ao_play(dev, workAround, done);
  44.     }
  45.     cout << md5(workAround) << " " << sizeof(workAround) << endl;
  46.     cout << "Buffer size " << buffer_size << endl;
  47.     cout << "Done size " << done << endl;
  48.     cout << "Channels " << channels << endl;
  49.     cout << "Encoding " << encoding << endl;
  50.     cout << "Rate " << rate << endl;
  51.     /* clean up */
  52.     free(buffer);
  53.     ao_close(dev);
  54.     mpg123_close(mh);
  55.     mpg123_delete(mh);
  56.     mpg123_exit();
  57.     ao_shutdown();
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment