From 7f3ab31ad5cbd4e69815ef389897e3956e706ae8 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Fri, 26 Jul 2024 18:50:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=E6=9F=90?= =?UTF-8?q?=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B=E6=92=AD=E6=94=BE=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E6=97=B6=EF=BC=8C=E5=A4=84=E4=BA=8E=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=BD=86=E6=98=AF=E8=BF=9B=E5=BA=A6=E6=9D=A1?= =?UTF-8?q?=E4=B8=8D=E8=B5=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/renderer/plugins/player/index.ts | 46 +++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index c71aec6e..fe7f7dc5 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -12,6 +12,7 @@ - 修复 MacOS 下点击 dock 右键菜单的退出按钮时,程序没有退出的问题(#1923) - 修复 OpenAPI 的 `lyricLineAllText` 在切换到无歌词的音乐时内容没有更新的问题(#1925) - 修复切换音源时可能出现切换死循环的问题 +- 尝试修复某些情况下播放音频时,处于播放状态但是进度条不走的问题 ### 变更 diff --git a/src/renderer/plugins/player/index.ts b/src/renderer/plugins/player/index.ts index 7ee404fa..5fc4d9d0 100644 --- a/src/renderer/plugins/player/index.ts +++ b/src/renderer/plugins/player/index.ts @@ -51,7 +51,7 @@ let convolverDynamicsCompressor: DynamicsCompressorNode let gainNode: GainNode let panner: PannerNode let pitchShifterNode: AudioWorkletNode -let pitchShifterNodePitchFactor: AudioParam +let pitchShifterNodePitchFactor: AudioParam | null let pitchShifterNodeLoadStatus: 'none' | 'loading' | 'unconnect' | 'connected' = 'none' let pitchShifterNodeTempValue = 1 let defaultChannelCount = 2 @@ -119,7 +119,7 @@ const initAdvancedAudioFeatures = () => { initConvolver() initPanner() initGain() - // source -> analyser -> biquadFilter -> [(convolver & convolverSource)->convolverDynamicsCompressor] -> panner -> gain + // source -> analyser -> biquadFilter -> pitchShifter -> [(convolver & convolverSource)->convolverDynamicsCompressor] -> panner -> gain mediaSource = audioContext.createMediaElementSource(audio) mediaSource.connect(analyser) analyser.connect(biquads.get(`hz${freqs[0]}`)!) @@ -129,8 +129,43 @@ const initAdvancedAudioFeatures = () => { convolverDynamicsCompressor.connect(panner) panner.connect(gainNode) gainNode.connect(audioContext.destination) + + // 音频输出设备改变时刷新 audio node 连接 + window.app_event.on('playerDeviceChanged', handleMediaListChange) + + // audio.addEventListener('playing', connectAudioNode) + // audio.addEventListener('pause', disconnectAudioNode) + // audio.addEventListener('waiting', disconnectAudioNode) + // audio.addEventListener('emptied', disconnectAudioNode) + // if (!audio.paused) connectAudioNode() } +const handleMediaListChange = () => { + mediaSource.disconnect() + mediaSource.connect(analyser) +} + +// let isConnected = true +// const connectAudioNode = () => { +// if (isConnected) return +// console.log('connect Node') +// mediaSource.connect(analyser) +// isConnected = true +// if (pitchShifterNodeTempValue == 1 && pitchShifterNodeLoadStatus == 'connected') { +// disconnectPitchShifterNode() +// } +// } + +// const disconnectAudioNode = () => { +// if (!isConnected) return +// console.log('disconnect Node') +// mediaSource.disconnect() +// isConnected = false +// if (pitchShifterNodeTempValue == 1 && pitchShifterNodeLoadStatus == 'connected') { +// disconnectPitchShifterNode() +// } +// } + export const getAudioContext = () => { initAdvancedAudioFeatures() return audioContext @@ -140,8 +175,8 @@ let unsubMediaListChangeEvent: (() => void) | null = null export const setMaxOutputChannelCount = (enable: boolean) => { if (enable) { initAdvancedAudioFeatures() - audioContext.destination.channelCount = audioContext.destination.maxChannelCount audioContext.destination.channelCountMode = 'max' + audioContext.destination.channelCount = audioContext.destination.maxChannelCount // navigator.mediaDevices.addEventListener('devicechange', handleMediaListChange) if (!unsubMediaListChangeEvent) { let handleMediaListChange = () => { @@ -287,7 +322,7 @@ const connectPitchShifterNode = () => { // convolverDynamicsCompressor.connect(pitchShifterNode) // pitchShifterNode.connect(panner) pitchShifterNodeLoadStatus = 'connected' - pitchShifterNodePitchFactor.value = pitchShifterNodeTempValue + pitchShifterNodePitchFactor!.value = pitchShifterNodeTempValue } const disconnectPitchShifterNode = () => { console.log('disconnect Pitch Shifter Node') @@ -296,6 +331,7 @@ const disconnectPitchShifterNode = () => { lastBiquadFilter.connect(convolver) lastBiquadFilter.connect(convolverSourceGainNode) pitchShifterNodeLoadStatus = 'unconnect' + pitchShifterNodePitchFactor = null audio!.removeEventListener('playing', connectNode) audio!.removeEventListener('pause', disconnectNode) @@ -337,7 +373,7 @@ export const setPitchShifter = (val: number) => { case 'connected': // a: 1 = 半音 // value = 2 ** (a / 12) - pitchShifterNodePitchFactor.value = val + pitchShifterNodePitchFactor!.value = val break case 'unconnect': connectPitchShifterNode()