From 2ce8795b4a06c068b6a356b66f91ea4d2409b8f7 Mon Sep 17 00:00:00 2001 From: shweet Date: Tue, 6 Oct 2026 22:26:00 -0400 Subject: [PATCH] Trigger sounds: overlap instead of cutting off, and play once per frame - A sound can play over itself: each play uses a track that isn't busy (reusing finished ones), so close triggers no longer restart each other. Rendered audio mixes them the same way. - The preview plays each frame's trigger sound once as playback reaches the frame, instead of on every tick spent in it, and no longer skips the sound of a frame passed over between ticks. Co-Authored-By: Claude Opus 5.5 --- src/imgui/window/animation_preview.cpp | 17 ++++++--- src/imgui/window/animation_preview.hpp | 1 + src/resource/audio.cpp | 51 +++++++++++++++----------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index 44b2da0..8d468a7 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -177,17 +177,22 @@ namespace anm2ed::imgui } else { - if (!manager.isRecording && !document.sounds.empty() && isSound) - { - if (auto soundID = trigger_sound_id_get(document, animation, playback.time); soundID != -1) - if (auto sound = document.sound_get(soundID)) audio::play(*sound, false, mixer); - } - auto fps = std::max(model.info.fps, 1); auto range = model::animation_play_range_get(*animation); + if (!wasPlaybackPlaying) soundFrame = (int)std::floor(playback.time) - 1; playback.tick(fps, range.x, range.y, (animation->isLoop || settings.playbackIsLoop) && !manager.isRecording, deltaSeconds); + // Each frame playback reaches plays its trigger's sound once, however many ticks it lasts or skips. + auto frame = (int)std::floor(playback.time); + for (int step = 0; soundFrame != frame && step < range.y - range.x; ++step) + { + soundFrame = soundFrame + 1 >= range.y || soundFrame + 1 < range.x ? range.x : soundFrame + 1; + if (manager.isRecording || document.sounds.empty() || !isSound) continue; + if (auto soundID = trigger_sound_id_get(document, animation, (float)soundFrame); soundID != -1) + if (auto sound = document.sound_get(soundID)) audio::play(*sound, false, mixer); + } + frameTime = playback.time; } } diff --git a/src/imgui/window/animation_preview.hpp b/src/imgui/window/animation_preview.hpp index 5355323..39380c1 100644 --- a/src/imgui/window/animation_preview.hpp +++ b/src/imgui/window/animation_preview.hpp @@ -18,6 +18,7 @@ namespace anm2ed::imgui MIX_Mixer* mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, nullptr); AudioStream audioStream = AudioStream(mixer); bool wasPlaybackPlaying{}; + int soundFrame{-1}; bool isMoveDragging{}; glm::vec2 moveOffset{}; glm::vec2 nullRectScaleAnchor{}; diff --git a/src/resource/audio.cpp b/src/resource/audio.cpp index 46896d9..dee7aa8 100644 --- a/src/resource/audio.cpp +++ b/src/resource/audio.cpp @@ -4,13 +4,14 @@ #include #include #include +#include namespace anm2ed::resource::audio { struct Entry { MIX_Audio* audio{}; - MIX_Track* track{}; + std::vector tracks{}; // one per overlapping play, reused once finished std::atomic squares[2]{}; std::atomic sampleCount{}; }; @@ -47,50 +48,56 @@ namespace anm2ed::resource::audio return it->second.audio ? &it->second : nullptr; } - // The playing track, when it belongs to mixer (or any mixer, when none is given). - MIX_Track* track_get(const AudioData& data, MIX_Mixer* mixer) - { - auto it = entries.find(data.uid); - if (it == entries.end() || !it->second.track) return nullptr; - return !mixer || MIX_GetTrackMixer(it->second.track) == mixer ? it->second.track : nullptr; - } + bool is_on_mixer(MIX_Track* track, MIX_Mixer* mixer) { return !mixer || MIX_GetTrackMixer(track) == mixer; } + // A sound can play over itself: while a track is still playing it, another track takes the new play. void play(const AudioData& data, bool isLoop, MIX_Mixer* mixer) { auto entry = entry_get(data); auto targetMixer = mixer ? mixer : mixer_get(); if (!entry || !targetMixer) return; - if (entry->track && MIX_GetTrackMixer(entry->track) != targetMixer) track_detach(data); - if (!entry->track) entry->track = MIX_CreateTrack(targetMixer); - if (!entry->track) return; - MIX_SetTrackCookedCallback(entry->track, levels_update, entry); - - MIX_SetTrackAudio(entry->track, entry->audio); + auto free = std::ranges::find_if(entry->tracks, [&](MIX_Track* track) + { return is_on_mixer(track, targetMixer) && !MIX_TrackPlaying(track); }); + auto track = free != entry->tracks.end() ? *free : MIX_CreateTrack(targetMixer); + if (!track) return; + if (free == entry->tracks.end()) + { + entry->tracks.push_back(track); + MIX_SetTrackCookedCallback(track, levels_update, entry); + MIX_SetTrackAudio(track, entry->audio); + } SDL_PropertiesID options = isLoop ? SDL_CreateProperties() : 0; if (options) SDL_SetNumberProperty(options, MIX_PROP_PLAY_LOOPS_NUMBER, -1); - MIX_PlayTrack(entry->track, options); + MIX_PlayTrack(track, options); if (options) SDL_DestroyProperties(options); } void stop(const AudioData& data, MIX_Mixer* mixer) { - if (auto track = track_get(data, mixer)) MIX_StopTrack(track, 0); + if (auto it = entries.find(data.uid); it != entries.end()) + for (auto track : it->second.tracks) + if (is_on_mixer(track, mixer)) MIX_StopTrack(track, 0); } void track_detach(const AudioData& data, MIX_Mixer* mixer) { - if (!track_get(data, mixer)) return; - auto& track = entries[data.uid].track; - MIX_DestroyTrack(track); - track = nullptr; + auto it = entries.find(data.uid); + if (it == entries.end()) return; + std::erase_if(it->second.tracks, + [&](MIX_Track* track) + { + if (!is_on_mixer(track, mixer)) return false; + MIX_DestroyTrack(track); + return true; + }); } bool is_playing(const AudioData& data) { - auto track = track_get(data, nullptr); - return track && MIX_TrackPlaying(track); + auto it = entries.find(data.uid); + return it != entries.end() && std::ranges::any_of(it->second.tracks, MIX_TrackPlaying); } // The left/right loudness (RMS, 0-1) of what the track played since the last call.