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.
fun createBuffer(): UriCreate a resizable buffer that can hold arbitrary data.
- Returns a uri to identify the buffer.
fun destroyBuffer(uri: Uri)Destroy a buffer and free its resources.
- uri: the uri of the buffer to destroy.
fun setBufferData(    uri: Uri,    offset: Int,    data: ByteBuffer,)Set the data of a buffer.
- 
uri: the uri of the buffer.
- 
offset: the offset in bytes at which to start writing.
- 
data: the data to write. Note that it has to be a directByteBuffer, created either via
ByteBuffer.allocateDirect or via JNI NewDirectByteBuffer API.
fun getBufferData(    uri: Uri,    offset: Int,    length: Int,): ByteBufferGet the data of a buffer.
- 
uri: the uri of the buffer.
- 
offset: the offset in bytes at which to start reading.
- 
length: the number of bytes to read.
- 
Returns the data read from the buffer or an error. 
fun setBufferLength(    uri: Uri,    length: Int,)Set the length of a buffer.
- 
uri: the uri of the buffer.
- 
length: the new length of the buffer in bytes.
fun getBufferLength(uri: Uri): IntGet the length of a buffer.
- 
uri: the uri of the buffer.
- 
Returns the length of the buffer in bytes. 
Full Code#
Here’s the full code:
// Create an audio block and append it to the pageval audioBlock = engine.block.create(DesignBlockType.Audio)engine.block.appendChild(parent = page, child = audioBlock)
// Create a bufferval audioBuffer = engine.editor.createBuffer()
// Reference the audio buffer resource from the audio blockengine.block.setUri(    block = audioBlock,    property = "audio/fileURI",    value = audioBuffer)
// Generate 10 seconds of stereo 48 kHz audio dataval sampleCount = 10 * 48000val byteBuffer = ByteBuffer.allocate(2 * 4 * sampleCount) //2 channels, each 4 bytesrepeat(sampleCount) {    val sample = sin((440 * it * 2 * PI) / 48000).toFloat()    byteBuffer.putFloat(sample)    byteBuffer.putFloat(sample)}// Assign the audio data to the bufferval data = ByteArray(byteBuffer.capacity())byteBuffer.position(0)byteBuffer.get(data)engine.editor.setBufferData(uri = audioBuffer, offset = 0, data = data)
// We can get subranges of the buffer dataval chunk = engine.editor.getBufferData(uri = audioBuffer, offset = 0, length = 4096)
// Get current length of the buffer in bytesval length = engine.editor.getBufferLength(uri = audioBuffer)
// Reduce the buffer to half its length, leading to 5 seconds worth of audioengine.editor.setBufferLength(uri = audioBuffer, length = data.size / 2)
// Free dataengine.editor.destroyBuffer(uri = audioBuffer)