Davidobot / love.js

LÖVE ported to the web using Emscripten, updated to the latest Emscripten and LÖVE (v11.5)
MIT License
624 stars 28 forks source link

Audio Playback stops after ~1 second in compatibility mode #2

Closed jiaaro closed 2 years ago

jiaaro commented 4 years ago

Even with "static", audio stops playing after about 1 second. In Safari it only plays the first time :play() is called, in chrome it plays the first ~1 second each time :play() is called

Davidobot commented 4 years ago

Issue is more complex than it seems. Quoting slime:

I'm not sure what the love.js code does currently so maybe this is a redundant idea, but would it make sense for love.event.pump to also process audio, when threads aren't available?

rameshvarun commented 2 years ago

I was able to fix this with the following patch. All it does is call the pool->update function in Event:pump(), which then triggers individual sources to decode more samples into their buffer.

This function is normally called in a loop by a thread, so there are probably some inefficiencies introduced by calling it every frame.

diff --git a/src/modules/audio/openal/Audio.h b/src/modules/audio/openal/Audio.h
index c73a0be7..2e63925a 100644
--- a/src/modules/audio/openal/Audio.h
+++ b/src/modules/audio/openal/Audio.h
@@ -130,6 +130,9 @@ class Audio : public love::audio::Audio

    bool getEffectID(const char *name, ALuint &id);

+   // The Pool.
+   Pool *pool;
+
 private:
    void initializeEFX();
    // The OpenAL device.
@@ -152,9 +155,6 @@ class Audio : public love::audio::Audio
    int MAX_SCENE_EFFECTS = 64;
    int MAX_SOURCE_EFFECTS = 64;

-   // The Pool.
-   Pool *pool;
-
    class PoolThread: public thread::Threadable
    {
    protected:
diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp
index a3f9ae39..af426001 100644
--- a/src/modules/event/sdl/Event.cpp
+++ b/src/modules/event/sdl/Event.cpp
@@ -30,6 +30,7 @@
 #include "window/Window.h"
 #include "common/Exception.h"
 #include "audio/Audio.h"
+#include "audio/openal/Audio.h"
 #include "common/config.h"
 #include "timer/Timer.h"

@@ -129,6 +130,8 @@ void Event::pump()
            msg->release();
        }
    }
+
+   Module::getInstance<love::audio::openal::Audio>(Module::M_AUDIO)->pool->update();
 }

 Message *Event::wait()
Davidobot commented 2 years ago

Oh that's very cool @rameshvarun!!! I'll test on mine and merge soon.

EDIT: I will probably tinker around with this to make this change happen only when the compat mode is being built, to prevent it from adversely affecting the full mode.

Davidobot commented 2 years ago

Fixed in https://github.com/Davidobot/love.js/commit/8f9a7b85da9149b6d4d0af46442ead1e71f76477