summaryrefslogtreecommitdiff
path: root/audioin/src/ofApp.h
diff options
context:
space:
mode:
authorTim Redfern <tim@getdrop.com>2022-11-15 23:44:36 +0000
committerTim Redfern <tim@getdrop.com>2022-11-15 23:44:36 +0000
commit4e4884d602887c5d8f886bf51188c645a8e7d310 (patch)
treef5805be11cb6383f577ea4f9560454911ed3941e /audioin/src/ofApp.h
parent8c69df689a9327b634f765f11984a76133d3777e (diff)
offset read by 1
Diffstat (limited to 'audioin/src/ofApp.h')
-rw-r--r--audioin/src/ofApp.h54
1 files changed, 50 insertions, 4 deletions
diff --git a/audioin/src/ofApp.h b/audioin/src/ofApp.h
index d62dfa0..3ac6b39 100644
--- a/audioin/src/ofApp.h
+++ b/audioin/src/ofApp.h
@@ -23,16 +23,62 @@ public:
//delete[] data; //why is this throwing an error
}
}
+/*
+if the size is 1000 and the num is 600
+1st frame: writePoint=0
+memcpy(&data[0],input,min(600,1000-0))
+-> we copied the whole 600 starting at 0 and ending at 599
+2nd frame: writePoint=600
+memcpy(&data[600],input, min(600,1000-600))
+-> we copied 400 starting at 600 and ending at 999
+yes (1000-600<600)
+ so
+ memcpy(data,&input[1000-600],600-(1000-600))
+ -> we copied 200 starting at input:400 and ending at 599 to the start of data
+ writePoint=600-(1000-600)
+3rd frame: writePoint=200
+memcpy(&data[200],input, min(600,1000-200))
+-> we copied the whole 600 starting at 200 and ending at 799
+4th frame: writePoint=800
+memcpy(&data[800],input, min(600,1000-800))
+-> we copied 200 starting at 800 and ending at 999
+yes (1000-800<600)
+ so
+ memcpy(data,&input[1000-800],600-(1000-800))
+ -> we copied 400 starting at input:200 and ending at 599 to the start of data
+ writePoint=600-(1000-800)
+5th frame: writePoint=400
+memcpy(&data[400],input, min(600,1000-400))
+-> we copied the whole 600 starting at 400 and ending at 999
+
+back to square one. 5*600=3000
+*/
void add(float * input, int num){
memcpy(&data[writePoint],input,min(num,size-writePoint)*sizeof(float));
- if (size-writePoint<num){
- memcpy(data,&input[(size-writePoint)],(num-(size-writePoint))*sizeof(float));
+ if (size-writePoint<num){ //we have to wrap
+ //copy the remaining chunk to the start
+ 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)];}
- float& operator [] (int i) {return data[writePoint>i?writePoint-i:size-(i-writePoint)];}
+/*
+if size is 1000 and writePoint is 1000
+0: return data[1000]
+writePoint can never be 1000?
+
+if size is 1000 and writePoint is 800
+0: return data[800]
+1: return data[799]
+799: return data[1] --> this should be data[0] !?
+800: return data[1000-(800-800)->1000] -> this should be data[999]
+*/
+ 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;