Răsfoiți Sursa

Audio support for Android using OpenSL ES. Note that multiple features (pitch, 3D audio) are not currently supported by Android.

Chris Culy 14 ani în urmă
părinte
comite
21c2f28786
2 a modificat fișierele cu 96 adăugiri și 95 ștergeri
  1. 2 1
      .gitignore
  2. 94 94
      gameplay/src/AudioSource.cpp

+ 2 - 1
.gitignore

@@ -123,4 +123,5 @@
 /gameplay-samples/sample03-character/android/gen
 /gameplay-samples/sample03-character/android/libs
 /gameplay-samples/sample03-character/android/obj
-/gameplay-samples/sample03-character/android/NUL
+/gameplay-samples/sample03-character/android/NUL
+/gameplay-samples/sample01-longboard/NUL

+ 94 - 94
gameplay/src/AudioSource.cpp

@@ -24,41 +24,41 @@ 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 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).
@@ -149,7 +149,7 @@ AudioSource* AudioSource::create(const char* path)
     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 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)
@@ -158,10 +158,10 @@ AudioSource* AudioSource::create(const char* path)
         return NULL;
     }
 
-    result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
-    if(result != SL_RESULT_SUCCESS)
-    {
-        WARN("AudioSource::create - Failed to realize OpenSL audio player.");
+    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);
@@ -232,27 +232,27 @@ 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;
-        }
+#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
 
@@ -263,14 +263,14 @@ 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.");
-        }
+#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
 }
@@ -280,13 +280,13 @@ 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.");
-        }
+    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
 }
@@ -304,13 +304,13 @@ 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.");
-        }
+    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 
 }
@@ -320,13 +320,13 @@ 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.");
-        }
+    if (_playerPlay != NULL)
+    {
+        SLresult result = (*_playerPlay)->SetMarkerPosition(_playerPlay, 0);
+        if (result != SL_RESULT_SUCCESS)
+        {
+            WARN("AudioSource::rewind() failed to set player marker position.");
+        }
     }
 #endif
 }
@@ -374,7 +374,7 @@ void AudioSource::setGain(float gain)
 #else
     if (_playerVolume)
     {
-        SLmillibel volume = gain * (_maxVolume - SL_MILLIBEL_MIN) + SL_MILLIBEL_MIN;
+        SLmillibel volume = (gain < MATH_EPSILON) ? SL_MILLIBEL_MIN : (10.0f * log10(gain)) * 100;
         SLresult result = (*_playerVolume)->SetVolumeLevel(_playerVolume, volume);
         if (result != SL_RESULT_SUCCESS)
         {
@@ -398,9 +398,9 @@ void AudioSource::setPitch(float pitch)
     if (_playerPitch)
     {
         SLresult result = (*_playerPitch)->SetPitch(_playerPitch, (SLpermille)(pitch * 1000));
-        if (result != SL_RESULT_SUCCESS)
-        {
-            WARN("AudioSource::setPitch() failed to set player pitch.");
+        if (result != SL_RESULT_SUCCESS)
+        {
+            WARN("AudioSource::setPitch() failed to set player pitch.");
         }
     }
 #endif