embed_with_elliot-circular_buffer
embed_with_elliot-circular_buffer copied to clipboard
using circular buffer for audio samples
hi, I'm trying to use your circular buffer to buffer audio samples before sending them on over an RF module with an arduino due and an audio codec. There is actually a 64byte fifo buffer on the RF IC but I'd like to learn how to use circular buffers all the same !
I am finding it difficult to understand where or how i should copy in the samples to the curcular buffer. I would be greatful if you could point me in the right direction. hopfuly i'm not totally off track !! i removed all the RF and audio Codec code to try and make my problem clearer.. the samples come in at one sample at a time, so i'm not sure about your dump to UART example using a for loop.. should I send the cucular array one byte at a time and the array decides where the next free slot in the array is ?
Also where would I read data from ? how do I point to the least oldest byte in the array so I can send that over my RF module..
`// arduino circular buffer, digital audio TX
define BUFFER_SIZE 16
enum BufferStatus {BUFFER_OK, BUFFER_EMPTY, BUFFER_FULL};
enum BufferStatus status; uint8_t byte_from_buffer;
uint8_t tempy;
enum BufferStatus status_tx;
uint8_t newAudioSamples[16]; // samples come in one at a time , but I could make a normal array of 16 bytes at a time ?
struct Buffer { uint8_t data[BUFFER_SIZE]; uint8_t newest_index; uint8_t oldest_index;};
// checks for writing data into the circular buffer ? enum BufferStatus bufferWrite(uint8_t byte){ uint8_t next_index = (buffer.newest_index+1) % BUFFER_SIZE;
if (next_index == buffer.oldest_index){ return BUFFER_FULL; } buffer.data[buffer.newest_index] = byte; buffer.newest_index = next_index; return BUFFER_OK; }
// checks for reading data from the circular buffer ? enum BufferStatus bufferRead(uint8_t *byte){ if (buffer.newest_index == buffer.oldest_index){ return BUFFER_EMPTY; } *byte = buffer.data[buffer.oldest_index]; buffer.oldest_index = (buffer.oldest_index+1) % BUFFER_SIZE; return BUFFER_OK; }
void setup() { // initialise_audio_codec(); // initialise_RFmodule(); }
void loop() {
rxDATA(); // recieve samples into circular buffer txDATA(); // send samples form circular buffer to RF module
}
// RECIEVE DATA INTO BUFFER IF ROOM void rxDATA {
status_tx = bufferRead(&tx_buffer, &tempy);
if (status_tx == BUFFER_EMPTY){
return 0;
}
else {
for (uint8_t i=0; i<BUFFER_SIZE; i++){
&buffer.data(i) = newAudioSamples(i); /// ????? // should i specify an array position for samples, would'nt it be differnt? ?? how do i write into circular buffer?
}}
}
// SEND DATA
void txDATA { status = bufferRead(&byte_from_buffer); if (status == BUFFER_OK) { /* we have data */ for (uint8_t i=0; i<BUFFER_SIZE; i++){ RH_RF24.send (&buffer.data(i) // dump TX data over RF module ??? do i need to specify an array position ?? }} else { return; } } `