forked from W4D/soundcube-firmware
streaming max 4 stereo wave files at the same time
This commit is contained in:
49
tools/ringbuffer.h
Normal file
49
tools/ringbuffer.h
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
template<typename T>
|
||||
class RingBuffer{
|
||||
public:
|
||||
RingBuffer(size_t size) : bufferSize(size) {}
|
||||
|
||||
void begin(){
|
||||
buffer = new T[bufferSize];
|
||||
}
|
||||
|
||||
bool push(T data){
|
||||
std::cout << "write: " << data << std::endl;
|
||||
if(counter < bufferSize){
|
||||
buffer[write] = data;
|
||||
write = (write+1) % bufferSize;
|
||||
counter++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
T pop(){
|
||||
T retval;
|
||||
if(counter > 0) {
|
||||
counter--;
|
||||
retval = buffer[read];
|
||||
read = (read+1) % bufferSize;
|
||||
}
|
||||
std::cout << "read: " << retval << std::endl;
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool isEmpty(){
|
||||
return counter == 0;
|
||||
}
|
||||
|
||||
bool isFull(){
|
||||
return counter == bufferSize;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t bufferSize = 0;
|
||||
int counter = 0;
|
||||
int write = 0;
|
||||
int read = 0;
|
||||
T *buffer;
|
||||
};
|
||||
Reference in New Issue
Block a user