mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
@@ -552,6 +552,9 @@
|
||||
1A570311180BCF190088DEC7 /* CCComponentContainer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A57030A180BCF190088DEC7 /* CCComponentContainer.cpp */; };
|
||||
1A570312180BCF190088DEC7 /* CCComponentContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A57030B180BCF190088DEC7 /* CCComponentContainer.h */; };
|
||||
1A570313180BCF190088DEC7 /* CCComponentContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A57030B180BCF190088DEC7 /* CCComponentContainer.h */; };
|
||||
1A5FB7CA1DF10E3500C918C1 /* AudioDecoder.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */; };
|
||||
1A5FB7CB1DF10E3500C918C1 /* AudioDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */; };
|
||||
1A5FB7CC1DF10E3500C918C1 /* AudioMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */; };
|
||||
1A9DCA27180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */; };
|
||||
1A9DCA28180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */; };
|
||||
1A9DCA29180E6955007A3AD4 /* CCGLBufferedNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A9DCA03180E6955007A3AD4 /* CCGLBufferedNode.h */; };
|
||||
@@ -1823,6 +1826,9 @@
|
||||
1A570309180BCF190088DEC7 /* CCComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCComponent.h; sourceTree = "<group>"; };
|
||||
1A57030A180BCF190088DEC7 /* CCComponentContainer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCComponentContainer.cpp; sourceTree = "<group>"; };
|
||||
1A57030B180BCF190088DEC7 /* CCComponentContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCComponentContainer.h; sourceTree = "<group>"; };
|
||||
1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = AudioDecoder.mm; sourceTree = "<group>"; };
|
||||
1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioDecoder.h; sourceTree = "<group>"; };
|
||||
1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioMacros.h; sourceTree = "<group>"; };
|
||||
1A97ABFC1A1D962A0076D9CC /* MathUtilNeon64.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MathUtilNeon64.inl; sourceTree = "<group>"; };
|
||||
1A97ABFD1A1D962A0076D9CC /* MathUtilSSE.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MathUtilSSE.inl; sourceTree = "<group>"; };
|
||||
1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCGLBufferedNode.cpp; sourceTree = "<group>"; };
|
||||
@@ -3936,8 +3942,11 @@
|
||||
children = (
|
||||
50CB246F19D9C5A100687767 /* AudioCache.h */,
|
||||
50CB247019D9C5A100687767 /* AudioCache.mm */,
|
||||
1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */,
|
||||
1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */,
|
||||
50CB247119D9C5A100687767 /* AudioEngine-inl.h */,
|
||||
50CB247219D9C5A100687767 /* AudioEngine-inl.mm */,
|
||||
1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */,
|
||||
50CB247319D9C5A100687767 /* AudioPlayer.h */,
|
||||
50CB247419D9C5A100687767 /* AudioPlayer.mm */,
|
||||
);
|
||||
@@ -4471,6 +4480,7 @@
|
||||
BAFF7C781D59E0DA0051B92F /* CCActionFrameEasing.h in Headers */,
|
||||
1A5701FD180BCBAD0088DEC7 /* CCMenuItem.h in Headers */,
|
||||
A63CF00E1CD9CF3500A6971D /* CCUITextInput.h in Headers */,
|
||||
1A5FB7CB1DF10E3500C918C1 /* AudioDecoder.h in Headers */,
|
||||
ED3057831BEC76C90083C3ED /* unzip.h in Headers */,
|
||||
1A570204180BCBD40088DEC7 /* CCClippingNode.h in Headers */,
|
||||
BAFF7CE61D59E0DB0051B92F /* CCUtilMath.h in Headers */,
|
||||
@@ -4577,6 +4587,7 @@
|
||||
50ABBDA11925AB4100A911A9 /* CCGroupCommand.h in Headers */,
|
||||
BAFF7D6C1D5C1CF80051B92F /* BoneData.h in Headers */,
|
||||
29394CF019B01DBA00D2DE1A /* UIWebView.h in Headers */,
|
||||
1A5FB7CC1DF10E3500C918C1 /* AudioMacros.h in Headers */,
|
||||
BAFF7DBC1D5C1CF80051B92F /* SkeletonRenderer.h in Headers */,
|
||||
15AE186519AAD31D00C27E9E /* CDOpenALSupport.h in Headers */,
|
||||
15AE1B5C19AADA9900C27E9E /* UITextAtlas.h in Headers */,
|
||||
@@ -5506,6 +5517,7 @@
|
||||
1A5702F2180BCE750088DEC7 /* CCTMXObjectGroup.cpp in Sources */,
|
||||
BAFF7CE41D59E0DB0051B92F /* CCUtilMath.cpp in Sources */,
|
||||
15AE189419AAD33D00C27E9E /* CCLayerLoader.cpp in Sources */,
|
||||
1A5FB7CA1DF10E3500C918C1 /* AudioDecoder.mm in Sources */,
|
||||
826294351AAF004C00CB7CF7 /* HttpCookie.cpp in Sources */,
|
||||
BAFF7DD41D5C1CF80051B92F /* TransformConstraintData.c in Sources */,
|
||||
1A5702FA180BCE750088DEC7 /* CCTMXXMLParser.cpp in Sources */,
|
||||
|
||||
@@ -28,41 +28,13 @@
|
||||
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
||||
|
||||
#import <OpenAL/al.h>
|
||||
#import <AudioToolbox/AudioToolbox.h>
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "platform/CCPlatformMacros.h"
|
||||
|
||||
#define QUEUEBUFFER_NUM 3
|
||||
#define QUEUEBUFFER_TIME_STEP 0.1
|
||||
|
||||
#define QUOTEME_(x) #x
|
||||
#define QUOTEME(x) QUOTEME_(x)
|
||||
|
||||
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
||||
#define ALOGV(fmt, ...) printf("V/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define ALOGV(fmt, ...) do {} while(false)
|
||||
#endif
|
||||
#define ALOGD(fmt, ...) printf("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGI(fmt, ...) printf("I/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGW(fmt, ...) printf("W/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGE(fmt, ...) printf("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
|
||||
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
||||
#define CHECK_AL_ERROR_DEBUG() \
|
||||
do { \
|
||||
GLenum __error = alGetError(); \
|
||||
if (__error) { \
|
||||
ALOGE("OpenAL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
|
||||
} \
|
||||
} while (false)
|
||||
#else
|
||||
#define CHECK_AL_ERROR_DEBUG()
|
||||
#endif
|
||||
#include "audio/apple/AudioMacros.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
namespace experimental{
|
||||
@@ -98,27 +70,24 @@ protected:
|
||||
void invokingLoadCallbacks();
|
||||
|
||||
//pcm data related stuff
|
||||
ALsizei _dataSize;
|
||||
ALenum _format;
|
||||
ALsizei _sampleRate;
|
||||
float _duration;
|
||||
int _bytesPerFrame;
|
||||
AudioStreamBasicDescription _outputFormat;
|
||||
uint32_t _totalFrames;
|
||||
uint32_t _framesRead;
|
||||
|
||||
/*Cache related stuff;
|
||||
* Cache pcm data when sizeInBytes less than PCMDATA_CACHEMAXSIZE
|
||||
*/
|
||||
ALuint _alBufferId;
|
||||
char* _pcmData;
|
||||
SInt64 _bytesOfRead;
|
||||
|
||||
/*Queue buffer related stuff
|
||||
* Streaming in openal when sizeInBytes greater then PCMDATA_CACHEMAXSIZE
|
||||
*/
|
||||
char* _queBuffers[QUEUEBUFFER_NUM];
|
||||
ALsizei _queBufferSize[QUEUEBUFFER_NUM];
|
||||
UInt32 _queBufferFrames;
|
||||
UInt32 _queBufferBytes;
|
||||
uint32_t _queBufferFrames;
|
||||
|
||||
std::mutex _playCallbackMutex;
|
||||
std::vector< std::function<void()> > _playCallbacks;
|
||||
@@ -138,10 +107,9 @@ protected:
|
||||
|
||||
friend class AudioEngineImpl;
|
||||
friend class AudioPlayer;
|
||||
} ;
|
||||
};
|
||||
|
||||
}
|
||||
NS_CC_END
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -31,11 +31,12 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <OpenAL/alc.h>
|
||||
#import <AudioToolbox/ExtendedAudioFile.h>
|
||||
#include <thread>
|
||||
#include "base/CCDirector.h"
|
||||
#include "base/CCScheduler.h"
|
||||
|
||||
#include "audio/apple/AudioDecoder.h"
|
||||
|
||||
#define VERY_VERY_VERBOSE_LOGGING
|
||||
#ifdef VERY_VERY_VERBOSE_LOGGING
|
||||
#define ALOGVV ALOGV
|
||||
@@ -69,15 +70,13 @@ using namespace cocos2d;
|
||||
using namespace cocos2d::experimental;
|
||||
|
||||
AudioCache::AudioCache()
|
||||
: _dataSize(0)
|
||||
: _totalFrames(0)
|
||||
, _framesRead(0)
|
||||
, _format(-1)
|
||||
, _duration(0.0f)
|
||||
, _bytesPerFrame(0)
|
||||
, _alBufferId(INVALID_AL_BUFFER_ID)
|
||||
, _pcmData(nullptr)
|
||||
, _bytesOfRead(0)
|
||||
, _queBufferFrames(0)
|
||||
, _queBufferBytes(0)
|
||||
, _state(State::INITIAL)
|
||||
, _isDestroyed(std::make_shared<bool>(false))
|
||||
, _id(++__idIndex)
|
||||
@@ -90,8 +89,6 @@ AudioCache::AudioCache()
|
||||
_queBuffers[i] = nullptr;
|
||||
_queBufferSize[i] = 0;
|
||||
}
|
||||
|
||||
memset(&_outputFormat, 0, sizeof(_outputFormat));
|
||||
}
|
||||
|
||||
AudioCache::~AudioCache()
|
||||
@@ -111,7 +108,7 @@ AudioCache::~AudioCache()
|
||||
//wait for the 'readDataTask' task to exit
|
||||
_readDataTaskMutex.lock();
|
||||
_readDataTaskMutex.unlock();
|
||||
|
||||
|
||||
if (_pcmData)
|
||||
{
|
||||
if (_state == State::READY)
|
||||
@@ -127,10 +124,10 @@ AudioCache::~AudioCache()
|
||||
{
|
||||
ALOGW("AudioCache (%p), id=%u, buffer isn't ready, state=%d", this, _id, _state);
|
||||
}
|
||||
|
||||
|
||||
free(_pcmData);
|
||||
}
|
||||
|
||||
|
||||
if (_queBufferFrames > 0)
|
||||
{
|
||||
for (int index = 0; index < QUEUEBUFFER_NUM; ++index)
|
||||
@@ -145,182 +142,150 @@ void AudioCache::readDataTask(unsigned int selfId)
|
||||
{
|
||||
//Note: It's in sub thread
|
||||
ALOGVV("readDataTask, cache id=%u", selfId);
|
||||
|
||||
|
||||
_readDataTaskMutex.lock();
|
||||
_state = State::LOADING;
|
||||
|
||||
CFURLRef fileURL = nil;
|
||||
ExtAudioFileRef extRef = nullptr;
|
||||
|
||||
|
||||
AudioDecoder decoder;
|
||||
do
|
||||
{
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
AudioStreamBasicDescription theFileFormat;
|
||||
UInt32 thePropertySize = sizeof(theFileFormat);
|
||||
|
||||
SInt64 theFileLengthInFrames;
|
||||
SInt64 readInFrames;
|
||||
SInt64 dataSize;
|
||||
SInt64 frames;
|
||||
AudioBufferList theDataBuffer;
|
||||
|
||||
NSString *fileFullPath = [[NSString alloc] initWithCString:_fileFullPath.c_str() encoding:NSUTF8StringEncoding];
|
||||
fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath];
|
||||
[fileFullPath release];
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
if (!decoder.open(_fileFullPath.c_str()))
|
||||
break;
|
||||
|
||||
auto error = ExtAudioFileOpenURL(fileURL, &extRef);
|
||||
if (error) {
|
||||
ALOGE("%s: ExtAudioFileOpenURL FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
// Get the audio data format
|
||||
error = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat);
|
||||
if(error)
|
||||
const uint32_t originalTotalFrames = decoder.getTotalFrames();
|
||||
const uint32_t bytesPerFrame = decoder.getBytesPerFrame();
|
||||
const uint32_t sampleRate = decoder.getSampleRate();
|
||||
const uint32_t channelCount = decoder.getChannelCount();
|
||||
|
||||
uint32_t totalFrames = originalTotalFrames;
|
||||
uint32_t dataSize = totalFrames * bytesPerFrame;
|
||||
uint32_t remainingFrames = totalFrames;
|
||||
uint32_t adjustFrames = 0;
|
||||
|
||||
_format = channelCount > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
|
||||
_sampleRate = (ALsizei)sampleRate;
|
||||
_duration = 1.0f * totalFrames / sampleRate;
|
||||
_totalFrames = totalFrames;
|
||||
|
||||
if (dataSize <= PCMDATA_CACHEMAXSIZE)
|
||||
{
|
||||
ALOGE("%s: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error);
|
||||
break;
|
||||
}
|
||||
|
||||
if (theFileFormat.mChannelsPerFrame > 2)
|
||||
{
|
||||
ALOGE("%s: Unsupported Format, channel count is greater than stereo",__PRETTY_FUNCTION__);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
// 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 = theFileFormat.mSampleRate;
|
||||
_outputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame;
|
||||
|
||||
_bytesPerFrame = 2 * _outputFormat.mChannelsPerFrame;
|
||||
_outputFormat.mFormatID = kAudioFormatLinearPCM;
|
||||
_outputFormat.mBytesPerPacket = _bytesPerFrame;
|
||||
_outputFormat.mFramesPerPacket = 1;
|
||||
_outputFormat.mBytesPerFrame = _bytesPerFrame;
|
||||
_outputFormat.mBitsPerChannel = 16;
|
||||
_outputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
|
||||
|
||||
error = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_outputFormat), &_outputFormat);
|
||||
if(error)
|
||||
{
|
||||
ALOGE("%s: ExtAudioFileSetProperty FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
// Get the total frame count
|
||||
thePropertySize = sizeof(theFileLengthInFrames);
|
||||
error = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
|
||||
if(error)
|
||||
{
|
||||
ALOGE("%s: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
_dataSize = (ALsizei)(theFileLengthInFrames * _outputFormat.mBytesPerFrame);
|
||||
_format = (_outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
|
||||
_sampleRate = (ALsizei)_outputFormat.mSampleRate;
|
||||
_duration = 1.0f * theFileLengthInFrames / _outputFormat.mSampleRate;
|
||||
|
||||
if (_dataSize <= PCMDATA_CACHEMAXSIZE)
|
||||
{
|
||||
_pcmData = (char*)malloc(_dataSize);
|
||||
uint32_t framesRead = 0;
|
||||
const uint32_t framesToReadOnce = std::min(totalFrames, static_cast<uint32_t>(sampleRate * QUEUEBUFFER_TIME_STEP * QUEUEBUFFER_NUM));
|
||||
|
||||
BREAK_IF_ERR_LOG(!decoder.seek(totalFrames), "AudioDecoder::seek(%u) error", totalFrames);
|
||||
|
||||
char* tmpBuf = (char*)malloc(framesToReadOnce * bytesPerFrame);
|
||||
std::vector<char> adjustFrameBuf;
|
||||
adjustFrameBuf.reserve(framesToReadOnce * bytesPerFrame);
|
||||
|
||||
do
|
||||
{
|
||||
framesRead = decoder.read(framesToReadOnce, tmpBuf);
|
||||
if (framesRead > 0)
|
||||
{
|
||||
adjustFrames += framesRead;
|
||||
adjustFrameBuf.insert(adjustFrameBuf.end(), tmpBuf, tmpBuf + framesRead * bytesPerFrame);
|
||||
}
|
||||
|
||||
} while (framesRead > 0);
|
||||
|
||||
if (adjustFrames > 0)
|
||||
{
|
||||
ALOGV("Orignal total frames: %u, adjust frames: %u, current total frames: %u", totalFrames, adjustFrames, totalFrames + adjustFrames);
|
||||
totalFrames += adjustFrames;
|
||||
_totalFrames = remainingFrames = totalFrames;
|
||||
}
|
||||
|
||||
// Reset dataSize
|
||||
dataSize = totalFrames * bytesPerFrame;
|
||||
|
||||
free(tmpBuf);
|
||||
|
||||
// Reset to frame 0
|
||||
BREAK_IF_ERR_LOG(!decoder.seek(0), "AudioDecoder::seek(0) failed!");
|
||||
|
||||
_pcmData = (char*)malloc(dataSize);
|
||||
memset(_pcmData, 0, dataSize);
|
||||
|
||||
if (adjustFrames > 0)
|
||||
{
|
||||
memcpy(_pcmData + (dataSize - adjustFrameBuf.size()), adjustFrameBuf.data(), adjustFrameBuf.size());
|
||||
}
|
||||
|
||||
alGenBuffers(1, &_alBufferId);
|
||||
auto alError = alGetError();
|
||||
if (alError != AL_NO_ERROR) {
|
||||
ALOGE("%s: attaching audio to buffer fail: %x", __PRETTY_FUNCTION__, alError);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
alBufferDataStaticProc(_alBufferId, _format, _pcmData, _dataSize, _sampleRate);
|
||||
|
||||
readInFrames = theFileFormat.mSampleRate * QUEUEBUFFER_TIME_STEP * QUEUEBUFFER_NUM;
|
||||
dataSize = _outputFormat.mBytesPerFrame * readInFrames;
|
||||
if (dataSize > _dataSize) {
|
||||
dataSize = _dataSize;
|
||||
readInFrames = theFileLengthInFrames;
|
||||
}
|
||||
theDataBuffer.mNumberBuffers = 1;
|
||||
theDataBuffer.mBuffers[0].mDataByteSize = (UInt32)dataSize;
|
||||
theDataBuffer.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame;
|
||||
|
||||
theDataBuffer.mBuffers[0].mData = _pcmData;
|
||||
frames = readInFrames;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
|
||||
if (*_isDestroyed) break;
|
||||
|
||||
|
||||
if (*_isDestroyed)
|
||||
break;
|
||||
|
||||
alBufferDataStaticProc(_alBufferId, _format, _pcmData, (ALsizei)dataSize, (ALsizei)sampleRate);
|
||||
|
||||
do
|
||||
{
|
||||
framesRead = decoder.read(std::min(framesToReadOnce, remainingFrames), _pcmData + _framesRead * bytesPerFrame);
|
||||
if (framesRead == 0)
|
||||
break;
|
||||
_framesRead += framesRead;
|
||||
remainingFrames -= framesRead;
|
||||
} while (_framesRead < framesToReadOnce);
|
||||
|
||||
if (*_isDestroyed)
|
||||
break;
|
||||
|
||||
_state = State::READY;
|
||||
|
||||
_bytesOfRead += dataSize;
|
||||
|
||||
invokingPlayCallbacks();
|
||||
|
||||
while (!*_isDestroyed && _bytesOfRead + dataSize < _dataSize) {
|
||||
theDataBuffer.mBuffers[0].mData = _pcmData + _bytesOfRead;
|
||||
frames = readInFrames;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
_bytesOfRead += dataSize; //FIXME: the buffer size of read frames may be fewer than dataSize.
|
||||
|
||||
uint32_t frames = 0;
|
||||
while (!*_isDestroyed && _framesRead < originalTotalFrames)
|
||||
{
|
||||
frames = std::min(framesToReadOnce, remainingFrames);
|
||||
if (_framesRead + frames > originalTotalFrames)
|
||||
{
|
||||
frames = originalTotalFrames - _framesRead;
|
||||
}
|
||||
framesRead = decoder.read(frames, _pcmData + _framesRead * bytesPerFrame);
|
||||
if (framesRead == 0)
|
||||
break;
|
||||
_framesRead += framesRead;
|
||||
remainingFrames -= framesRead;
|
||||
}
|
||||
|
||||
dataSize = _dataSize - _bytesOfRead;
|
||||
if (!*_isDestroyed && dataSize > 0) {
|
||||
theDataBuffer.mBuffers[0].mDataByteSize = (UInt32)dataSize;
|
||||
theDataBuffer.mBuffers[0].mData = _pcmData + _bytesOfRead;
|
||||
frames = readInFrames;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
}
|
||||
|
||||
_bytesOfRead = _dataSize;
|
||||
|
||||
CCASSERT(_framesRead == originalTotalFrames, "The read frame count isn't equal to orignal frame count!");
|
||||
ALOGV("pcm buffer was loaded successfully, total frames: %u, total read frames: %u, adjust frames: %u, remainingFrames: %u", totalFrames, _framesRead, adjustFrames, remainingFrames);
|
||||
|
||||
_framesRead += adjustFrames;
|
||||
}
|
||||
else
|
||||
{
|
||||
_queBufferFrames = theFileFormat.mSampleRate * QUEUEBUFFER_TIME_STEP;
|
||||
if (_queBufferFrames == 0)
|
||||
break;
|
||||
|
||||
_queBufferBytes = _queBufferFrames * _outputFormat.mBytesPerFrame;
|
||||
|
||||
theDataBuffer.mNumberBuffers = 1;
|
||||
theDataBuffer.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame;
|
||||
for (int index = 0; index < QUEUEBUFFER_NUM; ++index) {
|
||||
_queBuffers[index] = (char*)malloc(_queBufferBytes);
|
||||
|
||||
theDataBuffer.mBuffers[0].mDataByteSize = _queBufferBytes;
|
||||
theDataBuffer.mBuffers[0].mData = _queBuffers[index];
|
||||
frames = _queBufferFrames;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
|
||||
_queBufferSize[index] = theDataBuffer.mBuffers[0].mDataByteSize;
|
||||
_queBufferFrames = sampleRate * QUEUEBUFFER_TIME_STEP;
|
||||
BREAK_IF_ERR_LOG(_queBufferFrames == 0, "_queBufferFrames == 0");
|
||||
|
||||
const uint32_t queBufferBytes = _queBufferFrames * bytesPerFrame;
|
||||
|
||||
for (int index = 0; index < QUEUEBUFFER_NUM; ++index)
|
||||
{
|
||||
_queBuffers[index] = (char*)malloc(queBufferBytes);
|
||||
_queBufferSize[index] = queBufferBytes;
|
||||
|
||||
decoder.read(_queBufferFrames, _queBuffers[index]);
|
||||
}
|
||||
|
||||
|
||||
_state = State::READY;
|
||||
}
|
||||
|
||||
|
||||
} while (false);
|
||||
|
||||
if (fileURL != nil)
|
||||
CFRelease(fileURL);
|
||||
|
||||
if (extRef != nullptr)
|
||||
ExtAudioFileDispose(extRef);
|
||||
|
||||
|
||||
decoder.close();
|
||||
|
||||
//FIXME: Why to invoke play callback first? Should it be after 'load' callback?
|
||||
invokingPlayCallbacks();
|
||||
invokingLoadCallbacks();
|
||||
|
||||
|
||||
_isLoadingFinished = true;
|
||||
if (_state != State::READY)
|
||||
{
|
||||
@@ -332,7 +297,7 @@ void AudioCache::readDataTask(unsigned int selfId)
|
||||
_alBufferId = INVALID_AL_BUFFER_ID;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_readDataTaskMutex.unlock();
|
||||
}
|
||||
|
||||
@@ -345,14 +310,14 @@ void AudioCache::addPlayCallback(const std::function<void()>& callback)
|
||||
case State::LOADING:
|
||||
_playCallbacks.push_back(callback);
|
||||
break;
|
||||
|
||||
|
||||
case State::READY:
|
||||
// If state is failure, we still need to invoke the callback
|
||||
// since the callback will set the 'AudioPlayer::_removeByAudioEngine' flag to true.
|
||||
case State::FAILED:
|
||||
callback();
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
ALOGE("Invalid state: %d", _state);
|
||||
break;
|
||||
@@ -362,12 +327,12 @@ void AudioCache::addPlayCallback(const std::function<void()>& callback)
|
||||
void AudioCache::invokingPlayCallbacks()
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(_playCallbackMutex);
|
||||
|
||||
|
||||
for (auto&& cb : _playCallbacks)
|
||||
{
|
||||
cb();
|
||||
}
|
||||
|
||||
|
||||
_playCallbacks.clear();
|
||||
}
|
||||
|
||||
@@ -379,14 +344,14 @@ void AudioCache::addLoadCallback(const std::function<void(bool)>& callback)
|
||||
case State::LOADING:
|
||||
_loadCallbacks.push_back(callback);
|
||||
break;
|
||||
|
||||
|
||||
case State::READY:
|
||||
callback(true);
|
||||
break;
|
||||
case State::FAILED:
|
||||
callback(false);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
ALOGE("Invalid state: %d", _state);
|
||||
break;
|
||||
@@ -400,7 +365,7 @@ void AudioCache::invokingLoadCallbacks()
|
||||
ALOGV("AudioCache (%p) was destroyed, don't invoke preload callback ...", this);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
auto isDestroyed = _isDestroyed;
|
||||
auto scheduler = Director::getInstance()->getScheduler();
|
||||
scheduler->performFunctionInCocosThread([&, isDestroyed](){
|
||||
@@ -409,11 +374,12 @@ void AudioCache::invokingLoadCallbacks()
|
||||
ALOGV("invokingLoadCallbacks perform in cocos thread, AudioCache (%p) was destroyed!", this);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
for (auto&& cb : _loadCallbacks)
|
||||
{
|
||||
cb(_state == State::READY);
|
||||
}
|
||||
|
||||
_loadCallbacks.clear();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2016 Chukong Technologies Inc.
|
||||
|
||||
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
|
||||
|
||||
#include <stdint.h>
|
||||
#import <AudioToolbox/ExtendedAudioFile.h>
|
||||
|
||||
namespace cocos2d { namespace experimental {
|
||||
|
||||
/**
|
||||
* @brief The class for decoding compressed audio file to PCM buffer.
|
||||
*/
|
||||
class AudioDecoder
|
||||
{
|
||||
public:
|
||||
static const uint32_t INVALID_FRAME_INDEX = UINT32_MAX;
|
||||
|
||||
AudioDecoder();
|
||||
~AudioDecoder();
|
||||
|
||||
/**
|
||||
* @brief Opens an audio file specified by a file path.
|
||||
* @return true if succeed, otherwise false.
|
||||
*/
|
||||
bool open(const char* path);
|
||||
|
||||
/**
|
||||
* @brief Checks whether decoder has opened file successfully.
|
||||
* @return true if succeed, otherwise false.
|
||||
*/
|
||||
bool isOpened() const;
|
||||
|
||||
/**
|
||||
* @brief Closes opened audio file.
|
||||
* @note The method will also be automatically invoked in the destructor.
|
||||
*/
|
||||
void close();
|
||||
|
||||
/**
|
||||
* @brief Reads audio frames of PCM format.
|
||||
* @param framesToRead The number of frames excepted to be read.
|
||||
* @param pcmBuf The buffer to hold the frames to be read, its size should be >= framesToRead * _bytesPerFrame.
|
||||
* @return The number of frames actually read, it's probably less than 'framesToRead'. Returns 0 means reach the end of file.
|
||||
*/
|
||||
uint32_t read(uint32_t framesToRead, char* pcmBuf);
|
||||
|
||||
/**
|
||||
* @brief Sets frame offest to be read.
|
||||
* @param frameOffset The frame offest to be set.
|
||||
* @return true if succeed, otherwise false
|
||||
*/
|
||||
bool seek(uint32_t frameOffset);
|
||||
|
||||
/**
|
||||
* @brief Tells the current frame offset.
|
||||
* @return The current frame offset.
|
||||
*/
|
||||
uint32_t tell() const;
|
||||
|
||||
/** Gets total frames of current audio.*/
|
||||
uint32_t getTotalFrames() const;
|
||||
|
||||
/** Gets bytes per frame of current audio.*/
|
||||
uint32_t getBytesPerFrame() const;
|
||||
|
||||
/** Gets sample rate of current audio.*/
|
||||
uint32_t getSampleRate() const;
|
||||
|
||||
/** Gets the channel count of current audio.
|
||||
* @note Currently we only support 1 or 2 channels.
|
||||
*/
|
||||
uint32_t getChannelCount() const;
|
||||
|
||||
private:
|
||||
bool _isOpened;
|
||||
ExtAudioFileRef _extRef;
|
||||
uint32_t _totalFrames;
|
||||
uint32_t _bytesPerFrame;
|
||||
uint32_t _sampleRate;
|
||||
uint32_t _channelCount;
|
||||
|
||||
AudioStreamBasicDescription _outputFormat;
|
||||
};
|
||||
|
||||
}} // namespace cocos2d { namespace experimental {
|
||||
@@ -0,0 +1,207 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2016 Chukong Technologies Inc.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "audio/apple/AudioDecoder.h"
|
||||
#include "audio/apple/AudioMacros.h"
|
||||
|
||||
#define LOG_TAG "AudioDecoder"
|
||||
|
||||
namespace cocos2d { namespace experimental {
|
||||
|
||||
AudioDecoder::AudioDecoder()
|
||||
: _isOpened(false)
|
||||
, _extRef(nullptr)
|
||||
, _totalFrames(0)
|
||||
, _bytesPerFrame(0)
|
||||
, _sampleRate(0)
|
||||
, _channelCount(0)
|
||||
{
|
||||
memset(&_outputFormat, 0, sizeof(_outputFormat));
|
||||
}
|
||||
|
||||
AudioDecoder::~AudioDecoder()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
bool AudioDecoder::open(const char* path)
|
||||
{
|
||||
bool ret = false;
|
||||
CFURLRef fileURL = nil;
|
||||
do
|
||||
{
|
||||
BREAK_IF_ERR_LOG(path == nullptr || strlen(path) == 0, "Invalid path!");
|
||||
|
||||
NSString *fileFullPath = [[NSString alloc] initWithCString:path encoding:NSUTF8StringEncoding];
|
||||
fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath];
|
||||
[fileFullPath release];
|
||||
BREAK_IF_ERR_LOG(fileURL == nil, "Converting path to CFURLRef failed!");
|
||||
|
||||
OSStatus status = ExtAudioFileOpenURL(fileURL, &_extRef);
|
||||
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileOpenURL FAILED, Error = %ld", (long)ret);
|
||||
|
||||
AudioStreamBasicDescription fileFormat;
|
||||
UInt32 propertySize = sizeof(fileFormat);
|
||||
|
||||
// Get the audio data format
|
||||
ret = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileDataFormat, &propertySize, &fileFormat);
|
||||
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld", (long)ret);
|
||||
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;
|
||||
|
||||
ret = ExtAudioFileSetProperty(_extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_outputFormat), &_outputFormat);
|
||||
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileSetProperty FAILED, Error = %ld", (long)ret);
|
||||
|
||||
// Get the total frame count
|
||||
SInt64 totalFrames = 0;
|
||||
propertySize = sizeof(totalFrames);
|
||||
ret = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileLengthFrames, &propertySize, &totalFrames);
|
||||
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld", (long)ret);
|
||||
_totalFrames = static_cast<uint32_t>(totalFrames);
|
||||
_isOpened = true;
|
||||
|
||||
ret = true;
|
||||
} while (false);
|
||||
|
||||
if (fileURL != nil)
|
||||
CFRelease(fileURL);
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void AudioDecoder::close()
|
||||
{
|
||||
if (_extRef != nullptr)
|
||||
{
|
||||
ExtAudioFileDispose(_extRef);
|
||||
_extRef = nullptr;
|
||||
|
||||
_totalFrames = 0;
|
||||
_bytesPerFrame = 0;
|
||||
_sampleRate = 0;
|
||||
_channelCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::read(uint32_t framesToRead, char* pcmBuf)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool AudioDecoder::seek(uint32_t frameOffset)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::tell() const
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::getTotalFrames() const
|
||||
{
|
||||
return _totalFrames;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::getBytesPerFrame() const
|
||||
{
|
||||
return _bytesPerFrame;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::getSampleRate() const
|
||||
{
|
||||
return _sampleRate;
|
||||
}
|
||||
|
||||
uint32_t AudioDecoder::getChannelCount() const
|
||||
{
|
||||
return _channelCount;
|
||||
}
|
||||
|
||||
bool AudioDecoder::isOpened() const
|
||||
{
|
||||
return _isOpened;
|
||||
}
|
||||
|
||||
}} // namespace cocos2d { namespace experimental {
|
||||
@@ -88,4 +88,3 @@ private:
|
||||
NS_CC_END
|
||||
#endif // __AUDIO_ENGINE_INL_H_
|
||||
#endif
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio
|
||||
else if (kAudioSessionEndInterruption == interruption_state)
|
||||
{
|
||||
OSStatus result = AudioSessionSetActive(true);
|
||||
if (result) NSLog(@"Error setting audio session active! %d\n", result);
|
||||
if (result) NSLog(@"Error setting audio session active! %d\n", static_cast<int>(result));
|
||||
|
||||
alcMakeContextCurrent(s_ALContext);
|
||||
}
|
||||
@@ -192,7 +192,7 @@ bool AudioEngineImpl::init()
|
||||
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
|
||||
_alSourceUsed[_alSources[i]] = false;
|
||||
}
|
||||
|
||||
|
||||
// fixed #16170: Random crash in alGenBuffers(AudioCache::readDataTask) at startup
|
||||
// Please note that, as we know the OpenAL operation is atomic (threadsafe),
|
||||
// 'alGenBuffers' may be invoked by different threads. But in current implementation of 'alGenBuffers',
|
||||
@@ -200,15 +200,15 @@ bool AudioEngineImpl::init()
|
||||
// Why? OpenAL is opensource by Apple and could be found at
|
||||
// http://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp .
|
||||
/*
|
||||
|
||||
|
||||
void InitializeBufferMap()
|
||||
{
|
||||
if (gOALBufferMap == NULL) // Position 1
|
||||
{
|
||||
gOALBufferMap = new OALBufferMap (); // Position 2
|
||||
|
||||
|
||||
// Position Gap
|
||||
|
||||
|
||||
gBufferMapLock = new CAGuard("OAL:BufferMapLock"); // Position 3
|
||||
gDeadOALBufferMap = new OALBufferMap ();
|
||||
|
||||
@@ -233,7 +233,7 @@ bool AudioEngineImpl::init()
|
||||
...
|
||||
...
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
// 'gBufferMapLock' will be initialized in the 'InitializeBufferMap' function,
|
||||
// that's the problem. It means that 'InitializeBufferMap' may be invoked in different threads.
|
||||
@@ -243,18 +243,18 @@ bool AudioEngineImpl::init()
|
||||
// that 'gOALBufferMap' isn't NULL, B just jump over 'InitialBufferMap' and goto 'Position 4'.
|
||||
// Meanwhile, A is still at 'Position Gap', B will crash at '*gBufferMapLock' since 'gBufferMapLock'
|
||||
// is still a null pointer. Oops, how could Apple implemented this method in this fucking way?
|
||||
|
||||
|
||||
// Workaround is do an unused invocation in the mainthread right after OpenAL is initialized successfully
|
||||
// as bellow.
|
||||
// ================ Workaround begin ================ //
|
||||
|
||||
|
||||
ALuint unusedAlBufferId = 0;
|
||||
alGenBuffers(1, &unusedAlBufferId);
|
||||
alDeleteBuffers(1, &unusedAlBufferId);
|
||||
|
||||
|
||||
// ================ Workaround end ================ //
|
||||
|
||||
|
||||
|
||||
|
||||
_scheduler = Director::getInstance()->getScheduler();
|
||||
ret = true;
|
||||
ALOGI("OpenAL was initialized successfully!");
|
||||
@@ -288,7 +288,7 @@ AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function<
|
||||
audioCache = &it->second;
|
||||
}
|
||||
|
||||
if(audioCache && callback)
|
||||
if (audioCache && callback)
|
||||
{
|
||||
audioCache->addLoadCallback(callback);
|
||||
}
|
||||
@@ -319,7 +319,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
|
||||
if (player == nullptr) {
|
||||
return AudioEngine::INVALID_AUDIO_ID;
|
||||
}
|
||||
|
||||
|
||||
player->_alSource = alSource;
|
||||
player->_loop = loop;
|
||||
player->_volume = volume;
|
||||
@@ -329,16 +329,16 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
|
||||
delete player;
|
||||
return AudioEngine::INVALID_AUDIO_ID;
|
||||
}
|
||||
|
||||
|
||||
player->setCache(audioCache);
|
||||
_threadMutex.lock();
|
||||
_audioPlayers[_currentAudioID] = player;
|
||||
_threadMutex.unlock();
|
||||
|
||||
|
||||
_alSourceUsed[alSource] = true;
|
||||
|
||||
|
||||
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
|
||||
|
||||
|
||||
if (_lazyInitLoop) {
|
||||
_lazyInitLoop = false;
|
||||
_scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
|
||||
@@ -356,7 +356,7 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
|
||||
auto playerIt = _audioPlayers.find(audioID);
|
||||
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
|
||||
_scheduler->performFunctionInCocosThread([audioID](){
|
||||
|
||||
|
||||
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
|
||||
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
|
||||
}
|
||||
@@ -511,8 +511,8 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time)
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (player->_audioCache->_bytesOfRead != player->_audioCache->_dataSize &&
|
||||
(time * player->_audioCache->_sampleRate * player->_audioCache->_bytesPerFrame) > player->_audioCache->_bytesOfRead) {
|
||||
if (player->_audioCache->_framesRead != player->_audioCache->_totalFrames &&
|
||||
(time * player->_audioCache->_sampleRate) > player->_audioCache->_framesRead) {
|
||||
ALOGE("%s: audio id = %d", __PRETTY_FUNCTION__,audioID);
|
||||
break;
|
||||
}
|
||||
@@ -540,18 +540,18 @@ void AudioEngineImpl::update(float dt)
|
||||
ALint sourceState;
|
||||
int audioID;
|
||||
AudioPlayer* player;
|
||||
|
||||
|
||||
// ALOGV("AudioPlayer count: %d", (int)_audioPlayers.size());
|
||||
|
||||
|
||||
for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) {
|
||||
audioID = it->first;
|
||||
player = it->second;
|
||||
alGetSourcei(player->_alSource, AL_SOURCE_STATE, &sourceState);
|
||||
|
||||
if(player->_removeByAudioEngine)
|
||||
if (player->_removeByAudioEngine)
|
||||
{
|
||||
_alSourceUsed[player->_alSource] = false;
|
||||
|
||||
|
||||
AudioEngine::remove(audioID);
|
||||
_threadMutex.lock();
|
||||
it = _audioPlayers.erase(it);
|
||||
@@ -559,7 +559,7 @@ void AudioEngineImpl::update(float dt)
|
||||
delete player;
|
||||
}
|
||||
else if (player->_ready && sourceState == AL_STOPPED) {
|
||||
|
||||
|
||||
_alSourceUsed[player->_alSource] = false;
|
||||
if (player->_finishCallbak) {
|
||||
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2016 Chukong Technologies Inc.
|
||||
|
||||
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
|
||||
|
||||
#define QUEUEBUFFER_NUM (3)
|
||||
#define QUEUEBUFFER_TIME_STEP (0.1f)
|
||||
|
||||
#define QUOTEME_(x) #x
|
||||
#define QUOTEME(x) QUOTEME_(x)
|
||||
|
||||
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
||||
#define ALOGV(fmt, ...) printf("V/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define ALOGV(fmt, ...) do {} while(false)
|
||||
#endif
|
||||
#define ALOGD(fmt, ...) printf("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGI(fmt, ...) printf("I/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGW(fmt, ...) printf("W/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
#define ALOGE(fmt, ...) printf("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
|
||||
|
||||
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
||||
#define CHECK_AL_ERROR_DEBUG() \
|
||||
do { \
|
||||
GLenum __error = alGetError(); \
|
||||
if (__error) { \
|
||||
ALOGE("OpenAL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \
|
||||
} \
|
||||
} while (false)
|
||||
#else
|
||||
#define CHECK_AL_ERROR_DEBUG()
|
||||
#endif
|
||||
|
||||
#define BREAK_IF(condition) \
|
||||
do {\
|
||||
if (!!(condition)) { \
|
||||
break; \
|
||||
} \
|
||||
} while(false)
|
||||
|
||||
#define BREAK_IF_ERR_LOG(condition, fmt, ...) \
|
||||
do {\
|
||||
if (!!(condition)) { \
|
||||
ALOGE("(" QUOTEME(condition) ") failed, message: " fmt, ##__VA_ARGS__); \
|
||||
break; \
|
||||
} \
|
||||
} while(false)
|
||||
@@ -57,7 +57,7 @@ protected:
|
||||
void setCache(AudioCache* cache);
|
||||
void rotateBufferThread(int offsetFrame);
|
||||
bool play2d();
|
||||
|
||||
|
||||
AudioCache* _audioCache;
|
||||
|
||||
float _volume;
|
||||
@@ -90,4 +90,3 @@ protected:
|
||||
NS_CC_END
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "audio/apple/AudioPlayer.h"
|
||||
#include "audio/apple/AudioCache.h"
|
||||
#include "platform/CCFileUtils.h"
|
||||
#import <AudioToolbox/ExtendedAudioFile.h>
|
||||
#include "audio/apple/AudioDecoder.h"
|
||||
|
||||
#define VERY_VERY_VERBOSE_LOGGING
|
||||
#ifdef VERY_VERY_VERBOSE_LOGGING
|
||||
@@ -157,12 +157,12 @@ bool AudioPlayer::play2d()
|
||||
ALOGE("alBuffer isn't ready for play!");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
alSourcei(_alSource, AL_BUFFER, 0);CHECK_AL_ERROR_DEBUG();
|
||||
alSourcef(_alSource, AL_PITCH, 1.0f);CHECK_AL_ERROR_DEBUG();
|
||||
alSourcef(_alSource, AL_GAIN, _volume);CHECK_AL_ERROR_DEBUG();
|
||||
alSourcei(_alSource, AL_LOOPING, AL_FALSE);CHECK_AL_ERROR_DEBUG();
|
||||
|
||||
|
||||
if (_audioCache->_queBufferFrames == 0)
|
||||
{
|
||||
if (_loop) {
|
||||
@@ -173,7 +173,7 @@ bool AudioPlayer::play2d()
|
||||
else
|
||||
{
|
||||
alGenBuffers(3, _bufferIds);
|
||||
|
||||
|
||||
auto alError = alGetError();
|
||||
if (alError == AL_NO_ERROR)
|
||||
{
|
||||
@@ -190,12 +190,12 @@ bool AudioPlayer::play2d()
|
||||
}
|
||||
_streamingSource = true;
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(_sleepMutex);
|
||||
if (_isDestroyed)
|
||||
break;
|
||||
|
||||
|
||||
if (_streamingSource)
|
||||
{
|
||||
alSourceQueueBuffers(_alSource, QUEUEBUFFER_NUM, _bufferIds);
|
||||
@@ -210,7 +210,7 @@ bool AudioPlayer::play2d()
|
||||
|
||||
alSourcePlay(_alSource);
|
||||
}
|
||||
|
||||
|
||||
auto alError = alGetError();
|
||||
if (alError != AL_NO_ERROR)
|
||||
{
|
||||
@@ -224,103 +224,91 @@ bool AudioPlayer::play2d()
|
||||
_ready = true;
|
||||
ret = true;
|
||||
} while (false);
|
||||
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
_removeByAudioEngine = true;
|
||||
}
|
||||
|
||||
|
||||
_play2dMutex.unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void AudioPlayer::rotateBufferThread(int offsetFrame)
|
||||
{
|
||||
ALint sourceState;
|
||||
ALint bufferProcessed = 0;
|
||||
ExtAudioFileRef extRef = nullptr;
|
||||
char* tmpBuffer = nullptr;
|
||||
AudioDecoder decoder;
|
||||
do
|
||||
{
|
||||
BREAK_IF(!decoder.open(_audioCache->_fileFullPath.c_str()));
|
||||
|
||||
NSString *fileFullPath = [[NSString alloc] initWithCString:_audioCache->_fileFullPath.c_str() encoding:NSUTF8StringEncoding];
|
||||
auto fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath];
|
||||
[fileFullPath release];
|
||||
char* tmpBuffer = (char*)malloc(_audioCache->_queBufferBytes);
|
||||
auto frames = _audioCache->_queBufferFrames;
|
||||
uint32_t framesRead = 0;
|
||||
const uint32_t framesToRead = _audioCache->_queBufferFrames;
|
||||
const uint32_t bufferSize = framesToRead * decoder.getBytesPerFrame();
|
||||
tmpBuffer = (char*)malloc(bufferSize);
|
||||
memset(tmpBuffer, 0, bufferSize);
|
||||
|
||||
auto error = ExtAudioFileOpenURL(fileURL, &extRef);
|
||||
if(error) {
|
||||
ALOGE("%s: ExtAudioFileOpenURL FAILED, Error = %ld", __PRETTY_FUNCTION__,(long) error);
|
||||
goto ExitBufferThread;
|
||||
}
|
||||
if (offsetFrame != 0) {
|
||||
decoder.seek(offsetFrame);
|
||||
}
|
||||
|
||||
error = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_audioCache->_outputFormat), &_audioCache->_outputFormat);
|
||||
AudioBufferList theDataBuffer;
|
||||
theDataBuffer.mNumberBuffers = 1;
|
||||
theDataBuffer.mBuffers[0].mData = tmpBuffer;
|
||||
theDataBuffer.mBuffers[0].mDataByteSize = _audioCache->_queBufferBytes;
|
||||
theDataBuffer.mBuffers[0].mNumberChannels = _audioCache->_outputFormat.mChannelsPerFrame;
|
||||
ALint sourceState;
|
||||
ALint bufferProcessed = 0;
|
||||
bool needToExitThread = false;
|
||||
|
||||
if (offsetFrame != 0) {
|
||||
ExtAudioFileSeek(extRef, offsetFrame);
|
||||
}
|
||||
|
||||
while (!_isDestroyed) {
|
||||
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
|
||||
if (sourceState == AL_PLAYING) {
|
||||
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
|
||||
while (bufferProcessed > 0) {
|
||||
bufferProcessed--;
|
||||
if (_timeDirty) {
|
||||
_timeDirty = false;
|
||||
offsetFrame = _currTime * _audioCache->_outputFormat.mSampleRate;
|
||||
ExtAudioFileSeek(extRef, offsetFrame);
|
||||
}
|
||||
else {
|
||||
_currTime += QUEUEBUFFER_TIME_STEP;
|
||||
if (_currTime > _audioCache->_duration) {
|
||||
if (_loop) {
|
||||
_currTime = 0.0f;
|
||||
} else {
|
||||
_currTime = _audioCache->_duration;
|
||||
while (!_isDestroyed) {
|
||||
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
|
||||
if (sourceState == AL_PLAYING) {
|
||||
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
|
||||
while (bufferProcessed > 0) {
|
||||
bufferProcessed--;
|
||||
if (_timeDirty) {
|
||||
_timeDirty = false;
|
||||
offsetFrame = _currTime * decoder.getSampleRate();
|
||||
decoder.seek(offsetFrame);
|
||||
}
|
||||
else {
|
||||
_currTime += QUEUEBUFFER_TIME_STEP;
|
||||
if (_currTime > _audioCache->_duration) {
|
||||
if (_loop) {
|
||||
_currTime = 0.0f;
|
||||
} else {
|
||||
_currTime = _audioCache->_duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frames = _audioCache->_queBufferFrames;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
if (frames <= 0) {
|
||||
if (_loop) {
|
||||
ExtAudioFileSeek(extRef, 0);
|
||||
frames = _audioCache->_queBufferFrames;
|
||||
theDataBuffer.mBuffers[0].mDataByteSize = _audioCache->_queBufferBytes;
|
||||
ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer);
|
||||
} else {
|
||||
_isDestroyed = true;
|
||||
break;
|
||||
framesRead = decoder.read(framesToRead, tmpBuffer);
|
||||
|
||||
if (framesRead == 0) {
|
||||
if (_loop) {
|
||||
decoder.seek(0);
|
||||
framesRead = decoder.read(framesToRead, tmpBuffer);
|
||||
} else {
|
||||
needToExitThread = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ALuint bid;
|
||||
alSourceUnqueueBuffers(_alSource, 1, &bid);
|
||||
alBufferData(bid, _audioCache->_format, tmpBuffer, framesRead * decoder.getBytesPerFrame(), decoder.getSampleRate());
|
||||
alSourceQueueBuffers(_alSource, 1, &bid);
|
||||
}
|
||||
|
||||
ALuint bid;
|
||||
alSourceUnqueueBuffers(_alSource, 1, &bid);
|
||||
alBufferData(bid, _audioCache->_format, tmpBuffer, frames * _audioCache->_outputFormat.mBytesPerFrame, _audioCache->_sampleRate);
|
||||
alSourceQueueBuffers(_alSource, 1, &bid);
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lk(_sleepMutex);
|
||||
if (_isDestroyed || needToExitThread) {
|
||||
break;
|
||||
}
|
||||
|
||||
_sleepCondition.wait_for(lk,std::chrono::milliseconds(75));
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lk(_sleepMutex);
|
||||
if (_isDestroyed) {
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
_sleepCondition.wait_for(lk,std::chrono::milliseconds(75));
|
||||
}
|
||||
|
||||
ExitBufferThread:
|
||||
ALOGV("Exit rotate buffer thread ...");
|
||||
CFRelease(fileURL);
|
||||
// Dispose the ExtAudioFileRef, it is no longer needed
|
||||
if (extRef){
|
||||
ExtAudioFileDispose(extRef);
|
||||
}
|
||||
decoder.close();
|
||||
free(tmpBuffer);
|
||||
_isRotateThreadExited = true;
|
||||
}
|
||||
|
||||
@@ -857,6 +857,11 @@ void EventDispatcher::dispatchCustomEvent(const std::string &eventName, void *op
|
||||
dispatchEvent(&ev);
|
||||
}
|
||||
|
||||
bool EventDispatcher::hasEventListener(const EventListener::ListenerID& listenerID) const
|
||||
{
|
||||
return getListeners(listenerID) != nullptr;
|
||||
}
|
||||
|
||||
void EventDispatcher::dispatchTouchEvent(EventTouch* event)
|
||||
{
|
||||
sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID);
|
||||
|
||||
@@ -177,6 +177,14 @@ public:
|
||||
*/
|
||||
void dispatchCustomEvent(const std::string &eventName, void *optionalUserData = nullptr);
|
||||
|
||||
/** Query whether the specified event listener id has been added.
|
||||
*
|
||||
* @param listenerID The listenerID of the event listener id.
|
||||
*
|
||||
* @return True if dispatching events is exist
|
||||
*/
|
||||
bool hasEventListener(const EventListener::ListenerID& listenerID) const;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
|
||||
/** Constructor of EventDispatcher.
|
||||
|
||||
@@ -203,8 +203,11 @@
|
||||
"cocos/audio/android/utils/Utils.h",
|
||||
"cocos/audio/apple/AudioCache.h",
|
||||
"cocos/audio/apple/AudioCache.mm",
|
||||
"cocos/audio/apple/AudioDecoder.h",
|
||||
"cocos/audio/apple/AudioDecoder.mm",
|
||||
"cocos/audio/apple/AudioEngine-inl.h",
|
||||
"cocos/audio/apple/AudioEngine-inl.mm",
|
||||
"cocos/audio/apple/AudioMacros.h",
|
||||
"cocos/audio/apple/AudioPlayer.h",
|
||||
"cocos/audio/apple/AudioPlayer.mm",
|
||||
"cocos/audio/include/AudioEngine.h",
|
||||
|
||||
Reference in New Issue
Block a user