2014-09-29 10:15:41 +08:00
|
|
|
/****************************************************************************
|
2016-12-29 10:05:13 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
2014-09-29 10:15:41 +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.
|
|
|
|
****************************************************************************/
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-09-30 09:57:28 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
|
2014-09-29 10:15:41 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
2016-12-29 10:05:13 +08:00
|
|
|
#include <memory>
|
2017-02-08 10:22:05 +08:00
|
|
|
#ifdef OPENAL_PLAIN_INCLUDES
|
|
|
|
#include <al.h>
|
|
|
|
#else
|
2014-11-18 22:27:43 +08:00
|
|
|
#include <AL/al.h>
|
2017-02-08 10:22:05 +08:00
|
|
|
#endif
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2016-12-29 10:05:13 +08:00
|
|
|
#include "audio/apple/AudioMacros.h"
|
2016-08-04 09:53:30 +08:00
|
|
|
|
2014-09-29 10:15:41 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
namespace experimental{
|
|
|
|
|
|
|
|
class AudioEngineImpl;
|
|
|
|
class AudioPlayer;
|
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
class CC_DLL AudioCache
|
|
|
|
{
|
2014-09-29 10:15:41 +08:00
|
|
|
public:
|
2016-12-29 10:05:13 +08:00
|
|
|
|
|
|
|
enum class State
|
2014-09-29 10:15:41 +08:00
|
|
|
{
|
2016-12-29 10:05:13 +08:00
|
|
|
INITIAL,
|
|
|
|
LOADING,
|
|
|
|
READY,
|
|
|
|
FAILED
|
2014-09-29 10:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
AudioCache();
|
|
|
|
~AudioCache();
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
void addPlayCallback(const std::function<void()>& callback);
|
|
|
|
|
|
|
|
void addLoadCallback(const std::function<void(bool)>& callback);
|
2014-09-29 10:15:41 +08:00
|
|
|
|
|
|
|
protected:
|
2016-12-29 10:05:13 +08:00
|
|
|
void setSkipReadDataTask(bool isSkip) { _isSkipReadDataTask = isSkip; };
|
|
|
|
void readDataTask(unsigned int selfId);
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
void invokingPlayCallbacks();
|
2016-12-29 10:05:13 +08:00
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
void invokingLoadCallbacks();
|
2014-09-29 10:15:41 +08:00
|
|
|
|
|
|
|
//pcm data related stuff
|
2016-12-29 10:05:13 +08:00
|
|
|
ALenum _format;
|
|
|
|
ALsizei _sampleRate;
|
2014-09-29 10:15:41 +08:00
|
|
|
float _duration;
|
2016-12-29 10:05:13 +08:00
|
|
|
uint32_t _totalFrames;
|
|
|
|
uint32_t _framesRead;
|
|
|
|
|
2014-09-29 10:15:41 +08:00
|
|
|
/*Cache related stuff;
|
|
|
|
* Cache pcm data when sizeInBytes less than PCMDATA_CACHEMAXSIZE
|
|
|
|
*/
|
|
|
|
ALuint _alBufferId;
|
2016-12-29 10:05:13 +08:00
|
|
|
char* _pcmData;
|
2014-09-29 10:15:41 +08:00
|
|
|
|
|
|
|
/*Queue buffer related stuff
|
|
|
|
* Streaming in OpenAL when sizeInBytes greater then PCMDATA_CACHEMAXSIZE
|
|
|
|
*/
|
|
|
|
char* _queBuffers[QUEUEBUFFER_NUM];
|
|
|
|
ALsizei _queBufferSize[QUEUEBUFFER_NUM];
|
2016-12-29 10:05:13 +08:00
|
|
|
uint32_t _queBufferFrames;
|
2014-09-29 10:15:41 +08:00
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
std::mutex _playCallbackMutex;
|
|
|
|
std::vector< std::function<void()> > _playCallbacks;
|
|
|
|
|
|
|
|
// loadCallbacks doesn't need mutex since it's invoked only in Cocos thread.
|
2015-07-28 15:27:07 +08:00
|
|
|
std::vector< std::function<void(bool)> > _loadCallbacks;
|
2014-09-29 10:15:41 +08:00
|
|
|
|
2016-12-29 10:05:13 +08:00
|
|
|
std::mutex _readDataTaskMutex;
|
|
|
|
|
|
|
|
State _state;
|
|
|
|
|
|
|
|
std::shared_ptr<bool> _isDestroyed;
|
|
|
|
std::string _fileFullPath;
|
|
|
|
unsigned int _id;
|
|
|
|
bool _isLoadingFinished;
|
|
|
|
bool _isSkipReadDataTask;
|
2014-09-29 10:15:41 +08:00
|
|
|
|
|
|
|
friend class AudioEngineImpl;
|
|
|
|
friend class AudioPlayer;
|
2016-12-29 10:05:13 +08:00
|
|
|
};
|
2014-09-29 10:15:41 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif
|