capture.readAudio()
把麦克风采样写入 ByteSink。v0.1 使用交错、little-endian f32 PCM。
Syntax
const result = await host.capture.readAudio(microphone, output, options?);
Parameters
| 参数 | 类型 | 说明 |
|---|---|---|
microphone |
microphone-stream handle | requestMic() 返回的活动句柄 |
output |
ByteSink |
容量为 channels × 4 的正整数倍 |
options |
CallOptions |
可选取消信号 |
Return value
{
written: u32,
sampleRate: u32,
channels: 1 | 2,
format: "f32le",
frames: u32,
sequence: u64,
droppedFrames: u32
}
一次成功读取 1..min(output.capacity / (channels × 4), maxFramesPerRead) 个完整帧,并满足 written === frames × channels × 4。sequence 是返回块首个帧的零基绝对序号;droppedFrames 报告本块之前因有界缓冲溢出而新丢弃的帧数。容量不是正整数个完整帧时,在消费任何音频前以 sink-too-small 失败。
Examples
const output = new Uint8Array(16 * 1024);
const audio = await host.capture.readAudio(stream.microphone, output);
analyzer.push(output.subarray(0, audio.written), audio.sampleRate, audio.channels);
Errors
特有错误:stale-handle、resource-denied、sink-too-small、device-unavailable、busy、cancelled、limit-exceeded。
Requirements
- 授权:
handle-derived,需要 microphone-stream 的read权限 - 可取消:是;取消不关闭句柄
- 信任档位:yellow