2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#define LOG_TAG "AudioDecoderMp3"
|
|
|
|
#include "audio/include/AudioDecoderMp3.h"
|
|
|
|
#include "audio/include/AudioMacros.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
#include "base/CCConsole.h"
|
2019-11-24 14:54:45 +08:00
|
|
|
// because the cocos2d-x engine has ssize_t typedef, so disable mpg123 ssize_t
|
|
|
|
#define MPG123_DEF_SSIZE_T
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "mpg123.h"
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
|
|
|
static bool __mp3Inited = false;
|
2019-11-24 14:54:45 +08:00
|
|
|
|
|
|
|
static ssize_t mpg123_read_r(void * handle, void * buffer, size_t count)
|
|
|
|
{
|
|
|
|
return ((PXFileStream*)handle)->read(buffer, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static off_t mpg123_lseek_r(void * handle, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
return ((PXFileStream*)handle)->seek(offset, whence);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void mpg123_close_r(void* handle) {
|
|
|
|
((PXFileStream*)handle)->close();
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool AudioDecoderMp3::lazyInit()
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
if (!__mp3Inited)
|
|
|
|
{
|
|
|
|
int error = mpg123_init();
|
|
|
|
if (error == MPG123_OK)
|
|
|
|
{
|
|
|
|
__mp3Inited = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ALOGE("Basic setup goes wrong: %s", mpg123_plain_strerror(error));
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDecoderMp3::destroy()
|
|
|
|
{
|
|
|
|
if (__mp3Inited)
|
|
|
|
{
|
|
|
|
mpg123_exit();
|
|
|
|
__mp3Inited = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecoderMp3::AudioDecoderMp3()
|
|
|
|
: _mpg123handle(nullptr)
|
|
|
|
{
|
|
|
|
lazyInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecoderMp3::~AudioDecoderMp3()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
bool AudioDecoderMp3::open(const std::string& fullPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
long rate = 0;
|
|
|
|
int error = MPG123_OK;
|
|
|
|
int mp3Encoding = 0;
|
|
|
|
int channel = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_mpg123handle = mpg123_new(nullptr, &error);
|
|
|
|
if (nullptr == _mpg123handle)
|
|
|
|
{
|
|
|
|
ALOGE("Basic setup goes wrong: %s", mpg123_plain_strerror(error));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-24 14:54:45 +08:00
|
|
|
|
|
|
|
if (!_fileStream.open(fullPath))
|
|
|
|
{
|
|
|
|
ALOGE("Trouble with mpg123(1): %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpg123_replace_reader_handle(_mpg123handle, mpg123_read_r, mpg123_lseek_r, mpg123_close_r);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
if (mpg123_open_handle(_mpg123handle, &_fileStream) != MPG123_OK
|
2019-11-23 20:27:39 +08:00
|
|
|
|| mpg123_getformat(_mpg123handle, &rate, &channel, &mp3Encoding) != MPG123_OK)
|
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("Trouble with mpg123(2): %s\n", mpg123_strerror(_mpg123handle));
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_channelCount = channel;
|
|
|
|
_sampleRate = rate;
|
|
|
|
|
|
|
|
if (mp3Encoding == MPG123_ENC_SIGNED_16)
|
|
|
|
{
|
2020-06-03 23:09:45 +08:00
|
|
|
_bytesPerFrame = 2 * _channelCount;
|
2020-05-18 17:30:42 +08:00
|
|
|
_sourceFormat = AUDIO_SOURCE_FORMAT::PCM_16;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (mp3Encoding == MPG123_ENC_FLOAT_32)
|
|
|
|
{
|
2020-06-03 23:09:45 +08:00
|
|
|
_bytesPerFrame = 4 * _channelCount;
|
2020-05-18 17:30:42 +08:00
|
|
|
_sourceFormat = AUDIO_SOURCE_FORMAT::PCM_FLT32;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ALOGE("Bad encoding: 0x%x!\n", mp3Encoding);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that this output format will not change (it could, when we allow it). */
|
|
|
|
mpg123_format_none(_mpg123handle);
|
|
|
|
mpg123_format(_mpg123handle, rate, channel, mp3Encoding);
|
|
|
|
/* Ensure that we can get accurate length by call mpg123_length */
|
|
|
|
mpg123_scan(_mpg123handle);
|
|
|
|
|
|
|
|
_totalFrames = mpg123_length(_mpg123handle);
|
|
|
|
|
|
|
|
_isOpened = true;
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (_mpg123handle != nullptr)
|
|
|
|
{
|
|
|
|
mpg123_close(_mpg123handle);
|
|
|
|
mpg123_delete(_mpg123handle);
|
|
|
|
_mpg123handle = nullptr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDecoderMp3::close()
|
|
|
|
{
|
|
|
|
if (isOpened())
|
|
|
|
{
|
|
|
|
if (_mpg123handle != nullptr)
|
|
|
|
{
|
|
|
|
mpg123_close(_mpg123handle);
|
|
|
|
mpg123_delete(_mpg123handle);
|
|
|
|
_mpg123handle = nullptr;
|
|
|
|
}
|
|
|
|
_isOpened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioDecoderMp3::read(uint32_t framesToRead, char* pcmBuf)
|
|
|
|
{
|
2020-06-03 23:09:45 +08:00
|
|
|
int bytesToRead = (framesToRead * _bytesPerFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
size_t bytesRead = 0;
|
|
|
|
int err = mpg123_read(_mpg123handle, (unsigned char*)pcmBuf, bytesToRead, &bytesRead);
|
|
|
|
if (err == MPG123_ERR)
|
|
|
|
{
|
|
|
|
ALOGE("Trouble with mpg123: %s\n", mpg123_strerror(_mpg123handle) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:09:45 +08:00
|
|
|
return static_cast<uint32_t>((bytesRead) / _bytesPerFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioDecoderMp3::seek(uint32_t frameOffset)
|
|
|
|
{
|
|
|
|
off_t offset = mpg123_seek(_mpg123handle, frameOffset, SEEK_SET);
|
|
|
|
//ALOGD("mpg123_seek return: %d", (int)offset);
|
|
|
|
if (offset >= 0 && offset == frameOffset)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioDecoderMp3::tell() const
|
|
|
|
{
|
|
|
|
return static_cast<uint32_t>(mpg123_tell(_mpg123handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cocos2d {
|