Skip to content

Buffers

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(): Uri

Create 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 direct ByteBuffer, created either via

ByteBuffer.allocateDirect or via JNI NewDirectByteBuffer API.

fun getBufferData(
uri: Uri,
offset: Int,
length: Int,
): ByteBuffer

Get 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): Int

Get 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 page
val audioBlock = engine.block.create(DesignBlockType.Audio)
engine.block.appendChild(parent = page, child = audioBlock)
// Create a buffer
val audioBuffer = engine.editor.createBuffer()
// Reference the audio buffer resource from the audio block
engine.block.setUri(
block = audioBlock,
property = "audio/fileURI",
value = audioBuffer
)
// Generate 10 seconds of stereo 48 kHz audio data
val sampleCount = 10 * 48000
val byteBuffer = ByteBuffer.allocate(2 * 4 * sampleCount) //2 channels, each 4 bytes
repeat(sampleCount) {
val sample = sin((440 * it * 2 * PI) / 48000).toFloat()
byteBuffer.putFloat(sample)
byteBuffer.putFloat(sample)
}
// Assign the audio data to the buffer
val 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 data
val chunk = engine.editor.getBufferData(uri = audioBuffer, offset = 0, length = 4096)
// Get current length of the buffer in bytes
val length = engine.editor.getBufferLength(uri = audioBuffer)
// Reduce the buffer to half its length, leading to 5 seconds worth of audio
engine.editor.setBufferLength(uri = audioBuffer, length = data.size / 2)
// Free data
engine.editor.destroyBuffer(uri = audioBuffer)