Maintaining continuous circular buffer with data received from AudioBuffer and caluculate running median on it


#1

Hi guys,,  I want to implement a feature. The software i am working on maintains a continuous buffer of samples. This buffer is an AudioSampleBuffer.

somewhat like AudioSampleBuffer *dataBuffer;

where dataBuffer is a pointer to an incoming continuous buffer.

This buffer contains data from different electrodes, and each electrode has 2 channels. Actually the buffer is a group of many channel buffers, one for each channel. 

I want to maintain a Continuous Circular Buffer which stores samples for different channels for different electrodes. I want to calculate running median on this data, i have the sampling rate for the samples. 

I heed help in implementing this buffer,  how can i maintain this circular buffer,  ( real problem is adding data into buffer). 

 

i can share the file containing code if needed.


#2

Don’t know if this will help but I copied and modified an example of a simple Ring / Circular Buffer class for storing float samples. It uses new and delete so I’m sure could be modernised a bit and may not show current best practice.

/**
 * Simple ring buffer which is always the same length
 * for keeping a stream of float input values
 * Designed to give a snapshot in time 
 */

class RingBuffer
{
public:
    RingBuffer (int bufferSize) : bufferSize (bufferSize), count (0), head (0), buffer (nullptr), readBuffer (nullptr)
    {
        buffer     = new float[bufferSize];        
        readBuffer = new float[bufferSize];    
    }

    ~RingBuffer() 
    {
        delete buffer;
        delete readBuffer;
    }
    
    void push (float value)
    {
        if (count < bufferSize && head == 0)
        {
            buffer[count++] = value;
        }
        else if (count >= bufferSize)
        {
            if (head >= bufferSize)
            {
                //DBG ("reset head 0");
                head = 0;
            }
            buffer[head++] = value;
        }
    }

    /**
     * Return a snapshot of the buffer as a continous array
     */
    const float* getSnapshot () 
    {
         // Set up read buffer as continuous stream
        int readIndex = head;
         for (int i = 0; i < count; i++)
         {
             readBuffer[i] = buffer[readIndex];
             readIndex = (readIndex + 1) % bufferSize;
         }
         return readBuffer;
    }
    
private:
    int bufferSize, head, count;
    float* buffer;
    float* readBuffer;
};