2022-01-03 11:42:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2022-01-03 11:42:07 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define LOG_TAG "AudioDecoderOgg"
|
|
|
|
|
|
|
|
#include "audio/AudioDecoderOgg.h"
|
|
|
|
#include "audio/AudioMacros.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/FileUtils.h"
|
2022-01-03 11:42:07 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
2022-01-03 11:42:07 +08:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2022-01-03 11:42:07 +08:00
|
|
|
|
|
|
|
static size_t ov_fread_r(void* buffer, size_t element_size, size_t element_count, void* handle)
|
|
|
|
{
|
2023-08-31 22:08:50 +08:00
|
|
|
auto* fs = static_cast<FileStream*>(handle);
|
2022-01-03 11:42:07 +08:00
|
|
|
return fs->read(buffer, static_cast<uint32_t>(element_size * element_count));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ov_fseek_r(void* handle, ogg_int64_t offset, int whence)
|
|
|
|
{
|
2023-08-31 22:08:50 +08:00
|
|
|
auto* fs = static_cast<FileStream*>(handle);
|
2022-01-03 11:42:07 +08:00
|
|
|
return fs->seek(offset, whence) < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long ov_ftell_r(void* handle)
|
|
|
|
{
|
2023-08-31 22:08:50 +08:00
|
|
|
auto* fs = static_cast<FileStream*>(handle);
|
2022-01-03 11:42:07 +08:00
|
|
|
return fs->tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ov_fclose_r(void* handle)
|
|
|
|
{
|
2023-08-31 22:08:50 +08:00
|
|
|
auto* fs = static_cast<FileStream*>(handle);
|
2022-01-03 11:42:07 +08:00
|
|
|
delete fs;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecoderOgg::AudioDecoderOgg() {}
|
|
|
|
|
|
|
|
AudioDecoderOgg::~AudioDecoderOgg()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioDecoderOgg::open(std::string_view fullPath)
|
|
|
|
{
|
2023-08-31 22:08:50 +08:00
|
|
|
auto fs = FileUtils::getInstance()->openFileStream(fullPath, FileStream::Mode::READ).release();
|
2022-01-03 11:42:07 +08:00
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
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(fs, &_vf, nullptr, 0, OV_CALLBACKS_POSIX))
|
|
|
|
{
|
|
|
|
// header
|
|
|
|
vorbis_info* vi = ov_info(&_vf, -1);
|
|
|
|
_sampleRate = static_cast<uint32_t>(vi->rate);
|
|
|
|
_channelCount = vi->channels;
|
|
|
|
_bytesPerBlock = vi->channels * sizeof(short);
|
|
|
|
_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;
|
|
|
|
int bytesToRead = framesToBytes(framesToRead);
|
2022-11-20 08:58:34 +08:00
|
|
|
int32_t bytesRead = static_cast<int32_t>(ov_read(&_vf, pcmBuf, bytesToRead, 0, 2, 1, ¤tSection));
|
2022-01-03 11:42:07 +08:00
|
|
|
return bytesToFrames(bytesRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioDecoderOgg::seek(uint32_t frameOffset)
|
|
|
|
{
|
|
|
|
return 0 == ov_pcm_seek(&_vf, frameOffset);
|
|
|
|
}
|
2022-08-29 20:51:22 +08:00
|
|
|
NS_AX_END // namespace ax
|
2023-07-15 19:06:54 +08:00
|
|
|
|
|
|
|
#undef LOG_TAG
|