SimpleAudioEngine.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * cocos2d-x http://www.cocos2d-x.org
  3. *
  4. * Copyright (c) 2010-2011 - cocos2d-x community
  5. *
  6. * Portions Copyright (c) Microsoft Open Technologies, Inc.
  7. * All Rights Reserved
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and limitations under the License.
  17. */
  18. #include "audio/include/SimpleAudioEngine.h"
  19. #include "audio/winrt/Audio.h"
  20. #include <map>
  21. #include "platform/CCPlatformMacros.h"
  22. #include "platform/CCFileUtils.h"
  23. using namespace std;
  24. USING_NS_CC;
  25. namespace CocosDenshion {
  26. Audio* s_audioController = nullptr;
  27. bool s_initialized = false;
  28. SimpleAudioEngine* SimpleAudioEngine::getInstance()
  29. {
  30. static SimpleAudioEngine s_SharedEngine;
  31. return &s_SharedEngine;
  32. }
  33. static Audio* sharedAudioController()
  34. {
  35. if (! s_audioController || !s_initialized)
  36. {
  37. if (s_audioController == nullptr)
  38. {
  39. s_audioController = new Audio;
  40. }
  41. s_audioController->Initialize();
  42. s_audioController->CreateResources();
  43. s_initialized = true;
  44. }
  45. return s_audioController;
  46. }
  47. SimpleAudioEngine::SimpleAudioEngine()
  48. {
  49. }
  50. SimpleAudioEngine::~SimpleAudioEngine()
  51. {
  52. }
  53. void SimpleAudioEngine::end()
  54. {
  55. sharedAudioController()->StopBackgroundMusic(true);
  56. sharedAudioController()->StopAllSoundEffects(true);
  57. sharedAudioController()->ReleaseResources();
  58. s_initialized = false;
  59. }
  60. //////////////////////////////////////////////////////////////////////////
  61. // BackgroundMusic
  62. //////////////////////////////////////////////////////////////////////////
  63. void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
  64. {
  65. if (! pszFilePath)
  66. {
  67. return;
  68. }
  69. string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
  70. sharedAudioController()->PlayBackgroundMusic(fullPath.c_str(), bLoop);
  71. }
  72. void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
  73. {
  74. sharedAudioController()->StopBackgroundMusic(bReleaseData);
  75. }
  76. void SimpleAudioEngine::pauseBackgroundMusic()
  77. {
  78. sharedAudioController()->PauseBackgroundMusic();
  79. }
  80. void SimpleAudioEngine::resumeBackgroundMusic()
  81. {
  82. sharedAudioController()->ResumeBackgroundMusic();
  83. }
  84. void SimpleAudioEngine::rewindBackgroundMusic()
  85. {
  86. sharedAudioController()->RewindBackgroundMusic();
  87. }
  88. bool SimpleAudioEngine::willPlayBackgroundMusic()
  89. {
  90. return false;
  91. }
  92. bool SimpleAudioEngine::isBackgroundMusicPlaying()
  93. {
  94. return sharedAudioController()->IsBackgroundMusicPlaying();
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. // effect function
  98. //////////////////////////////////////////////////////////////////////////
  99. unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,float pitch, float pan, float gain)
  100. {
  101. unsigned int sound;
  102. string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
  103. sharedAudioController()->PlaySoundEffect(fullPath.c_str(), bLoop, sound); // TODO: need to support playEffect parameters
  104. return sound;
  105. }
  106. void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
  107. {
  108. sharedAudioController()->StopSoundEffect(nSoundId);
  109. }
  110. void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
  111. {
  112. string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
  113. sharedAudioController()->PreloadSoundEffect(fullPath.c_str());
  114. }
  115. void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
  116. {
  117. sharedAudioController()->PauseSoundEffect(nSoundId);
  118. }
  119. void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
  120. {
  121. sharedAudioController()->ResumeSoundEffect(nSoundId);
  122. }
  123. void SimpleAudioEngine::pauseAllEffects()
  124. {
  125. sharedAudioController()->PauseAllSoundEffects();
  126. }
  127. void SimpleAudioEngine::resumeAllEffects()
  128. {
  129. sharedAudioController()->ResumeAllSoundEffects();
  130. }
  131. void SimpleAudioEngine::stopAllEffects()
  132. {
  133. sharedAudioController()->StopAllSoundEffects(false);
  134. }
  135. void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
  136. {
  137. UNUSED_PARAM(pszFilePath);
  138. }
  139. void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
  140. {
  141. string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
  142. sharedAudioController()->UnloadSoundEffect(fullPath.c_str());
  143. }
  144. //////////////////////////////////////////////////////////////////////////
  145. // volume interface
  146. //////////////////////////////////////////////////////////////////////////
  147. float SimpleAudioEngine::getBackgroundMusicVolume()
  148. {
  149. return sharedAudioController()->GetBackgroundVolume();
  150. }
  151. void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
  152. {
  153. sharedAudioController()->SetBackgroundVolume((volume<=0.0f)? 0.0f : volume);
  154. }
  155. float SimpleAudioEngine::getEffectsVolume()
  156. {
  157. return sharedAudioController()->GetSoundEffectVolume();
  158. }
  159. void SimpleAudioEngine::setEffectsVolume(float volume)
  160. {
  161. sharedAudioController()->SetSoundEffectVolume((volume<=0.0f)? 0.0f : volume);
  162. }
  163. } // end of namespace CocosDenshion