defineProperty("pause", globalPropertyf("sim/time/paused"))
defineProperty("frame_time", globalPropertyf("frame_time"))
defineProperty("SoS", globalPropertyf("sim/weather/speed_sound_ms"))
defineProperty("mastervol", globalPropertyi("cl300/master_vol"))

defineProperty("ext_sound_vol", globalPropertyf("cl300/ext_sound_vol"))
defineProperty("ext_sound_mute", globalPropertyi("cl300/ext_sound_mute"))

defineProperty("view", globalPropertyf("sim/graphics/view/view_is_external"))
defineProperty("dsp1", globalPropertyf("DSP1"))
defineProperty("dsp2", globalPropertyf("DSP2"))
defineProperty("dsp3", globalPropertyf("DSP3"))
defineProperty("dsp4", globalPropertyf("DSP4"))

defineProperty("viewX", globalPropertyf("sim/graphics/view/view_x"))
defineProperty("viewY", globalPropertyf("sim/graphics/view/view_y"))
defineProperty("viewZ", globalPropertyf("sim/graphics/view/view_z"))
defineProperty("acfposX", globalPropertyf("sim/flightmodel/position/local_x"))
defineProperty("acfposY", globalPropertyf("sim/flightmodel/position/local_y"))
defineProperty("acfposZ", globalPropertyf("sim/flightmodel/position/local_z"))
defineProperty("acfpitch", globalPropertyf("sim/flightmodel/position/theta"))
defineProperty("acfyaw", globalPropertyf("sim/flightmodel/position/beta"))
defineProperty("acfroll", globalPropertyf("sim/flightmodel/position/phi"))

defineProperty("customslider2_r", globalPropertyf("cl300/customslider2_r"))
defineProperty("customslider3_r", globalPropertyf("cl300/customslider3_r"))
defineProperty("customslider4_r", globalPropertyf("cl300/customslider4_r"))

