mirror of
https://codeberg.org/tenplus1/ambience.git
synced 2024-12-24 01:30:30 +01:00
better sound handling when changing sets
This commit is contained in:
parent
d5ff07abe0
commit
11eff9462b
123
init.lua
123
init.lua
@ -199,88 +199,81 @@ minetest.register_globalstep(function(dtime)
|
|||||||
|
|
||||||
ok = true -- everything starts off ok
|
ok = true -- everything starts off ok
|
||||||
|
|
||||||
-- check set exists
|
-- stop current sound if another set active or gain changed
|
||||||
if set_name ~= nil and not sound_sets[set_name] then
|
if playing[player_name]
|
||||||
|
and playing[player_name].handler then
|
||||||
|
|
||||||
print("[ambience] sound set doesn't exist:", set_name)
|
if playing[player_name].set ~= set_name
|
||||||
|
or (playing[player_name].set == set_name
|
||||||
|
and playing[player_name].gain ~= MORE_GAIN) then
|
||||||
|
|
||||||
ok = false -- don't continue, set missing
|
--print ("-- change stop", set_name, playing[player_name].old_handler)
|
||||||
|
|
||||||
|
minetest.sound_stop(playing[player_name].old_handler)
|
||||||
|
|
||||||
|
playing[player_name].set = nil
|
||||||
|
playing[player_name].handler = nil
|
||||||
|
playing[player_name].gain = nil
|
||||||
|
else
|
||||||
|
ok = false -- sound set still playing, skip new sound
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- are we ok and have a set name
|
-- set random chance and reset seed
|
||||||
if ok and set_name then
|
chance = random(1, 1000)
|
||||||
|
|
||||||
-- stop current sound if another set active or gain changed
|
math.randomseed(tod + chance)
|
||||||
if playing[player_name]
|
|
||||||
and playing[player_name].handler then
|
|
||||||
|
|
||||||
if playing[player_name].sound ~= set_name
|
-- if chance is lower than set frequency then select set
|
||||||
or (playing[player_name].sound == set_name
|
if ok and set_name and chance < sound_sets[set_name].frequency then
|
||||||
and playing[player_name].gain ~= MORE_GAIN) then
|
|
||||||
--print ("-- change stop")
|
|
||||||
minetest.sound_stop(playing[player_name].handler)
|
|
||||||
|
|
||||||
playing[player_name].sound = nil
|
-- choose random sound from set
|
||||||
playing[player_name].handler = nil
|
number = random(#sound_sets[set_name].sounds)
|
||||||
playing[player_name].gain = nil
|
ambience = sound_sets[set_name].sounds[number]
|
||||||
else
|
|
||||||
ok = false -- sound set still playing, skip new sound
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- set random chance and reset seed
|
-- play sound
|
||||||
chance = random(1, 1000)
|
handler = minetest.sound_play(ambience.name, {
|
||||||
|
to_player = player_name,
|
||||||
math.randomseed(tod + chance)
|
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME,
|
||||||
|
pitch = ambience.pitch or 1.0
|
||||||
-- if chance is lower than set frequency then select set
|
}, ambience.ephemeral)
|
||||||
if ok and chance < sound_sets[set_name].frequency then
|
|
||||||
|
|
||||||
-- choose random sound from set selected
|
|
||||||
number = random(1, #sound_sets[set_name].sounds)
|
|
||||||
ambience = sound_sets[set_name].sounds[number]
|
|
||||||
|
|
||||||
-- play sound
|
|
||||||
handler = minetest.sound_play(ambience.name, {
|
|
||||||
to_player = player_name,
|
|
||||||
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME
|
|
||||||
}, ambience.ephemeral)
|
|
||||||
|
|
||||||
-- only continue if sound is playing
|
|
||||||
if handler then
|
|
||||||
|
|
||||||
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
|
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
|
||||||
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN)
|
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN)
|
||||||
|
|
||||||
-- set what player is currently listening to
|
-- only continue if sound playing returns handler
|
||||||
playing[player_name] = playing[player_name] or {}
|
if handler then
|
||||||
playing[player_name].handler = handler
|
|
||||||
playing[player_name].sound = set_name
|
|
||||||
playing[player_name].gain = MORE_GAIN
|
|
||||||
playing[player_name].old_handler = handler
|
|
||||||
|
|
||||||
-- set timer to stop sound
|
--print("-- current handler", handler)
|
||||||
minetest.after(ambience.length, function(args)
|
|
||||||
|
|
||||||
local player_name = args[2]
|
-- set what player is currently listening to
|
||||||
|
playing[player_name] = playing[player_name] or {}
|
||||||
|
playing[player_name].handler = handler
|
||||||
|
playing[player_name].set = set_name
|
||||||
|
playing[player_name].gain = MORE_GAIN
|
||||||
|
playing[player_name].old_handler = handler
|
||||||
|
|
||||||
-- make sure we are stopping same sound we started
|
-- set timer to stop sound
|
||||||
if playing[player_name]
|
minetest.after(ambience.length, function()
|
||||||
and playing[player_name].handler
|
|
||||||
and playing[player_name].sound == set_name
|
|
||||||
and handler == playing[player_name].old_handler then
|
|
||||||
--print("-- timed stop")
|
|
||||||
minetest.sound_stop(playing[player_name].handler)
|
|
||||||
|
|
||||||
-- reset player variables
|
-- make sure we are stopping same sound we started
|
||||||
playing[player_name].sound = nil
|
if playing[player_name]
|
||||||
playing[player_name].handler = nil
|
and playing[player_name].handler
|
||||||
playing[player_name].gain = nil
|
and playing[player_name].set == set_name
|
||||||
playing[player_name].old_handler = handler
|
and handler == playing[player_name].old_handler then
|
||||||
end
|
|
||||||
|
|
||||||
end, {ambience, player_name})
|
--print("-- timed stop", set_name, handler)
|
||||||
end
|
|
||||||
|
--minetest.sound_stop(playing[player_name].handler)
|
||||||
|
minetest.sound_stop(playing[player_name].old_handler)
|
||||||
|
|
||||||
|
-- reset player variables and backup handler
|
||||||
|
playing[player_name].set = nil
|
||||||
|
playing[player_name].handler = nil
|
||||||
|
playing[player_name].gain = nil
|
||||||
|
playing[player_name].old_handler = handler
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user