summaryrefslogtreecommitdiff
path: root/FESgui/src/audiobuffer.h
blob: a56dd4b38020410a763a9bfdc1a61e0dcfd1db8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once


#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

class Buffer{
public:
    Buffer(size_t sz=0){
        if (sz){
            data =new float[sz];
            memset(data,0,sz*sizeof(float));
            size=sz;
        }
        else data=NULL;
    }
    ~Buffer(){
        if (data){
            delete[] data; //why is this throwing an error
        }
    } 
    void add(float * input, int num){
        //this assumes that num < size
        memcpy(&data[writePoint],input,min(num,size-writePoint)*sizeof(float));
        if (size-writePoint<num){ //we have to wrap
            //copy the remaining chunk to the start and set the new writePoint
            memcpy(data,&input[size-writePoint],(num-(size-writePoint))*sizeof(float));
            writePoint=num-(size-writePoint);
        }
        else writePoint+=num;
    }
    float operator [] (int i) const {
        return data[(writePoint>i?writePoint-i:size-(i-writePoint))-1];
    }
    float& operator [] (int i) {
        return data[(writePoint>i?writePoint-i:size-(i-writePoint))-1];
    }
private:
    size_t size;
    float *data;
    int writePoint;
};