I’m working on a project where I need my audio to play one after another. So when I call for a new audio snippet, it should interrupt the current one and play this next.
To make things easier to manage I created something like a two-track audio player. master
is the first track, where the music
api is used. And track
is used with sound
api to overdub the current audio while fading-in the next audio snippet. When fading is done, the overdub track becomes the master track.
The issue here however is, that when I swap track
for master
I get a awkward delay of roughly 5 frames. And I can’t tell where it comes from?! I would normally set music.currentTime = track.time
, where track.time is the time of the tween, which again is the same time as the fade-in lasted, but then the two tracks aren’t in sync anymore.
Does anyone have a solution for this? (Or, alternatively, a suggestion on how to make this better?)
local master, track
local function remaster()
if track then
tween.stop(master)
tween.stop(track)
music(track._name, track._loop, track.subject.volume)
music.currentTime = track.time + 5*DeltaTime -- unknown delay, maybe due to pre-loading?
track.subject:stop()
master, track = track, nil
end
end
function sfx(conf)
assert(type(conf.name) == "string", "sound missing")
conf.blend = conf.blend or .01
conf.effect = conf.effect or tween.easing.linear
remaster()
master = tween(conf.blend, music, {volume = 0}, conf.effect, remaster)
track = tween(conf.blend, sound(conf.name, 0), {volume = conf.volume or 1}, conf.effect)
track._name = conf.name
track._loop = not not conf.loop
end