Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. int decodeMp3(string filename, string outString)
  2. {
  3.     mpg123_handle *mh;
  4.     //unsigned char *buffer;
  5.     unsigned char *buffer;
  6.     size_t buffer_size;
  7.     size_t done;
  8.     int err;
  9.  
  10.     int channels, encoding;
  11.     long rate;
  12.  
  13.     mpg123_init();
  14.     mh = mpg123_new(NULL, &err);
  15.     buffer_size = mpg123_outblock(mh);
  16.     buffer = (unsigned char*)malloc(buffer_size * sizeof(unsigned char));
  17.  
  18.     /* open the file and get the decoding format */
  19.     mpg123_open(mh, filename.c_str());
  20.     mpg123_getformat(mh, &rate, &channels, &encoding);
  21.  
  22.     /* set the output format and open the output device */
  23.     int m_bits = mpg123_encsize(encoding);
  24.     int m_rate = rate;
  25.     int m_channels = channels;
  26.     std::ofstream out(outString.c_str());
  27.     /* decode and play */
  28.     char *data = new char[buffer_size * sizeof(unsigned char)];
  29.     for (int totalBtyes = 0; mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK; )
  30.     {
  31.         for (int i = 0; i < buffer_size / 2; i++)
  32.         {
  33.             data[i] = static_cast<char>(buffer[i]);
  34.         }
  35.         for (int i = 0; i != done+1; i++)
  36.         {
  37.             out << data[i];
  38.         }
  39.         totalBtyes += done;
  40.     }
  41.     //out << buffer[i];
  42.    
  43.     cout << "Buffer size " << buffer_size << endl;
  44.     cout << "Done " << done << endl;
  45.     cout << "err " << err << endl;
  46.     cout << "channels " << channels << endl;
  47.     cout << "encoding " << encoding << endl;
  48.     cout << "Rate " << rate << endl;
  49.     cout << "m_bits " << m_bits << endl;
  50.     cout << "m_rate " << m_rate << endl;
  51.     cout << "m_channels " << m_channels << endl;
  52.     cout << "The size of buffer " << sizeof(buffer) << endl;
  53.     /* clean up */
  54.     free(buffer);
  55.     out.close();
  56.     mpg123_close(mh);
  57.     mpg123_delete(mh);
  58.     mpg123_exit();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement