From 7342126dd035155d012546dc68dae6a9ee7c82c8 Mon Sep 17 00:00:00 2001 From: shadowcz007 Date: Sun, 9 Jun 2024 17:43:44 +0800 Subject: [PATCH] Update sound-lab.js --- web/javascript/sound-lab.js | 55 +++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/web/javascript/sound-lab.js b/web/javascript/sound-lab.js index d34310a..1c66a7d 100644 --- a/web/javascript/sound-lab.js +++ b/web/javascript/sound-lab.js @@ -67,18 +67,45 @@ const createWaveSurfer = (wavesurfer, id) => { barRadius: 6 }) - wavesurfer._auto=true + wavesurfer._auto = true // 监听播放结束事件,重新开始播放以实现循环播放 wavesurfer.on('finish', function () { - if(wavesurfer._auto) wavesurfer.play() + if (wavesurfer._auto) wavesurfer.play() }) wavesurfer.on('interaction', () => { - wavesurfer._auto=false + wavesurfer._auto = false if (!wavesurfer.isPlaying()) wavesurfer.play() }) + // 获取当前播放时间的峰值 + wavesurfer.on('audioprocess', () => { + if (wavesurfer.isPlaying()) { + const channelData = wavesurfer.getDecodedData().getChannelData(0); + const currentTime = wavesurfer.getCurrentTime() + // console.log(wavesurfer) + const sampleRate = wavesurfer.getDecodedData().sampleRate + + // 定义要分析的时间窗口(例如1秒) + const windowSize = 1 + const startSample = Math.floor(currentTime * sampleRate) + const endSample = Math.min( + startSample + windowSize * sampleRate, + channelData.length + ) + + let peak = 0 + for (let i = startSample; i < endSample; i++) { + const value = Math.abs(channelData[i]) + if (value > peak) { + peak = value + } + } + // console.log('Current Peak:', peak) + } + }) + return wavesurfer } @@ -86,28 +113,29 @@ const createWaveSurfer = (wavesurfer, id) => { function updateWaveWidgetValue (widgets, id, url, prompt, wavesurfer) { let widget = widgets.filter(w => w.name == 'AudioPlay')[0] // 手动更新widget值 - widget.value = [url, prompt]; + widget.value = [url, prompt] - if (widget.div) { - widget.div.querySelector('.wave').id = `AudioPlay_${id}` + if (widget.div) { + widget.div.querySelector('.wave').id = `AudioPlay_${id}` } wavesurfer = createWaveSurfer(wavesurfer, `AudioPlay_${id}`) - wavesurfer.on('ready', (duration) => { + wavesurfer.on('ready', duration => { console.log('Audio duration: ' + duration + ' seconds') if (widget.div) { - widget.div.setAttribute('data-url', url) + widget.div.setAttribute('data-url', url) widget.div.querySelector('.link').setAttribute('href', url) widget.div.querySelector( '.info' ).innerHTML = `${duration.toFixed(2)} seconds
${prompt}
` + margin: 8px;">${duration.toFixed( + 2 + )} seconds
${prompt}
` } }) - wavesurfer.load(url) return wavesurfer } @@ -178,11 +206,10 @@ app.registerExtension({ playBtn.addEventListener('click', e => { e.preventDefault() - if(that[`wavesurfer_${this.id}`]){ + if (that[`wavesurfer_${this.id}`]) { that[`wavesurfer_${this.id}`]?.playPause() - that[`wavesurfer_${this.id}`]._auto=true; + that[`wavesurfer_${this.id}`]._auto = true } - }) btns.appendChild(playBtn) @@ -250,7 +277,7 @@ app.registerExtension({ node.id, url, prompt, - this[`wavesurfer_${node.id}`] + this[`wavesurfer_${node.id}`] ) }