三个音频模块升级不再依赖全局变量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={
vol=1,
default=false,
getList=function()error("Cannot getList before initialize!")end,
getCount=function()return 0 end,
play=NULL,
freshVolume=NULL,
stop=NULL,
onChange=NULL,
--nowPlay=[str:playing ID]
@@ -13,7 +13,7 @@ local BGM={
local function task_fadeOut(src)
while true do
coroutine.yield()
local v=src:getVolume()-.025*SETTING.bgm
local v=src:getVolume()-.025*BGM.vol
src:setVolume(v>0 and v or 0)
if v<=0 then
src:pause()
@@ -24,10 +24,10 @@ end
local function task_fadeIn(src)
while true do
coroutine.yield()
local v=SETTING.bgm
local v=BGM.vol
v=math.min(v,src:getVolume()+.025*v)
src:setVolume(v)
if v>=SETTING.bgm then
if v>=BGM.vol then
return true
end
end
@@ -41,6 +41,10 @@ end
function BGM.setChange(func)
BGM.onChange=func
end
function BGM.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1)
BGM.vol=v
end
function BGM.init(list)
BGM.init=nil
local Sources={}
@@ -71,6 +75,18 @@ function BGM.init(list)
LOG("No BGM: "..name,5)
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
for name in next,Sources do
_load(name)
@@ -79,7 +95,7 @@ function BGM.init(list)
function BGM.play(name)
name=name or BGM.default
if not _load(name)then return end
if SETTING.bgm==0 then
if BGM.vol==0 then
BGM.nowPlay=name
BGM.playing=Sources[name]
return true
@@ -117,17 +133,6 @@ function BGM.init(list)
BGM.playing:play()
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()
TASK.removeTask_code(task_fadeIn)
if BGM.nowPlay then

View File

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

View File

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