14.【cococos 2 d-xソース分析】:Audioの詳細分析


対応ソース位置:cococos 2 d-x-3.3cocosaudioincludeSimpleAudioEngine
SimpleAudioEngineの実装SimpleAudioEngineは、簡単な音響再生と、バックミュージックを再生する機能を提供しています.
class EXPORT_DLL SimpleAudioEngine
{
public:
   
    static SimpleAudioEngine* getInstance();
    static void end();
protected:
    SimpleAudioEngine();
    virtual ~SimpleAudioEngine();
public:
	//      
    virtual void preloadBackgroundMusic(const char* pszFilePath);
    virtual void playBackgroundMusic(const char* pszFilePath, bool bLoop = false);
    virtual void stopBackgroundMusic(bool bReleaseData = false);
    virtual void pauseBackgroundMusic();
    virtual void resumeBackgroundMusic();
    virtual void rewindBackgroundMusic();
    virtual bool willPlayBackgroundMusic();
    virtual bool isBackgroundMusicPlaying();
    virtual float getBackgroundMusicVolume();
    virtual void setBackgroundMusicVolume(float volume);
    //    
    virtual float getEffectsVolume();
    virtual void setEffectsVolume(float volume);
    virtual unsigned int playEffect(const char* pszFilePath, bool bLoop = false,
                                    float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
    virtual void pauseEffect(unsigned int nSoundId);
    virtual void pauseAllEffects();
    virtual void resumeEffect(unsigned int nSoundId);
    virtual void resumeAllEffects();
    virtual void stopEffect(unsigned int nSoundId);
    virtual void stopAllEffects();
    virtual void preloadEffect(const char* pszFilePath);
    virtual void unloadEffect(const char* pszFilePath);
};

win 32の実装例:
//                   MCI
typedef map<unsigned int, MciPlayer *> EffectList;
//         
typedef pair<unsigned int, MciPlayer *> Effect;

static char     s_szRootPath[MAX_PATH];
static DWORD    s_dwRootLen;
static char     s_szFullPath[MAX_PATH];
//     
static std::string _FullPath(const char * szPath);
//     hash 
static unsigned int _Hash(const char *key);

#define BREAK_IF(cond)  if (cond) break;
//         
static EffectList& sharedList()
{
    static EffectList s_List;
    return s_List;
}
//           
static MciPlayer& sharedMusic()
{
    static MciPlayer s_Music;
    return s_Music;
}
//    
SimpleAudioEngine* SimpleAudioEngine::getInstance()
{
    static SimpleAudioEngine s_SharedEngine;
    return &s_SharedEngine;
}
//        
void SimpleAudioEngine::end()
{
    sharedMusic().Close();
    EffectList::iterator p = sharedList().begin();
    while (p != sharedList().end())
    {
        delete p->second;
        p->second = NULL;
        p++;
    }   
    sharedList().clear();
    return;
}

//////////////////////////////////////////////////////////////////////////
// BackgroundMusic
//////////////////////////////////////////////////////////////////////////
//         
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
    if (! pszFilePath)
    {
        return;
    }

    sharedMusic().Open(_FullPath(pszFilePath).c_str(), _Hash(pszFilePath));
    sharedMusic().Play((bLoop) ? -1 : 1);
}

void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{
    if (bReleaseData)
    {
        sharedMusic().Close();
    }
    else
    {
        sharedMusic().Stop();
    }
}

void SimpleAudioEngine::pauseBackgroundMusic()
{
    sharedMusic().Pause();
}

void SimpleAudioEngine::resumeBackgroundMusic()
{
    sharedMusic().Resume();
}

void SimpleAudioEngine::rewindBackgroundMusic()
{
    sharedMusic().Rewind();
}
//  
bool SimpleAudioEngine::willPlayBackgroundMusic()
{
    return false;
}

bool SimpleAudioEngine::isBackgroundMusicPlaying()
{
    return sharedMusic().IsPlaying();
}

//////////////////////////////////////////////////////////////////////////
// effect function
//////////////////////////////////////////////////////////////////////////
//           
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,
                                           float pitch, float pan, float gain)
{
    unsigned int nRet = _Hash(pszFilePath);
	//     
    preloadEffect(pszFilePath);
    //       
    EffectList::iterator p = sharedList().find(nRet);
    if (p != sharedList().end())
    {
        p->second->Play((bLoop) ? -1 : 1);
    }

    return nRet;
}
//stop   
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{
    EffectList::iterator p = sharedList().find(nSoundId);
    if (p != sharedList().end())
    {
        p->second->Stop();
    }
}

void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
    int nRet = 0;
    do 
    {
        BREAK_IF(! pszFilePath);
        nRet = _Hash(pszFilePath);
        BREAK_IF(sharedList().end() != sharedList().find(nRet));
        sharedList().insert(Effect(nRet, new MciPlayer()));
        MciPlayer * pPlayer = sharedList()[nRet];
        //   new  MciPlayer       
        //       
        pPlayer->Open(_FullPath(pszFilePath).c_str(), nRet);
		//                    
        BREAK_IF(nRet == pPlayer->GetSoundID());

        delete pPlayer;
        sharedList().erase(nRet);
        nRet = 0;
    } while (0);
}
//     
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
{
    EffectList::iterator p = sharedList().find(nSoundId);
    if (p != sharedList().end())
    {
        p->second->Pause();
    }
}
//       
void SimpleAudioEngine::pauseAllEffects()
{
    EffectList::iterator iter;
    for (iter = sharedList().begin(); iter != sharedList().end(); iter++)
    {
        iter->second->Pause();
    }
}
//          
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
{
    EffectList::iterator p = sharedList().find(nSoundId);
    if (p != sharedList().end())
    {
        p->second->Resume();
    }
}
//        
void SimpleAudioEngine::resumeAllEffects()
{
    EffectList::iterator iter;
    for (iter = sharedList().begin(); iter != sharedList().end(); iter++)
    {
        iter->second->Resume();
    }
}
//         stop
void SimpleAudioEngine::stopAllEffects()
{
    EffectList::iterator iter;
    for (iter = sharedList().begin(); iter != sharedList().end(); iter++)
    {
        iter->second->Stop();
    }
}
//    
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{

}
//    map   
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{
    unsigned int nID = _Hash(pszFilePath);
    EffectList::iterator p = sharedList().find(nID);
    if (p != sharedList().end())
    {
        delete p->second;
        p->second = NULL;
        sharedList().erase(nID);
    }    
}

//////////////////////////////////////////////////////////////////////////
// volume interface
//////////////////////////////////////////////////////////////////////////

float SimpleAudioEngine::getBackgroundMusicVolume()
{
    return 1.0;
}
//         
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
{
}
float SimpleAudioEngine::getEffectsVolume()
{
    return 1.0;
}
void SimpleAudioEngine::setEffectsVolume(float volume)
{
}

//////////////////////////////////////////////////////////////////////////
// static function
//////////////////////////////////////////////////////////////////////////
//      
static std::string _FullPath(const char * szPath)
{
    return FileUtils::getInstance()->fullPathForFilename(szPath);
}
//  hash
unsigned int _Hash(const char *key)
{
    unsigned int len = strlen(key);
    const char *end=key+len;
    unsigned int hash;

    for (hash = 0; key < end; key++)
    {
        hash *= 16777619;
        hash ^= (unsigned int) (unsigned char) toupper(*key);
    }
    return (hash);
}
MciPlayerの実装SimpleAudioEngineのすべての操作がMciPlayerに渡されたことがわかる.
class MciPlayer
{
public:
    MciPlayer();
    ~MciPlayer();
    void Close();
    void Open(const char* pFileName, UINT uId);
    void Play(UINT uTimes = 1);
    void Pause();
    void Resume();
    void Stop();
    void Rewind();
    bool IsPlaying();
    UINT GetSoundID();
private:
    friend LRESULT WINAPI _SoundPlayProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    void _SendGenericCommand(int nCommand, DWORD_PTR param1 = 0, DWORD_PTR parma2 = 0);
    HWND        _wnd;
    MCIDEVICEID _dev;
    UINT        _soundID;
    UINT        _times;
    bool        _playing;
    std::string strExt;
};
//     MCI     
void MciPlayer::Open(const char* pFileName, UINT uId)
{
    do 
    {
        BREAK_IF(! pFileName || ! _wnd);
        int nLen = (int)strlen(pFileName);
        BREAK_IF(! nLen);
        std::string strFile(pFileName);
        int nPos = strFile.rfind(".") + 1;
        strExt = strFile.substr(nPos, strFile.length() - nPos);

        Close();

        MCI_OPEN_PARMS mciOpen = {0};
        MCIERROR mciError;
        mciOpen.lpstrDeviceType = (LPCTSTR)MCI_ALL_DEVICE_ID;
		WCHAR* fileNameWideChar = new WCHAR[nLen + 1];
		BREAK_IF(! fileNameWideChar);
		MultiByteToWideChar(CP_ACP, 0, pFileName, nLen + 1, fileNameWideChar, nLen + 1);
        mciOpen.lpstrElementName = fileNameWideChar;

        mciError = mciSendCommand(0,MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen));
		CC_SAFE_DELETE_ARRAY(mciOpen.lpstrElementName);
        BREAK_IF(mciError);

        _dev = mciOpen.wDeviceID;
        _soundID = uId;
        _playing = false;
    } while (0);
}

void MciPlayer::Play(UINT uTimes /* = 1 */)
{
    if (! _dev)
    {
        return;
    }
    MCI_PLAY_PARMS mciPlay = {0};
    mciPlay.dwCallback = reinterpret_cast<DWORD_PTR>(_wnd);
    s_mciError = mciSendCommand(_dev,MCI_PLAY, MCI_FROM|MCI_NOTIFY,reinterpret_cast<DWORD_PTR>(&mciPlay));
    if (! s_mciError)
    {
        _playing = true;
        _times = uTimes;
    }
}

void MciPlayer::Close()
{
    if (_playing)
    {
        Stop();
    }
    if (_dev)
    {
         _SendGenericCommand(MCI_CLOSE);
    }
    _dev = 0;
    _playing = false;
}

void MciPlayer::Pause()
{
    _SendGenericCommand(MCI_PAUSE);
}

void MciPlayer::Resume()
{
    if (strExt == "mid" || strExt == "MID")
    {
        // midi not supprt MCI_RESUME, should get the position and use MCI_FROM
        MCI_STATUS_PARMS mciStatusParms;
        MCI_PLAY_PARMS   mciPlayParms;  
        mciStatusParms.dwItem = MCI_STATUS_POSITION;   
        _SendGenericCommand(MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms)); // MCI_STATUS   
        mciPlayParms.dwFrom = mciStatusParms.dwReturn;  // get position  
        _SendGenericCommand(MCI_PLAY, MCI_FROM, reinterpret_cast<DWORD_PTR>(&mciPlayParms)); // MCI_FROM
    } 
    else
    {
        _SendGenericCommand(MCI_RESUME);
    }   
}

最後に
次はlocalStorageの実装を分析します.