Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <cassert>
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- using namespace std;
- class ModBuffer
- {
- public:
- ModBuffer(int inOsFactor, int inMaxSize)
- : osFactor(inOsFactor)
- , writeIndex(0)
- {
- assert(osFactor > 0);
- assert(inMaxSize > 0);
- maxSize = 2 * osFactor + inMaxSize;
- data = new float[maxSize];
- }
- ~ModBuffer()
- {
- delete data;
- }
- void beginWrite(int sampleCount)
- {
- // Copy block from last cycle to start of buffer if necessary
- sampleCount += overflow; // carry over from last cycle
- blockCount = sampleCount / osFactor;
- overflow = sampleCount % osFactor;
- blockCount = (overflow == 0) ? blockCount : blockCount + 1; // but sometimes blocks = 0 or blocks -= 1
- }
- int getBlockCount() const { return blockCount; }
- void write(float sampleIn)
- {
- assert((writeIndex + osFactor) < maxSize);
- assert((writeIndex + osFactor) < (blockCount * osFactor));
- data[writeIndex += osFactor] = sampleIn;
- }
- void endWrite()
- {
- // lerp it
- }
- int getReadIndex() const { return prevOverflow; }
- private:
- const int osFactor;
- int maxSize;
- float* data;
- int writeIndex;
- int blockCount;
- int overflow;
- int prevOverflow;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement