Created
November 20, 2016 14:48
-
-
Save alfaleader/bb65dd79e05e286d4183dfd4c8c4711e to your computer and use it in GitHub Desktop.
Test of a circular FIFO in JUCE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CIRCULARFIFO_H_INCLUDED | |
#define CIRCULARFIFO_H_INCLUDED | |
#include "../JuceLibraryCode/JuceHeader.h" | |
template <typename T> | |
class CircularFifo { | |
public: | |
CircularFifo() noexcept { | |
validRead.set(31); //next addItem will add to 0 | |
for (int i = 0; i < 32; i++) buffer[i] = T(); //fill with defaults (do we need this?) Or add an extra variable with # of valid values | |
} | |
~CircularFifo() noexcept { | |
} | |
//Add item to the circular FIFO and after it's added, set validRead to that position | |
void addItem(T item) noexcept { | |
int newWrite = (validRead.get() + 1) % 32; | |
buffer[newWrite] = item; //Write item to buffer (1 place ahead of the validRead) | |
validRead.set(newWrite); //Now this is valid for readreturn buffer[validRead.get()]; | |
} | |
//Get the last item (at validRead position) | |
T getLastItem() noexcept { | |
return buffer[validRead.get()]; | |
} | |
//get the last x items counting back from validRead !! has to be lower then buffer - 1 | |
T getLastItemAtPosition(int position) noexcept { | |
//check if we have enough values to read | |
jassert(position < 0 || position > 31); | |
if (position < 0 || position > 31) return T(); | |
return buffer[(validRead.get() - position) % 32]; | |
} | |
private: | |
/** | |
Validread is the position from where we can read values from | |
*/ | |
Atomic <int> validRead; | |
T buffer[32]; | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CircularFifo) | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment