AudioSource.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. }
  245. void AudioSource::pause()
  246. {
  247. #ifndef __ANDROID__
  248. alSourcePause(_alSource);
  249. #else
  250. if (_playerPlay != NULL)
  251. {
  252. SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_PAUSED);
  253. if (result != SL_RESULT_SUCCESS)
  254. {
  255. WARN("AudioSource::pause() failed to set player state.");
  256. }
  257. }
  258. #endif
  259. }
  260. void AudioSource::resume()
  261. {
  262. if (getState() == PAUSED)
  263. {
  264. play();
  265. }
  266. }
  267. void AudioSource::stop()
  268. {
  269. #ifndef __ANDROID__
  270. alSourceStop(_alSource);
  271. #else
  272. if (_playerPlay != NULL)
  273. {
  274. SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_STOPPED);
  275. if (result != SL_RESULT_SUCCESS)
  276. {
  277. WARN("AudioSource::stop() failed to set player state.");
  278. }
  279. }
  280. #endif
  281. }
  282. void AudioSource::rewind()
  283. {
  284. #ifndef __ANDROID__
  285. alSourceRewind(_alSource);
  286. #else
  287. if (_playerPlay != NULL)
  288. {
  289. SLresult result = (*_playerPlay)->SetMarkerPosition(_playerPlay, 0);
  290. if (result != SL_RESULT_SUCCESS)
  291. {
  292. WARN("AudioSource::rewind() failed to set player marker position.");
  293. }
  294. }
  295. #endif
  296. }
  297. bool AudioSource::isLooped() const
  298. {
  299. return _looped;
  300. }
  301. void AudioSource::setLooped(bool looped)
  302. {
  303. #ifndef __ANDROID__
  304. // Clear error state.
  305. alGetError();
  306. alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE);
  307. ALCenum error = alGetError();
  308. if (error != AL_NO_ERROR)
  309. {
  310. LOG_ERROR_VARG("AudioSource::setLooped Error: %d", error);
  311. }
  312. #else
  313. if (_playerSeek)
  314. {
  315. SLresult result = (*_playerSeek)->SetLoop(_playerSeek, looped, 0, SL_TIME_UNKNOWN);
  316. if (result != SL_RESULT_SUCCESS)
  317. {
  318. WARN("AudioSource::setLooped() failed.");
  319. }
  320. }
  321. #endif
  322. _looped = looped;
  323. }
  324. float AudioSource::getGain() const
  325. {
  326. return _gain;
  327. }
  328. void AudioSource::setGain(float gain)
  329. {
  330. #ifndef __ANDROID__
  331. alSourcef(_alSource, AL_GAIN, gain);
  332. #else
  333. if (_playerVolume)
  334. {
  335. SLmillibel volume = (gain < MATH_EPSILON) ? SL_MILLIBEL_MIN : (10.0f * log10(gain)) * 100;
  336. SLresult result = (*_playerVolume)->SetVolumeLevel(_playerVolume, volume);
  337. if (result != SL_RESULT_SUCCESS)
  338. {
  339. WARN("AudioSource::setGain() failed to set player gain.");
  340. }
  341. }
  342. #endif
  343. _gain = gain;
  344. }
  345. float AudioSource::getPitch() const
  346. {
  347. return _pitch;
  348. }
  349. void AudioSource::setPitch(float pitch)
  350. {
  351. #ifndef __ANDROID__
  352. alSourcef(_alSource, AL_PITCH, pitch);
  353. #else
  354. if (_playerPitch)
  355. {
  356. SLresult result = (*_playerPitch)->SetPitch(_playerPitch, (SLpermille)(pitch * 1000));
  357. if (result != SL_RESULT_SUCCESS)
  358. {
  359. WARN("AudioSource::setPitch() failed to set player pitch.");
  360. }
  361. }
  362. #endif
  363. _pitch = pitch;
  364. }
  365. const Vector3& AudioSource::getVelocity() const
  366. {
  367. return _velocity;
  368. }
  369. void AudioSource::setVelocity(const Vector3& velocity)
  370. {
  371. #ifndef __ANDROID__
  372. alSourcefv(_alSource, AL_VELOCITY, (ALfloat*)&velocity);
  373. #else
  374. if (_playerDoppler)
  375. {
  376. SLVec3D v;
  377. v.x = velocity.x;
  378. v.y = velocity.y;
  379. v.z = velocity.z;
  380. SLresult result = (*_playerDoppler)->SetVelocityCartesian(_playerDoppler, &v);
  381. if (result != SL_RESULT_SUCCESS)
  382. {
  383. WARN("AudioSource::setVelocity - failed to set velocity.");
  384. }
  385. }
  386. #endif
  387. _velocity = velocity;
  388. }
  389. Node* AudioSource::getNode() const
  390. {
  391. return _node;
  392. }
  393. void AudioSource::setNode(Node* node)
  394. {
  395. if (_node != node)
  396. {
  397. // Disconnect our current transform.
  398. if (_node)
  399. {
  400. _node->removeListener(this);
  401. }
  402. // Connect the new node.
  403. _node = node;
  404. if (_node)
  405. {
  406. _node->addListener(this);
  407. // Update the audio source position.
  408. transformChanged(_node, 0);
  409. }
  410. }
  411. }
  412. void AudioSource::transformChanged(Transform* transform, long cookie)
  413. {
  414. #ifndef __ANDROID__
  415. if (_node)
  416. {
  417. Vector3 translation = _node->getTranslationWorld();
  418. alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&translation.x);
  419. }
  420. #else
  421. if (_playerLocation)
  422. {
  423. SLVec3D position;
  424. position.x = transform->getTranslationX();
  425. position.y = transform->getTranslationY();
  426. position.z = transform->getTranslationZ();
  427. SLresult result = (*_playerLocation)->SetLocationCartesian(_playerLocation, &position);
  428. if (result != SL_RESULT_SUCCESS)
  429. {
  430. WARN("AudioSource::transformChanged - failed to update location.");
  431. }
  432. }
  433. #endif
  434. }
  435. AudioSource* AudioSource::clone(NodeCloneContext &context) const
  436. {
  437. #ifndef __ANDROID__
  438. ALuint alSource = 0;
  439. alGenSources(1, &alSource);
  440. if (alGetError() != AL_NO_ERROR)
  441. {
  442. LOG_ERROR("AudioSource::createAudioSource - Error generating audio source.");
  443. return NULL;
  444. }
  445. AudioSource* audioClone = new AudioSource(_buffer, alSource);
  446. #else
  447. // TODO: Implement cloning audio source for Android
  448. AudioSource* audioClone = new AudioSource(_buffer, _playerObject);
  449. #endif
  450. _buffer->addRef();
  451. audioClone->setLooped(isLooped());
  452. audioClone->setGain(getGain());
  453. audioClone->setPitch(getPitch());
  454. audioClone->setVelocity(getVelocity());
  455. if (Node* node = getNode())
  456. {
  457. Node* clonedNode = context.findClonedNode(node);
  458. if (clonedNode)
  459. {
  460. audioClone->setNode(clonedNode);
  461. }
  462. }
  463. return audioClone;
  464. }
  465. }