三个音频模块升级不再依赖全局变量SETTING

但更新音量的时候必须需手动调用setVol
整理代码
This commit is contained in:
MrZ626
2021-10-16 23:10:15 +08:00
parent 9b6855b424
commit e6a8cf7a10
8 changed files with 59 additions and 36 deletions

View File

@@ -1,9 +1,9 @@
local BGM={ local BGM={
vol=1,
default=false, default=false,
getList=function()error("Cannot getList before initialize!")end, getList=function()error("Cannot getList before initialize!")end,
getCount=function()return 0 end, getCount=function()return 0 end,
play=NULL, play=NULL,
freshVolume=NULL,
stop=NULL, stop=NULL,
onChange=NULL, onChange=NULL,
--nowPlay=[str:playing ID] --nowPlay=[str:playing ID]
@@ -13,7 +13,7 @@ local BGM={
local function task_fadeOut(src) local function task_fadeOut(src)
while true do while true do
coroutine.yield() coroutine.yield()
local v=src:getVolume()-.025*SETTING.bgm local v=src:getVolume()-.025*BGM.vol
src:setVolume(v>0 and v or 0) src:setVolume(v>0 and v or 0)
if v<=0 then if v<=0 then
src:pause() src:pause()
@@ -24,10 +24,10 @@ end
local function task_fadeIn(src) local function task_fadeIn(src)
while true do while true do
coroutine.yield() coroutine.yield()
local v=SETTING.bgm local v=BGM.vol
v=math.min(v,src:getVolume()+.025*v) v=math.min(v,src:getVolume()+.025*v)
src:setVolume(v) src:setVolume(v)
if v>=SETTING.bgm then if v>=BGM.vol then
return true return true
end end
end end
@@ -41,6 +41,10 @@ end
function BGM.setChange(func) function BGM.setChange(func)
BGM.onChange=func BGM.onChange=func
end end
function BGM.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1)
BGM.vol=v
end
function BGM.init(list) function BGM.init(list)
BGM.init=nil BGM.init=nil
local Sources={} local Sources={}
@@ -71,6 +75,18 @@ function BGM.init(list)
LOG("No BGM: "..name,5) LOG("No BGM: "..name,5)
end end
end end
function BGM.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1)
BGM.vol=v
if BGM.playing then
if BGM.vol>0 then
BGM.playing:setVolume(BGM.vol)
BGM.playing:play()
elseif BGM.nowPlay then
BGM.playing:pause()
end
end
end
function BGM.loadAll()--Not neccessary, unless you want avoid the lag when playing something for the first time function BGM.loadAll()--Not neccessary, unless you want avoid the lag when playing something for the first time
for name in next,Sources do for name in next,Sources do
_load(name) _load(name)
@@ -79,7 +95,7 @@ function BGM.init(list)
function BGM.play(name) function BGM.play(name)
name=name or BGM.default name=name or BGM.default
if not _load(name)then return end if not _load(name)then return end
if SETTING.bgm==0 then if BGM.vol==0 then
BGM.nowPlay=name BGM.nowPlay=name
BGM.playing=Sources[name] BGM.playing=Sources[name]
return true return true
@@ -117,17 +133,6 @@ function BGM.init(list)
BGM.playing:play() BGM.playing:play()
end end
end end
function BGM.freshVolume()
if BGM.playing then
local v=SETTING.bgm
if v>0 then
BGM.playing:setVolume(v)
BGM.playing:play()
elseif BGM.nowPlay then
BGM.playing:pause()
end
end
end
function BGM.stop() function BGM.stop()
TASK.removeTask_code(task_fadeIn) TASK.removeTask_code(task_fadeIn)
if BGM.nowPlay then if BGM.nowPlay then

View File

@@ -1,4 +1,6 @@
local SFX={ local SFX={
vol=1,
stereo=1,
getCount=function()return 0 end, getCount=function()return 0 end,
load=function()error("Cannot load before init!")end, load=function()error("Cannot load before init!")end,
fieldPlay=NULL, fieldPlay=NULL,
@@ -6,6 +8,14 @@ local SFX={
fplay=NULL, fplay=NULL,
reset=NULL, reset=NULL,
} }
function SFX.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1)
SFX.vol=v
end
function SFX.setStereo(v)
assert(type(v)=='number'and v>=0 and v<=1)
SFX.stereo=v
end
function SFX.init(list) function SFX.init(list)
SFX.init=nil SFX.init=nil
local rem=table.remove local rem=table.remove
@@ -23,7 +33,7 @@ function SFX.init(list)
end end
function SFX.play(s,vol,pos) function SFX.play(s,vol,pos)
if SETTING.sfx==0 or vol==0 then return end if SFX.vol==0 or vol==0 then return end
local S=Sources[s]--Source list local S=Sources[s]--Source list
if not S then return end if not S then return end
local n=1 local n=1
@@ -38,13 +48,13 @@ function SFX.init(list)
S=S[n]--AU_SRC S=S[n]--AU_SRC
if S:getChannelCount()==1 then if S:getChannelCount()==1 then
if pos then if pos then
pos=pos*SETTING.stereo pos=pos*SFX.stereo
S:setPosition(pos,1-pos^2,0) S:setPosition(pos,1-pos^2,0)
else else
S:setPosition(0,0,0) S:setPosition(0,0,0)
end end
end end
S:setVolume(((vol or 1)*SETTING.sfx)^1.626) S:setVolume(((vol or 1)*SFX.vol)^1.626)
S:play() S:play()
end end
function SFX.fplay(s,vol,pos) function SFX.fplay(s,vol,pos)
@@ -62,7 +72,7 @@ function SFX.init(list)
S=S[n]--AU_SRC S=S[n]--AU_SRC
if S:getChannelCount()==1 then if S:getChannelCount()==1 then
if pos then if pos then
pos=pos*SETTING.stereo pos=pos*SFX.stereo
S:setPosition(pos,1-pos^2,0) S:setPosition(pos,1-pos^2,0)
else else
S:setPosition(0,0,0) S:setPosition(0,0,0)

View File

@@ -1,4 +1,5 @@
local VOC={ local VOC={
vol=1,
getCount=function()return 0 end, getCount=function()return 0 end,
getQueueCount=function()return 0 end, getQueueCount=function()return 0 end,
load=function()error("Cannot load before init!")end, load=function()error("Cannot load before init!")end,
@@ -6,6 +7,10 @@ local VOC={
play=NULL, play=NULL,
update=NULL, update=NULL,
} }
function VOC.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1)
VOC.vol=v
end
function VOC.init(list) function VOC.init(list)
VOC.init=nil VOC.init=nil
local rnd=math.random local rnd=math.random
@@ -67,7 +72,7 @@ function VOC.init(list)
end end
function VOC.play(s,chn) function VOC.play(s,chn)
if SETTING.voc>0 then if VOC.vol>0 then
local _=Source[s] local _=Source[s]
if not _ then return end if not _ then return end
if chn then if chn then
@@ -90,13 +95,13 @@ function VOC.init(list)
end end
elseif Q.s==1 then--Waiting load source elseif Q.s==1 then--Waiting load source
Q[1]=_getVoice(Q[1]) Q[1]=_getVoice(Q[1])
Q[1]:setVolume(SETTING.voc) Q[1]:setVolume(VOC.vol)
Q[1]:play() Q[1]:play()
Q.s=Q[2]and 2 or 4 Q.s=Q[2]and 2 or 4
elseif Q.s==2 then--Playing 1,ready 2 elseif Q.s==2 then--Playing 1,ready 2
if Q[1]:getDuration()-Q[1]:tell()<.08 then if Q[1]:getDuration()-Q[1]:tell()<.08 then
Q[2]=_getVoice(Q[2]) Q[2]=_getVoice(Q[2])
Q[2]:setVolume(SETTING.voc) Q[2]:setVolume(VOC.vol)
Q[2]:play() Q[2]:play()
Q.s=3 Q.s=3
end end

View File

@@ -36,7 +36,7 @@ math.randomseed(os.time()*626)
love.setDeprecationOutput(false) love.setDeprecationOutput(false)
love.keyboard.setKeyRepeat(true) love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(false) love.keyboard.setTextInput(false)
if SYSTEM=='Android'or SYSTEM=='iOS'then if MOBILE then
local w,h,f=love.window.getMode() local w,h,f=love.window.getMode()
f.resizable=false f.resizable=false
love.window.setMode(w,h,f) love.window.setMode(w,h,f)

View File

@@ -36,6 +36,9 @@ function applySettings()
love.audio.setVolume(SETTING.mainVol) love.audio.setVolume(SETTING.mainVol)
love.mouse.setVisible(SETTING.sysCursor) love.mouse.setVisible(SETTING.sysCursor)
VK.setShape(SETTING.VKSkin) VK.setShape(SETTING.VKSkin)
BGM.setVol(SETTING.bgm)
SFX.setVol(SETTING.sfx)
VOC.setVol(SETTING.voc)
applyBlockSatur(SETTING.blockSatur) applyBlockSatur(SETTING.blockSatur)
applyFieldSatur(SETTING.fieldSatur) applyFieldSatur(SETTING.fieldSatur)
applyLanguage() applyLanguage()

View File

@@ -218,9 +218,9 @@ end
scene.widgetList={ scene.widgetList={
WIDGET.newText{name="title", x=640, y=-5,font=50}, WIDGET.newText{name="title", x=640, y=-5,font=50},
WIDGET.newSlider{name="bgm", x=1000,y=80,lim=130,w=250,disp=SETval('bgm'),code=function(v)SETTING.bgm=v BGM.freshVolume()end}, WIDGET.newSlider{name="bgm", x=1000,y=80, lim=130,w=250,disp=SETval('bgm'),code=function(v)SETTING.bgm=v BGM.setVol(SETTING.bgm)end},
WIDGET.newSlider{name="sfx", x=1000,y=150,lim=130,w=250,disp=SETval('sfx'),code=SETsto('sfx'),change=function()SFX.play('blip_1')end}, WIDGET.newSlider{name="sfx", x=1000,y=150,lim=130,w=250,disp=SETval('sfx'),code=function(v)SETTING.sfx=v SFX.setVol(SETTING.sfx)end},
WIDGET.newSlider{name="voc", x=1000,y=220,lim=130,w=250,disp=SETval('voc'),code=SETsto('voc'),change=function()VOC.play('test')end}, WIDGET.newSlider{name="voc", x=1000,y=220,lim=130,w=250,disp=SETval('voc'),code=function(v)SETTING.voc=v VOC.setVol(SETTING.voc)end},
WIDGET.newSwitch{name="label",x=1200,y=290,lim=160,disp=function()return showLabel end,code=pressKey"space",}, WIDGET.newSwitch{name="label",x=1200,y=290,lim=160,disp=function()return showLabel end,code=pressKey"space",},
WIDGET.newButton{name="music",x=1140,y=540,w=170,h=80,font=40,code=pressKey"tab"}, WIDGET.newButton{name="music",x=1140,y=540,w=170,h=80,font=40,code=pressKey"tab"},
WIDGET.newButton{name="back", x=1140,y=640,w=170,h=80,font=60,fText=CHAR.icon.back,code=backScene}, WIDGET.newButton{name="back", x=1140,y=640,w=170,h=80,font=60,fText=CHAR.icon.back,code=backScene},

View File

@@ -116,7 +116,7 @@ scene.widgetList={
code=function(v)BGM.seek(v*BGM.playing:getDuration())end, code=function(v)BGM.seek(v*BGM.playing:getDuration())end,
hideF=function()return not BGM.nowPlay end hideF=function()return not BGM.nowPlay end
}, },
WIDGET.newSlider{name="bgm", x=760,y=80,w=400,disp=SETval('bgm'),code=function(v)SETTING.bgm=v BGM.freshVolume()end}, WIDGET.newSlider{name="bgm", x=760,y=80,w=400,disp=SETval('bgm'),code=function(v)SETTING.bgm=v BGM.setVol(SETTING.bgm)end},
WIDGET.newButton{name="up", x=200,y=250,w=120,code=pressKey"up",hideF=function()return selected==1 end,font=60,fText=CHAR.key.up}, WIDGET.newButton{name="up", x=200,y=250,w=120,code=pressKey"up",hideF=function()return selected==1 end,font=60,fText=CHAR.key.up},
WIDGET.newButton{name="play", x=200,y=390,w=120,code=pressKey"space",font=65,fText=CHAR.icon.play_pause}, WIDGET.newButton{name="play", x=200,y=390,w=120,code=pressKey"space",font=65,fText=CHAR.icon.play_pause},
WIDGET.newButton{name="down", x=200,y=530,w=120,code=pressKey"down",hideF=function()return selected==#bgmList end,font=60,fText=CHAR.key.down}, WIDGET.newButton{name="down", x=200,y=530,w=120,code=pressKey"down",hideF=function()return selected==#bgmList end,font=60,fText=CHAR.key.down},

View File

@@ -79,14 +79,14 @@ scene.widgetList={
WIDGET.newButton{name="game", x=200, y=80,w=240,h=80,color='lC',font=35,code=swapScene('setting_game','swipeR')}, WIDGET.newButton{name="game", x=200, y=80,w=240,h=80,color='lC',font=35,code=swapScene('setting_game','swipeR')},
WIDGET.newButton{name="graphic", x=1080,y=80,w=240,h=80,color='lC',font=35,code=swapScene('setting_video','swipeL')}, WIDGET.newButton{name="graphic", x=1080,y=80,w=240,h=80,color='lC',font=35,code=swapScene('setting_video','swipeL')},
WIDGET.newSlider{name="mainVol", x=300, y=170,w=420,lim=220,color='lG',disp=SETval('mainVol'),code=function(v)SETTING.mainVol=v love.audio.setVolume(SETTING.mainVol)end}, WIDGET.newSlider{name="mainVol", x=300, y=170,w=420,lim=220,color='lG',disp=SETval('mainVol'), code=function(v)SETTING.mainVol=v love.audio.setVolume(SETTING.mainVol)end},
WIDGET.newSlider{name="bgm", x=300, y=240,w=420,lim=220,color='lG',disp=SETval('bgm'),code=function(v)SETTING.bgm=v BGM.freshVolume()end}, WIDGET.newSlider{name="bgm", x=300, y=240,w=420,lim=220,color='lG',disp=SETval('bgm'), code=function(v)SETTING.bgm=v BGM.setVol(SETTING.bgm)end},
WIDGET.newSlider{name="sfx", x=300, y=310,w=420,lim=220,color='lC',change=function()SFX.play('blip_1')end,disp=SETval('sfx'),code=SETsto('sfx')}, WIDGET.newSlider{name="sfx", x=300, y=310,w=420,lim=220,color='lC',disp=SETval('sfx'), code=function(v)SETTING.sfx=v SFX.setVol(SETTING.sfx)end, change=function()SFX.play('blip_1')end},
WIDGET.newSlider{name="stereo", x=300, y=380,w=420,lim=220,color='lC',change=function()SFX.play('move',1,-1)SFX.play('lock',1,1)end,disp=SETval('stereo'),code=SETsto('stereo'),hideF=function()return SETTING.sfx==0 end}, WIDGET.newSlider{name="stereo", x=300, y=380,w=420,lim=220,color='lC',disp=SETval('stereo'), code=function(v)SETTING.stereo=v SFX.setStereo(SETTING.stereo)end,change=function()SFX.play('move',1,-1)SFX.play('lock',1,1)end,hideF=function()return SETTING.sfx==0 end},
WIDGET.newSlider{name="spawn", x=300, y=450,w=420,lim=220,color='lC',change=function()SFX.fplay('spawn_'..math.random(7),SETTING.sfx_spawn)end,disp=SETval('sfx_spawn'),code=SETsto('sfx_spawn')}, WIDGET.newSlider{name="spawn", x=300, y=450,w=420,lim=220,color='lC',disp=SETval('sfx_spawn'), code=function(v)SETTING.sfx_spawn=v end, change=function()SFX.fplay('spawn_'..math.random(7),SETTING.sfx_spawn)end,},
WIDGET.newSlider{name="warn", x=300, y=520,w=420,lim=220,color='lC',change=function()SFX.fplay('warning',SETTING.sfx_warn)end,disp=SETval('sfx_warn'),code=SETsto('sfx_warn')}, WIDGET.newSlider{name="warn", x=300, y=520,w=420,lim=220,color='lC',disp=SETval('sfx_warn'), code=function(v)SETTING.sfx_warn=v end, change=function()SFX.fplay('warning',SETTING.sfx_warn)end},
WIDGET.newSlider{name="vib", x=300, y=590,w=420,lim=220,color='lN',unit=10,change=function()if SETTING.vib>0 then VIB(SETTING.vib+2)end end,disp=SETval('vib'),code=SETsto('vib')}, WIDGET.newSlider{name="vib", x=300, y=590,w=420,lim=220,color='lN',disp=SETval('vib'),unit=10,code=function(v)SETTING.vib=v end, change=function()if SETTING.vib>0 then VIB(SETTING.vib+2)end end},
WIDGET.newSlider{name="voc", x=300, y=660,w=420,lim=220,color='lN',change=function()VOC.play('test')end,disp=SETval('voc'),code=SETsto('voc')}, WIDGET.newSlider{name="voc", x=300, y=660,w=420,lim=220,color='lN',disp=SETval('voc'), code=function(v)SETTING.voc=v VOC.setVol(SETTING.voc)end, change=function()VOC.play('test')end},
WIDGET.newSwitch{name="autoMute", x=1150,y=180,lim=380,disp=SETval('autoMute'),code=SETrev('autoMute')}, WIDGET.newSwitch{name="autoMute", x=1150,y=180,lim=380,disp=SETval('autoMute'),code=SETrev('autoMute')},
WIDGET.newSwitch{name="fine", x=1150,y=250,lim=380,disp=SETval('fine'),code=function()SETTING.fine=not SETTING.fine if SETTING.fine then SFX.play('finesseError',.6)end end}, WIDGET.newSwitch{name="fine", x=1150,y=250,lim=380,disp=SETval('fine'),code=function()SETTING.fine=not SETTING.fine if SETTING.fine then SFX.play('finesseError',.6)end end},