Web PCM 音频播放器
在浏览器中播放 PCM 音频的软件包 。 签名的16位英寸和32位浮标编码都得到了支持.
玩家需要给定 AudioContext 在内部播放音频。 它不会管理/改变音频上下文的生命周期(这留给消费者).
安装
国家预防机制
这个软件包可以从国家预防机制安装
npm i @speechmatics/web-pcm-player
导入地图
您也可以从 UNPKG 获取代码, 用于导入地图 :
<script type="importmap">
{
"imports": {
"@speechmatics/web-pcm-player": "https://unpkg.com/@speechmatics/[email protected]/dist/index.min.js"
}
}
</script>
使用量
此库可用于播放来自下列任一方的PCM数据:
Int16Array用于16位无符号整数PCM或Float32Array. 32 位浮点数 PCM
这些格式对应 pcm_s16le 财务报告和审定财务报表 pcm_f32le 如本报告所述 这里的 PCM 格式 FAMPEG 指南。如果您想要添加更多的PCM格式,请随意提出一个问题.
Base64 音频示例
下面是游戏的用法示例 pcm_s16le 作为64号基地提供的音频.
对于完整的示例,请打开文件 examples/example.html 在浏览器中.
import { PCMPlayer } from "@speechmatics/web-pcm-player";
async function playTestAudio() {
const audioContext = new AudioContext({ sampleRate: 16_000 });
await audioContext.resume();
const player = new PCMPlayer(audioContext);
for (const audioChunk of base64Data) {
const buffer = Uint8Array.from(atob(audioChunk), (c) =>
c.charCodeAt(0)
);
const data = new Int16Array(buffer.buffer);
player.playAudio(data);
}
}
window.onload = () => {
document.getElementById("play-button").addEventListener("click", () => {
playTestAudio();
});
};
const base64Data = [
// See data in the examples/ folder
];