|
|
@@ -1,11 +1,15 @@
|
|
|
#include "Base.h"
|
|
|
#include "Node.h"
|
|
|
#include "AudioBuffer.h"
|
|
|
+#include "AudioController.h"
|
|
|
#include "AudioSource.h"
|
|
|
+#include "Game.h"
|
|
|
|
|
|
namespace gameplay
|
|
|
{
|
|
|
|
|
|
+
|
|
|
+#ifndef __ANDROID__
|
|
|
AudioSource::AudioSource(AudioBuffer* buffer, ALuint source)
|
|
|
: _alSource(source), _buffer(buffer), _looped(true), _gain(1.0f), _pitch(1.0f), _node(NULL)
|
|
|
{
|
|
|
@@ -15,14 +19,86 @@ AudioSource::AudioSource(AudioBuffer* buffer, ALuint source)
|
|
|
alSourcef(_alSource, AL_GAIN, _gain);
|
|
|
alSourcefv(_alSource, AL_VELOCITY, (const ALfloat*)&_velocity);
|
|
|
}
|
|
|
+#else
|
|
|
+AudioSource::AudioSource(AudioBuffer* buffer, const SLObjectItf& player)
|
|
|
+ : _playerObject(player), _playerDoppler(NULL), _playerLocation(NULL), _playerPlay(NULL), _playerPitch(NULL),
|
|
|
+ _playerSeek(NULL), _playerVolume(NULL), _buffer(buffer), _looped(true), _gain(1.0f), _pitch(1.0f), _node(NULL)
|
|
|
+{
|
|
|
+ // Get the different interfaces for the OpenSL audio player that we need.
|
|
|
+ SLresult result = (*_playerObject)->GetInterface(_playerObject, SL_IID_3DDOPPLER, &_playerDoppler);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get 3D doppler interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*_playerObject)->GetInterface(_playerObject, SL_IID_3DLOCATION, &_playerLocation);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get 3D location interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*_playerObject)->GetInterface(_playerObject, SL_IID_PLAY, &_playerPlay);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get play interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*_playerObject)->GetInterface(_playerObject, SL_IID_PITCH, &_playerPitch);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get rate pitch interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*_playerObject)->GetInterface(_playerObject, SL_IID_SEEK, &_playerSeek);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get seek interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*_playerObject)->GetInterface(_playerObject, SL_IID_VOLUME, &_playerVolume);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get volume interface for OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get the max volume level (used to convert from our API's parameter to OpenSL's expected units).
|
|
|
+ if (_playerVolume)
|
|
|
+ {
|
|
|
+ result = (*_playerVolume)->GetMaxVolumeLevel(_playerVolume, &_maxVolume);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::AudioSource() - Failed to get the max volume level for OpenSL audio player (needed for parameter conversion).");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setLooped(_looped);
|
|
|
+ setPitch(_pitch);
|
|
|
+ setGain(_gain);
|
|
|
+ setVelocity(_velocity);
|
|
|
+}
|
|
|
+#endif
|
|
|
|
|
|
AudioSource::~AudioSource()
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
if (_alSource)
|
|
|
{
|
|
|
alDeleteSources(1, &_alSource);
|
|
|
_alSource = 0;
|
|
|
}
|
|
|
+#else
|
|
|
+ if (_playerObject)
|
|
|
+ {
|
|
|
+ (*_playerObject)->Destroy(_playerObject);
|
|
|
+ _playerObject = NULL;
|
|
|
+ _playerDoppler = NULL;
|
|
|
+ _playerLocation = NULL;
|
|
|
+ _playerPlay = NULL;
|
|
|
+ _playerPitch = NULL;
|
|
|
+ _playerSeek = NULL;
|
|
|
+ _playerVolume = NULL;
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
SAFE_RELEASE(_buffer);
|
|
|
}
|
|
|
@@ -52,17 +128,44 @@ AudioSource* AudioSource::create(const char* path)
|
|
|
if (buffer == NULL)
|
|
|
return NULL;
|
|
|
|
|
|
+#ifndef __ANDROID__
|
|
|
// Load the audio source.
|
|
|
- ALuint alSource;
|
|
|
+ ALuint alSource = 0;
|
|
|
+
|
|
|
alGenSources(1, &alSource);
|
|
|
if (alGetError() != AL_NO_ERROR)
|
|
|
{
|
|
|
SAFE_RELEASE(buffer);
|
|
|
- LOG_ERROR("AudioSource::createAudioSource Error generating audio source.");
|
|
|
+ LOG_ERROR("AudioSource::createAudioSource - Error generating audio source.");
|
|
|
return NULL;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return new AudioSource(buffer, alSource);
|
|
|
+#else
|
|
|
+ AudioController* audioController = Game::getInstance()->getAudioController();
|
|
|
+ SLDataLocator_OutputMix locator = {SL_DATALOCATOR_OUTPUTMIX, audioController->_outputMixObject};
|
|
|
+
|
|
|
+ SLDataSource dataSource = {&buffer->_data, &buffer->_mime};
|
|
|
+ SLDataSink dataSink = {&locator, NULL};
|
|
|
+
|
|
|
+ SLObjectItf player;
|
|
|
+ const SLInterfaceID interfaces[] = {SL_IID_3DDOPPLER, SL_IID_3DLOCATION, SL_IID_PLAY, SL_IID_PITCH, SL_IID_SEEK, SL_IID_VOLUME};
|
|
|
+ const SLboolean required[] = {SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE};
|
|
|
+ SLresult result = (*audioController->_engineEngine)->CreateAudioPlayer(audioController->_engineEngine, &player, &dataSource, &dataSink, 6, interfaces, required);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::create - Failed to create OpenSL audio player.");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
|
|
|
+ if(result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::create - Failed to realize OpenSL audio player.");
|
|
|
+ }
|
|
|
+
|
|
|
+ return new AudioSource(buffer, player);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
AudioSource* AudioSource::create(Properties* properties)
|
|
|
@@ -114,6 +217,7 @@ AudioSource* AudioSource::create(Properties* properties)
|
|
|
|
|
|
AudioSource::State AudioSource::getState() const
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
ALint state;
|
|
|
alGetSourcei(_alSource, AL_SOURCE_STATE, &state);
|
|
|
|
|
|
@@ -128,18 +232,63 @@ AudioSource::State AudioSource::getState() const
|
|
|
default:
|
|
|
return INITIAL;
|
|
|
}
|
|
|
+#else
|
|
|
+ if (_playerPlay != NULL)
|
|
|
+ {
|
|
|
+ SLuint32 state;
|
|
|
+ SLresult result = (*_playerPlay)->GetPlayState(_playerPlay, &state);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::getState() failed to get player state.");
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (state)
|
|
|
+ {
|
|
|
+ case SL_PLAYSTATE_PLAYING:
|
|
|
+ return PLAYING;
|
|
|
+ case SL_PLAYSTATE_PAUSED:
|
|
|
+ return PAUSED;
|
|
|
+ case SL_PLAYSTATE_STOPPED:
|
|
|
+ return STOPPED;
|
|
|
+ default:
|
|
|
+ return INITIAL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
return INITIAL;
|
|
|
}
|
|
|
|
|
|
void AudioSource::play()
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcePlay(_alSource);
|
|
|
+#else
|
|
|
+ if (_playerPlay != NULL)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_PLAYING);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::play() failed to set player state.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void AudioSource::pause()
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcePause(_alSource);
|
|
|
+#else
|
|
|
+ if (_playerPlay != NULL)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_PAUSED);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::pause() failed to set player state.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void AudioSource::resume()
|
|
|
@@ -152,12 +301,34 @@ void AudioSource::resume()
|
|
|
|
|
|
void AudioSource::stop()
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourceStop(_alSource);
|
|
|
+#else
|
|
|
+ if (_playerPlay != NULL)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerPlay)->SetPlayState(_playerPlay, SL_PLAYSTATE_STOPPED);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::stop() failed to set player state.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void AudioSource::rewind()
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourceRewind(_alSource);
|
|
|
+#else
|
|
|
+ if (_playerPlay != NULL)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerPlay)->SetMarkerPosition(_playerPlay, 0);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::rewind() failed to set player marker position.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
bool AudioSource::isLooped() const
|
|
|
@@ -167,6 +338,7 @@ bool AudioSource::isLooped() const
|
|
|
|
|
|
void AudioSource::setLooped(bool looped)
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
// Clear error state.
|
|
|
alGetError();
|
|
|
alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE);
|
|
|
@@ -176,6 +348,16 @@ void AudioSource::setLooped(bool looped)
|
|
|
{
|
|
|
LOG_ERROR_VARG("AudioSource::setLooped Error: %d", error);
|
|
|
}
|
|
|
+#else
|
|
|
+ if (_playerSeek)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerSeek)->SetLoop(_playerSeek, looped, 0, SL_TIME_UNKNOWN);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::setLooped() failed.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
_looped = looped;
|
|
|
}
|
|
|
@@ -187,7 +369,19 @@ float AudioSource::getGain() const
|
|
|
|
|
|
void AudioSource::setGain(float gain)
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcef(_alSource, AL_GAIN, gain);
|
|
|
+#else
|
|
|
+ if (_playerVolume)
|
|
|
+ {
|
|
|
+ SLmillibel volume = gain * (_maxVolume - SL_MILLIBEL_MIN) + SL_MILLIBEL_MIN;
|
|
|
+ SLresult result = (*_playerVolume)->SetVolumeLevel(_playerVolume, volume);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::setGain() failed to set player gain.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
_gain = gain;
|
|
|
}
|
|
|
|
|
|
@@ -198,7 +392,18 @@ float AudioSource::getPitch() const
|
|
|
|
|
|
void AudioSource::setPitch(float pitch)
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcef(_alSource, AL_PITCH, pitch);
|
|
|
+#else
|
|
|
+ if (_playerPitch)
|
|
|
+ {
|
|
|
+ SLresult result = (*_playerPitch)->SetPitch(_playerPitch, (SLpermille)(pitch * 1000));
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::setPitch() failed to set player pitch.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
_pitch = pitch;
|
|
|
}
|
|
|
|
|
|
@@ -209,7 +414,22 @@ const Vector3& AudioSource::getVelocity() const
|
|
|
|
|
|
void AudioSource::setVelocity(const Vector3& velocity)
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcefv(_alSource, AL_VELOCITY, (ALfloat*)&velocity);
|
|
|
+#else
|
|
|
+ if (_playerDoppler)
|
|
|
+ {
|
|
|
+ SLVec3D v;
|
|
|
+ v.x = velocity.x;
|
|
|
+ v.y = velocity.y;
|
|
|
+ v.z = velocity.z;
|
|
|
+ SLresult result = (*_playerDoppler)->SetVelocityCartesian(_playerDoppler, &v);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::setVelocity - failed to set velocity.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
_velocity = velocity;
|
|
|
}
|
|
|
|
|
|
@@ -240,7 +460,22 @@ void AudioSource::setNode(Node* node)
|
|
|
|
|
|
void AudioSource::transformChanged(Transform* transform, long cookie)
|
|
|
{
|
|
|
+#ifndef __ANDROID__
|
|
|
alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&transform->getTranslation());
|
|
|
+#else
|
|
|
+ if (_playerLocation)
|
|
|
+ {
|
|
|
+ SLVec3D position;
|
|
|
+ position.x = transform->getTranslationX();
|
|
|
+ position.y = transform->getTranslationY();
|
|
|
+ position.z = transform->getTranslationZ();
|
|
|
+ SLresult result = (*_playerLocation)->SetLocationCartesian(_playerLocation, &position);
|
|
|
+ if (result != SL_RESULT_SUCCESS)
|
|
|
+ {
|
|
|
+ WARN("AudioSource::transformChanged - failed to update location.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
}
|