Hey @maxvat – we’ve discussed adding an API to specify sample-accurate volume-fade points, which I think would make designing loops more straightforward
In the meantime, a trick you can do is to search for similar starting/ending conditions using GetWaveformAsync – the loop will sound smoothest when both the value and slope match at the boundaries.
Here is an example function – passing the current LoopRegion or PlaybackRegion into findNearestTimes returns a new NumberRange that begins/ends at zero-crossings, attempting to match the slope:
local SEARCH_RADIUS = 0.05 -- how many seconds to search around the start/end times
local SAMPLES = 4096 -- waveform resolution per window
local function collectZeroCrossings(player, center, totalLength)
local winStart = math.max(0, center - SEARCH_RADIUS)
local winStop = math.min(totalLength, center + SEARCH_RADIUS)
if winStop <= winStart then
return {}
end
local wave = player:GetWaveformAsync(NumberRange.new(winStart, winStop), SAMPLES)
local n = #wave
if n < 2 then
return {}
end
local dt = (winStop - winStart) / n
local crossings = {}
for k = 1, n - 1 do
local v0, v1 = wave[k], wave[k + 1]
local rising = v0 <= 0 and v1 > 0
if rising or (v0 >= 0 and v1 < 0) then
table.insert(crossings, {
time = winStart + (k - 1 + v0 / (v0 - v1)) * dt,
rising = rising,
})
end
end
return crossings
end
local function nearest(crossings, target, rising)
local best, bestDist = nil, math.huge
for _, c in crossings do
local dist = math.abs(c.time - target)
if (rising == nil or c.rising == rising) and dist < bestDist then
best, bestDist = c, dist
end
end
return best
end
local function findNearestTimes(player : AudioPlayer, timeRange : NumberRange) : NumberRange
local startTime = timeRange.Min
local stopTime = timeRange.Max
while not player.IsReady do
task.wait()
end
local totalLength = player.TimeLength
startTime = math.clamp(startTime, 0, totalLength)
stopTime = math.clamp(stopTime, 0, totalLength)
local startCrossings = collectZeroCrossings(player, startTime, totalLength)
local stopCrossings = collectZeroCrossings(player, stopTime, totalLength)
local chosenStart, chosenStop, bestScore = nil, nil, math.huge
for _, rising in { true, false } do
local s = nearest(startCrossings, startTime, rising)
local e = nearest(stopCrossings, stopTime, rising)
if s and e then
local score = math.abs(s.time - startTime) + math.abs(e.time - stopTime)
if score < bestScore then
chosenStart, chosenStop, bestScore = s, e, score
end
end
end
-- in case no nearby zero crossings have matching scope, just pick the nearest ones
chosenStart = chosenStart or nearest(startCrossings, startTime)
chosenStop = chosenStop or nearest(stopCrossings, stopTime)
local newStart = if chosenStart then chosenStart.time else startTime
local newStop = if chosenStop then chosenStop.time else stopTime
if newStart >= newStop then
newStart, newStop = startTime, stopTime
end
return NumberRange.new(newStart, newStop)
end
depending on how much low-frequency content is in your audio file, you may need to adjust the search radius to accommodate