defineProperty("door1", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[0]"))
defineProperty("door2", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[1]"))
defineProperty("door3", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[2]"))
defineProperty("door4", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[3]"))
defineProperty("door5", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[4]"))
defineProperty("door6", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[5]"))

defineProperty("acf_hdg", globalPropertyf("sim/flightmodel/position/psi"))
defineProperty("view_hdg", globalPropertyf("sim/graphics/view/view_heading"))

defineProperty("ENGN1", globalPropertyf("sim/cockpit/engine/APU_N1"))
defineProperty("reverse", globalPropertyf("sim/flightmodel2/engines/thrust_reverser_deploy_ratio[0]"))

defineProperty("volume", globalPropertyf("volumeX"))

defineProperty("xF", globalPropertyf("maxF"))
defineProperty("iF", globalPropertyf("minF"))
defineProperty("xB", globalPropertyf("maxB"))
defineProperty("iB", globalPropertyf("minB"))

ENGNsound_bl = loadSample('sounds/engine/EngineBL.wav')
ENGNsound_br = loadSample('sounds/engine/EngineBR.wav')
ENGNsound_fl = loadSample('sounds/engine/EngineFL.wav')
ENGNsound_fr = loadSample('sounds/engine/EngineFR.wav')
apu_inn = loadSample('sounds/engine/apu_inn.wav')
--bass = loadSample('sounds/engine/bass.wav')
--bass_l = loadSample('sounds/engine/bass_l.wav')
--bass_r = loadSample('sounds/engine/bass_r.wav')
alpha = 0
alphaP = 0
zeta=0

LR=1.6 -- left/right damping
UD=1.2 -- up/down damping

Tb=get(iB)
Tf=get(iF)
TbM=get(xB)
TfM=get(xF)
backgain = 300
backgainR = 300
backgainL = 300

frontgain = 300
frontgainR = 300
frontgainL = 300

inngain = 800

temp=0
temp1=0
t=0

pitchb=1000
pitchf=1000
pitch_inn=1000
DIS_old=0
v_t = 0
function update ()



	LOC={0, 3.6, -1.97}

	--LOC={4.4, -1.6, -1.97} -- 2nd engine

	FT=get(frame_time)

	W=-get(acfpitch)*3.141592/180
	U=-get(acfroll)*3.141592/180


	-- Camera and source direction
	beta=get(view_hdg)
	gamma=get(acf_hdg)-5.56429

	if gamma<0 then gamma=360+gamma end
	V=gamma*3.141592/180

	v=get(view)
	e=get(ENGN1)


	-- Engine location

	SHIFT={LOC[1]*math.cos(U)-LOC[3]*math.sin(U), LOC[2], LOC[3]*math.cos(U)+LOC[1]*math.sin(U)}
	LOC=SHIFT
	SHIFT={LOC[1], LOC[2]*math.cos(W)-LOC[3]*math.sin(W), LOC[3]*math.cos(W)+LOC[2]*math.sin(W)}
	LOC=SHIFT
	SHIFT={LOC[1]*math.cos(V)-LOC[2]*math.sin(V), LOC[2]*math.cos(V)+LOC[1]*math.sin(V), LOC[3]}


	--- Sound source position ---------------------------------------------
	Ox=get(acfposX)+SHIFT[1]
	Oy=get(acfposZ)+SHIFT[2]
	Oz=get(acfposY)+SHIFT[3]

	--- Camera position ---------------------------------------------
	X=get(viewX)
	Y=get(viewZ)
	Z=get(viewY)

	--- Distance
	cam_pos = {X - Ox, Y-Oy, Z-Oz}
	DIS=(cam_pos[1])^2 + (cam_pos[2])^2 + (cam_pos[3])^2


	lDIS=math.sqrt((X-get(acfposX))^2+(Y-get(acfposZ))^2+(Z-get(acfposY))^2)

	--- Throttle ---------------------------------

	Tb=get(iB)*get(volume)
	Tf=get(iF)*get(volume)
	TbM=get(xB)*get(volume)
	TfM=get(xF)*get(volume)

	if e>33 then
		SM=(TbM-Tb)/67*e+TbM-(TbM-Tb)/67*100
		SMf=(TfM-Tf)/67*e+TfM-(TfM-Tf)/67*100
	else
		SM=Tb*e/33
		SMf=Tf*e/33
	end

	if v_t < v then v_t = v_t + 0.1 end
	if v_t > v then v_t = v_t - 0.1 end


	if FT>0 then
		pitch_inn=math.min(math.max(700*(1+(e-33)/67), 200), 2500)
		if v_t > 0.94 then
		pitchb=math.min(math.max(1000*(1+(e-33)/67)*(1-((lDIS-DIS_old)/FT)/1500), 200), 2500)/4
		pitchf=math.min(math.max(900*(1+(e-33)/120)*(1-((lDIS-DIS_old)/FT)/1500), 200), 2500)/2

		DIS_old=lDIS
		else
		pitchb=math.min(math.max(1000*(1+(e-33)/67), 200), 2500)/4
		pitchf=math.min(math.max(900*(1+(e-33)/120), 200), 2500)/2
		end
	end
	--- Camera ---------------------------------
	if (Ox-X)<0 then
		alpha = 180/3.141592*math.atan((Oy-Y)/(Ox-X))+270
	else
		alpha = 180/3.141592*math.atan((Oy-Y)/(Ox-X))+90
	end


	if beta>alpha then
		temp=(beta-alpha)/180-1
	else
		temp=-(alpha-beta)/180+1
	end

	if temp>0.5 then t=1-temp
	elseif temp<-0.5 then t=-temp-1
	else t=temp
	end

	--- Engine direction ---------------------------------
	alphaP=math.mod(180+alpha, 360)

	if gamma>alphaP then
		temp1=(gamma-alphaP)/180-1
	else
		temp1=-(alphaP-gamma)/180+1
	end

	rev=0
	t1=((1.5-math.abs(temp1))/1.5)*((1.5-rev)/1.5)+(math.abs(temp1))*rev
	t2=(math.abs(temp1)+0.5)/1.5

	--- Inside sound ---------------------------------
	factorL=0.1+math.max(get(customslider2_r),get(customslider4_r)*0.5,get(customslider3_r)*0.3)
	factorR=0.1+math.max(get(customslider2_r),get(customslider4_r)*0.5,get(customslider3_r)*0.3)

	--- Vertical
	zeta = -180/3.141592*math.sin((Oz-Z)/math.sqrt(DIS))

--	tzb=math.abs(zeta-W*180/3.141592)/45*v
--	tzf=math.abs(zeta+W*180/3.141592)/45*v

	tzb=math.abs(zeta*0.6-W*180/3.141592)/45*v
	tzf=math.abs(zeta*0.6+W*180/3.141592)/45*v
	--- Sound Power ---------------------------------
	backgain = (SM*(1+rev*0.5)*math.exp(-DIS/80000))*(1-tzb*UD/2)
	frontgain = (SMf*(1-rev*0.8)*math.exp(-DIS/80000))*(1-tzf*UD/2)

	--- Back/Front ---------------------------------



	if v == 1 then
		backgainR=(backgain*(1+LR*t)*(v*t1+(1-v)*factorR)/4)*get(ext_sound_vol)*get(ext_sound_mute)
		backgainL=(backgain*(1-LR*t)*(v*t1+(1-v)*factorL)/4)*get(ext_sound_vol)*get(ext_sound_mute)

		frontgainR=(frontgain*(1+LR*t)*(v*t2+(1-v)*factorR)/4)*get(ext_sound_vol)*get(ext_sound_mute)
		frontgainL=(frontgain*(1-LR*t)*(v*t2+(1-v)*factorL)/4)*get(ext_sound_vol)*get(ext_sound_mute)

		inngain = 0
	else
		backgainR=backgain*(0.2)*factorR/4
		backgainL=backgain*(0.2)*factorL/4

		frontgainR=frontgain*(0.1)*factorR/4
		frontgainL=frontgain*(0.1)*factorL/4

		inngain = get(ENGN1)*0.5

		--[=[

		backgainR=backgain*(1.2+LR*t/2)*factorR
		backgainL=backgain*(1.2-LR*t/2)*factorL

		frontgainR=frontgain*(1.2+LR*t/2)*factorR
		frontgainL=frontgain*(1.2-LR*t/2)*factorL

		--]=]
	end



	--- Sound ---------------------------------
	setSamplePitch(ENGNsound_bl, pitchb*1.7)
	setSamplePitch(ENGNsound_br, pitchb*1.7)

	setSamplePitch(ENGNsound_fl, pitchf*1.7)
	setSamplePitch(ENGNsound_fr, pitchf*1.7)

	setSamplePitch(apu_inn, pitch_inn)
--		setSamplePitch(bass, (1900+get(ENGN1)*5))
--	setSamplePitch(bass_l, (2300+get(ENGN1)*5))
--	setSamplePitch(bass_r, (2300+get(ENGN1)*5))

	setSampleGain(ENGNsound_bl, backgainL*get(mastervol))
	setSampleGain(ENGNsound_br, backgainR*get(mastervol))

	setSampleGain(ENGNsound_fl, frontgainL*get(mastervol))
	setSampleGain(ENGNsound_fr, frontgainR*get(mastervol))

	setSampleGain(apu_inn, inngain*get(mastervol))
--	setSampleGain(bass, get(ENGN1)*2*(1-v))
--	setSampleGain(bass_l, backgainL*0.6*(1+rev*0.2))
--	setSampleGain(bass_r, backgainR*0.6*(1+rev*0.2))

	if isSamplePlaying(ENGNsound_bl) == false and isSamplePlaying(ENGNsound_br) == false then
		playSample(ENGNsound_bl, 1)
		playSample(ENGNsound_br, 1)
	end

	if isSamplePlaying(ENGNsound_fl) == false and isSamplePlaying(ENGNsound_fr) == false then
		playSample(ENGNsound_fl, 1)
		playSample(ENGNsound_fr, 1)
	end


	if isSamplePlaying(apu_inn) == false then
	playSample(apu_inn, 1)
	end
--[=[
	if isSamplePlaying(bass) == false then
	playSample(bass, 1)
	end
	if isSamplePlaying(bass_l) == false then
	playSample(bass_l, 1)
	end
	if isSamplePlaying(bass_r) == false then
	playSample(bass_r, 1)
	end
	--]=]


	--set(dsp1, get(lDIS))
	--set(dsp2, pitchb)

	--set(dsp4, get(frontgainL))




end


