Skip to content

Commit

Permalink
fmod启动失败时不会再检查bank内容了
Browse files Browse the repository at this point in the history
优化fmod初始化流程
  • Loading branch information
MrZ626 committed Jun 25, 2024
1 parent 62880d4 commit 9f659e8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
3 changes: 3 additions & 0 deletions assets/fmod20221/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require'cdef'

---@class FMOD.Master
local M=require'master'
M.banks={}

-- (Old method) search for fmod shared libraries in package.cpath
-- local fmodPath=package.searchpath('fmod',package.cpath)
Expand Down Expand Up @@ -67,6 +68,7 @@ function M.loadBank(path,flag)
if not studio then return end
local bank,res=studio:loadBankFile(path,flag or M.FMOD_STUDIO_LOAD_BANK_NORMAL)
if res~=M.FMOD_OK then return nil,M.errorString[res] end
M.banks[path]=bank
return bank
end

Expand All @@ -82,6 +84,7 @@ function M.loadBank2(path,flag)
local bank,res=studio:loadBankMemory(data:getPointer(),size,0,flag or M.FMOD_STUDIO_LOAD_BANK_NORMAL)
file:close(); file:release(); data:release()
assert(res==M.FMOD_OK,M.errorString[res])
M.banks[path]=bank
return bank
end

Expand Down
1 change: 1 addition & 0 deletions assets/fmod20221/wrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3683,6 +3683,7 @@ end

