2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2019-11-24 14:54:45 +08:00
|
|
|
Copyright (c) 2018 HALX99.
|
2019-11-23 20:27:39 +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-30 06:11:17 +08:00
|
|
|
#include "audio/apple/AudioDecoderEXT.h"
|
2019-11-26 02:33:54 +08:00
|
|
|
#include "audio/include/AudioMacros.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "AudioDecoder"
|
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
AudioDecoderEXT::AudioDecoderEXT()
|
|
|
|
: _extRef(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
memset(&_outputFormat, 0, sizeof(_outputFormat));
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
AudioDecoderEXT::~AudioDecoderEXT()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
bool AudioDecoderEXT::open(const std::string& fullPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
CFURLRef fileURL = nil;
|
|
|
|
do
|
|
|
|
{
|
2019-11-30 06:11:17 +08:00
|
|
|
BREAK_IF_ERR_LOG(fullPath.empty(), "Invalid path!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
NSString *fileFullPath = [[NSString alloc] initWithCString:fullPath.c_str() encoding:NSUTF8StringEncoding];
|
2019-11-23 20:27:39 +08:00
|
|
|
fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath];
|
|
|
|
[fileFullPath release];
|
|
|
|
BREAK_IF_ERR_LOG(fileURL == nil, "Converting path to CFURLRef failed!");
|
|
|
|
|
|
|
|
OSStatus status = ExtAudioFileOpenURL(fileURL, &_extRef);
|
2019-11-24 14:54:45 +08:00
|
|
|
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileOpenURL FAILED, Error = %d", (int)status);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
AudioStreamBasicDescription fileFormat;
|
|
|
|
UInt32 propertySize = sizeof(fileFormat);
|
|
|
|
|
|
|
|
// Get the audio data format
|
|
|
|
status = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileDataFormat, &propertySize, &fileFormat);
|
2019-11-24 14:54:45 +08:00
|
|
|
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %d", (int)status);
|
2019-11-23 20:27:39 +08:00
|
|
|
BREAK_IF_ERR_LOG(fileFormat.mChannelsPerFrame > 2, "Unsupported Format, channel count is greater than stereo!");
|
|
|
|
|
|
|
|
// Set the client format to 16 bit signed integer (native-endian) data
|
|
|
|
// Maintain the channel count and sample rate of the original source format
|
|
|
|
_outputFormat.mSampleRate = fileFormat.mSampleRate;
|
|
|
|
_outputFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame;
|
|
|
|
_outputFormat.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
_outputFormat.mFramesPerPacket = 1;
|
|
|
|
_outputFormat.mBitsPerChannel = 16;
|
|
|
|
_outputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
|
|
|
|
|
|
|
|
_sampleRate = _outputFormat.mSampleRate;
|
|
|
|
_channelCount = _outputFormat.mChannelsPerFrame;
|
|
|
|
_bytesPerFrame = 2 * _outputFormat.mChannelsPerFrame;
|
|
|
|
|
|
|
|
_outputFormat.mBytesPerPacket = _bytesPerFrame;
|
|
|
|
_outputFormat.mBytesPerFrame = _bytesPerFrame;
|
|
|
|
|
|
|
|
status = ExtAudioFileSetProperty(_extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_outputFormat), &_outputFormat);
|
2019-11-24 14:54:45 +08:00
|
|
|
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileSetProperty FAILED, Error = %d", (int)status);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Get the total frame count
|
|
|
|
SInt64 totalFrames = 0;
|
|
|
|
propertySize = sizeof(totalFrames);
|
|
|
|
status = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileLengthFrames, &propertySize, &totalFrames);
|
2019-11-24 14:54:45 +08:00
|
|
|
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %d",(int)status);
|
2019-11-30 06:11:17 +08:00
|
|
|
BREAK_IF_ERR_LOG(totalFrames <= 0, "Total frames is 0, it's an invalid audio file: %s", fullPath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
_totalFrames = static_cast<uint32_t>(totalFrames);
|
|
|
|
_isOpened = true;
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (fileURL != nil)
|
|
|
|
CFRelease(fileURL);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
void AudioDecoderEXT::close()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_extRef != nullptr)
|
|
|
|
{
|
|
|
|
ExtAudioFileDispose(_extRef);
|
|
|
|
_extRef = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
uint32_t AudioDecoderEXT::read(uint32_t framesToRead, char* pcmBuf)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned");
|
|
|
|
BREAK_IF_ERR_LOG(framesToRead == INVALID_FRAME_INDEX, "frameToRead is INVALID_FRAME_INDEX");
|
|
|
|
BREAK_IF_ERR_LOG(framesToRead == 0, "frameToRead is 0");
|
|
|
|
BREAK_IF_ERR_LOG(pcmBuf == nullptr, "pcmBuf is nullptr");
|
|
|
|
|
|
|
|
AudioBufferList bufferList;
|
|
|
|
bufferList.mNumberBuffers = 1;
|
|
|
|
bufferList.mBuffers[0].mDataByteSize = framesToRead * _bytesPerFrame;
|
|
|
|
bufferList.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame;
|
|
|
|
bufferList.mBuffers[0].mData = pcmBuf;
|
|
|
|
|
|
|
|
UInt32 frames = framesToRead;
|
|
|
|
OSStatus status = ExtAudioFileRead(_extRef, &frames, &bufferList);
|
|
|
|
BREAK_IF(status != noErr);
|
|
|
|
ret = frames;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
bool AudioDecoderEXT::seek(uint32_t frameOffset)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned");
|
|
|
|
BREAK_IF_ERR_LOG(frameOffset == INVALID_FRAME_INDEX, "frameIndex is INVALID_FRAME_INDEX");
|
|
|
|
|
|
|
|
OSStatus status = ExtAudioFileSeek(_extRef, frameOffset);
|
|
|
|
BREAK_IF(status != noErr);
|
|
|
|
ret = true;
|
|
|
|
} while(false);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-30 06:11:17 +08:00
|
|
|
uint32_t AudioDecoderEXT::tell() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
uint32_t ret = INVALID_FRAME_INDEX;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned");
|
|
|
|
SInt64 frameIndex = INVALID_FRAME_INDEX;
|
|
|
|
OSStatus status = ExtAudioFileTell(_extRef, &frameIndex);
|
|
|
|
BREAK_IF(status != noErr);
|
|
|
|
ret = static_cast<uint32_t>(frameIndex);
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cocos2d {
|