Ring Buffer

Ringbuffer is designed in addition to use as a data buffer, also used to connect Audio Elements. Each Element that requests data from the Ringbuffer will block the task until the data is available. Or block the task when writing data and the Buffer is full. Of course, we can stop this block at any time.

Ring Buffer used in Audio Pipeline

Application Example

In most of ESP-ADF examples connecting of Elements with Ringbuffers is done “behind the scenes” by a function audio_pipeline_link(). To see this operation exposed check player/element_sdcard_mp3 example.

API Reference

Functions

ringbuf_handle_t rb_create(int block_size, int n_blocks)

Create ringbuffer with total size = block_size * n_blocks.

Return
ringbuf_handle_t
Parameters
  • block_size: Size of each block
  • n_blocks: Number of blocks

esp_err_t rb_destroy(ringbuf_handle_t rb)

Cleanup and free all memory created by ringbuf_handle_t.

Return
  • ESP_OK
  • ESP_FAIL
Parameters
  • rb: The Ringbuffer handle

esp_err_t rb_abort(ringbuf_handle_t rb)

Abort waiting until there is space for reading or writing of the ringbuffer.

Return
  • ESP_OK
  • ESP_FAIL
Parameters
  • rb: The Ringbuffer handle

esp_err_t rb_reset(ringbuf_handle_t rb)

Reset ringbuffer, clear all values as initial state.

Return
  • ESP_OK
  • ESP_FAIL
Parameters
  • rb: The Ringbuffer handle

int rb_bytes_available(ringbuf_handle_t rb)

Get total bytes available of Ringbuffer.

Return
total bytes available
Parameters
  • rb: The Ringbuffer handle

int rb_bytes_filled(ringbuf_handle_t rb)

Get the number of bytes that have filled the ringbuffer.

Return
The number of bytes that have filled the ringbuffer
Parameters
  • rb: The Ringbuffer handle

int rb_get_size(ringbuf_handle_t rb)

Get total size of Ringbuffer (in bytes)

Return
total size of Ringbuffer
Parameters
  • rb: The Ringbuffer handle

int rb_read(ringbuf_handle_t rb, char *buf, int len, TickType_t ticks_to_wait)

Read from Ringbuffer to buf with len and wait tick_to_wait ticks until enough bytes to read if the ringbuffer bytes available is less than len. If buf argument provided is NULL, then ringbuffer do pseudo reads by simply advancing pointers.

Return
Number of bytes read
Parameters
  • rb: The Ringbuffer handle
  • buf: The buffer pointer to read out data
  • len: The length request
  • ticks_to_wait: The ticks to wait

int rb_write(ringbuf_handle_t rb, char *buf, int len, TickType_t ticks_to_wait)

Write to Ringbuffer from buf with len and wait tick_to_wait ticks until enough space to write if the ringbuffer space available is less than len

Return
Number of bytes written
Parameters
  • rb: The Ringbuffer handle
  • buf: The buffer
  • len: The length
  • ticks_to_wait: The ticks to wait

esp_err_t rb_done_write(ringbuf_handle_t rb)

Set status of writing to ringbuffer is done.

Return
  • ESP_OK
  • ESP_FAIL
Parameters
  • rb: The Ringbuffer handle

esp_err_t rb_unblock_reader(ringbuf_handle_t rb)

Unblock from rb_read.

Return
  • ESP_OK
  • ESP_FAIL
Parameters
  • rb: The Ringbuffer handle

Macros

RB_OK
RB_FAIL
RB_DONE
RB_ABORT
RB_TIMEOUT

Type Definitions

typedef struct ringbuf *ringbuf_handle_t