Advertisement
thenuke321

Untitled

May 9th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. int playPCM(string thePath)
  2. {
  3. mpg123_handle *mh;
  4. unsigned char *buffer = new char unsigned[32];
  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. /* initializations */
  16. ao_initialize();
  17. driver = ao_default_driver_id();
  18. mpg123_init();
  19. mh = mpg123_new(NULL, &err);
  20. buffer_size = mpg123_outblock(mh);
  21. /* open the file and get the decoding format */
  22. mpg123_open(mh, thePath.c_str());
  23. mpg123_getformat(mh, &rate, &channels, &encoding);
  24. /* set the output format and open the output device */
  25. format.bits = mpg123_encsize(encoding) * BITS;
  26. format.rate = rate;
  27. format.channels = channels;
  28. format.byte_format = AO_FMT_NATIVE;
  29. format.matrix = 0;
  30. dev = ao_open_live(driver, &format, NULL);
  31. //char *workAround = new char[buffer_size * sizeof(unsigned char)];
  32. char *workAround = new char[32];
  33. /* decode and play */
  34. std::ofstream out("raw", ios::binary);
  35. while (mpg123_read(mh, buffer, 32, &done) == MPG123_OK)
  36. {
  37. for (auto i = 0; i != 32; i++)
  38. {
  39. workAround[i] = static_cast<char>(buffer[i]);
  40. buffer[i] = '\0';
  41. }
  42. out << workAround;
  43. // This is the output of the pcm data
  44. }
  45. free(buffer);
  46. ao_close(dev);
  47. mpg123_close(mh);
  48. mpg123_delete(mh);
  49. mpg123_exit();
  50. ao_shutdown();
  51.  
  52. return 0;
  53. }
  54. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement