Skip to content

Commit

Permalink
不再第一帧就启动fmod库,尝试修fmod奇怪问题
Browse files Browse the repository at this point in the history
整理代码
  • Loading branch information
MrZ626 committed Apr 20, 2024
1 parent d3cbe31 commit 1185afb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
40 changes: 23 additions & 17 deletions assets/fmod20221/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ end
---@type table<any,FMOD.Studio.EventDescription>
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])
Expand All @@ -73,6 +74,7 @@ end
---@type table<any,FMOD.Studio.EventDescription>
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])
Expand All @@ -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

--------------------------
Expand All @@ -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?}?
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})

--------------------------

Expand All @@ -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<number,number>, param?:table}
---@return FMOD.Studio.EventInstance?
Expand Down Expand Up @@ -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})

--------------------------

Expand All @@ -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

--------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion assets/gamefunc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions assets/progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 18 additions & 14 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1185afb

Please sign in to comment.