Advertisement
olivercromwell

Untitled

May 28th, 2023 (edited)
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. Filter filter_left;
  2. Filter filter_right;
  3.  
  4.  
  5. std::array<float, 8192> arr_left = {};
  6. std::array<float, 8192> arr_right = {};
  7.  
  8. #include <array>
  9. #include <memory>
  10.  
  11. template <class T> class Circular_Buffer {
  12. private:
  13.     std::unique_ptr<T[]> buffer;
  14.     size_t head = 0;
  15.     size_t tail = 0;
  16.     size_t max_size;
  17.  
  18. public:
  19.     Circular_Buffer<T>(size_t max_size)
  20.         : buffer(std::make_unique<T[]>(max_size)), max_size(max_size) {};
  21.  
  22.     bool enqueue(T item) {
  23.         if (!is_full()) {
  24.             buffer[tail] = item;
  25.             tail = (tail + 1) % max_size;
  26.             return true;
  27.         }
  28.         return false;
  29.     }
  30.      bool dequeue(T& item) {
  31.     if (is_empty())
  32.       return false;
  33.  
  34.     item = buffer[head];
  35.     head = (head + 1) % max_size;
  36.     return true;
  37.   }
  38.  
  39.     void discard() {
  40.         if (!is_empty()) {
  41.             head = (head + 1) % max_size;
  42.         }
  43.             //silently do absolutely jack-all if the buffer is empty
  44.     }
  45.  
  46.  
  47.     bool is_empty() { return head == tail; }
  48.  
  49.     bool is_full() { return (tail + 1) % max_size == head; }
  50.  
  51.     size_t size() {
  52.         return (max_size + tail - head) % max_size;
  53.     }
  54. };
  55.  
  56.  
  57. Circular_Buffer<float> output_buffer_left(3 * 8192);
  58. Circular_Buffer<float> output_buffer_right(3 * 8192);
  59. Circular_Buffer<float> buffer_left(3 * 8192);
  60. Circular_Buffer<float> buffer_right(3 * 8192);
  61.  
  62. void SSBCleanupProcessorAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages) {
  63.     if (buffer.getNumChannels() == 0 || buffer.getNumSamples() == 0) // for example
  64.     {
  65.         buffer.clear();
  66.         return;
  67.     }
  68.     juce::ScopedNoDenormals noDenormals;
  69.     auto totalNumInputChannels = getTotalNumInputChannels();
  70.     auto totalNumOutputChannels = getTotalNumOutputChannels();
  71.     auto numSamples = buffer.getNumSamples();
  72.  
  73.     auto* audiobufferleft = buffer.getWritePointer(0);
  74.     auto* audiobufferright = buffer.getWritePointer(1);
  75.  
  76.     //each time that the user wants to process audio, update the slider values
  77.     filter_left.setConstant(entropyValue);
  78.     filter_left.set_NBINS(binsValue);
  79.     filter_right.setConstant(entropyValue);
  80.     filter_right.set_NBINS(binsValue);
  81.  
  82.  
  83.     for (int i = 0; i < numSamples; ++i) {
  84.         buffer_left.enqueue(audiobufferleft[i]);
  85.         buffer_right.enqueue(audiobufferright[i]);
  86.     }
  87.  
  88.  
  89.     if (buffer_left.size() >= 8192) {
  90.         int multiples = buffer_left.size() / 8192;
  91.         int remainder = buffer_left.size() % 8192;
  92.  
  93.         for (int m = 0; m < multiples; ++m) {
  94.             for (int i = 0; i < 8192; ++i) {
  95.                 if (!buffer_left.dequeue(arr_left[i])) {
  96.                     throw std::runtime_error("buffer_left is empty");
  97.                 }
  98.             }
  99.  
  100.             // Process and push to output_buffer_left...
  101.             arr_left = filter_left.process(arr_left);
  102.  
  103.             for (const auto& sample : arr_left) {
  104.                 output_buffer_left.enqueue(sample);
  105.             }
  106.         }
  107.  
  108.  
  109.     }
  110.     int flag = 0;
  111.     if (buffer_right.size() >= 8192) {
  112.         int multiples = buffer_right.size() / 8192;
  113.         int remainder = buffer_right.size() % 8192;
  114.  
  115.         for (int m = 0; m < multiples; ++m) {
  116.             for (int i = 0; i < 8192; ++i) {
  117.                 if (!buffer_right.dequeue(arr_right[i])) {
  118.                     throw std::runtime_error("buffer_right is empty");
  119.                 }
  120.             }
  121.  
  122.             // Process and push to output_buffer_left...
  123.             arr_right = filter_right.process(arr_right);
  124.             for (const auto& sample : arr_right) {
  125.                 output_buffer_right.enqueue(sample);
  126.             }
  127.         }
  128.  
  129.         // Erase the processed values
  130.         flag = 1;
  131.     }
  132.  
  133.  
  134.  
  135.     int availableSamples_left = output_buffer_left.size();
  136.     int samplesToCopy_left = std::min(numSamples, availableSamples_left);
  137.     if (samplesToCopy_left > 0) {
  138.         for (size_t i = 0; i < samplesToCopy_left; ++i) {
  139.             if (!output_buffer_left.dequeue(audiobufferleft[i])) {
  140.                 throw std::runtime_error("buffer_right is empty");
  141.             }
  142.         }
  143.         flag = 1;
  144.     }
  145.  
  146.     int availableSamples_right = output_buffer_right.size();
  147.     int samplesToCopy_right = std::min(numSamples, availableSamples_right);
  148.     if (samplesToCopy_right > 0) {
  149.         for (size_t i = 0; i < samplesToCopy_right; ++i) {
  150.             if (!output_buffer_right.dequeue(audiobufferright[i])) {
  151.                 throw std::runtime_error("buffer_right is empty");
  152.             }
  153.         }
  154.         flag = 1;
  155.     }
  156.     if (flag == 0) {
  157.         buffer.clear();
  158.         return;
  159.     }
  160.     return;
  161.     // If there were not enough samples in the buffer, fill the rest of the output with zeros
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement