2014-09-03 18:19:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 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.
|
|
|
|
****************************************************************************/
|
2014-09-30 01:07:11 +08:00
|
|
|
|
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-09-22 15:38:12 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
2014-09-30 01:07:11 +08:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "audio/apple/AudioPlayer.h"
|
|
|
|
#include "audio/apple/AudioCache.h"
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#import <AudioToolbox/ExtendedAudioFile.h>
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
2014-09-16 14:57:58 +08:00
|
|
|
using namespace cocos2d::experimental;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
AudioPlayer::AudioPlayer()
|
2014-11-26 09:53:52 +08:00
|
|
|
: _audioCache(nullptr)
|
2014-09-03 18:19:21 +08:00
|
|
|
, _finishCallbak(nullptr)
|
2015-08-11 11:40:36 +08:00
|
|
|
, _beDestroy(false)
|
|
|
|
, _removeByAudioEngine(false)
|
2014-09-03 18:19:21 +08:00
|
|
|
, _ready(false)
|
2014-11-26 09:53:52 +08:00
|
|
|
, _currTime(0.0f)
|
|
|
|
, _streamingSource(false)
|
|
|
|
, _timeDirty(false)
|
|
|
|
{
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioPlayer::~AudioPlayer()
|
|
|
|
{
|
2015-08-11 11:40:36 +08:00
|
|
|
if (_streamingSource) {
|
|
|
|
_beDestroy = true;
|
|
|
|
_sleepCondition.notify_one();
|
2014-09-11 10:53:01 +08:00
|
|
|
if (_rotateBufferThread.joinable()) {
|
|
|
|
_rotateBufferThread.join();
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
alDeleteBuffers(3, _bufferIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
void AudioPlayer::destroy()
|
|
|
|
{
|
|
|
|
alSourceStop(_alSource);
|
|
|
|
alSourcei(_alSource, AL_BUFFER, NULL);
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lk(_sleepMutex);
|
|
|
|
_beDestroy = true;
|
|
|
|
if (_streamingSource) {
|
|
|
|
_sleepCondition.notify_one();
|
|
|
|
}
|
|
|
|
else if (_ready) {
|
|
|
|
_removeByAudioEngine = true;
|
|
|
|
}
|
|
|
|
_ready = false;
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
bool AudioPlayer::play2d(AudioCache* cache)
|
|
|
|
{
|
|
|
|
if (!cache->_alBufferReady) {
|
2015-08-11 11:40:36 +08:00
|
|
|
_removeByAudioEngine = true;
|
2014-09-03 18:19:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_audioCache = cache;
|
|
|
|
|
2014-09-30 01:07:11 +08:00
|
|
|
alSourcei(_alSource, AL_BUFFER, 0);
|
2014-09-03 18:19:21 +08:00
|
|
|
alSourcef(_alSource, AL_PITCH, 1.0f);
|
|
|
|
alSourcef(_alSource, AL_GAIN, _volume);
|
2015-07-01 17:19:28 +08:00
|
|
|
alSourcei(_alSource, AL_LOOPING, AL_FALSE);
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
if (_audioCache->_queBufferFrames == 0) {
|
|
|
|
if (_loop) {
|
|
|
|
alSourcei(_alSource, AL_LOOPING, AL_TRUE);
|
|
|
|
}
|
2015-08-11 11:40:36 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
alGetError();
|
2014-09-03 18:19:21 +08:00
|
|
|
alGenBuffers(3, _bufferIds);
|
2015-08-11 11:40:36 +08:00
|
|
|
auto alError = alGetError();
|
2014-09-03 18:19:21 +08:00
|
|
|
if (alError == AL_NO_ERROR) {
|
|
|
|
for (int index = 0; index < QUEUEBUFFER_NUM; ++index) {
|
2014-09-22 16:59:24 +08:00
|
|
|
alBufferData(_bufferIds[index], _audioCache->_format, _audioCache->_queBuffers[index], _audioCache->_queBufferSize[index], _audioCache->_sampleRate);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s:alGenBuffers error code:%x", __PRETTY_FUNCTION__,alError);
|
2015-08-11 11:40:36 +08:00
|
|
|
_removeByAudioEngine = true;
|
2014-09-11 10:53:01 +08:00
|
|
|
return false;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2015-08-11 11:40:36 +08:00
|
|
|
_streamingSource = true;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(_sleepMutex);
|
|
|
|
if (_beDestroy) {
|
|
|
|
_removeByAudioEngine = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_streamingSource) {
|
|
|
|
alSourceQueueBuffers(_alSource, QUEUEBUFFER_NUM, _bufferIds);
|
|
|
|
_rotateBufferThread = std::thread(&AudioPlayer::rotateBufferThread,this, _audioCache->_queBufferFrames * QUEUEBUFFER_NUM + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alSourcei(_alSource, AL_BUFFER, _audioCache->_alBufferId);
|
|
|
|
}
|
|
|
|
|
|
|
|
alGetError();
|
|
|
|
alSourcePlay(_alSource);
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
auto alError = alGetError();
|
2014-09-03 18:19:21 +08:00
|
|
|
if (alError != AL_NO_ERROR) {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s:alSourcePlay error code:%x\n", __PRETTY_FUNCTION__,alError);
|
2015-08-11 11:40:36 +08:00
|
|
|
_removeByAudioEngine = true;
|
2014-09-11 10:53:01 +08:00
|
|
|
return false;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2015-08-11 11:40:36 +08:00
|
|
|
_ready = true;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2014-09-11 10:53:01 +08:00
|
|
|
return true;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioPlayer::rotateBufferThread(int offsetFrame)
|
|
|
|
{
|
|
|
|
ALint sourceState;
|
|
|
|
ALint bufferProcessed = 0;
|
|
|
|
ExtAudioFileRef extRef = nullptr;
|
|
|
|
|
2016-05-30 10:47:48 +08:00
|
|
|
NSString *fileFullPath = [[NSString alloc] initWithCString:_audioCache->_fileFullPath.c_str() encoding:NSUTF8StringEncoding];
|
2015-03-31 07:27:05 +08:00
|
|
|
auto fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath];
|
|
|
|
[fileFullPath release];
|
2014-09-03 18:19:21 +08:00
|
|
|
char* tmpBuffer = (char*)malloc(_audioCache->_queBufferBytes);
|
|
|
|
auto frames = _audioCache->_queBufferFrames;
|
|
|
|
|
|
|
|
auto error = ExtAudioFileOpenURL(fileURL, &extRef);
|
|
|
|
if(error) {
|
2014-09-30 11:58:38 +08:00
|
|
|
printf("%s: ExtAudioFileOpenURL FAILED, Error = %ld\n", __PRETTY_FUNCTION__,(long) error);
|
2014-09-03 18:19:21 +08:00
|
|
|
goto ExitBufferThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (offsetFrame != 0) {
|
|
|
|
ExtAudioFileSeek(extRef, offsetFrame);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
while (!_beDestroy) {
|
2014-09-03 18:19:21 +08:00
|
|
|
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
if (sourceState == AL_PLAYING) {
|
|
|
|
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
|
|
|
|
while (bufferProcessed > 0) {
|
|
|
|
bufferProcessed--;
|
2014-09-11 10:53:01 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-08-11 11:40:36 +08:00
|
|
|
_beDestroy = true;
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ALuint bid;
|
|
|
|
alSourceUnqueueBuffers(_alSource, 1, &bid);
|
|
|
|
alBufferData(bid, _audioCache->_format, tmpBuffer, frames * _audioCache->outputFormat.mBytesPerFrame, _audioCache->_sampleRate);
|
|
|
|
alSourceQueueBuffers(_alSource, 1, &bid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
std::unique_lock<std::mutex> lk(_sleepMutex);
|
|
|
|
if (_beDestroy) {
|
2014-09-29 14:17:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-08-11 11:40:36 +08:00
|
|
|
|
2014-09-29 14:17:51 +08:00
|
|
|
_sleepCondition.wait_for(lk,std::chrono::milliseconds(75));
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExitBufferThread:
|
|
|
|
CFRelease(fileURL);
|
|
|
|
// Dispose the ExtAudioFileRef, it is no longer needed
|
|
|
|
if (extRef){
|
|
|
|
ExtAudioFileDispose(extRef);
|
|
|
|
}
|
|
|
|
free(tmpBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioPlayer::setLoop(bool loop)
|
|
|
|
{
|
2015-08-11 11:40:36 +08:00
|
|
|
if (!_beDestroy ) {
|
2014-09-03 18:19:21 +08:00
|
|
|
_loop = loop;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioPlayer::setTime(float time)
|
|
|
|
{
|
2015-08-11 11:40:36 +08:00
|
|
|
if (!_beDestroy && time >= 0.0f && time < _audioCache->_duration) {
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
_currTime = time;
|
2014-09-11 10:53:01 +08:00
|
|
|
_timeDirty = true;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-22 15:38:12 +08:00
|
|
|
|
|
|
|
#endif
|