Let playback ticks catch up, and let go of decoded audio nothing holds
While busy only one tick ran a pass, so drawing slower than the tick rate played animations in slow motion with a growing backlog. Recording takes its time from the recorder, so every pass now runs the ticks owed (still at most TICK_DELTA_MAX_MS of them). Decoded sounds were kept for the session. Every few seconds, those no open document or library holds are released once they stop playing.
This commit is contained in:
1 parent
e963bc50d3
commit
9e193a3d5c
5 files changed
+33
-6
No files matched your search
@@ -175,6 +175,19 @@ void audio_stop(const AudioData& data)
|
||||
}
|
||||
}
|
||||
|
||||
void audio_prune(const std::set<std::uint64_t>& 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);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "vec.h"
|
||||
#include <cstdint>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#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<std::uint64_t>&);
|
||||
bool is_audio_playing(const AudioData&);
|
||||
Vec2 audio_levels_get(const AudioData&);
|
||||
|
||||
|
||||
+2
-3
@@ -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)
|
||||
|
||||
+10
-3
@@ -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<std::uint64_t> 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();
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user