-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
export const worker = ` | ||
const DEFAULT_BUFFER_SIZE = 2048; | ||
const DEFAULT_SAMPLE_RATE = 16000; | ||
function encode(buffer, sampleRate, targetSampleRate) { | ||
if (sampleRate > targetSampleRate) { | ||
throw new Error('downsampling rate show be smaller than original sample rate'); | ||
} | ||
const sampleRateRatio = sampleRate / targetSampleRate; | ||
const newLength = Math.round(buffer.length / sampleRateRatio); | ||
const result = new Int16Array(newLength); | ||
let offsetResult = 0; | ||
let offsetBuffer = 0; | ||
while (offsetResult < result.length) { | ||
const nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio); | ||
let accum = 0; | ||
let count = 0; | ||
for (let i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) { | ||
accum += buffer[i]; | ||
count++; | ||
} | ||
result[offsetResult] = Math.min(1, accum / count) * 0x7fff; | ||
offsetResult++; | ||
offsetBuffer = nextOffsetBuffer; | ||
} | ||
return result.buffer; | ||
} | ||
class PcmWorkletProcessor extends AudioWorkletProcessor { | ||
_bufferSize = DEFAULT_BUFFER_SIZE; | ||
_bytesWritten = 0; | ||
_buffer = new Float32Array(this._bufferSize); | ||
_sampleRate = DEFAULT_SAMPLE_RATE; | ||
_targetSampleRate = DEFAULT_SAMPLE_RATE; | ||
constructor(options) { | ||
super(); | ||
this._bufferSize = options.bufferSize || DEFAULT_BUFFER_SIZE; | ||
this._buffer = new Float32Array(this._bufferSize); | ||
this._sampleRate = options.sampleRate || DEFAULT_SAMPLE_RATE; | ||
this._targetSampleRate = options.targetSampleRate || DEFAULT_SAMPLE_RATE; | ||
} | ||
append(channelData) { | ||
if (!channelData) { | ||
return; | ||
} | ||
for (let i = 0; i < channelData.length; i++) { | ||
this._buffer[this._bytesWritten++] = channelData[i]; | ||
if (this._bytesWritten >= this._bufferSize) { | ||
this.push(); | ||
} | ||
} | ||
} | ||
process (inputs, outputs, parameters) { | ||
this.append(inputs[0][0]); | ||
return true; | ||
} | ||
push() { | ||
this.port.postMessage( | ||
encode( | ||
this._bytesWritten < this.bufferSize | ||
? this._buffer.slice(0, this._bytesWritten) | ||
: this._buffer, | ||
this._sampleRate, | ||
this._targetSampleRate) | ||
); | ||
this._bytesWritten = 0; | ||
} | ||
} | ||
registerProcessor('pcm-processor', PcmWorkletProcessor); | ||
`; |