class "amk_music"
function amk_music:__init()
	self.left = nil
	self.right = nil
	self.end_time=nil
end

function amk_music:initialize(sound)
	self.right = sound_object(sound.."_r")
	self.left = sound_object(sound.."_l")
	self.end_time = nil
  if (not self.right) then
    abort("amk_music:initialize: Cannot open sound file "..sound.."_r")
  end
  if (not self.left) then
    abort("amk_music:initialize: Cannot open sound file "..sound.."_l")
  end
end

function amk_music:play()
  amk.oau_reason="play begin"
  if self:playing() then
  amk.oau_reason="play pre stop"
		self:stop()
	end
  if not db.actor then
    return
  end
  amk.oau_reason="play pre dir"
	local dir = device().cam_right:set_length(0.1)
  amk.oau_reason="play post dir"
	dir.y=0
  amk.oau_reason="play pre play right "..type(self.right)
	self.right:play_at_pos(db.actor,dir, 0, sound_object.s2d)
--	self.right:play_no_feedback(db.actor,sound_object.s2d, 0, dir, 1.0)
  amk.oau_reason="play post play right"
	dir.x=-dir.x
	dir.z=-dir.z
  amk.oau_reason="play pre play left"
	self.left:play_at_pos(db.actor,dir, 0, sound_object.s2d)
--	self.left:play_no_feedback(db.actor,sound_object.s2d, 0, dir, 1.0)
  amk.oau_reason="play pre length"
	self.end_time = time_global() + self.left:length()
  amk.oau_reason="play pre return"  
  self:set_volume(get_console():get_float('snd_volume_music')) 
	return self.end_time
end

function amk_music:play_at_time(time)
	if self:playing() then
		self:stop()
	end
  if not db.actor then
    return
  end
	local dir = device().cam_right:set_length(0.1)
	dir.y=0
	self.right:play_at_pos(db.actor,dir, (time - time_global())/1000, sound_object.s2d)
--	self.right:play_no_feedback(db.actor,sound_object.s2d, (time - time_global())/1000, dir, 1.0)
	dir.x=-dir.x
	dir.z=-dir.z
	self.left:play_at_pos(db.actor,dir,  (time - time_global())/1000, sound_object.s2d)
--	self.left:play_no_feedback(db.actor,sound_object.s2d, (time - time_global())/1000, dir, 1.0)
	self.end_time = time + self.left:length()
	self:set_volume(get_console():get_float('snd_volume_music')) 
	return self.end_time
end

function amk_music:playing()
	return self.left and self.left:playing()
end

function amk_music:update()
	if self:playing() then
		local dir = device().cam_right:set_length(0.1)
		dir.y=0
		self.right:set_position(dir)
		dir.x=-dir.x
		dir.z=-dir.z
		self.left:set_position(dir)
		self:set_volume(get_console():get_float('snd_volume_music')) 
	end
end

function amk_music:stop()
	if self.right:playing() then self.right:stop() end
	if self.left:playing() then self.left:stop() end
end

function amk_music:length()
	return self.left and self.left:length() or 1
end

function amk_music:set_volume(num)
	if self.left then
		self.left.volume=num
		self.right.volume=num
	end
end

