Problem with playing sound in Safari using JS and AudioContext

My game has background music and separate sounds, I play all of this differently. The problem is that the looped background music disappears at some point, some sounds play with a defect.

Here is my JS code to perform these functions

const allSounds   = []
let allSoundsName = [
    //names of sounds
]


function preloadSounds() {
    allSoundsName.forEach(soundName => {
        let audio = new Audio(`./assets/audio/${soundName}.mp3`)

        allSounds.push({
            name: soundName,
            audio: audio,
        })
    })
}

function playAudio({
                       audioName,
                       isLoop,
                       startTime = 0,
                       endTime = null,
                       volume = 1,
                       delay = 0,
                       decrease = 0,
                       duration,
                   }) {
    if (!audioCtx) {
        audioCtx               = new (window.AudioContext || window.webkitAudioContext)();
        audioCtx.onstatechange = () => {
            if (audioCtx.state === 'suspended') {
                console.log('suspended')
                audioCtx.resume();
            }
        };
        addAllSoundGain()
    }
    let sound = getSound(audioName)

    setVolume(sound, off ? 0 : volume)
    if (volume !== 1) addVolumes(audioName, volume)

    sound.audio.currentTime = startTime

    if (endTime) {
        setTime(sound.audio, startTime, endTime, isLoop)

    } else if (isLoop) {
        setLoop(sound.audio, startTime)
    }

    setTimeout(() => {
        sound.audio.play()
        setDurationAndDecrease(sound.audio, audioName, duration, decrease)

    }, delay)

    return sound.audio
}
function addAllSoundGain() {
    allSounds.forEach(sound => {
        let source   = audioCtx.createMediaElementSource(sound.audio);
        let gainNode = audioCtx.createGain();
        source.connect(gainNode);
        gainNode.connect(audioCtx.destination);
        sound.source   = source
        sound.gainNode = gainNode
    })
}
function setDurationAndDecrease(audio, audioName, duration, decrease) {
    if (duration) setTimeout(() => pauseAudio({audioName, decrease}), duration)
}

function setLoop(audio, startTime) {
    audio.onended = () => {
        audio.currentTime = startTime
        audio.play()
    }
}

function setTime(audio, startTime, endTime, isLoop) {
    audio.ontimeupdate = () => {

        if (audio.currentTime >= endTime && isLoop) {
            audio.currentTime = startTime
        } else if (audio.currentTime >= endTime) {
            audio.pause()
        }
    }

}

I don’t know what to do, because everything is fine in Chrome, this problem only occurs in Safari Mac