Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Filter filter_left;
- Filter filter_right;
- std::array<float, 8192> arr_left = {};
- std::array<float, 8192> arr_right = {};
- #include <array>
- #include <memory>
- template <class T> class Circular_Buffer {
- private:
- std::unique_ptr<T[]> buffer;
- size_t head = 0;
- size_t tail = 0;
- size_t max_size;
- public:
- Circular_Buffer<T>(size_t max_size)
- : buffer(std::make_unique<T[]>(max_size)), max_size(max_size) {};
- bool enqueue(T item) {
- if (!is_full()) {
- buffer[tail] = item;
- tail = (tail + 1) % max_size;
- return true;
- }
- return false;
- }
- bool dequeue(T& item) {
- if (is_empty())
- return false;
- item = buffer[head];
- head = (head + 1) % max_size;
- return true;
- }
- void discard() {
- if (!is_empty()) {
- head = (head + 1) % max_size;
- }
- //silently do absolutely jack-all if the buffer is empty
- }
- bool is_empty() { return head == tail; }
- bool is_full() { return (tail + 1) % max_size == head; }
- size_t size() {
- return (max_size + tail - head) % max_size;
- }
- };
- Circular_Buffer<float> output_buffer_left(3 * 8192);
- Circular_Buffer<float> output_buffer_right(3 * 8192);
- Circular_Buffer<float> buffer_left(3 * 8192);
- Circular_Buffer<float> buffer_right(3 * 8192);
- void SSBCleanupProcessorAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages) {
- if (buffer.getNumChannels() == 0 || buffer.getNumSamples() == 0) // for example
- {
- buffer.clear();
- return;
- }
- juce::ScopedNoDenormals noDenormals;
- auto totalNumInputChannels = getTotalNumInputChannels();
- auto totalNumOutputChannels = getTotalNumOutputChannels();
- auto numSamples = buffer.getNumSamples();
- auto* audiobufferleft = buffer.getWritePointer(0);
- auto* audiobufferright = buffer.getWritePointer(1);
- //each time that the user wants to process audio, update the slider values
- filter_left.setConstant(entropyValue);
- filter_left.set_NBINS(binsValue);
- filter_right.setConstant(entropyValue);
- filter_right.set_NBINS(binsValue);
- for (int i = 0; i < numSamples; ++i) {
- buffer_left.enqueue(audiobufferleft[i]);
- buffer_right.enqueue(audiobufferright[i]);
- }
- if (buffer_left.size() >= 8192) {
- int multiples = buffer_left.size() / 8192;
- int remainder = buffer_left.size() % 8192;
- for (int m = 0; m < multiples; ++m) {
- for (int i = 0; i < 8192; ++i) {
- if (!buffer_left.dequeue(arr_left[i])) {
- throw std::runtime_error("buffer_left is empty");
- }
- }
- // Process and push to output_buffer_left...
- arr_left = filter_left.process(arr_left);
- for (const auto& sample : arr_left) {
- output_buffer_left.enqueue(sample);
- }
- }
- }
- int flag = 0;
- if (buffer_right.size() >= 8192) {
- int multiples = buffer_right.size() / 8192;
- int remainder = buffer_right.size() % 8192;
- for (int m = 0; m < multiples; ++m) {
- for (int i = 0; i < 8192; ++i) {
- if (!buffer_right.dequeue(arr_right[i])) {
- throw std::runtime_error("buffer_right is empty");
- }
- }
- // Process and push to output_buffer_left...
- arr_right = filter_right.process(arr_right);
- for (const auto& sample : arr_right) {
- output_buffer_right.enqueue(sample);
- }
- }
- // Erase the processed values
- flag = 1;
- }
- int availableSamples_left = output_buffer_left.size();
- int samplesToCopy_left = std::min(numSamples, availableSamples_left);
- if (samplesToCopy_left > 0) {
- for (size_t i = 0; i < samplesToCopy_left; ++i) {
- if (!output_buffer_left.dequeue(audiobufferleft[i])) {
- throw std::runtime_error("buffer_right is empty");
- }
- }
- flag = 1;
- }
- int availableSamples_right = output_buffer_right.size();
- int samplesToCopy_right = std::min(numSamples, availableSamples_right);
- if (samplesToCopy_right > 0) {
- for (size_t i = 0; i < samplesToCopy_right; ++i) {
- if (!output_buffer_right.dequeue(audiobufferright[i])) {
- throw std::runtime_error("buffer_right is empty");
- }
- }
- flag = 1;
- }
- if (flag == 0) {
- buffer.clear();
- return;
- }
- return;
- // If there were not enough samples in the buffer, fill the rest of the output with zeros
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement