Browse Source

Emscripten audio support

Chris Friesen 11 years ago
parent
commit
b8c04a3e9e
2 changed files with 59 additions and 33 deletions
  1. 52 32
      Source/Urho3D/Audio/Audio.cpp
  2. 7 1
      Source/Urho3D/Audio/Audio.h

+ 52 - 32
Source/Urho3D/Audio/Audio.cpp

@@ -55,10 +55,10 @@ Audio::Audio(Context* context) :
 {
     // Set the master to the default value
     masterGain_[SOUND_MASTER_HASH] = 1.0f;
-    
+
     // Register Audio library object factories
     RegisterAudioLibrary(context_);
-    
+
     SubscribeToEvent(E_RENDERUPDATE, HANDLER(Audio, HandleRenderUpdate));
 }
 
@@ -70,32 +70,47 @@ Audio::~Audio()
 bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation)
 {
     Release();
-    
+
     bufferLengthMSec = Max(bufferLengthMSec, MIN_BUFFERLENGTH);
     mixRate = Clamp(mixRate, MIN_MIXRATE, MAX_MIXRATE);
-    
+
     SDL_AudioSpec desired;
     SDL_AudioSpec obtained;
-    
+
     desired.freq = mixRate;
-    desired.format = AUDIO_S16SYS;
+
+// The concept behind the emspcripten audio port is to treat it as 16 bit until the final acumulation form the clip buffer
+#ifdef EMSCRIPTEN
+    desired.format = AUDIO_F32LSB;
+#else
+    desired.format = AUDIO_S16;
+#endif
     desired.channels = stereo ? 2 : 1;
     desired.callback = SDLAudioCallback;
     desired.userdata = this;
-    
+
     // SDL uses power of two audio fragments. Determine the closest match
     int bufferSamples = mixRate * bufferLengthMSec / 1000;
     desired.samples = NextPowerOfTwo(bufferSamples);
     if (Abs((int)desired.samples / 2 - bufferSamples) < Abs((int)desired.samples - bufferSamples))
         desired.samples /= 2;
-    
+
     deviceID_ = SDL_OpenAudioDevice(0, SDL_FALSE, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
     if (!deviceID_)
     {
         LOGERROR("Could not initialize audio output");
         return false;
-    }
-    
+    }
+
+#ifdef EMSCRIPTEN
+    if (obtained.format != AUDIO_F32LSB && obtained.format != AUDIO_F32MSB && obtained.format != AUDIO_F32SYS)
+    {
+        LOGERROR("Could not initialize audio output, 32-bit float buffer format not supported");
+        SDL_CloseAudioDevice(deviceID_);
+        deviceID_ = 0;
+        return false;
+    }
+#else
     if (obtained.format != AUDIO_S16SYS && obtained.format != AUDIO_S16LSB && obtained.format != AUDIO_S16MSB)
     {
         LOGERROR("Could not initialize audio output, 16-bit buffer format not supported");
@@ -103,25 +118,26 @@ bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpo
         deviceID_ = 0;
         return false;
     }
-    
+#endif
+
     stereo_ = obtained.channels == 2;
     sampleSize_ = stereo_ ? sizeof(int) : sizeof(short);
     // Guarantee a fragment size that is low enough so that Vorbis decoding buffers do not wrap
     fragmentSize_ = Min((int)NextPowerOfTwo(mixRate >> 6), (int)obtained.samples);
-    mixRate_ = mixRate;
+    mixRate_ = obtained.freq;
     interpolation_ = interpolation;
     clipBuffer_ = new int[stereo ? fragmentSize_ << 1 : fragmentSize_];
-    
+
     LOGINFO("Set audio mode " + String(mixRate_) + " Hz " + (stereo_ ? "stereo" : "mono") + " " +
         (interpolation_ ? "interpolated" : ""));
-    
+
     return Play();
 }
 
 void Audio::Update(float timeStep)
 {
     PROFILE(UpdateAudio);
-    
+
     // Update in reverse order, because sound sources might remove themselves
     for (unsigned i = soundSources_.Size() - 1; i < soundSources_.Size(); --i)
         soundSources_[i]->Update(timeStep);
@@ -131,15 +147,15 @@ bool Audio::Play()
 {
     if (playing_)
         return true;
-    
+
     if (!deviceID_)
     {
         LOGERROR("No audio mode set, can not start playback");
         return false;
     }
-    
+
     SDL_PauseAudioDevice(deviceID_, 0);
-    
+
     playing_ = true;
     return true;
 }
@@ -220,10 +236,9 @@ float Audio::GetSoundSourceMasterGain(StringHash typeHash) const
 void SDLAudioCallback(void *userdata, Uint8* stream, int len)
 {
     Audio* audio = static_cast<Audio*>(userdata);
-    
     {
         MutexLock Lock(audio->GetMutex());
-        audio->MixOutput(stream, len / audio->GetSampleSize());
+        audio->MixOutput(stream, len / audio->GetSampleSize() / Audio::SAMPLE_SIZE_MUL);
     }
 }
 
@@ -231,10 +246,10 @@ void Audio::MixOutput(void *dest, unsigned samples)
 {
     if (!playing_ || !clipBuffer_)
     {
-        memset(dest, 0, samples * sampleSize_);
+        memset(dest, 0, samples * sampleSize_ * SAMPLE_SIZE_MUL);
         return;
     }
-    
+
     while (samples)
     {
         // If sample count exceeds the fragment (clip buffer) size, split the work
@@ -242,36 +257,41 @@ void Audio::MixOutput(void *dest, unsigned samples)
         unsigned clipSamples = workSamples;
         if (stereo_)
             clipSamples <<= 1;
-        
+
         // Clear clip buffer
         int* clipPtr = clipBuffer_.Get();
         memset(clipPtr, 0, clipSamples * sizeof(int));
-        
+
         // Mix samples to clip buffer
         for (PODVector<SoundSource*>::Iterator i = soundSources_.Begin(); i != soundSources_.End(); ++i)
             (*i)->Mix(clipPtr, workSamples, mixRate_, stereo_, interpolation_);
-        
-        // Copy output from clip buffer to destination
-        short* destPtr = (short*)dest;
+
+        // Copy output from clip buffer to destination
+#ifdef EMSCRIPTEN
+        float* destPtr = (float*)dest;
         while (clipSamples--)
-            *destPtr++ = Clamp(*clipPtr++, -32768, 32767);
-        
+            *destPtr++ = (float)Clamp(*clipPtr++, -32768, 32767) / 32768.0f;
+#else
+        short* destPtr = (short*)dest;
+        while (clipSamples--)
+            *destPtr++ = Clamp(*clipPtr++, -32768, 32767);
+#endif
         samples -= workSamples;
-        ((unsigned char*&)dest) += sampleSize_ * workSamples;
+        ((unsigned char*&)dest) += sampleSize_ * SAMPLE_SIZE_MUL * workSamples;
     }
 }
 
 void Audio::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
 {
     using namespace RenderUpdate;
-    
+
     Update(eventData[P_TIMESTEP].GetFloat());
 }
 
 void Audio::Release()
 {
     Stop();
-    
+
     if (deviceID_)
     {
         SDL_CloseAudioDevice(deviceID_);

+ 7 - 1
Source/Urho3D/Audio/Audio.h

@@ -94,7 +94,13 @@ public:
 
     /// Mix sound sources into the buffer.
     void MixOutput(void *dest, unsigned samples);
-
+
+    /// Final multiplier for for audio byte conversion
+#ifdef EMSCRIPTEN
+    static const int SAMPLE_SIZE_MUL = 2;
+#else
+    static const int SAMPLE_SIZE_MUL = 1;
+#endif
 private:
     /// Handle render update event.
     void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);