Advertisement
ErnstHot

Multirate stuff

Feb 2nd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. class ModBuffer
  11. {
  12. public:
  13.     ModBuffer(int inOsFactor, int inMaxSize)
  14.         : osFactor(inOsFactor)
  15.         , writeIndex(0)
  16.     {
  17.         assert(osFactor > 0);
  18.         assert(inMaxSize > 0);
  19.  
  20.         maxSize = 2 * osFactor + inMaxSize;
  21.  
  22.         data = new float[maxSize];
  23.     }
  24.  
  25.     ~ModBuffer()
  26.     {
  27.         delete data;
  28.     }
  29.  
  30.  
  31.     void beginWrite(int sampleCount)
  32.     {
  33.         // Copy block from last cycle to start of buffer if necessary
  34.  
  35.         sampleCount += overflow; // carry over from last cycle
  36.  
  37.         blockCount = sampleCount / osFactor;
  38.         overflow = sampleCount % osFactor;
  39.         blockCount = (overflow == 0) ? blockCount : blockCount + 1;     // but sometimes blocks = 0 or blocks -= 1
  40.     }
  41.    
  42.  
  43.     int  getBlockCount() const      { return blockCount; }
  44.  
  45.  
  46.     void write(float sampleIn)
  47.     {
  48.         assert((writeIndex + osFactor) < maxSize);
  49.         assert((writeIndex + osFactor) < (blockCount * osFactor));
  50.  
  51.         data[writeIndex += osFactor] = sampleIn;
  52.     }
  53.    
  54.  
  55.     void endWrite()
  56.     {
  57.         // lerp it
  58.     }
  59.    
  60.  
  61.     int  getReadIndex() const       { return prevOverflow; }
  62.  
  63. private:
  64.     const int osFactor;
  65.     int maxSize;
  66.  
  67.     float* data;
  68.  
  69.     int writeIndex;
  70.     int blockCount;
  71.     int overflow;
  72.     int prevOverflow;  
  73. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement