/* eslint-disable */
function QG_JS_Sound_Create_Channel(o, i) {
    if (0 != WEBAudio.audioWebEnabled && window.qg) {
        var e = function () {
                if (GameGlobal.unityNamespace.Module.dynCall) {
                    o && GameGlobal.unityNamespace.Module.dynCall("vi", o, [i])
                }
                if (GameGlobal.unityNamespace.Module.dynCall_vi) {
                    o && GameGlobal.unityNamespace.Module.dynCall_vi(o, [i])
                }
            },
            u = {
                audio: null,
                loop: !1,
                volume: 1,
                isBind: !1,
                isStop: !1,
                stopTime: 0,
                isPlay: !1,
                playTime: 0,
                isStopped: !1,
                playAudio: function (o, i) {
                    //每次playAudio时都去创建新的context实例，解决同一实例无法连续播放问题
                    if (this.audio) {
                        const audioTemp = this.audio
                        this.audio = window.qg.createInnerAudioContext()
                        this.audio.src = audioTemp.src
                        this.audio.onCanplay = audioTemp.onCanplay
                        this.audio.onError = audioTemp.onError
                        this.audio.volume = audioTemp.volume
                        this.audio.loop = audioTemp.loop
                    }
                    this.setup(), o > 0 ? (this.audio.volume = 0, this.isPlay = !0, this.playTime = o) : (this.audio.volume = this.volume, this.audio.loop = this.loop), this.audio.play(), 0 != i && this.audio.seek(i)
                },
                setup: function () {
                    if (this.audio && 0 == this.isBind) {
                        this.audio.offEnded(e), this.audio.onEnded(e);
                        var o = this;
                        this.isStoped = !1, this.audio.offTimeUpdate(null), this.audio.onTimeUpdate(function () {
                            1 == o.isStop && Number.parseFloat(o.audio.currentTime) >= o.stopTime && (o.stopTime = 0, o.isStop = !1, o.audio.stop(), o.audio.destroy(), o.audio.src = "", o.isBind = !1, o.isStoped = !0), !o.isStoped && 1 == o.isPlay && Number.parseFloat(o.audio.currentTime) >= o.playTime && (o.isPlay = !1, o.playTime = 0, o.audio.volume = o.volume, o.audio.loop = o.loop)
                        }), this.isBind = !0
                    }
                },
                stopAudio: function (o) {
                    0 == o ? (this.audio.offTimeUpdate(null), this.audio.offEnded(e), this.stopTime = 0, this.isStop = !1, this.audio.pause(), this.isBind = !1) : (this.stopTime = o, this.isStop = !0)
                }
            };
        return WEBAudio.audioInstances.push(u) - 1
    }
}

function QG_JS_Sound_GetLength(o) {
    if (0 == WEBAudio.audioWebEnabled) return 0;
    var i = WEBAudio.audioInstances[o];
    return i ? window.qg ? i.bufferLength : void 0 : 0
}

function QG_JS_Sound_GetLoadState(o) {
    if (0 == WEBAudio.audioWebEnabled) return 2;
    if (window.qg) {
        var i = WEBAudio.audioInstances[o],
            e = i.loadState;
        return 1 == e ? 2 : 2 == e ? 0 : 1
    }
}

function QG_JS_Sound_Init() {
    window.qg && (WEBAudio.audioWebEnabled = 1)
}

function QG_JS_Sound_Load(o, i) {
    if (0 == WEBAudio.audioWebEnabled) return 0;
    if (window.qg) {
        var e = {
                buffer: null,
                error: !1,
                file: "",
                audio: window.qg.createInnerAudioContext(),
                bufferLength: 0,
                ptr: o,
                loadState: 0
            },
            u = window.qg.getFileSystemManager(),
            t = WEBAudio.audioInstances.push(e) - 1;
        e.file = window.qg.env.USER_DATA_PATH + "/audio" + o + i + ".mp3", console.log("_JS_Sound_Load " + e.file), e.buffer = GameGlobal.unityNamespace.Module.HEAPU8.buffer.slice(o, o + i)
        // 兼容微信新标准
        let needWrite = true
        try {
            const exsit = u.accessSync(e.file)
            if (exsit === undefined || exsit) {
                needWrite = false
            }
        } catch(e) {
        }
        if (needWrite) {
            u.writeFileSync(e.file, e.buffer, "binary")
        }
        return e.audio.onCanplay(function () {
            e.bufferLength = 44100 * e.audio.duration | 0, e.loadState = 2
        }), e.audio.onError(function (o) {
            console.log(o), e.loadState = 1
        }), e.audio.src = e.file, t
    }
}

function QG_JS_Sound_Load_PCM(o, i, e, u) {
    if (0 == WEBAudio.audioWebEnabled) return 0;
    for (var t = {
            buffer: WEBAudio.audioContext.createBuffer(o, i, e),
            error: !1
        }, n = 0; n < o; n++) {
        var d = (u >> 2) + i * n,
            a = t.buffer,
            s = a.copyToChannel || function (o, i, e) {
                var u = o.subarray(0, Math.min(o.length, this.length - (0 | e)));
                this.getChannelData(0 | i).set(u, 0 | e)
            };
        s.apply(a, [GameGlobal.unityNamespace.Module.HEAPF32.subarray(d, d + i), n, 0])
    }
    var r = WEBAudio.audioInstances.push(t) - 1;
    return r
}

function QG_JS_Sound_Play(o, i, e, u) {
    if (window.qg) {
        // if (QG_JS_Sound_Stop(i, 0), console.log("sound play", o, i, e, u), 0 == WEBAudio.audioWebEnabled) return;
        var t = WEBAudio.audioInstances[o],
            n = WEBAudio.audioInstances[i];
        if (t.buffer) try {
            if (console.log("play", t.file), "" !== t.audio.src) n.audio = t.audio, n.audio.volume = n.volume, n.audio.loop = n.loop, n.playAudio(Number.parseFloat(t.audio.currentTime) + u, e);
            else {
                t.loadState = 0, t.audio = window.qg.createInnerAudioContext(), t.audio.onCanplay(function () {
                    t.bufferLength = 44100 * t.audio.duration | 0, t.loadState = 2
                }), t.audio.onError(function (o) {
                    console.log(o), t.loadState = 1
                }), t.audio.src = t.file, n.audio = t.audio, n.audio.volume = n.volume, n.audio.loop = n.loop;
                var d = t.loadState;
                if (0 == d) var a = setInterval(function () {
                    0 !== t.loadState && (2 == t.loadState && n.playAudio(Number.parseFloat(t.audio.currentTime) + u, e), clearInterval(a))
                }, 100);
                else 2 == d && n.playAudio(Number.parseFloat(t.audio.currentTime) + u, e)
            }
        } catch (o) {
            console.error("playBuffer error. Exception: " + o)
        } else console.log("Trying to play sound which is not loaded.")
    }
}

function QG_JS_Sound_ReleaseInstance(o) {
    WEBAudio.audioInstances[o] = null
}

function QG_JS_Sound_ResumeIfNeeded() {
    0 != WEBAudio.audioWebEnabled && (window.qg || "suspended" === WEBAudio.audioContext.state && WEBAudio.audioContext.resume())
}

function QG_JS_Sound_Set3D(o, i) {
    if (!window.qg) {
        var e = WEBAudio.audioInstances[o];
        e.threeD != i && (e.threeD = i, e.setupPanning())
    }
}

function QG_JS_Sound_SetListenerOrientation(o, i, e, u, t, n) {
    0 != WEBAudio.audioWebEnabled && (window.qg || (WEBAudio.audioContext.listener.forwardX ? (WEBAudio.audioContext.listener.forwardX.setValueAtTime(-o, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.forwardY.setValueAtTime(-i, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.forwardZ.setValueAtTime(-e, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.upX.setValueAtTime(u, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.upY.setValueAtTime(t, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.upZ.setValueAtTime(n, WEBAudio.audioContext.currentTime)) : WEBAudio.audioContext.listener.setOrientation(-o, -i, -e, u, t, n)))
}

function QG_JS_Sound_SetListenerPosition(o, i, e) {
    0 != WEBAudio.audioWebEnabled && (window.qg || (WEBAudio.audioContext.listener.positionX ? (WEBAudio.audioContext.listener.positionX.setValueAtTime(o, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.positionY.setValueAtTime(i, WEBAudio.audioContext.currentTime), WEBAudio.audioContext.listener.positionZ.setValueAtTime(e, WEBAudio.audioContext.currentTime)) : WEBAudio.audioContext.listener.setPosition(o, i, e)))
}

function QG_JS_Sound_SetLoop(o, i) {
    if (0 != WEBAudio.audioWebEnabled && window.qg) {
        var e = WEBAudio.audioInstances[o],
            u = 1 == i;
        e.loop = u, null !== e.audio && (e.audio.loop = u)
    }
}

function QG_JS_Sound_SetLoopPoints(o, i, e) {
    if (!window.qg) {
        if (0 == WEBAudio.audioWebEnabled) return;
        var u = WEBAudio.audioInstances[o];
        u.source.loopStart = i, u.source.loopEnd = e
    }
}

function QG_JS_Sound_SetPaused(a, d) {
    if (0 != WEBAudio.audioWebEnabled && window.qg) {
        var u = WEBAudio.audioInstances[a],
            i = !u.audio || u.audio.paused;
        d != i && (d ? u.audio.pause() : u.audio.play())
    }
}

function QG_JS_Sound_SetPitch(o, i) {
    0 != WEBAudio.audioWebEnabled && (window.qg || WEBAudio.audioInstances[o].source.playbackRate.setValueAtTime(i, WEBAudio.audioContext.currentTime))
}

function QG_JS_Sound_SetPosition(o, i, e, u) {
    0 != WEBAudio.audioWebEnabled && (window.qg || WEBAudio.audioInstances[o].panner.setPosition(i, e, u))
}

function QG_JS_Sound_SetVolume(o, i) {
    if (window.qg) {
        if (0 == WEBAudio.audioWebEnabled) return;
        if (i > 1) {
            i = 1;
        }
        var e = WEBAudio.audioInstances[o];
        e.volume = i, null !== e.audio && (e.audio.volume = i)
    }
}

function QG_JS_Sound_Stop(o, i) {
    if (window.qg) {
        if (0 == WEBAudio.audioWebEnabled) return;
        console.log("stop", o, i);
        var e = WEBAudio.audioInstances[o];
        e.audio && (console.log(o), 0 == i ? (e.stopAudio(i), e.audio.offEnded(null)) : e.stopAudio(Number.parseFloat(e.audio.currentTime) + i), e.audio = null)
    }
}
var WEBAudio = {
    audioInstanceIdCounter: 0,
    audioInstances: [],
    audioContext: null,
    audioWebEnabled: 0
};

window.WXWASMSDK._JS_Sound_Create_Channel = QG_JS_Sound_Create_Channel
window.WXWASMSDK._JS_Sound_GetLength = QG_JS_Sound_GetLength
window.WXWASMSDK._JS_Sound_GetLoadState = QG_JS_Sound_GetLoadState
window.WXWASMSDK._JS_Sound_Init = QG_JS_Sound_Init
window.WXWASMSDK._JS_Sound_Load = QG_JS_Sound_Load
window.WXWASMSDK._JS_Sound_Load_PCM = QG_JS_Sound_Load_PCM
window.WXWASMSDK._JS_Sound_Play = QG_JS_Sound_Play
window.WXWASMSDK._JS_Sound_ReleaseInstance = QG_JS_Sound_ReleaseInstance
window.WXWASMSDK._JS_Sound_ResumeIfNeeded = QG_JS_Sound_ResumeIfNeeded
window.WXWASMSDK._JS_Sound_Set3D = QG_JS_Sound_Set3D
window.WXWASMSDK._JS_Sound_SetListenerOrientation = QG_JS_Sound_SetListenerOrientation
window.WXWASMSDK._JS_Sound_SetListenerPosition = QG_JS_Sound_SetListenerPosition
window.WXWASMSDK._JS_Sound_SetLoop = QG_JS_Sound_SetLoop
window.WXWASMSDK._JS_Sound_SetLoopPoints = QG_JS_Sound_SetLoopPoints
window.WXWASMSDK._JS_Sound_SetPaused = QG_JS_Sound_SetPaused
window.WXWASMSDK._JS_Sound_SetPitch = QG_JS_Sound_SetPitch
window.WXWASMSDK._JS_Sound_SetPosition = QG_JS_Sound_SetPosition
window.WXWASMSDK._JS_Sound_SetVolume = QG_JS_Sound_SetVolume
window.WXWASMSDK._JS_Sound_Stop = QG_JS_Sound_Stop
