项目文件夹

文件
wehub-resource-sync adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:17:32 +08:00

60 行
1.6 KiB
TypeScript

export function audioBufferToWav(audioBuffer: AudioBuffer): Uint8Array {
const numOfChan = audioBuffer.numberOfChannels;
const length = audioBuffer.length * numOfChan * 2 + 44;
const buffer = new ArrayBuffer(length);
const view = new DataView(buffer);
let offset = 0;
// Write WAV header
const writeString = function (
view: DataView,
offset: number,
string: string
): void {
for (let i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
};
writeString(view, offset, "RIFF");
offset += 4;
view.setUint32(offset, length - 8, true);
offset += 4;
writeString(view, offset, "WAVE");
offset += 4;
writeString(view, offset, "fmt ");
offset += 4;
view.setUint32(offset, 16, true);
offset += 4; // Sub-chunk size, 16 for PCM
view.setUint16(offset, 1, true);
offset += 2; // PCM format
view.setUint16(offset, numOfChan, true);
offset += 2;
view.setUint32(offset, audioBuffer.sampleRate, true);
offset += 4;
view.setUint32(offset, audioBuffer.sampleRate * 2 * numOfChan, true);
offset += 4;
view.setUint16(offset, numOfChan * 2, true);
offset += 2;
view.setUint16(offset, 16, true);
offset += 2;
writeString(view, offset, "data");
offset += 4;
view.setUint32(offset, audioBuffer.length * numOfChan * 2, true);
offset += 4;
// Write PCM audio data
for (let i = 0; i < audioBuffer.length; i++) {
for (let channel = 0; channel < numOfChan; channel++) {
const sample = Math.max(
-1,
Math.min(1, audioBuffer.getChannelData(channel)[i])
);
view.setInt16(offset, sample * 0x7fff, true);
offset += 2;
}
}
return new Uint8Array(buffer);
}