AudioSource.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "AudioBuffer.h"
  4. #include "AudioController.h"
  5. #include "AudioSource.h"
  6. #include "Game.h"
  7. #include "Node.h"
  8. namespace gameplay
  9. {
  10. #ifndef __ANDROID__
  11. AudioSource::AudioSource(AudioBuffer* buffer, ALuint source)
  12. : _alSource(source), _buffer(buffer), _looped(true), _gain(1.0f), _pitch(1.0f), _node(NULL)
  13. {
  14. alSourcei(_alSource, AL_BUFFER, buffer->_alBuffer);
  15. alSourcei(_alSource, AL_LOOPING, _looped);
  16. alSourcef(_alSource, AL_PITCH, _pitch);
  17. alSourcef(_alSource, AL_GAIN, _gain);
  18. alSourcefv(_alSource, AL_VELOCITY, (const ALfloat*)&_velocity);
  19. }
  20. #else
  21. AudioSource::AudioSource(AudioBuffer* buffer, const SLObjectItf& player)
  22. : _playerObject(player), _playerDoppler(NULL), _playerLocation(NULL), _playerPlay(NULL), _playerPitch(NULL),
  23. _playerSeek(NULL), _playerVolume(NULL), _buffer(buffer), _looped(true), _gain(1.0f), _pitch(1.0f), _node(NULL)
  24. {
  25. // Get the different interfaces for the OpenSL audio player that we need.
  26. SLresult result = (*_playerObject)->GetInterface(_playerObject, SL_IID_3DDOPPLER, &_playerDoppler);
  27. if (result != SL_RESULT_SUCCESS)
  28. {
  29. WARN("AudioSource::AudioSource() - Failed to get 3D doppler interface for OpenSL audio player.");
  30. }
  31. result = (*_playerObject)->GetInterface(_playerObject, SL_IID_3DLOCATION, &_playerLocation);
  32. if (result != SL_RESULT_SUCCESS)
  33. {
  34. WARN("AudioSource::AudioSource() - Failed to get 3D location interface for OpenSL audio player.");
  35. }
  36. result = (*_playerObject)->GetInterface(_playerObject, SL_IID_PLAY, &_playerPlay);
  37. if (result != SL_RESULT_SUCCESS)
  38. {
  39. WARN("AudioSource::AudioSource() - Failed to get play interface for OpenSL audio player.");
  40. }
  41. result = (*_playerObject)->GetInterface(_playerObject, SL_IID_PITCH, &_playerPitch);
  42. if (result != SL_RESULT_SUCCESS)
  43. {
  44. WARN("AudioSource::AudioSource() - Failed to get rate pitch interface for OpenSL audio player.");
  45. }
  46. result = (*_playerObject)->GetInterface(_playerObject, SL_IID_SEEK, &_playerSeek);
  47. if (result != SL_RESULT_SUCCESS)
  48. {
  49. WARN("AudioSource::AudioSource() - Failed to get seek interface for OpenSL audio player.");
  50. }
  51. result = (*_playerObject)->GetInterface(_playerObject, SL_IID_VOLUME, &_playerVolume);
  52. if (result != SL_RESULT_SUCCESS)
  53. {
  54. WARN("AudioSource::AudioSource() - Failed to get volume interface for OpenSL audio player.");
  55. }
  56. // Get the max volume level (used to convert from our API's parameter to OpenSL's expected units).
  57. if (_playerVolume)
  58. {
  59. result = (*_playerVolume)->GetMaxVolumeLevel(_playerVolume, &_maxVolume);
  60. if (result != SL_RESULT_SUCCESS)
  61. {
  62. WARN("AudioSource::AudioSource() - Failed to get the max volume level for OpenSL audio player (needed for parameter conversion).");
  63. }
  64. }
  65. setLooped(_looped);
  66. setPitch(_pitch);
  67. setGain(_gain);
  68. setVelocity(_velocity);
  69. }
  70. #endif
  71. AudioSource::~AudioSource()
  72. {
  73. #ifndef __ANDROID__
  74. if (_alSource)
  75. {
  76. alDeleteSources(1, &_alSource);
  77. _alSource = 0;
  78. }
  79. #else
  80. if (_playerObject)
  81. {
  82. (*_playerObject)->Destroy(_playerObject);
  83. _playerObject = NULL;
  84. _playerDoppler = NULL;
  85. _playerLocation = NULL;
  86. _playerPlay = NULL;
  87. _playerPitch = NULL;
  88. _playerSeek = NULL;
  89. _playerVolume = NULL;
  90. }
  91. #endif
  92. SAFE_RELEASE(_buffer);
  93. }
  94. AudioSource* AudioSource::create(const char* path)
  95. {
  96. assert(path);
  97. // Load from a .audio file.
  98. std::string pathStr = path;
  99. if (pathStr.find(".audio") != pathStr.npos)
  100. {
  101. Properties* properties = Properties::create(path);
  102. assert(properties);
  103. if (properties == NULL)
  104. {
  105. return NULL;
  106. }
  107. AudioSource* audioSource = create(properties->getNextNamespace());
  108. SAFE_DELETE(properties);
  109. return audioSource;
  110. }
  111. // Create an audio buffer from this path.
  112. AudioBuffer* buffer = AudioBuffer::create(path);
  113. if (buffer == NULL)
  114. return NULL;
  115. #ifndef __ANDROID__
  116. // Load the audio source.
  117. ALuint alSource = 0;
  118. alGenSources(1, &alSource);
  119. if (alGetError() != AL_NO_ERROR)
  120. {
  121. SAFE_RELEASE(buffer);
  122. LOG_ERROR("AudioSource::createAudioSource - Error generating audio source.");
  123. return NULL;
  124. }
  125. return new AudioSource(buffer, alSource);
  126. #else
  127. AudioController* audioController = Game::getInstance()->getAudioController();
  128. SLDataLocator_OutputMix locator = {SL_DATALOCATOR_OUTPUTMIX, audioController->_outputMixObject};
  129. SLDataSource dataSource = {&buffer->_data, &buffer->_mime};
  130. SLDataSink dataSink = {&locator, NULL};
  131. SLObjectItf player;
  132. const SLInterfaceID interfaces[] = {SL_IID_3DDOPPLER, SL_IID_3DLOCATION, SL_IID_PLAY, SL_IID_PITCH, SL_IID_SEEK, SL_IID_VOLUME};
  133. const SLboolean required[] = {SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE};
  134. SLresult result = (*audioController->_engineEngine)->CreateAudioPlayer(audioController->_engineEngine, &player, &dataSource, &dataSink, 6, interfaces, required);
  135. if (result != SL_RESULT_SUCCESS)
  136. {
  137. WARN("AudioSource::create - Failed to create OpenSL audio player.");
  138. return NULL;
  139. }
  140. result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  141. if (result != SL_RESULT_SUCCESS)
  142. {
  143. WARN("AudioSource::create - Failed to realize OpenSL audio player.");
  144. }
  145. return new AudioSource(buffer, player);
  146. #endif
  147. }
  148. AudioSource* AudioSource::create(Properties* properties)
  149. {
  150. // Check if the properties is valid and has a valid namespace.
  151. assert(properties);
  152. if (!properties || !(strcmp(properties->getNamespace(), "audio") == 0))
  153. {
  154. WARN("Failed to load audio source from properties object: must be non-null object and have namespace equal to \'audio\'.");
  155. return NULL;
  156. }
  157. const char* path = properties->getString("path");
  158. if (path == NULL)
  159. {
  160. WARN("Audio file failed to load; the file path was not specified.");
  161. return NULL;
  162. }
  163. // Create the audio source.
  164. AudioSource* audio = AudioSource::create(path);
  165. if (audio == NULL)
  166. {
  167. WARN_VARG("Audio file '%s' failed to load properly.", path);
  168. return NULL;
  169. }
  170. // Set any properties that the user specified in the .audio file.
  171. if (properties->getString("looped") != NULL)
  172. {
  173. audio->setLooped(properties->getBool("looped"));
  174. }
  175. if (properties->getString("gain") != NULL)
  176. {
  177. audio->setGain(properties->getFloat("gain"));
  178. }
  179. if (properties->getString("pitch") != NULL)
  180. {
  181. audio->setPitch(properties->getFloat("pitch"));
  182. }
  183. Vector3 v;
  184. if (properties->getVector3("velocity", &v))
  185. {
  186. audio->setVelocity(v);
  187. }
  188. return audio;
  189. }
  190. AudioSource::State AudioSource::getState() const
  191. {
  192. #ifndef __ANDROID__
  193. ALint state;
  194. alGetSourcei(_alSource, AL_SOURCE_STATE, &state);
  195. switch (state)
  196. {
  197. case AL_PLAYING:
  198. return PLAYING;
  199. case AL_PAUSED:
  200. return PAUSED;
  201. case AL_STOPPED:
  202. return STOPPED;
  203. default:
  204. return INITIAL;
  205. }
  206. #else
  207. if (_playerPlay != NULL)
  208. {
  209. SLuint32 state;
  210. SLresult result = (*_playerPlay)->GetPlayState(_playerPlay, &state);
  211. if (result != SL_RESULT_SUCCESS)
  212. {
  213. WARN("AudioSource::getState() failed to get player state.");
  214. }
  215. switch (state)
  216. {
  217. case SL_PLAYSTATE_PLAYING:
  218. return PLAYING;
  219. case SL_PLAYSTATE_PAUSED:
  220. return PAUSED;
  221. case SL_PLAYSTATE_STOPPED:
  222. return STOPPED;
  223. default:
  224. return INITIAL;
  225. }
  226. }
  227. #endif
  228. return INITIAL;
  229. }
  230. void AudioSource::play()
  231. {
  232. #ifndef __ANDROID__
  233. alSourcePlay(_alSource);
  234. #else
  235. if (_playerPlay != NULL)
  236. {
  237. SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_PLAYING);
  238. if (result != SL_RESULT_SUCCESS)
  239. {
  240. WARN("AudioSource::play() failed to set player state.");
  241. }
  242. }
  243. #endif
  244. // Add the source to the controller's list of currently playing sources.
  245. AudioController* audioController = Game::getInstance()->getAudioController();
  246. if (audioController->_playingSources.find(this) == audioController->_playingSources.end())
  247. audioController->_playingSources.insert(this);
  248. }
  249. void AudioSource::pause()
  250. {
  251. #ifndef __ANDROID__
  252. alSourcePause(_alSource);
  253. #else
  254. if (_playerPlay != NULL)
  255. {
  256. SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_PAUSED);
  257. if (result != SL_RESULT_SUCCESS)
  258. {
  259. WARN("AudioSource::pause() failed to set player state.");
  260. }
  261. }
  262. #endif
  263. // Remove the source from the controller's set of currently playing sources
  264. // if the source is being paused by the user and not the controller itself.
  265. AudioController* audioController = Game::getInstance()->getAudioController();
  266. if (audioController->_pausingSource != this)
  267. {
  268. std::set<AudioSource*>::iterator iter = audioController->_playingSources.find(this);
  269. if (iter != audioController->_playingSources.end())
  270. {
  271. WARN("\n\nRemoving an audio source from the list of playing sources...\n\n\n");
  272. audioController->_playingSources.erase(iter);
  273. }
  274. }
  275. }
  276. void AudioSource::resume()
  277. {
  278. if (getState() == PAUSED)
  279. {
  280. play();
  281. }
  282. }
  283. void AudioSource::stop()
  284. {
  285. #ifndef __ANDROID__
  286. alSourceStop(_alSource);
  287. #else
  288. if (_playerPlay != NULL)
  289. {
  290. SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_STOPPED);
  291. if (result != SL_RESULT_SUCCESS)
  292. {
  293. WARN("AudioSource::stop() failed to set player state.");
  294. }
  295. }
  296. #endif
  297. // Remove the source from the controller's set of currently playing sources.
  298. AudioController* audioController = Game::getInstance()->getAudioController();
  299. std::set<AudioSource*>::iterator iter = audioController->_playingSources.find(this);
  300. if (iter != audioController->_playingSources.end())
  301. audioController->_playingSources.erase(iter);
  302. }
  303. void AudioSource::rewind()
  304. {
  305. #ifndef __ANDROID__
  306. alSourceRewind(_alSource);
  307. #else
  308. if (_playerPlay != NULL)
  309. {
  310. SLresult result = (*_playerPlay)->SetMarkerPosition(_playerPlay, 0);
  311. if (result != SL_RESULT_SUCCESS)
  312. {
  313. WARN("AudioSource::rewind() failed to set player marker position.");
  314. }
  315. }
  316. #endif
  317. }
  318. bool AudioSource::isLooped() const
  319. {
  320. return _looped;
  321. }
  322. void AudioSource::setLooped(bool looped)
  323. {
  324. #ifndef __ANDROID__
  325. // Clear error state.
  326. alGetError();
  327. alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE);
  328. ALCenum error = alGetError();
  329. if (error != AL_NO_ERROR)
  330. {
  331. LOG_ERROR_VARG("AudioSource::setLooped Error: %d", error);
  332. }
  333. #else
  334. if (_playerSeek)
  335. {
  336. SLresult result = (*_playerSeek)->SetLoop(_playerSeek, looped, 0, SL_TIME_UNKNOWN);
  337. if (result != SL_RESULT_SUCCESS)
  338. {
  339. WARN("AudioSource::setLooped() failed.");
  340. }
  341. }
  342. #endif
  343. _looped = looped;
  344. }
  345. float AudioSource::getGain() const
  346. {
  347. return _gain;
  348. }
  349. void AudioSource::setGain(float gain)
  350. {
  351. #ifndef __ANDROID__
  352. alSourcef(_alSource, AL_GAIN, gain);
  353. #else
  354. if (_playerVolume)
  355. {
  356. SLmillibel volume = (gain < MATH_EPSILON) ? SL_MILLIBEL_MIN : (10.0f * log10(gain)) * 100;
  357. SLresult result = (*_playerVolume)->SetVolumeLevel(_playerVolume, volume);
  358. if (result != SL_RESULT_SUCCESS)
  359. {
  360. WARN("AudioSource::setGain() failed to set player gain.");
  361. }
  362. }
  363. #endif
  364. _gain = gain;
  365. }
  366. float AudioSource::getPitch() const
  367. {
  368. return _pitch;
  369. }
  370. void AudioSource::setPitch(float pitch)
  371. {
  372. #ifndef __ANDROID__
  373. alSourcef(_alSource, AL_PITCH, pitch);
  374. #else
  375. if (_playerPitch)
  376. {
  377. SLresult result = (*_playerPitch)->SetPitch(_playerPitch, (SLpermille)(pitch * 1000));
  378. if (result != SL_RESULT_SUCCESS)
  379. {
  380. WARN("AudioSource::setPitch() failed to set player pitch.");
  381. }
  382. }
  383. #endif
  384. _pitch = pitch;
  385. }
  386. const Vector3& AudioSource::getVelocity() const
  387. {
  388. return _velocity;
  389. }
  390. void AudioSource::setVelocity(const Vector3& velocity)
  391. {
  392. #ifndef __ANDROID__
  393. alSourcefv(_alSource, AL_VELOCITY, (ALfloat*)&velocity);
  394. #else
  395. if (_playerDoppler)
  396. {
  397. SLVec3D v;
  398. v.x = velocity.x;
  399. v.y = velocity.y;
  400. v.z = velocity.z;
  401. SLresult result = (*_playerDoppler)->SetVelocityCartesian(_playerDoppler, &v);
  402. if (result != SL_RESULT_SUCCESS)
  403. {
  404. WARN("AudioSource::setVelocity - failed to set velocity.");
  405. }
  406. }
  407. #endif
  408. _velocity = velocity;
  409. }
  410. Node* AudioSource::getNode() const
  411. {
  412. return _node;
  413. }
  414. void AudioSource::setNode(Node* node)
  415. {
  416. if (_node != node)
  417. {
  418. // Disconnect our current transform.
  419. if (_node)
  420. {
  421. _node->removeListener(this);
  422. }
  423. // Connect the new node.
  424. _node = node;
  425. if (_node)
  426. {
  427. _node->addListener(this);
  428. // Update the audio source position.
  429. transformChanged(_node, 0);
  430. }
  431. }
  432. }
  433. void AudioSource::transformChanged(Transform* transform, long cookie)
  434. {
  435. #ifndef __ANDROID__
  436. if (_node)
  437. {
  438. Vector3 translation = _node->getTranslationWorld();
  439. alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&translation.x);
  440. }
  441. #else
  442. if (_playerLocation)
  443. {
  444. SLVec3D position;
  445. position.x = transform->getTranslationX();
  446. position.y = transform->getTranslationY();
  447. position.z = transform->getTranslationZ();
  448. SLresult result = (*_playerLocation)->SetLocationCartesian(_playerLocation, &position);
  449. if (result != SL_RESULT_SUCCESS)
  450. {
  451. WARN("AudioSource::transformChanged - failed to update location.");
  452. }
  453. }
  454. #endif
  455. }
  456. AudioSource* AudioSource::clone(NodeCloneContext &context) const
  457. {
  458. #ifndef __ANDROID__
  459. ALuint alSource = 0;
  460. alGenSources(1, &alSource);
  461. if (alGetError() != AL_NO_ERROR)
  462. {
  463. LOG_ERROR("AudioSource::createAudioSource - Error generating audio source.");
  464. return NULL;
  465. }
  466. AudioSource* audioClone = new AudioSource(_buffer, alSource);
  467. #else
  468. // TODO: Implement cloning audio source for Android
  469. AudioSource* audioClone = new AudioSource(_buffer, _playerObject);
  470. #endif
  471. _buffer->addRef();
  472. audioClone->setLooped(isLooped());
  473. audioClone->setGain(getGain());
  474. audioClone->setPitch(getPitch());
  475. audioClone->setVelocity(getVelocity());
  476. if (Node* node = getNode())
  477. {
  478. Node* clonedNode = context.findClonedNode(node);
  479. if (clonedNode)
  480. {
  481. audioClone->setNode(clonedNode);
  482. }
  483. }
  484. return audioClone;
  485. }
  486. }