local function Sound(player)
    local vehicle = player:getVehicle()
    if (vehicle and GetPanzerType(vehicle)) then
        local type = GetPanzerType(vehicle)
        local TrackSoundType = PanzerList[type].TrackSoundType
        local CrewLaungage = PanzerList[type].CrewLaungage
        if player:getModData().CrewLaungage then
            CrewLaungage = player:getModData().CrewLaungage
        else
            -- player:getModData().CrewLaungage = "IWS"
        end
        if not TrackSoundType then
            return
        end

        local SoundPlayer = vehicle:getEmitter()
        local engineSpeedValue = 0;
        if vehicle:isEngineRunning() then
            engineSpeedValue = math.max(0, math.min(1, (vehicle:getEngineSpeed() - 1000) / (7000 - 1000)));
            local modData = vehicle:getModData()
            local RealSpeed = vehicle:getSpeed2D()
            local EngineSpeed = math.floor(engineSpeedValue * 100) / 10
            local SoundData = vehicle:getModData().SoundData
            if SoundData == nil then
                vehicle:getModData().SoundData = {
                    EngineInt = nil,
                    TrackInt = nil,
                    CrewSoundTick = 600
                }
            end
            if SoundData.CrewSoundTick == nil then
                SoundData.CrewSoundTick = 600
            end
            if SoundData.CrewSoundTick >= 0 then
                SoundData.CrewSoundTick = SoundData.CrewSoundTick - 1
            else
                SoundData.CrewSoundTick = 600
                if modData.CrewLeaderShutUp then
                    if EngineSpeed > 0 and EngineSpeed <= 3 then
                        SoundPlayer:playSound(CrewLaungage .. "Panzer_Crew_Start", false)
                    elseif EngineSpeed > 3 and EngineSpeed <= 5 then
                        SoundPlayer:playSound(CrewLaungage .. "Panzer_Crew_SpeedUp", false)
                    elseif EngineSpeed > 5 then
                        SoundPlayer:playSound(CrewLaungage .. "Panzer_Crew_FullSpeed", false)
                    end
                end
            end
            if TrackSoundType then
                if RealSpeed <= 1 then
                    if SoundData.TrackInt ~= nil and SoundData.TrackSpeedBefore ~= "0" and TrackSoundType then
                        SoundPlayer:stopSound(SoundData.TrackInt)
                        SoundData.TrackInt = nil
                        SoundData.TrackSpeedBefore = "None"
                    end
                    if SoundData.TrackSpeedBefore ~= "0" or SoundData.TrackInt == nil and TrackSoundType then
                        SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Idle", true)
                        SoundData.TrackSpeedBefore = "0"
                    end
                    if SoundData.TrackSpeedBefore == "0" and SoundData.TrackInt ~= nil then
                        if not SoundPlayer:isPlaying(SoundData.TrackInt) then
                            SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Idle", true)
                        end
                    end
                elseif RealSpeed > 1 and RealSpeed <= 10 then
                    if SoundData.TrackInt ~= nil and SoundData.TrackSpeedBefore ~= "1" and TrackSoundType then
                        SoundPlayer:stopSound(SoundData.TrackInt)
                        SoundData.TrackInt = nil
                        SoundData.TrackSpeedBefore = "None"
                    end
                    if SoundData.TrackSpeedBefore ~= "1" or SoundData.TrackInt == nil and TrackSoundType then
                        SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_1", true)
                        SoundData.TrackSpeedBefore = "1"
                    end
                    if SoundData.TrackSpeedBefore == "1" and SoundData.TrackInt ~= nil then
                        if not SoundPlayer:isPlaying(SoundData.TrackInt) then
                            SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_1", true)
                        end
                    end
                elseif RealSpeed > 10 and RealSpeed <= 20 then
                    if SoundData.TrackInt ~= nil and SoundData.TrackSpeedBefore ~= "2" and TrackSoundType then
                        SoundPlayer:stopSound(SoundData.TrackInt)
                        SoundData.TrackInt = nil
                        SoundData.TrackSpeedBefore = "None"
                    end
                    if SoundData.TrackSpeedBefore ~= "2" or SoundData.TrackInt == nil and TrackSoundType then
                        SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_2", true)
                        SoundData.TrackSpeedBefore = "2"
                    end
                    if SoundData.TrackSpeedBefore == "2" and SoundData.TrackInt ~= nil then
                        if not SoundPlayer:isPlaying(SoundData.TrackInt) then
                            SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_2", true)
                        end
                    end
                elseif RealSpeed > 20 and RealSpeed <= 30 then
                    if SoundData.TrackInt ~= nil and SoundData.TrackSpeedBefore ~= "3" and TrackSoundType then
                        SoundPlayer:stopSound(SoundData.TrackInt)
                        SoundData.TrackInt = nil
                        SoundData.TrackSpeedBefore = "None"
                    end
                    if SoundData.TrackSpeedBefore ~= "3" or SoundData.TrackInt == nil and TrackSoundType then
                        SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_3", true)
                        SoundData.TrackSpeedBefore = "3"
                    end
                    if SoundData.TrackSpeedBefore == "3" and SoundData.TrackInt ~= nil then
                        if not SoundPlayer:isPlaying(SoundData.TrackInt) then
                            SoundData.TrackInt = SoundPlayer:playSound(TrackSoundType .. "_Stage_3", true)
                        end
                    end
                end
            end
            if EngineSpeed == 0 then
                if SoundData.EngineInt ~= nil and SoundData.EngineSpeedBefore ~= "0" then
                    SoundPlayer:stopSound(SoundData.EngineInt)
                    SoundData.EngineInt = nil
                    SoundData.EngineSpeedBefore = "None"
                end
                if SoundData.EngineSpeedBefore ~= "0" or SoundData.EngineInt == nil then
                    SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_Idle", true)

                    SoundData.EngineSpeedBefore = "0"
                end
                if SoundData.EngineSpeedBefore == "0" and SoundData.EngineInt ~= nil then
                    if not SoundPlayer:isPlaying(SoundData.EngineInt) then
                        SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_Idle", true)
                    end
                end
            elseif EngineSpeed > 0 and EngineSpeed <= 3 then
                if SoundData.EngineInt ~= nil and SoundData.EngineSpeedBefore ~= "1" then
                    SoundPlayer:stopSound(SoundData.EngineInt)
                    SoundData.EngineInt = nil
                    SoundData.EngineSpeedBefore = "None"
                end
                if SoundData.EngineSpeedBefore ~= "1" or SoundData.EngineInt == nil then
                    SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_1", true)
                    SoundData.EngineSpeedBefore = "1"
                end
                if SoundData.EngineSpeedBefore == "1" and SoundData.EngineInt ~= nil then
                    if not SoundPlayer:isPlaying(SoundData.EngineInt) then
                        SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_1", true)
                    end
                end
            elseif EngineSpeed > 3 and EngineSpeed <= 5 then
                if SoundData.EngineInt ~= nil and SoundData.EngineSpeedBefore ~= "2" then
                    SoundPlayer:stopSound(SoundData.EngineInt)

                    SoundData.EngineInt = nil
                    SoundData.EngineSpeedBefore = "None"
                end
                if SoundData.EngineSpeedBefore ~= "2" or SoundData.EngineInt == nil then
                    SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_2", true)

                    SoundData.EngineSpeedBefore = "2"
                end
                if SoundData.EngineSpeedBefore == "2" and SoundData.EngineInt ~= nil then
                    if not SoundPlayer:isPlaying(SoundData.EngineInt) then
                        SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_2", true)

                    end
                end
            elseif EngineSpeed > 5 then
                if SoundData.EngineInt ~= nil and SoundData.EngineSpeedBefore ~= "3" then
                    SoundPlayer:stopSound(SoundData.EngineInt)

                    SoundData.EngineInt = nil
                    SoundData.EngineSpeedBefore = "None"
                end
                if SoundData.EngineSpeedBefore ~= "3" or SoundData.EngineInt == nil then
                    SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_3", true)

                    SoundData.EngineSpeedBefore = "3"
                end
                if SoundData.EngineSpeedBefore == "3" and SoundData.EngineInt ~= nil then
                    if not SoundPlayer:isPlaying(SoundData.EngineInt) then
                        SoundData.EngineInt = SoundPlayer:playSound(type .. "_Engine_Stage_3", true)

                    end
                end
            end
        else
            local SoundData = vehicle:getModData().SoundData
            if SoundData == nil then
                vehicle:getModData().SoundData = {
                    EngineInt = nil,
                    EngineSpeedBefore = "None",
                    TrackInt = nil,
                    TrackSpeedBefore = "None"
                }
            end
            if SoundData and SoundData.EngineInt ~= nil then
                SoundPlayer:stopSound(SoundData.EngineInt)
                SoundData.EngineInt = nil
                SoundData.EngineSpeedBefore = "None"
                SoundData.TrackInt = nil
                SoundData.TrackSpeedBefore = "None"
            end
        end
        vehicle:update()
    end
end

Events.OnPlayerUpdate.Add(Sound)
