The EncodedAudioChunk class represents a single chunk of encoded audio data. Unlike video, most audio codecs produce chunks that are all keyframes (independently decodable), though some codecs may use delta encoding.
import { EncodedAudioChunk } from 'node-webcodecs';// Create from encoded AAC dataconst chunk = new EncodedAudioChunk({ type: 'key', // Audio chunks are typically all 'key' timestamp: 0, // Presentation time in microseconds duration: 21333, // Duration in microseconds (~21ms for AAC frame) data: encodedAACData // Uint8Array or ArrayBuffer});// Access propertiesconsole.log(chunk.type); // 'key'console.log(chunk.timestamp); // 0console.log(chunk.byteLength); // Size of encoded data// Copy data to another bufferconst buffer = new Uint8Array(chunk.byteLength);chunk.copyTo(buffer);
An ArrayBuffer or TypedArray to copy the data into. Must have at least byteLength bytes available.
Throws:TypeError if the destination buffer is smaller than byteLength.
Example: Copying chunk data
Copy
const chunk = new EncodedAudioChunk({ type: 'key', timestamp: 0, data: someEncodedData});// Allocate buffer with exact size neededconst buffer = new Uint8Array(chunk.byteLength);chunk.copyTo(buffer);// Now buffer contains a copy of the encoded dataconsole.log(`Copied ${buffer.length} bytes of audio`);