In this example, we will show you how to use the CreativeEditor SDK’s CreativeEngine to create buffers through the editor
API. Buffers can hold arbitrary data.
createBuffer(): string
Create a resizable buffer that can hold arbitrary data.
- Returns A URI to identify the buffer.
destroyBuffer(uri: string): void
Destroy a buffer and free its resources.
uri
: The URI of the buffer to destroy.
setBufferData(uri: string, offset: number, data: Uint8Array): void
Set the data of a buffer at a given offset.
uri
: The URI of the buffer to update.offset
: The offset in bytes at which to start writing.data
: The data to write.
getBufferData(uri: string, offset: number, length: number): Uint8Array
Get the data of a buffer at a given offset.
uri
: The URI of the buffer to query.offset
: The offset in bytes at which to start reading.length
: The number of bytes to read.- Returns The data at the given offset.
setBufferLength(uri: string, length: number): void
Set the length of a buffer.
uri
: The URI of the buffer to update.length
: The new length of the buffer in bytes.
getBufferLength(uri: string): number
Get the length of a buffer.
uri
: The URI of the buffer to query.- Returns The length of the buffer in bytes.
Full Code
Here’s the full code:
// Create an audio block and append it to the pageconst audioBlock = engine.block.create('audio');engine.block.appendChild(page, audioBlock);
// Create a bufferconst audioBuffer = engine.editor.createBuffer();
// Reference the audio buffer resource from the audio blockengine.block.setString(audioBlock, 'audio/fileURI', audioBuffer);
// Generate 10 seconds of stereo 48 kHz audio dataconst samples = new Float32Array(10 * 2 * 48000);for (let i = 0; i < samples.length; i += 2) { samples[i] = samples[i + 1] = Math.sin((440 * i * Math.PI) / 48000);}
// Assign the audio data to the bufferengine.editor.setBufferData(audioBuffer, 0, new Uint8Array(samples.buffer));
// We can get subranges of the buffer dataconst chunk = engine.editor.getBufferData(audioBuffer, 0, 4096);
// Get current length of the buffer in bytesconst length = engine.editor.getBufferLength(audioBuffer);
// Reduce the buffer to half its length, leading to 5 seconds worth of audioengine.editor.setBufferLength(audioBuffer, length / 2);
// Free dataengine.editor.destroyBuffer(audioBuffer);