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 <noreply@anthropic.com>
This commit is contained in:
1 parent
ef5796d667
commit
2ce8795b4a
3 files changed
+41
-28
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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{};
|
||||
|
||||
+29
-22
@@ -4,13 +4,14 @@
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace anm2ed::resource::audio
|
||||
{
|
||||
struct Entry
|
||||
{
|
||||
MIX_Audio* audio{};
|
||||
MIX_Track* track{};
|
||||
std::vector<MIX_Track*> tracks{}; // one per overlapping play, reused once finished
|
||||
std::atomic<float> squares[2]{};
|
||||
std::atomic<int> 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.
|
||||
|
||||
Reference in new issue
Block a user