/**************************************************************************** Copyright (c) 2016 Chukong Technologies Inc. Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated engine source code (the "Software"), a limited, worldwide, royalty-free, non-assignable, revocable and non-exclusive license to use Cocos Creator solely to develop games on your target platforms. You shall not use Cocos Creator software for developing other software or tools that's used for developing games. You are not granted to publish, distribute, sublicense, and/or sell copies of Cocos Creator. The software or tools in this License Agreement are licensed, not sold. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. 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. ****************************************************************************/ #include "audio/oalsoft/AudioDecoderOgg.h" #include "audio/oalsoft/AudioMacros.h" #include "platform/FileUtils.h" #if CC_PLATFORM == CC_PLATFORM_OHOS #include "audio/ohos/FsCallback.h" namespace { int ohosSeek_wrap(void *source, ogg_int64_t offset, int whence) { //NOLINT return cc::ohosSeek(source, static_cast(offset), whence); //NOLINT } ov_callbacks ogg_callbacks = { //NOLINT static_cast(cc::ohosRead), static_cast(ohosSeek_wrap), static_cast(cc::ohosClose), static_cast(cc::ohosTell)}; //NOLINT } // namespace #endif #ifdef LOG_TAG #undef LOG_TAG #endif #define LOG_TAG "AudioDecoderOgg" namespace cc { AudioDecoderOgg::AudioDecoderOgg() = default; AudioDecoderOgg::~AudioDecoderOgg() { close(); } bool AudioDecoderOgg::open(const char *path) { std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path); #if CC_PLATFORM == CC_PLATFORM_WINDOWS if (0 == ov_fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), &_vf)) { #elif CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX if (0 == ov_fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), &_vf)) { #elif CC_PLATFORM == CC_PLATFORM_OHOS auto *fp = cc::ohosOpen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), this); if (0 == ov_open_callbacks(fp, &_vf, nullptr, 0, ogg_callbacks)) { #endif // header vorbis_info *vi = ov_info(&_vf, -1); _sampleRate = static_cast(vi->rate); _channelCount = vi->channels; _bytesPerFrame = vi->channels * sizeof(int16_t); _totalFrames = static_cast(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; auto bytesToRead = framesToRead * _bytesPerFrame; #if CC_PLATFORM == CC_PLATFORM_WINDOWS auto bytesRead = ov_read(&_vf, pcmBuf, bytesToRead, 0, 2, 1, ¤tSection); #elif CC_PLATFORM == CC_PLATFORM_OHOS int bitstream = 0; auto bytesRead = ov_read(&_vf, pcmBuf, bytesToRead, &bitstream); #elif CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX auto bytesRead = ov_read(&_vf, pcmBuf, bytesToRead, 0, 2, 1, ¤tSection); #endif return static_cast(bytesRead / _bytesPerFrame); } bool AudioDecoderOgg::seek(uint32_t frameOffset) { return 0 == ov_pcm_seek(&_vf, frameOffset); } uint32_t AudioDecoderOgg::tell() const { return static_cast(ov_pcm_tell(const_cast(&_vf))); } } // namespace cc