no message
This commit is contained in:
87
cocos/audio/ohos/AudioDecoderWav.cpp
Normal file
87
cocos/audio/ohos/AudioDecoderWav.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 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 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 "AudioDecoderWav"
|
||||
|
||||
#include "audio/ohos/AudioDecoderWav.h"
|
||||
#include "audio/ohos/FsCallback.h"
|
||||
#include "base/Log.h"
|
||||
|
||||
namespace {
|
||||
snd_callbacks wavCallbacks = {
|
||||
.open = cc::ohosOpen,
|
||||
.read = cc::ohosRead,
|
||||
.seek = cc::ohosSeek,
|
||||
.close = cc::ohosClose,
|
||||
.tell = cc::ohosTell,
|
||||
};
|
||||
}
|
||||
|
||||
namespace cc {
|
||||
|
||||
AudioDecoderWav::AudioDecoderWav() {
|
||||
CC_LOG_DEBUG("Create AudioDecoderWav");
|
||||
}
|
||||
|
||||
AudioDecoderWav::~AudioDecoderWav() {
|
||||
close();
|
||||
}
|
||||
|
||||
bool AudioDecoderWav::open(const char *path) {
|
||||
_sndHandle = sf_open_read(path, &_sndInfo, &wavCallbacks, this);
|
||||
_isOpened = (_sndHandle != nullptr) && _sndInfo.frames > 0;
|
||||
if (!_isOpened) return false;
|
||||
|
||||
_pcmHeader.channelCount = _sndInfo.channels;
|
||||
_pcmHeader.sampleRate = _sndInfo.samplerate;
|
||||
_pcmHeader.bytesPerFrame = 2 * _pcmHeader.channelCount; // short
|
||||
_pcmHeader.totalFrames = _sndInfo.frames;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioDecoderWav::close() {
|
||||
if (_sndHandle) {
|
||||
sf_close(_sndHandle);
|
||||
_sndHandle = nullptr;
|
||||
}
|
||||
_isOpened = false;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoderWav::read(uint32_t framesToRead, char *pcmBuf) {
|
||||
auto *output = reinterpret_cast<int16_t *>(pcmBuf);
|
||||
auto actualFrames = sf_readf_short(_sndHandle, output, framesToRead);
|
||||
return actualFrames;
|
||||
}
|
||||
|
||||
bool AudioDecoderWav::seek(uint32_t frameOffset) {
|
||||
off_t offset = sf_seek(_sndHandle, frameOffset, SEEK_SET);
|
||||
return offset >= 0 && offset == frameOffset;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoderWav::tell() const {
|
||||
return sf_tell(_sndHandle);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
49
cocos/audio/ohos/AudioDecoderWav.h
Normal file
49
cocos/audio/ohos/AudioDecoderWav.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 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 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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "audio/android/tinysndfile.h"
|
||||
#include "audio/common/decoder/AudioDecoder.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class AudioDecoderWav : public AudioDecoder {
|
||||
public:
|
||||
AudioDecoderWav();
|
||||
~AudioDecoderWav() override;
|
||||
|
||||
bool open(const char *path) override;
|
||||
void close() override;
|
||||
uint32_t read(uint32_t framesToRead, char *pcmBuf) override;
|
||||
bool seek(uint32_t frameOffset) override;
|
||||
uint32_t tell() const override;
|
||||
|
||||
private:
|
||||
SF_INFO _sndInfo;
|
||||
SNDFILE *_sndHandle{};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
98
cocos/audio/ohos/FsCallback.cpp
Normal file
98
cocos/audio/ohos/FsCallback.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 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 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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "audio/ohos/FsCallback.h"
|
||||
|
||||
#include <rawfile/raw_dir.h>
|
||||
#include <rawfile/raw_file.h>
|
||||
#include "base/memory/Memory.h"
|
||||
#include "platform/ohos/FileUtils-ohos.h"
|
||||
|
||||
namespace {
|
||||
inline cc::FileUtilsOHOS *getFU() {
|
||||
return static_cast<cc::FileUtilsOHOS *>(cc::FileUtils::getInstance());
|
||||
}
|
||||
|
||||
struct FatFd {
|
||||
union {
|
||||
FILE *fp;
|
||||
RawFile *rf;
|
||||
} file;
|
||||
void *user;
|
||||
bool isRawFile;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace cc {
|
||||
void *ohosOpen(const char *path, void *user) {
|
||||
bool isRawfile = false;
|
||||
const auto newPath = getFU()->expandPath(path, &isRawfile);
|
||||
auto *ret = ccnew FatFd();
|
||||
if (isRawfile) {
|
||||
ret->file.rf = OpenRawFile(cc::FileUtilsOHOS::getResourceManager(), newPath.c_str());
|
||||
} else {
|
||||
ret->file.fp = fopen(newPath.c_str(), "rb");
|
||||
}
|
||||
ret->user = user;
|
||||
ret->isRawFile = isRawfile;
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t ohosRead(void *ptr, size_t size, size_t nmemb, void *datasource) {
|
||||
auto *fatFd = static_cast<FatFd *>(datasource);
|
||||
if (fatFd->isRawFile) {
|
||||
return ReadRawFile(fatFd->file.rf, ptr, size * nmemb) / size;
|
||||
}
|
||||
return fread(ptr, size, nmemb, fatFd->file.fp);
|
||||
}
|
||||
|
||||
int ohosSeek(void *datasource, long offset, int whence) { //NOLINT(google-runtime-int)
|
||||
auto *fatFd = static_cast<FatFd *>(datasource);
|
||||
if (fatFd->isRawFile) {
|
||||
return SeekRawFile(fatFd->file.rf, offset, whence);
|
||||
}
|
||||
return fseek(fatFd->file.fp, offset, whence);
|
||||
}
|
||||
|
||||
int ohosClose(void *datasource) {
|
||||
auto *fatFd = static_cast<FatFd *>(datasource);
|
||||
int code = 0;
|
||||
if (fatFd->isRawFile) {
|
||||
CloseRawFile(fatFd->file.rf);
|
||||
code = 0;
|
||||
} else {
|
||||
code = fclose(fatFd->file.fp);
|
||||
}
|
||||
delete fatFd;
|
||||
return code;
|
||||
}
|
||||
|
||||
long ohosTell(void *datasource) { //NOLINT(google-runtime-int)
|
||||
auto *fatFd = static_cast<FatFd *>(datasource);
|
||||
if (fatFd->isRawFile) {
|
||||
return GetRawFileOffset(fatFd->file.rf);
|
||||
}
|
||||
return ftell(fatFd->file.fp);
|
||||
}
|
||||
} // namespace cc
|
||||
40
cocos/audio/ohos/FsCallback.h
Normal file
40
cocos/audio/ohos/FsCallback.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 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 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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace cc {
|
||||
void *ohosOpen(const char *path, void *user);
|
||||
|
||||
size_t ohosRead(void *ptr, size_t size, size_t nmemb, void *datasource);
|
||||
|
||||
int ohosSeek(void *datasource, long offset, int whence); //NOLINT(google-runtime-int)
|
||||
|
||||
int ohosClose(void *datasource);
|
||||
|
||||
long ohosTell(void *datasource); //NOLINT(google-runtime-int)
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user