2016-12-29 10:05:13 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
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 "AudioDecoderOgg"
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#include "audio/include/AudioDecoderOgg.h"
|
|
|
|
#include "audio/include/AudioMacros.h"
|
2016-12-29 10:05:13 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
namespace cocos2d {
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
static size_t ov_fread_r(void* buffer, size_t element_size, size_t element_count, void* handle)
|
|
|
|
{
|
2019-11-30 16:20:25 +08:00
|
|
|
return ((PXFileStream*)handle)->read(buffer, static_cast<uint32_t>(element_size * element_count));
|
2019-11-24 14:54:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ov_fseek_r(void * handle, ogg_int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
auto n = ((PXFileStream*)handle)->seek(offset, whence);
|
|
|
|
return n >= 0 ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long ov_ftell_r(void * handle)
|
|
|
|
{
|
|
|
|
return ((PXFileStream*)handle)->seek(0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ov_fclose_r(void* handle) {
|
|
|
|
((PXFileStream*)handle)->close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
AudioDecoderOgg::AudioDecoderOgg()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecoderOgg::~AudioDecoderOgg()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
bool AudioDecoderOgg::open(const std::string& fullPath)
|
2016-12-29 10:05:13 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
if (!_fileStream.open(fullPath))
|
|
|
|
{
|
|
|
|
ALOGE("Trouble with ogg(1): %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ov_callbacks OV_CALLBACKS_POSIX = {
|
|
|
|
ov_fread_r,
|
|
|
|
ov_fseek_r,
|
|
|
|
ov_fclose_r,
|
|
|
|
ov_ftell_r
|
|
|
|
};
|
|
|
|
|
|
|
|
if (0 == ov_open_callbacks(&_fileStream, &_vf, nullptr, 0, OV_CALLBACKS_POSIX))
|
2016-12-29 10:05:13 +08:00
|
|
|
{
|
|
|
|
// header
|
|
|
|
vorbis_info* vi = ov_info(&_vf, -1);
|
|
|
|
_sampleRate = static_cast<uint32_t>(vi->rate);
|
|
|
|
_channelCount = vi->channels;
|
2020-06-04 12:26:25 +08:00
|
|
|
_bytesPerBlock = vi->channels * sizeof(short);
|
2016-12-29 10:05:13 +08:00
|
|
|
_totalFrames = static_cast<uint32_t>(ov_pcm_total(&_vf, -1));
|
|
|
|
_isOpened = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDecoderOgg::close()
|
|
|
|
{
|
|
|
|
if (isOpened())
|
|
|
|
{
|
|
|
|
ov_clear(&_vf);
|
|
|
|
_isOpened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioDecoderOgg::read(uint32_t framesToRead, char* pcmBuf)
|
|
|
|
{
|
|
|
|
int currentSection = 0;
|
2020-06-04 12:26:25 +08:00
|
|
|
int bytesToRead = framesToBytes(framesToRead);
|
2016-12-29 10:05:13 +08:00
|
|
|
long bytesRead = ov_read(&_vf, pcmBuf, bytesToRead, 0, 2, 1, ¤tSection);
|
2020-06-04 12:26:25 +08:00
|
|
|
return bytesToFrames(bytesRead);
|
2016-12-29 10:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioDecoderOgg::seek(uint32_t frameOffset)
|
|
|
|
{
|
|
|
|
return 0 == ov_pcm_seek(&_vf, frameOffset);
|
|
|
|
}
|
2019-10-23 14:58:31 +08:00
|
|
|
} // namespace cocos2d {
|