Skip to content

Commit

Permalink
use int16
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhendong committed Jan 9, 2025
1 parent 2363e44 commit 0eef994
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions wavesurfer/js/pcm-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand All @@ -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,
})
Expand All @@ -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
}
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion wavesurfer/wavesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0eef994

Please sign in to comment.