function update(npc_id)
if sound_table[npc_id] then
-- local t = type(sound_table[npc_id])
if not(sound_table[npc_id]:is_playing(npc_id)) then
-- if(t=="string") or not(sound_table[npc_id]:is_playing(npc_id)) then
-- if(t=="string") then
-- sound_table[npc_id] = sound_theme.theme[sound_table[npc_id]]
-- end

--printf("SOUND_CALLBACK from [%s] sound_path [%s]",npc_id,sound_table[npc_id].path)
sound_table[npc_id]:callback(npc_id)
sound_table[npc_id] = nil
end
end
end

looped_sound = {}

function play_sound_looped(npc_id, sound)
local snd_theme = sound_theme.theme[sound]
if snd_theme == nil then
abort("play_sound_looped. Wrong sound theme [%s], npc[%s]", tostring(sound), npc_id)
end
if snd_theme.class_id ~= "looped_sound" then
abort("You trying to play sound [%s] which type is not looped", sound)
end
if looped_sound[npc_id] ~= nil and looped_sound[npc_id][sound] ~= nil and looped_sound[npc_id][sound]:is_playing(npc_id) then
return
end
if snd_theme:play(npc_id) then
printf("PLAY2. looped theme[%s] object[%s]", tostring(sound), npc_id)
if looped_sound[npc_id] == nil then
looped_sound[npc_id] = {}
end
looped_sound[npc_id][sound] = snd_theme
end
end
function stop_sound_looped(npc_id, sound)
if sound then
if type(looped_sound[npc_id][sound]) ~= "string" then
if looped_sound[npc_id][sound] and looped_sound[npc_id][sound]:is_playing(npc_id) then
printf("looped sound ["..tostring(sound).."] stop for obj [%s]",npc_id)
looped_sound[npc_id][sound]:stop()
looped_sound[npc_id][sound] = nil
end
end
else
if looped_sound[npc_id] then
for k,v in pairs(looped_sound[npc_id]) do
if v and type(v) ~= "string" and v:is_playing(npc_id) then
printf("looped sound ["..tostring(k).."] stop for obj [%s]",npc_id)
v:stop()
end
end
looped_sound[npc_id] = nil
end
end
end

function set_volume_sound_looped(npc_id, sound, level)
if looped_sound[npc_id] then
if looped_sound[npc_id][sound] and looped_sound[npc_id][sound]:is_playing(npc_id) then
printf("looped sound ["..tostring(sound).."] for obj ["..npc_id.."] set volume "..level)
looped_sound[npc_id][sound]:set_volume(level)
end
end
end

function stop_all_sounds()
for k,v in pairs(sound_table) do
if(type(v)~="string") then
v:stop()
end
end
for k,v in pairs(looped_sound) do
for k,v in pairs(looped_sound[k]) do
if v and v:is_playing(npc_id) then
printf("looped sound ["..tostring(k).."] stopped ")
v:stop()
end
end
end
end

function stop_sounds_by_id(obj_id)
local sound = sound_table[obj_id]
if sound and sound.stop then
sound:stop(obj_id)
end
local looped_snd = looped_sound[obj_id]
if looped_snd then
for k,v in pairs(looped_snd) do
if v and v:is_playing(obj_id) then
v:stop(obj_id)
end
end
end
end

local snd_table = {}
local t = 0
for k,v in pairs(db.sound[npc_id].snd) do
snd_table[t] = {}
get_sound(sound_theme.theme[v], snd_table[t], npc)
t = t + 1
end
for k,v in pairs(snd_table) do
if v.into_id then db.sound[npc_id].last_snd[v.into_id] = nil end
if v.rnd_id then db.sound[npc_id].last_snd[v.rnd_id] = nil end
end