--SoundPurge
function SoundPurge()
	local Sound = script.Parent.Parent.BeltSet.SoundMain
	wait(2)
	Sound.PlaybackSpeed = 0.2
	wait(0.4)
	Sound.PlaybackSpeed = 0
end

SoundPurge()

RCMAddress.Contactors.Blower:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.BlowerOn.Value = RCMAddress.Contactors.Blower.Value
end)

RunCycle = false
CompleteStop = true
script.Parent.BlowerOn.Changed:Connect(function()
	if CompleteStop == true and script.Parent.BlowerOn.Value == true then
		CompleteStop = false
		--script.Parent.Parent.BeltSet.MotorStart:Play()
		repeat wait()
			if SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.On.Value == true then
				if script.Parent.BlowerOn.Value == true and script.Parent.Parent.BeltSet.Sound.PlaybackSpeed > 0 then
					local pitch = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed
					local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.7)/250,0.0004,0.07)
					--local formula = math.clamp(math.log(pitch+0.0001,0.7)/250,0.0004,0.07)
					script.Parent.Parent.BeltSet.Sound.PlaybackSpeed = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed - formula
				elseif script.Parent.BlowerOn.Value == false and script.Parent.Parent.BeltSet.Sound.PlaybackSpeed < 1 then
					local pitch = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed
					local formula = math.clamp(math.log(pitch+0.0001,0.7)/250,0.0004,0.07)
					--local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.7)/250,0.0004,0.07)
					--print(math.abs(pitch-1))
					script.Parent.Parent.BeltSet.Sound.PlaybackSpeed = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed + formula
				end
			else
				if script.Parent.BlowerOn.Value == true and script.Parent.Parent.BeltSet.Sound.PlaybackSpeed > 0 then
					local pitch = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed
					local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.7)/250,0.0004,0.07)
					--local formula = math.clamp(math.log(pitch+0.0001,0.7)/250,0.0004,0.07)
					script.Parent.Parent.BeltSet.Sound.PlaybackSpeed = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed - formula
				elseif script.Parent.BlowerOn.Value == false and script.Parent.Parent.BeltSet.Sound.PlaybackSpeed > 0 then
					local pitch = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed
					local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.7)/250,0.0004,0.07)
					--local formula = math.clamp(math.log(math.abs(pitch-1)+0.0001,0.7)/250,0.0004,0.07)
					--print(math.abs(pitch-1))
					script.Parent.Parent.BeltSet.Sound.PlaybackSpeed = script.Parent.Parent.BeltSet.Sound.PlaybackSpeed - formula
				end
			end
		until CompleteStop == true
	end
end)

LobeSet1 = script.Parent.Parent.Sutorbilt4MBlower.MovingParts.Set1Motor.HingeConstraint
LobeSet2 = script.Parent.Parent.Sutorbilt4MBlower.MovingParts.Set2Motor.HingeConstraint
BlowerMotor = script.Parent.Parent.BlowerMotor.Motor.HingeConstraint

function Rotate()
	LobeSet1.AngularVelocity = -script.Parent.CurrentBlowerSpeed.Value/.01
	LobeSet2.AngularVelocity = script.Parent.CurrentBlowerSpeed.Value/.01
	BlowerMotor.AngularVelocity = -script.Parent.CurrentBlowerSpeed.Value/.01
end

PlayAudio = false

function playAudio()
	if PlayAudio == false then
		PlayAudio = true
		script.Parent.Parent.BeltSet.SoundMain:Play()
		script.Parent.Parent.BeltSet.MotorStart.Volume = 0.5
		wait(0.1)
		if SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.On.Value == false then
			SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.RollOffMaxDistance = 0
			SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.RollOffMinDistance = 0
		end
	end
end

script.Parent.BlowerOn.Changed:Connect(function()
	if script.Parent.BlowerOn.Value == true then
		playAudio()
		RunCycle = true
		repeat game:GetService("RunService").Heartbeat:wait()
			script.Parent.CurrentBlowerSpeed.Value = script.Parent.CurrentBlowerSpeed.Value + 0.06
			script.Parent.Parent.BeltSet.SoundMain.PlaybackSpeed = script.Parent.CurrentBlowerSpeed.Value
			Rotate()
		until script.Parent.CurrentBlowerSpeed.Value >= 1 or script.Parent.BlowerOn.Value == false
		script.Parent.CurrentBlowerSpeed.Value = 1
		script.Parent.Parent.BeltSet.SoundMain.PlaybackSpeed = script.Parent.CurrentBlowerSpeed.Value
		Rotate()
	else
		RunCycle = false
		repeat game:GetService("RunService").Heartbeat:wait()
			script.Parent.CurrentBlowerSpeed.Value = script.Parent.CurrentBlowerSpeed.Value - 0.0032
			script.Parent.Parent.BeltSet.SoundMain.PlaybackSpeed = script.Parent.CurrentBlowerSpeed.Value
			Rotate()
		until script.Parent.CurrentBlowerSpeed.Value <= 0 or RunCycle == true
		if script.Parent.CurrentBlowerSpeed.Value <= 0 then
			script.Parent.CurrentBlowerSpeed.Value = 0
			script.Parent.Parent.BeltSet.SoundMain:Stop()
			PlayAudio = false
			CompleteStop = true
			script.Parent.Parent.BeltSet.SoundMain.Volume = 0
			script.Parent.Parent.BeltSet.SoundMain.PlaybackSpeed = script.Parent.CurrentBlowerSpeed.Value
			Rotate()
		end
	end
end)

script.Parent.BlowerOn.Changed:Connect(function()
	if script.Parent.BlowerOn.Value == true then
		script.Parent.Parent.BeltSet.SoundMain.Volume = 0.5
		script.Parent.Parent.BeltSet.MotorStart:Play()
	else
		script.Parent.Parent.BeltSet.MotorStart:Stop()
	end
end)

script.Parent.Parent.BeltSet.Sound.Changed:Connect(function()
	script.Parent.SpeedInvert.Value = script.Parent.Parent.BeltSet.SoundMain.PlaybackSpeed * -1
	script.Parent.Parent.BeltSet.MotorStart.Volume = script.Parent.SpeedInvert.Value + 1
end)


SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.On.Changed:Connect(function()
	if SirenAddress.SirenHeadMovingPartsMain.Sound.SoundMain.On.Value == true and script.Parent.BlowerOn.Value == false then
		script.Parent.Parent.BeltSet.Sound.PlaybackSpeed = 0.68
	end
end)