s0=0e3,
s1=402424-2e3
means. They seem to be track lengths? I think they are milliseconds?
How can I setup the music coding to stop this error?
Code: Select all
Music = Music or setmetatable( {},{__index=_ENV} )
local _ENV = Music
local dir = current_lua_dir();
local console,format in _ENV
local modf in math
--
-- so, after some messing around, i've learned that mp3s, (or
-- oggs, for that matter), cause trouble for track transitions.
-- for starters, the listed track length for a compressed file
-- is often a bit longer than the source -- adding an
-- additional; .1-.3 seconds. meanwhile, playing a compressed
-- track seems to cause the locations of downbeats to "drift" a
-- bit -- again, it seems to be another .1-.3 second kind of
-- error. it's not something i've figured out how to
-- compensate for -- or even precisely measure. however, i can
-- certainly hear it. if i try to crossfade an mp3 track with
-- itself percussion that should be in sync may be ahead or
-- behind by a quarter beat or so -- which makes seamless
-- looping pretty much impossible.
--
-- after some messing around, i've discovered that ogg files,
-- if they're loaded as streams (rather than samples), don't
-- seem to suffer the same sort of 'beat drift' issues as mp3s.
-- of course, steaming audio brings its own complications --
-- most notably, the fact that only one copy of a given stream
-- can be playing at any time. that condition makes
-- implementing a seamless crossfade between two streams
-- complicated -- one would need to load two distinct sound
-- objects to make it work, and that in turn complicates sound
-- resource management.
--
-- however, there's a simpler way of implementing seamless
-- looping with streaming audio -- and that's to find loop
-- start and end time for your files that imply seamless
-- playback even when the transition is a hard-cut. finding
-- such loop times (using audacity) isn't really that hard --
-- though in the case of some tracks, i've edited the original
-- compositions slightly to allow for better loop points.
--
--
local clock in os
local track_length = {
easy_math2 = 239973,
--rescue_mission = 164363,
active_network_l = 143185,
mind_in_motion = 127999,
under_the_dome_l = 213865,
new_media2 = 251591,
--cyber_consipiracies = 210000,
--lunar_expedition2 = 353678,
--battle_of_eml_outpost = 166186,
sis_warp_lanes = 290642,
sis_galaxy = 257684,
sis_battle = 280000,
sis_battle_redux = 402424,
sis_the_deep = 342857,
sis_the_wonder = 316521,
sis_legacies = 431515,
}
local track_loops = {}
do
-- i'm cutting a small delta off the end of the track
-- in an attempt to avoid null-reference issues inside the
-- cross-fading logic
track_loops.sis_the_deep = {
s0=0e3,
s1=338857,
}
track_loops.the_ancient_city = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy0 = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy1 = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy2 = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy3 = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy4 = {
s0=0e3,
s1=338857,
}
track_loops.Ascendancy5 = {
s0=0e3,
s1=338857,
}
track_loops.Imperium_Galactica_OST_Track_2 = {
s0=0e3,
s1=338857,
}
track_loops.Imperium_Galactica_OST_Track_3 = {
s0=0e3,
s1=338857,
}
track_loops.sis_the_wonder = {
s0=0e3,
s1=316521-2e3,
}
track_loops.sis_legacies = {
s0=0e3,
s1=track_length.sis_legacies-2e3,
}
track_loops.sis_galaxy = {
s0=0e3,
s1=252294,
}
track_loops.sis_battle_redux = {
s0=0e3,
s1=402424-2e3,
}
track_loops.sis_battle = {
s0=0e3,
s1=272508,
}
track_loops.sis_warp_lanes = {
s0=0e3,
s1=track_length.sis_warp_lanes-2e3,
}
end
do
local s0 = 0e3
local s1 = (3*60+6)*1e3
s0,s1+=3e3
track_loops.cyber_consipiracies = {..s0,..s1}
end
do
local s0 = (2*60+52.067075)*1e3
local s1 = 2*s0
s0,s1-=3e3
track_loops.lunar_expedition2 = {..s0,..s1}
end
do
local s0 = (60+56.110843)*1e3
local s1 = 2*s0
s0,s1-=3e3
track_loops.easy_math2 = {..s0,..s1}
end
do
local s1 = (2*60+30.762666)*1e3
local s0=(.762688-.762645)*1e3
track_loops.battle_of_eml_outpost = {..s0,..s1}
end
do
local s0 = 0e3
local s1 = 123.874987e3
s0,s1+=3e3
track_loops.new_media2 = {..s0,..s1}
end
do
local s1=(3*60+10.8223336)*1e3
local s0= s1-(60+55.208138)*1e3
s1,s0+=.3e3
track_loops.under_the_dome = {..s0,..s1}
end
do
local s1=(3*60+26.100799)*1e3
local s0= s1-(3*60+22.871699)*1e3
track_loops.under_the_dome_l = {..s0,..s1}
end
do
local s1=(2*60+6.591208)*1e3
local s0=s1-(60+9.817618)*1e3
s1,s0+=.3e3
track_loops.rescue_mission = {..s0,..s1}
end
do
local s1=(120+1.010794)*1e3
local s0=s1-(60+15)*1e3
track_loops.mind_in_motion = {..s0,..s1}
--console(1e-3*(s0))
end
do
local s1=(2*60+16.82061)*1e3
local s0=s1-(2*60+10.537285)*1e3
track_loops.active_network_l = {..s0,..s1}
end
local function get_length()
local table = {}
fs_walk(dir:sub(1,-2),function(fname)
if fname:sub(-4)=='.ogg'
local len = loadsound('test', dir..fname, 'accurate_time' )
table[fname:sub(1,-5)]=len
--console(..fname,..len)
end
end)
make_table_file(dir..'length.table','len',table)
end
--get_length()
local function load_stream(name, group, loop_count)
-- if loop_count>=0
-- loop_count=0
-- end
local fname=dir..name..'.ogg'
loop_count=loop_count or -1
make_child_group('music',group)
console(..fname,..name)
loadsound(name, dlc_or_main(fname), 'stream' )
local s0,s1 in track_loops![name]
local channel = playsound(name, group )
channel_set_loop(channel, loop_count, s0, s1 )
channel_set_position(channel,0)
local t0=1e3*clock()
local rval={ ..channel, ..t0, ..name}
if loop_count >= 0
local length = track_length![name]
local start_next=0
length += loop_count*(s1-s0)
local fade_in,fade_out=0,0
insert(rval,..length,..fade_out,..start_next,..fade_in)
else
rval.endless=true
end
return rval
end
local battle_themes = { 'battle1', 'battle2', 'battle3' }
function get_battle_theme(attack)
local roll=math.random(#battle_themes)
-- console('rolling',#battle_themes,roll, battle_themes[roll])
return battle_themes[roll]
end
local max_volume = setmetatable(
{
battle_of_eml_outpost=1, cyber_consipiracies=1, rescue_mission=.8,
mind_in_motion=.8, easy_math2=.5, active_network_l=.5,new_media2=.5,
},
{__index=catch(.7) }
)
local theme2tracks = {
--[[
battle = {
cyber_consipiracies = { min_repeat = 0, max_repeat=2 },
rescue_mission = { min_repeat = 0, max_repeat=2 },
battle_of_eml_outpost = { first=true, min_repeat = 0, max_repeat=2 },
},
--]]
battle1 = { sis_battle={} },
battle2 = { sis_battle={} },
battle3 = { sis_battle={} },
--[[
battle1 = { sis_battle={} },
battle2 = { sis_battle={} },
battle3 = { sis_battle={} },
--]]
main = {
-- lunar_expedition2 = { min_repeat = 0, max_repeat=4 },
-- active_network_l= { min_repeat=0, max_repeat=2 },
-- under_the_dome_l= { min_repeat=1, max_repeat=3 },
-- lunar_expedition2 = { min_repeat = 0, max_repeat=1 },
active_network_l= { min_repeat=0, max_repeat=1 },
under_the_dome_l= { min_repeat=0, max_repeat=1 },
sis_the_deep = { min_repeat=0, max_repeat=0, },
sis_the_wonder = { min_repeat=0, max_repeat=0, },
sis_battle_redux = {min_repeat=0, max_repeat=0, },
sis_galaxy = { min_repeat=0, max_repeat=0, },
sis_warp_lanes = {first = true, min_repeat=0, max_repeat=0, },
the_ancient_city= { min_repeat=0, max_repeat=1 },
Ascendancy0 = { min_repeat=0, max_repeat=1 },
Ascendancy1 = { min_repeat=0, max_repeat=0, },
Ascendancy2 = { min_repeat=0, max_repeat=0, },
Ascendancy3 = {min_repeat=0, max_repeat=0, },
Ascendancy4 = { min_repeat=0, max_repeat=0, },
Ascendancy5 = {first = true, min_repeat=0, max_repeat=0, },
Imperium_Galactica_OST_Track_2 = {first = true, min_repeat=0, max_repeat=0, },
Imperium_Galactica_OST_Track_3 = {first = true, min_repeat=0, max_repeat=0, },
},
design = {
mind_in_motion={},
},
diplo = {
new_media2={},
},
research = {
easy_math2={},
},
}
if DLC1
theme2tracks.main.sis_legacies = { first=true, min_repeat=0, max_repeat=0, }
theme2tracks.main.sis_warp_lanes.first=nil
end
local standard_delay = { start_next=6e3, fade_in = 3e3 }
local next_delay = {
active_network_l = {
under_the_dome_l = { start_next=6e3, fade_out=5e3, clip_length=0e3, fade_in=3e3 },
},
rescue_mission = {
clip_length=5e3,
fade_out=6e3,
},
sis_warp_lanes = {fade_in=100,fade_out=100,start_next=6e3},
sis_legacies = {fade_in=100,fade_out=100,start_next=6e3},
sis_the_wonder = {fade_in=100,fade_out=100,start_next=6e3},
sis_the_deep = {fade_in=100,fade_out=100,start_next=6e3},
sis_battle_redux = {fade_in=100,fade_out=100,start_next=6e3},
the_ancient_city = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy0 = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy1 = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy2 = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy3 = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy4 = {fade_in=100,fade_out=100,start_next=6e3},
Ascendancy5 = {fade_in=100,fade_out=100,start_next=6e3},
Imperium_Galactica_OST_Track_2 = {fade_in=100,fade_out=100,start_next=6e3},
Imperium_Galactica_OST_Track_3 = {fade_in=100,fade_out=100,start_next=6e3},
sis_galaxy = {fade_in=100,fade_out=100,start_next=5390-800},
}
local delay_proxy = function (name0, name1)
local t1 = next_delay[name0]?[name1]?
local t0 = next_delay[name0]?
return newproxy | function(k)
return t1[k] or t0[k] or standard_delay[k]
end
end
local function jump_to_transition(stream)
local channel, start_next, name, clip_length,fade_out, endless in stream
if start_next
local position=channel_get_position(channel)
-- local dt=length-start_next-10e3
-- console(t0,dt,t0-dt)
local length = track_length![name] - (clip_length or 0)
local dt = length-position-max(start_next,fade_out)- 3e3
local s0,s1 in track_loops![name]
channel_set_loop(channel, 0, s0, s1 )
stream.length = length
stream.t0 -= dt
console(position,dt)
channel_set_position(channel,position+dt)
console(dt*1e-3)
elseif endless
local s0,s1 in track_loops![name]
channel_set_position(channel,s1-10e3)
end
end
---[[
local function jump_to_next()
if nkeys(theme2streams?[current_theme]?)==1
local _,stream=next(theme2streams?[current_theme])
jump_to_transition(stream)
end
end
if lua_load_complete
jump_to_next()
end
--]]
local function get_following(theme, prior_name)
local tracks=theme2tracks[theme]
local n =nkeys( tracks )
local i= math.random(n-1)
for k ; tracks
if k~=prior_name
if i==1
return k
else
i-=1
end
end
end
trace_error('new stream rolling logic failed')
end
local function new_stream(theme, name, prior_name )
local min_repeat,max_repeat in theme2tracks[theme][name]!
console('loading a new track for theme', theme, name )
local rval = load_stream(name,theme, math.random(min_repeat,max_repeat) )
local following = get_following(theme,name)
local fade_in in delay_proxy(prior_name, name)
local fade_out, start_next, clip_length in delay_proxy(name,following)
rval.clip_length=clip_length
rval.length-=(clip_length or 0)
rval.fade_in=fade_in or 0
rval.fade_out=fade_out or 0
rval.start_next=start_next or 0
rval.following=following
--console(..t2s | rval)
return rval
end
function unload_music()
console 'unloading music tracks'
stop_channel_group 'music'
for theme, tracks ; theme2tracks
for name ; tracks
freesound(name)
end
end
theme2streams=nil
end
local function start_missing_tracks()
local tracks
end
local function ensure_theme2streams()
if not theme2streams
theme2streams={}
for theme ; theme2tracks
theme2streams[theme]={}
set_group_volume(theme, current_theme==theme and 1 or 0)
end
end
end
current_theme = current_theme or 'main'
function set_theme(theme)
current_theme=theme
end
-- global fade speed for theme transitions
local fade_speed=1.0/(2.5e3)
last_active = last_active or {}
--console(fmod_channel_memory(true))
local jump_start = {
-- skip the start of rescue_mission, if it's playing as a solo
-- endless loop.
rescue_mission = 51e3,
}
local function update_stream_volumes()
ensure_theme2streams()
local t=clock()*1e3
local dt=frame_dt()*1e3
local dv=fade_speed*dt
for theme, tracks ; theme2tracks
local v = get_group_volume(theme)
if theme==current_theme
v = min( 1, v+dv )
else
v = max( 0, v-dv )
end
set_group_volume(theme,v)
local ntracks = nkeys(tracks)
if v>0
last_active[theme]=t
local streams=theme2streams[theme]
if ntracks>1
if not next(streams)
for name, info ; tracks
if info.first
print(name,..info.first)
local new=new_stream(theme,name)
append( new, streams)
break
end
end
end
-- update stream volumes, and remove any that have finished
-- playing
for k,stream ; streams
local t0, name,channel,fade_out,fade_in,start_next, length in stream
local position = t - t0
local remaining = length-position
if start_next and start_next>=remaining
stream.start_next=nil
--console(..remaining,..length, ..start_next)
console('adding a stream')
local new=new_stream(theme, stream.following, name )
append( new, streams)
end
if remaining < 0
if not stream.start_next
streams[k]=nil
console('removing',stream.name)
channel_stop(channel)
else
console('cant remove',stream.start_next)
end
else
local v=1
if fade_in>0 and position < fade_in
v=position/fade_in
elseif fade_out>0 and remaining<fade_out
v = remaining/fade_out
end
channel_volume(channel,v * max_volume[name])
end
end
else
if not next(streams)
local name = next(tracks)
console('loading new stream for',theme,name)
streams[1]=load_stream(name,theme,-1)
channel_volume(streams[1].channel,max_volume[name])
local j = jump_start[name]
if j
channel_set_position(streams[1].channel,j)
end
elseif false
local _, stream = next(streams)
console(channel_get_position(stream.channel))
end
if false
local name = next(tracks)
console(max_volume[name],name)
channel_volume(streams[1].channel,max_volume[name])
end
end
else --v==0
-- wait longer before reseting when dealing with multi-track
-- themes
if last_active[theme] and (t-last_active[theme] > ntracks*10e3 )
console('clearing track state',theme)
local streams=theme2streams[theme]
stop_channel_group(theme)
last_active[theme]=nil
cleartable(streams)
end
end
end
end
local last_on=0
function update()
if get_group_volume 'music' > 0 and not headless_mode()
last_on=clock()
update_stream_volumes()
else
if theme2streams and ( clock()-last_on ) > 3
unload_music()
end
end
end