From 0eef994cfa75e95f2fd731a9c94f0f7899f25c57 Mon Sep 17 00:00:00 2001 From: pengzhendong <275331498@qq.com> Date: Thu, 9 Jan 2025 10:43:20 +0800 Subject: [PATCH] use int16 --- wavesurfer/js/pcm-player.js | 18 +++++++++--------- wavesurfer/wavesurfer.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/wavesurfer/js/pcm-player.js b/wavesurfer/js/pcm-player.js index 9859846..2a367ec 100644 --- a/wavesurfer/js/pcm-player.js +++ b/wavesurfer/js/pcm-player.js @@ -5,8 +5,8 @@ this.option = Object.assign({}, {channels: 1, sampleRate: 16000, flushTime: 100}, option) // 每隔 flushTime 毫秒调用一次 flush 函数 this.interval = setInterval(this.flush.bind(this), this.option.flushTime) - this.samples = new Float32Array() - this.all_samples = new Float32Array() + this.samples = new Int16Array() + this.all_samples = new Int16Array() this.url this.audioCtx = new (window.AudioContext || window.webkitAudioContext)() @@ -23,12 +23,12 @@ for (let i = 0; i < binaryString.length; i++) { bufferView[i] = binaryString.charCodeAt(i); } - const data = Float32Array.from(new Int16Array(buffer)).map(item => item / 32768) - this.samples = new Float32Array([...this.samples, ...data]) - this.all_samples = new Float32Array([...this.all_samples, ...data]) + const data = new Int16Array(buffer) + this.samples = new Int16Array([...this.samples, ...data]) + this.all_samples = new Int16Array([...this.all_samples, ...data]) const wavBytes = getWavBytes(this.all_samples.buffer, { - isFloat: true, + isFloat: false, numChannels: this.option.channels, sampleRate: this.option.sampleRate, }) @@ -44,7 +44,7 @@ const audioData = audioBuffer.getChannelData(channel) let offset = channel for (let i = 0; i < length; i++) { - audioData[i] = this.samples[offset] + audioData[i] = this.samples[offset] / 32768 offset += this.option.channels } } @@ -54,7 +54,7 @@ bufferSource.connect(this.gainNode) bufferSource.start(this.startTime) this.startTime += audioBuffer.duration - this.samples = new Float32Array() + this.samples = new Int16Array() } async continue() { @@ -125,7 +125,7 @@ function getWavHeader(options) { } function getWavBytes(buffer, options) { - const type = options.isFloat ? Float32Array : Uint16Array + const type = options.isFloat ? Float32Array : Int16Array const numFrames = buffer.byteLength / type.BYTES_PER_ELEMENT const headerBytes = getWavHeader(Object.assign({}, options, { numFrames })) const wavBytes = new Uint8Array(headerBytes.length + buffer.byteLength); diff --git a/wavesurfer/wavesurfer.py b/wavesurfer/wavesurfer.py index 361e7b0..bea9c0f 100644 --- a/wavesurfer/wavesurfer.py +++ b/wavesurfer/wavesurfer.py @@ -63,7 +63,7 @@ def encode(data, rate=None, with_header=True): scaled, nchan = Audio._validate_and_normalize_with_numpy(data, False) return base64.b64encode(scaled).decode("ascii") - def display_audio(self, audio, rate: int = None, verbose: bool = False, **kwargs): + def display_audio(self, audio, rate, verbose: bool = False, **kwargs): """ Render audio data and return the rendered result.