diff --git a/src/audio.cpp b/src/audio.cpp index 2a37686..f4ce32b 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -175,6 +175,19 @@ void audio_stop(const AudioData& data) } } +void audio_prune(const std::set& uids) +{ + std::erase_if(sounds, [&](auto& entry) { + auto& [uid, sound] = entry; + if (uids.contains(uid) || + std::ranges::any_of(sound.voices, [](auto& voice) { return is_voice_playing(*voice); })) + return false; + for (auto& voice : sound.voices) + SDL_DestroyAudioStream(voice->stream); + return true; + }); +} + bool is_audio_playing(const AudioData& data) { auto it = sounds.find(data.uid); diff --git a/src/audio.h b/src/audio.h index 0852ded..4614d4b 100644 --- a/src/audio.h +++ b/src/audio.h @@ -1,6 +1,8 @@ #pragma once #include "vec.h" +#include +#include #include #include "audio_data.h" @@ -18,6 +20,8 @@ enum AudioPlayMode // Each play is a stream on one shared device, opened on the first play, which mixes them. void audio_play(const AudioData&, AudioPlayMode = AUDIO_ONCE); void audio_stop(const AudioData&); +// Lets go of decoded sounds whose uids aren't in this set, once they stop playing. +void audio_prune(const std::set&); bool is_audio_playing(const AudioData&); Vec2 audio_levels_get(const AudioData&); diff --git a/src/main.cpp b/src/main.cpp index 02bccb2..042b756 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -379,13 +379,12 @@ static void loop_step(App& app, Editor& editor) app.isIdle = is_idle(app, editor); } - // Busy, one tick a pass keeps recording in step with drawing. Idle, the ticks only move timers along. + // Ticks catch up with the time passed, so slow drawing doesn't slow playback. A recording takes its time from the + // recorder as it draws. while (app.tickAccumulatorMs >= tickIntervalMs) { editor.dockspace.tick(editor.manager, app.settings, (float)(tickIntervalMs / 1000.0)); app.tickAccumulatorMs -= tickIntervalMs; - if (!app.isIdle) - break; } if (!app.isIdle) diff --git a/src/ui/dockspace.cpp b/src/ui/dockspace.cpp index 35103b9..c18e13c 100644 --- a/src/ui/dockspace.cpp +++ b/src/ui/dockspace.cpp @@ -12,13 +12,20 @@ void Dockspace::tick(Manager& manager, Settings& settings, float deltaSeconds) if (settings.windowIsAnimationPreview) animationPreview.tick(manager, settings, deltaSeconds); - // Spritesheets and libraries changed on disk are picked up every few seconds. - if (!settings.fileIsAutoReloadResources) - return; + // Every few seconds, decoded audio no document holds is let go, and spritesheets and libraries changed on disk are + // picked up. reloadTime += deltaSeconds; if (reloadTime < AUTO_RELOAD_INTERVAL) return; reloadTime = 0.0f; + std::set soundUids; + for (auto& document : manager.documents) + for (auto sound : document.sounds_get()) + soundUids.insert(sound->uid); + audio_prune(soundUids); + + if (!settings.fileIsAutoReloadResources) + return; for (auto& document : manager.documents) { document.libraries_reload(); diff --git a/tests/render/render.cpp b/tests/render/render.cpp index ceba941..1bcfe01 100644 --- a/tests/render/render.cpp +++ b/tests/render/render.cpp @@ -389,6 +389,10 @@ TEST_CASE("audio decodes and mixes") REQUIRE(mix.size() > start + 2); CHECK(mix[start - 2] == 0.0f); CHECK(mix[start + 2] == doctest::Approx(0.5f).epsilon(0.05)); + + // A sound let go of decodes again when it's next needed. + audio_prune({}); + CHECK(audio_mix({{}, {&sound}}, 30) == mix); } // A second editor hands its paths to the first, which has them queued once it answers, and wakes its event loop.