---@return table<number, FMOD.Studio.EventDescription>,number,FMOD.Result
function studio.Bank:getEventList(i1)
if not i1 then i1=self:getEventCount() end
local o1=ffi.new("FMOD_STUDIO_EVENTDESCRIPTION*[?]", i1)
local o2=ffi.new("int[1]")
local result=C2.FMOD_Studio_Bank_GetEventList(self,o1,i1,o2)
Expand Down
99 changes: 56 additions & 43 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,41 +346,47 @@ LANG.add{
LANG.setDefault('en')

function FMODLoadFunc() -- Will be called again when applying advanced options
local bankPath='soundbank/'
if FMOD.C then
FMOD.init{
maxChannel=math.min(SETTINGS.system.fmod_maxChannel,256),
DSPBufferCount=math.min(SETTINGS.system.fmod_DSPBufferCount,16),
DSPBufferLength=math.min(SETTINGS.system.fmod_DSPBufferLength,65536),
studioFlag=bit.bxor(FMOD.FMOD_STUDIO_INIT_SYNCHRONOUS_UPDATE,FMOD.FMOD_INIT_STREAM_FROM_UPDATE,FMOD.FMOD_INIT_MIX_FROM_UPDATE),
coreFlag=FMOD.FMOD_INIT_NORMAL,
}
if not (FMOD.C and FMOD.C2) then
MSG.new('error',"FMOD Studio initialization failed")
return
end
if not FMOD.loadBank2(bankPath..'Master.strings.bank') then

FMOD.init{
maxChannel=math.min(SETTINGS.system.fmod_maxChannel,256),
DSPBufferCount=math.min(SETTINGS.system.fmod_DSPBufferCount,16),
DSPBufferLength=math.min(SETTINGS.system.fmod_DSPBufferLength,65536),
studioFlag=bit.bxor(FMOD.FMOD_STUDIO_INIT_SYNCHRONOUS_UPDATE,FMOD.FMOD_INIT_STREAM_FROM_UPDATE,FMOD.FMOD_INIT_MIX_FROM_UPDATE),
coreFlag=FMOD.FMOD_INIT_NORMAL,
}
if not FMOD.loadBank2('soundbank/Master.strings.bank') then
MSG.new('warn',"Strings bank file load failed")
end
if not FMOD.loadBank2(bankPath..'Master.bank') then
if not FMOD.loadBank2('soundbank/Master.bank') then
MSG.new('warn',"Master bank file load failed")
end
FMOD.registerMusic((function()
if not love.filesystem.getInfo(bankPath..'Master.bank') then
if not love.filesystem.getInfo('soundbank/Master.bank') then
MSG.new('warn',"Music bank not found")
return {}
end
local L={}
for _,bankName in next,{'Music_Beepbox','Music_FL','Music_Community','Music_Extra'} do
local bankMusic=FMOD.loadBank2(bankPath..bankName..'.bank')
if not bankMusic then
MSG.new('warn',"bank "..bankName.." load failed")
if not love.filesystem.getInfo('soundbank/'..bankName..'.bank') then
MSG.new('warn',bankName.." bank file not found")
else
local l,c=bankMusic:getEventList(bankMusic:getEventCount())
for i=1,c do
local path=l[i-1]:getPath()
if path then
local name=path:match('/([^/]+)$'):lower()
L[name]=path
if not SONGBOOK[name] then SONGBOOK(name) end
-- print(name,path)
local bankMusic=FMOD.loadBank2('soundbank/'..bankName..'.bank')
if not bankMusic then
MSG.new('warn',"bank "..bankName.." load failed")
else
local l,c=bankMusic:getEventList()
for i=1,c do
local path=l[i-1]:getPath()
if path then
local name=path:match('/([^/]+)$'):lower()
L[name]=path
if not SONGBOOK[name] then SONGBOOK(name) end
-- print(name,path)
end
end
end
end
Expand All @@ -391,17 +397,17 @@ function FMODLoadFunc() -- Will be called again when applying advanced options
return L
end)())
FMOD.registerEffect((function()
if not love.filesystem.getInfo(bankPath..'Effect.bank') then
if not love.filesystem.getInfo('soundbank/Effect.bank') then
MSG.new('warn',"Effect bank not found")
return {}
end
local bankEffect=FMOD.loadBank2(bankPath..'Effect.bank')
local bankEffect=FMOD.loadBank2('soundbank/Effect.bank')
if not bankEffect then
MSG.new('warn',"Effect bank file load failed")
return {}
end
local L={}
local l,c=bankEffect:getEventList(bankEffect:getEventCount())
local l,c=bankEffect:getEventList()
for i=1,c do
local path=l[i-1]:getPath()
if path then
Expand All @@ -416,23 +422,6 @@ function FMODLoadFunc() -- Will be called again when applying advanced options
return L
end)())
end
TASK.new(function() -- Don't initialize studio at first frame, may cause some weird problem
DEBUG.yieldT(0.26)
FMODLoadFunc()
FMOD.setMainVolume(SETTINGS.system.mainVol,true)
for name,data in next,SONGBOOK do
if FMOD.music.getDesc(name) then
data.intensity=FMOD.music.getParamDesc(name,'intensity')~=nil
data.section=FMOD.music.getParamDesc(name,'section')~=nil
if not FMOD.music.getParamDesc(name,'fade') then
MSG.new('warn',"Missing 'fade' parameter in music '"..name.."'")
end
else
data.notFound=true
MSG.new('warn',"Music '"..name.."' not found in FMOD")
end
end
end)
-- Hijack the original SFX module, use FMOD instead
SFX[('play')]=function(name,vol,pos,tune)
FMOD.effect(name,{
Expand Down Expand Up @@ -564,18 +553,21 @@ for k,v in next,{
},
slowPixelize={{'tileSize',0.01}},
} do for i=1,#v do SHADER[k]:send(unpack(v[i])) end end

for _,v in next,love.filesystem.getDirectoryItems('assets/background') do
if FILE.isSafe('assets/background/'..v) and v:sub(-3)=='lua' then
local name=v:sub(1,-5)
BG.add(name,require('assets/background/'..name))
end
end

for _,v in next,love.filesystem.getDirectoryItems('assets/scene') do
if FILE.isSafe('assets/scene/'..v) then
local sceneName=v:sub(1,-5)
SCN.add(sceneName,require('assets/scene/'..sceneName))
end
end

for _,v in next,{
'brik_template', -- Shouldn't be used
'brik_plastic',
Expand All @@ -592,6 +584,7 @@ for _,v in next,{
SKIN.add(v,require('assets/skin/'..v))
end
end

SCN.addSwapStyle('fadeHeader',{
duration=.5,
timeChange=.25,
Expand Down Expand Up @@ -620,6 +613,26 @@ SCN.addSwapStyle('fastFadeHeader',{
GC.rectangle('fill',0,h+1,SCR.w,SCR.h-h)
end,
})

FMODLoadFunc()
if tostring(FMOD.studio):find('NULL') or TABLE.getSize(FMOD.banks)==0 then
MSG.new('error',"FMOD Studio initialization failed")
else
FMOD.setMainVolume(SETTINGS.system.mainVol,true)
for name,data in next,SONGBOOK do
if FMOD.music.getDesc(name) then
data.intensity=FMOD.music.getParamDesc(name,'intensity')~=nil
data.section=FMOD.music.getParamDesc(name,'section')~=nil
if not FMOD.music.getParamDesc(name,'fade') then
MSG.new('warn',"Missing 'fade' parameter in music '"..name.."'")
end
else
data.notFound=true
MSG.new('warn',"Music '"..name.."' not found in FMOD")
end
end
end

DEBUG.checkLoadTime("Load shaders/BGs/SCNs/skins")

--------------------------------------------------------------
Expand Down

0 comments on commit 9f659e8

Please sign in to comment.