AudioDecoderMp3.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. Copyright (c) 2016-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "audio/win32/AudioDecoderMp3.h"
  21. #include "audio/win32/AudioMacros.h"
  22. #include "platform/CCFileUtils.h"
  23. #include "base/CCConsole.h"
  24. #include "mpg123.h"
  25. #define LOG_TAG "AudioDecoderMp3"
  26. namespace cocos2d { namespace experimental {
  27. static bool __mp3Inited = false;
  28. bool AudioDecoderMp3::lazyInit()
  29. {
  30. bool ret = true;
  31. if (!__mp3Inited)
  32. {
  33. int error = mpg123_init();
  34. if (error == MPG123_OK)
  35. {
  36. __mp3Inited = true;
  37. }
  38. else
  39. {
  40. ALOGE("Basic setup goes wrong: %s", mpg123_plain_strerror(error));
  41. ret = false;
  42. }
  43. }
  44. return ret;
  45. }
  46. void AudioDecoderMp3::destroy()
  47. {
  48. if (__mp3Inited)
  49. {
  50. mpg123_exit();
  51. __mp3Inited = false;
  52. }
  53. }
  54. AudioDecoderMp3::AudioDecoderMp3()
  55. : _mpg123handle(nullptr)
  56. {
  57. lazyInit();
  58. }
  59. AudioDecoderMp3::~AudioDecoderMp3()
  60. {
  61. close();
  62. }
  63. bool AudioDecoderMp3::open(const char* path)
  64. {
  65. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
  66. long rate = 0;
  67. int error = MPG123_OK;
  68. int mp3Encoding = 0;
  69. int channel = 0;
  70. do
  71. {
  72. _mpg123handle = mpg123_new(nullptr, &error);
  73. if (nullptr == _mpg123handle)
  74. {
  75. ALOGE("Basic setup goes wrong: %s", mpg123_plain_strerror(error));
  76. break;
  77. }
  78. if (mpg123_open(_mpg123handle, fullPath.c_str()) != MPG123_OK
  79. || mpg123_getformat(_mpg123handle, &rate, &channel, &mp3Encoding) != MPG123_OK)
  80. {
  81. ALOGE("Trouble with mpg123: %s\n", mpg123_strerror(_mpg123handle) );
  82. break;
  83. }
  84. _channelCount = channel;
  85. _sampleRate = rate;
  86. if (mp3Encoding == MPG123_ENC_SIGNED_16)
  87. {
  88. _bytesPerFrame = 2 * _channelCount;
  89. }
  90. else if (mp3Encoding == MPG123_ENC_FLOAT_32)
  91. {
  92. _bytesPerFrame = 4 * _channelCount;
  93. }
  94. else
  95. {
  96. ALOGE("Bad encoding: 0x%x!\n", mp3Encoding);
  97. break;
  98. }
  99. /* Ensure that this output format will not change (it could, when we allow it). */
  100. mpg123_format_none(_mpg123handle);
  101. mpg123_format(_mpg123handle, rate, channel, mp3Encoding);
  102. /* Ensure that we can get accurate length by call mpg123_length */
  103. mpg123_scan(_mpg123handle);
  104. _totalFrames = mpg123_length(_mpg123handle);
  105. _isOpened = true;
  106. return true;
  107. } while (false);
  108. if (_mpg123handle != nullptr)
  109. {
  110. mpg123_close(_mpg123handle);
  111. mpg123_delete(_mpg123handle);
  112. _mpg123handle = nullptr;
  113. }
  114. return false;
  115. }
  116. void AudioDecoderMp3::close()
  117. {
  118. if (isOpened())
  119. {
  120. if (_mpg123handle != nullptr)
  121. {
  122. mpg123_close(_mpg123handle);
  123. mpg123_delete(_mpg123handle);
  124. _mpg123handle = nullptr;
  125. }
  126. _isOpened = false;
  127. }
  128. }
  129. uint32_t AudioDecoderMp3::read(uint32_t framesToRead, char* pcmBuf)
  130. {
  131. int bytesToRead = framesToRead * _bytesPerFrame;
  132. size_t bytesRead = 0;
  133. int err = mpg123_read(_mpg123handle, (unsigned char*)pcmBuf, bytesToRead, &bytesRead);
  134. if (err == MPG123_ERR)
  135. {
  136. ALOGE("Trouble with mpg123: %s\n", mpg123_strerror(_mpg123handle) );
  137. return 0;
  138. }
  139. return static_cast<uint32_t>(bytesRead / _bytesPerFrame);
  140. }
  141. bool AudioDecoderMp3::seek(uint32_t frameOffset)
  142. {
  143. off_t offset = mpg123_seek(_mpg123handle, frameOffset, SEEK_SET);
  144. //ALOGD("mpg123_seek return: %d", (int)offset);
  145. if (offset >= 0 && offset == frameOffset)
  146. {
  147. return true;
  148. }
  149. return false;
  150. }
  151. uint32_t AudioDecoderMp3::tell() const
  152. {
  153. return static_cast<uint32_t>(mpg123_tell(_mpg123handle));
  154. }
  155. }} // namespace cocos2d { namespace experimental {