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.
|
2020-05-19 12:52:34 +08:00
|
|
|
Copyright (c) 2018-2019 HALX99.
|
|
|
|
Copyright (c) 2020 c4games.com.
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#include "audio/include/AudioDecoder.h"
|
2021-04-26 12:21:56 +08:00
|
|
|
#include <memory>
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2020-05-19 11:42:54 +08:00
|
|
|
#if !defined(MAKE_FOURCC)
|
|
|
|
#define MAKE_FOURCC(a,b,c,d) ((uint32_t)((a) | ((b) << 8) | ((c) << 16) | (((uint32_t)(d)) << 24)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
typedef struct _GUID {
|
2021-09-02 13:39:28 +08:00
|
|
|
uint32_t Data1;
|
2020-05-19 11:42:54 +08:00
|
|
|
unsigned short Data2;
|
|
|
|
unsigned short Data3;
|
|
|
|
unsigned char Data4[8];
|
|
|
|
} GUID;
|
|
|
|
__inline int IsEqualGUID(const GUID& rguid1, const GUID& rguid2)
|
|
|
|
{
|
|
|
|
return !::memcmp(&rguid1, &rguid2, sizeof(GUID));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-18 14:56:13 +08:00
|
|
|
namespace cocos2d {
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
// http://soundfile.sapp.org/doc/WaveFormat/
|
2020-05-18 14:56:13 +08:00
|
|
|
enum class WAV_FORMAT : uint16_t
|
|
|
|
{
|
|
|
|
UNKNOWN = 0x0, // Unknown Wave Format
|
|
|
|
PCM = 0x1, // PCM Format
|
|
|
|
ADPCM = 0x2, // Microsoft ADPCM Format
|
|
|
|
IEEE = 0x3, // IEEE float/double
|
|
|
|
ALAW = 0x6, // 8-bit ITU-T G.711 A-law
|
|
|
|
MULAW = 0x7, // 8-bit ITU-T G.711 MUL-law
|
|
|
|
IMA_ADPCM = 0x11, // IMA ADPCM Format
|
|
|
|
EXT = 0xFFFE // Set via subformat
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push,1)
|
|
|
|
struct WAV_CHUNK_HEADER {
|
2019-11-24 14:54:45 +08:00
|
|
|
uint32_t ChunkID;
|
2020-05-18 14:56:13 +08:00
|
|
|
/*
|
|
|
|
** The ChunkSize gives the size of the valid data in the chunk. It does not include the padding, the size of chunkID, or the size of chunkSize.
|
|
|
|
** see: https://docs.microsoft.com/en-us/windows/win32/xaudio2/resource-interchange-file-format--riff-
|
|
|
|
*/
|
2019-11-24 14:54:45 +08:00
|
|
|
uint32_t ChunkSize;
|
2020-05-18 14:56:13 +08:00
|
|
|
};
|
|
|
|
struct WAV_RIFF_CHUNK {
|
|
|
|
WAV_CHUNK_HEADER Header;
|
2019-11-24 14:54:45 +08:00
|
|
|
uint32_t Format;
|
2020-05-18 14:56:13 +08:00
|
|
|
};
|
|
|
|
struct WAV_FMT_CHUNK {
|
|
|
|
WAV_CHUNK_HEADER Header;
|
|
|
|
WAV_FORMAT AudioFormat;
|
2019-11-24 14:54:45 +08:00
|
|
|
uint16_t NumChannels;
|
|
|
|
uint32_t SampleRate;
|
|
|
|
uint32_t ByteRate;
|
|
|
|
uint16_t BlockAlign;
|
|
|
|
uint16_t BitsPerSample;
|
2020-05-19 11:42:54 +08:00
|
|
|
struct {
|
|
|
|
/* The follow fields is optional */
|
|
|
|
uint16_t cbSize;
|
|
|
|
union {
|
|
|
|
uint16_t ValidBitsPerSample;
|
|
|
|
uint16_t SamplesPerBlock;
|
|
|
|
uint16_t Reserved;
|
|
|
|
} Samples;
|
2020-05-19 12:49:34 +08:00
|
|
|
uint32_t ChannelMask;
|
2020-05-19 11:42:54 +08:00
|
|
|
GUID SubFormat;
|
|
|
|
} ExtParams;
|
2020-05-18 14:56:13 +08:00
|
|
|
};
|
|
|
|
struct WAV_FILE_HEADER {
|
|
|
|
WAV_RIFF_CHUNK Riff;
|
|
|
|
WAV_FMT_CHUNK Fmt;
|
|
|
|
WAV_CHUNK_HEADER PcmData;
|
2019-11-24 14:54:45 +08:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
struct WAV_FILE
|
|
|
|
{
|
|
|
|
WAV_FILE_HEADER FileHeader;
|
2020-05-19 11:42:54 +08:00
|
|
|
AUDIO_SOURCE_FORMAT SourceFormat;
|
2019-11-24 14:54:45 +08:00
|
|
|
uint32_t PcmDataOffset;
|
2021-04-26 13:43:33 +08:00
|
|
|
std::unique_ptr<FileStream> Stream{};
|
2019-11-24 14:54:45 +08:00
|
|
|
};
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The class for decoding compressed audio file to PCM buffer.
|
|
|
|
*/
|
2019-11-24 14:54:45 +08:00
|
|
|
class AudioDecoderWav : public AudioDecoder
|
2016-12-29 10:05:13 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Opens an audio file specified by a file path.
|
|
|
|
* @return true if succeed, otherwise false.
|
|
|
|
*/
|
2020-06-04 12:26:25 +08:00
|
|
|
bool open(const std::string& path) override;
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2020-06-04 12:26:25 +08:00
|
|
|
/**
|
|
|
|
* @brief The helper function for convert frames to bytes
|
|
|
|
*/
|
2020-06-03 23:09:45 +08:00
|
|
|
uint32_t framesToBytes(uint32_t frames) const override;
|
|
|
|
|
2020-06-04 12:26:25 +08:00
|
|
|
/**
|
|
|
|
* @brief The helper function for convert bytes to frames
|
|
|
|
*/
|
|
|
|
uint32_t bytesToFrames(uint32_t bytes) const override;
|
2020-06-03 23:09:45 +08:00
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
/**
|
|
|
|
* @brief Closes opened audio file.
|
|
|
|
* @note The method will also be automatically invoked in the destructor.
|
|
|
|
*/
|
2020-06-04 12:26:25 +08:00
|
|
|
void close() override;
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads audio frames of PCM format.
|
|
|
|
* @param framesToRead The number of frames excepted to be read.
|
2020-06-04 12:26:25 +08:00
|
|
|
* @param pcmBuf The buffer to hold the frames to be read, its size should be >= |framesToRead| / samplesPerBlock * _bytesPerBlock.
|
2016-12-29 10:05:13 +08:00
|
|
|
* @return The number of frames actually read, it's probably less than 'framesToRead'. Returns 0 means reach the end of file.
|
|
|
|
*/
|
2020-06-04 12:26:25 +08:00
|
|
|
uint32_t read(uint32_t framesToRead, char* pcmBuf) override;
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets frame offest to be read.
|
|
|
|
* @param frameOffset The frame offest to be set.
|
|
|
|
* @return true if succeed, otherwise false
|
|
|
|
*/
|
2020-06-04 12:26:25 +08:00
|
|
|
bool seek(uint32_t frameOffset) override;
|
2020-06-03 23:09:45 +08:00
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
protected:
|
2020-06-04 12:26:25 +08:00
|
|
|
|
|
|
|
friend class AudioDecoderManager;
|
2019-11-24 14:54:45 +08:00
|
|
|
AudioDecoderWav();
|
|
|
|
~AudioDecoderWav();
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
mutable WAV_FILE _wavf;
|
2016-12-29 10:05:13 +08:00
|
|
|
};
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
} // namespace cocos2d {
|