diff --git a/assets/fmod20221/init.lua b/assets/fmod20221/init.lua index b6c30290..84f6bc16 100644 --- a/assets/fmod20221/init.lua +++ b/assets/fmod20221/init.lua @@ -63,6 +63,7 @@ end ---@type table local musicLib={} function M.registerMusic(map) + if not studio then return end for k,v in next,map do local desc,res=studio:getEvent(v) assert(res==M.FMOD_OK,M.errorString[res]) @@ -73,6 +74,7 @@ end ---@type table local effectLib={} function M.registerEffect(map) + if not studio then return end for k,v in next,map do local desc,res=studio:getEvent(v) assert(res==M.FMOD_OK,M.errorString[res]) @@ -85,17 +87,18 @@ function M.registerVocal(map) end -- Volume things need three parameters in your fmod project (mainVolume not included) -M.mainVolume=1 -M.musicVolume=1 -M.effectVolume=1 -M.vocalVolume=1 +M.mainVolume=-1 +M.musicVolume=-1 +M.effectVolume=-1 +M.vocalVolume=-1 ---@param v number ---@param instant? boolean only `true` take effect function M.setMainVolume(v,instant) M.mainVolume=v - studio:setParameterByName("MusicVolume",M.mainVolume*M.musicVolume,instant==true) - studio:setParameterByName("EffectVolume",M.mainVolume*M.effectVolume,instant==true) - studio:setParameterByName("VocalVolume",M.mainVolume*M.vocalVolume,instant==true) + if not studio then return end + studio:setParameterByName('MusicVolume',M.mainVolume*M.musicVolume,instant==true) + studio:setParameterByName('EffectVolume',M.mainVolume*M.effectVolume,instant==true) + studio:setParameterByName('VocalVolume',M.mainVolume*M.vocalVolume,instant==true) end -------------------------- @@ -107,7 +110,8 @@ M.music={} ---@param instant? boolean only `true` take effect function M.music.setVolume(v,instant) M.musicVolume=v - studio:setParameterByName("MusicVolume",M.mainVolume*M.musicVolume,instant==true) + if not studio then return end + studio:setParameterByName('MusicVolume',M.mainVolume*M.musicVolume,instant==true) end ---@type {desc:FMOD.Studio.EventDescription?, event:FMOD.Studio.EventInstance?}? @@ -134,8 +138,8 @@ function M.music.play(name,args) } if not (type(args)=='table' and args.instant==true) then - event:setParameterByName("fade",0,true) - event:setParameterByName("fade",1,false) + event:setParameterByName('fade',0,true) + event:setParameterByName('fade',1,false) end if args then @@ -169,10 +173,10 @@ function M.music.stop(instant) e:stop(M.FMOD_STUDIO_STOP_IMMEDIATE) else TASK.new(function() - e:setParameterByName("fade",0,true) + e:setParameterByName('fade',0,true) repeat coroutine.yield() - until e:getParameterByName("fade")==0 + until e:getParameterByName('fade')==0 e:stop(M.FMOD_STUDIO_STOP_IMMEDIATE) end) end @@ -212,7 +216,7 @@ function M.music.getPlaying() end local playMusic=M.music.play -setmetatable(M.music,{__call=function(_,...) playMusic(...) end}) +setmetatable(M.music,{__call=function(_,...) return playMusic(...) end}) -------------------------- @@ -223,14 +227,15 @@ M.effect={} ---@param instant? boolean only `true` take effect function M.effect.setVolume(v,instant) M.effectVolume=v - studio:setParameterByName("EffectVolume",M.mainVolume*M.effectVolume,instant==true) + if not studio then return end + studio:setParameterByName('EffectVolume',M.mainVolume*M.effectVolume,instant==true) end ---priority: pitch>tune>fine --- ---pos:{x,y,z} --- ----param:{"paramName", 0, true?} +---param:{'paramName', 0, true?} ---@param name string ---@param args? {volume?:number, pitch?:number, tune?:number, fine?:number, pos?:table, param?:table} ---@return FMOD.Studio.EventInstance? @@ -315,7 +320,7 @@ function M.effect.stop(name,instant) end local playEffect=M.effect.play -setmetatable(M.effect,{__call=function(_,...) playEffect(...) end}) +setmetatable(M.effect,{__call=function(_,...) return playEffect(...) end}) -------------------------- @@ -325,7 +330,8 @@ M.vocal={} ---@param instant? boolean only `true` take effect function M.vocal.setVolume(v,instant) M.vocalVolume=v - studio:setParameterByName("VocalVolume",M.mainVolume*M.vocalVolume,instant==true) + if not studio then return end + studio:setParameterByName('VocalVolume',M.mainVolume*M.vocalVolume,instant==true) end -------------------------------------------------------------- diff --git a/assets/gamefunc.lua b/assets/gamefunc.lua index d21599f3..db9350fc 100644 --- a/assets/gamefunc.lua +++ b/assets/gamefunc.lua @@ -15,7 +15,7 @@ function playBgm(name,full,noProgress) if full then FMOD.music(name) else - FMOD.music(name,{param={"intensity",0,true}}) + FMOD.music(name,{param={'intensity',0,true}}) end _bgmPlaying=name end diff --git a/assets/progress.lua b/assets/progress.lua index f21685a3..cb9d8c43 100644 --- a/assets/progress.lua +++ b/assets/progress.lua @@ -364,9 +364,9 @@ function PROGRESS.setMain(n) end end function PROGRESS.setBgmUnlocked(name,state) - local l=math.max(prgs.bgmUnlocked[name] or 0,state) - if l>(prgs.bgmUnlocked[name] or 0) then - prgs.bgmUnlocked[name]=l + local newState=math.max(prgs.bgmUnlocked[name] or 0,state) + if newState>(prgs.bgmUnlocked[name] or 0) then + prgs.bgmUnlocked[name]=newState PROGRESS.save() end end diff --git a/main.lua b/main.lua index ca626d54..a69fb7b0 100644 --- a/main.lua +++ b/main.lua @@ -61,7 +61,7 @@ bgmList=require'assets.bgmlist' FMOD=require("assets.fmod20221") DEBUG.checkLoadTime("Load game modules") -------------------------------------------------------------- --- Config Zenitha +-- Config Zenitha and Fmod Zenitha.setAppName('Techmino') Zenitha.setVersionText(VERSION.appVer) Zenitha.setFirstScene('hello') @@ -85,15 +85,15 @@ Zenitha.setDebugInfo{ {"Mouse", function() local x,y=SCR.xOy:inverseTransformPoint(love.mouse.getPosition()) return math.floor(x+.5)..' '..math.floor(y+.5) end}, -- {"FMOD", function() local a,b,c=FMOD.studio:getMemoryUsage() return a..","..b..","..c end}, -- Only available in logging builds Fmod } - Zenitha.setOnFocus(function(f) - if SETTINGS.system.autoMute then - if f then +Zenitha.setOnFocus(function(f) + if SETTINGS.system.autoMute then + if f then FMOD.setMainVolume(SETTINGS.system.mainVol) - elseif SCN.cur~='musicroom' then + elseif SCN.cur~='musicroom' then FMOD.setMainVolume(0) - end end - end) + end +end) FONT.setDefaultFallback('symbols') FONT.setDefaultFont('norm') @@ -207,14 +207,13 @@ LANG.add{ zh='assets/language/lang_zh.lua', } LANG.setDefault('en') -DEBUG.checkLoadTime("Load Zenitha resources") --------------------------------------------------------------- -function FMODLoadFunc() -- will be called again for restarting when applying advanced options + +function FMODLoadFunc() -- Will be called again when applying advanced options 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=FMOD.FMOD_STUDIO_INIT_SYNCHRONOUS_UPDATE, + 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.loadBank(love.filesystem.getSaveDirectory().."/soundbank/Master.strings.bank") then @@ -263,15 +262,20 @@ function FMODLoadFunc() -- will be called again for restarting when applying adv return L end)()) end -FMODLoadFunc() +TASK.new(function() -- Don't initialize studio at first frame, may cause some weird problem + DEBUG.yieldN(6) + DEBUG.yieldT(0.26) + FMODLoadFunc() + FMOD.setMainVolume(SETTINGS.system.mainVol,true) +end) -- Hijack the original SFX module, use FMOD instead SFX[('play')]=function(name,vol,pos,tune) - FMOD.effect.play(name,{ + FMOD.effect(name,{ volume=vol, tune=tune, }) end -DEBUG.checkLoadTime("Load FMod and Bank") +DEBUG.checkLoadTime("Load Zenitha resources and things relevant to Fmod") -------------------------------------------------------------- -- Load saving data TABLE.coverR(FILE.load('conf/settings','-json -canskip') or {},SETTINGS)