FahlGrahn Audio v1.0.0
Loading...
Searching...
No Matches
RingBuffer.h
1#pragma once
2#include <juce_audio_basics/juce_audio_basics.h>
3
5{
6 public:
7 RingBuffer(int size) : mAbstractFifo(size)
8 {
9 mAudioBuffer.setSize(1, size);
10 }
11
12 void setSize(int size)
13 {
14 mAbstractFifo.setTotalSize(size);
15 mAudioBuffer.setSize(1, size);
16 }
17
18 template <typename T> void addToFifo(const T *data, int numSamples)
19 {
20 const auto scope = mAbstractFifo.write(numSamples);
21
22 if (scope.blockSize1 > 0)
23 mAudioBuffer.copyFrom(0, scope.startIndex1, data, scope.blockSize1);
24 if (scope.blockSize2 > 0)
25 mAudioBuffer.copyFrom(0, scope.startIndex2, data + scope.blockSize1, scope.blockSize2);
26 }
27
28 template <typename T> void readFromFifo(T *destination, int numSamples)
29 {
30 const auto scope = mAbstractFifo.read(numSamples);
31
32 const float *buffer = mAudioBuffer.getReadPointer(0);
33
34 if (scope.blockSize1 > 0)
35 std::copy(buffer + scope.startIndex1, buffer + scope.startIndex1 + scope.blockSize1, destination);
36
37 if (scope.blockSize2 > 0)
38 std::copy(buffer + scope.startIndex2, buffer + scope.startIndex2 + scope.blockSize2,
39 destination + scope.blockSize1);
40 }
41
42 int getNumReady()
43 {
44 return mAbstractFifo.getNumReady();
45 }
46
47 juce::AbstractFifo mAbstractFifo;
48
49 private:
50 juce::AudioBuffer<float> mAudioBuffer;
51};
Definition RingBuffer.h:5