comparison scanpath.mm @ 3:d346ddc50f70

Vectorised a few things. Fiddled with new mesh creator... not solved much.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 07 Dec 2012 19:20:57 +0000
parents 1d1bf0aac99e
children 79c7cf39a0a0
comparison
equal deleted inserted replaced
2:0aab7a1d3c77 3:d346ddc50f70
8 */ 8 */
9 #include <vector> 9 #include <vector>
10 #include "scanpath.h" 10 #include "scanpath.h"
11 #include "testApp.h" 11 #include "testApp.h"
12 12
13 //---------------------------------------------------------------- 13 ScanPath scanPath;
14 ScanPath::ScanPath() : maxElements(2048) { 14
15 // audio thread does not touch mesh lumps/springs
16 //----------------------------------------------------------------
17 ScanPath::ScanPath() : maxElements(10000) {
15 // construct an empty scanpath 18 // construct an empty scanpath
16 // an ' element ' consists of 1 lump and one spring 19 // an ' element ' consists of 1 lump and one spring
17 20
18 21
19 numElements = 0; // numElements is a dynamic running count of elements when building - a bit crap 22 numElements = 0; // numElements is a dynamic running count of elements when building - a bit crap
20 springPath = new Spring*[maxElements]; 23
21 lumpPath = new Lump*[maxElements]; 24
22
23 framesPerSample = ofGetFrameRate()/SAMPLE_RATE;
24 frameInterpolator = 0.0; 25 frameInterpolator = 0.0;
25 currentLength = 0.0; 26 currentLength = 0.0;
26 restLength = 1.0; // ? 27 restLength = 1.0; // ?
27 28
28 scanMode = DISPLACEMENT; 29 scanMode = DISPLACEMENT;
29
30 initWavetables(); 30 initWavetables();
31 31 framesPerSample = 60.0/SAMPLE_RATE;
32
33 }
34 void ScanPath::init(){
35 framesPerSample = ofGetFrameRate()/SAMPLE_RATE;
36
32 } 37 }
33 //---------------------------------------------------------------- 38 //----------------------------------------------------------------
34 ScanPath::~ScanPath(){ 39 ScanPath::~ScanPath(){
35 delete [] springPath; 40
36 delete [] lumpPath; 41 //VECTORISE
42
37 delete [] wavetableNew; 43 delete [] wavetableNew;
38 delete [] wavetableOld; 44 delete [] wavetableOld;
39 delete [] wavetableUpdate; 45 delete [] wavetableUpdate;
40 cout << "destructed scanpath\n"; 46 cout << "destructed scanpath\n";
41 } 47 }
42 48
43 void ScanPath::clear(){ 49 void ScanPath::clear(){
44 for(int i = 0; i<numElements; i++){ 50 //VECTORISE
45 springPath[i]->removeFromScanPath(); 51 for(vector<Element>::iterator elitr = pathElements.begin(); elitr < pathElements.end(); elitr++){
46 lumpPath[i]->removeFromScanPath(); 52 (*elitr).eLump->removeFromScanPath();
47 } 53 (*elitr).eSpring->removeFromScanPath();
54 }
55 pathElements.clear();
48 numElements = 0; 56 numElements = 0;
49 // cant work? 57
50 } 58 }
51 int ScanPath::howManyElements(){ 59 int ScanPath::howManyElements(){
60 //return pathElements.size();
52 return numElements; 61 return numElements;
53 } 62 }
54 //---------------------------------------------------------------- 63 //----------------------------------------------------------------
55 void ScanPath::inscribe(double ax, double ay){ 64 void ScanPath::inscribe(double ax, double ay){
56 // look at coordinates, add the closest lump and it's connecting string 65 // look at coordinates, add the closest lump and it's connecting string
57 // if we're further away from current lump 66 // if we're further away from current lump
58 67
59 // check end points of connecting springs, pick closest 68 // check end points of connecting springs, pick closest
69
70 // NOW IN MESH
60 } 71 }
61 void ScanPath::draw(){ 72 void ScanPath::draw(){
62 // draw the actual waveform in the corner 73 // draw the actual waveform in the corner
63 74
64 int width = 768; 75 int width = 768;
101 //---------------------------------------------------------------- 112 //----------------------------------------------------------------
102 // add lump 113 // add lump
103 double ScanPath::getTotalLength(){ 114 double ScanPath::getTotalLength(){
104 // for interesting modulations... 115 // for interesting modulations...
105 currentLength = 0.0; 116 currentLength = 0.0;
106 for(int i = 0; i < numElements; i++){ 117
107 currentLength += springPath[i]->getLength(); 118 for(vector<Element>::iterator elitr = pathElements.begin(); elitr < pathElements.end(); elitr++){
119
120 currentLength = (*elitr).eSpring->getLength();
108 } 121 }
109 return currentLength; 122 return currentLength;
110 123
111 } 124 }
112 //---------------------------------------------------------------- 125 //----------------------------------------------------------------
121 wavetableUpdate[i] = 0.0; 134 wavetableUpdate[i] = 0.0;
122 } 135 }
123 136
124 } 137 }
125 //---------------------------------------------------------------- 138 //----------------------------------------------------------------
126 void ScanPath::addElement(Lump* aLump, Spring * aSpring){ 139 void ScanPath::addElement(Lump* const aLump, Spring * const aSpring){
127 // insert ptr to the lump and spring into array 140 // insert ptr to the lump and spring into array
128 if(numElements >= maxElements){ 141
129 cerr << "cannot add any more to scanpath - max elements: 2048\n"; 142
143 if(numElements + 1 >= maxElements){
144 cout << " max elements reached!\n";
130 return; 145 return;
131 } 146 }
132 lumpPath[numElements] = aLump; 147
133 springPath[numElements] = aSpring; 148 pathElements.push_back(Element(aLump,aSpring));
134 149
150 // maybe do this in mesh?
135 aLump->addToScanPath(); 151 aLump->addToScanPath();
136 aSpring->addToScanPath(); 152 aSpring->addToScanPath();
137
138 numElements++; 153 numElements++;
154
155
139 } 156 }
140 //---------------------------------------------------------------- 157 //----------------------------------------------------------------
141 void ScanPath::updateWavetables(){ 158 void ScanPath::updateWavetables(){
142 // swap old , new 159 // swap old , new
143 double * temp; 160 double * temp;
144 161
145 // TODO THRED MUTEX HERE!?? 162 // TODO THRED MUTEX HERE!??
146 163 // this is called from graphics thread
164 // reset the interp between frames
165
166 /*
167 int i = 0;
168 while(audioAccessing){
169 i++;
170 }
171 cout << "buffers swapped while update!" << i << "times\n";
172 updateAccessing = true;
173
174 */
147 switch(scanMode){ 175 switch(scanMode){
148 case DISPLACEMENT: 176 case DISPLACEMENT:
149 // now fill with new values 177 // now fill with new values
150 for(int i = 0; i < numElements; i++){ 178 for(int i = 0; i < numElements; i++){
151 179 // double check
152 wavetableUpdate[i] = lumpPath[i]->scanRadialDisplacement()/1.5; 180 if(pathElements[i].eLump->isInScanPath){
181 wavetableUpdate[i] = pathElements[i].eLump->scanRadialDisplacement()/1.5;
182 }
153 183
154 } 184 }
155 break; 185 break;
156 case SPEED: 186 case SPEED:
157 for(int i = 0; i < numElements; i++){ 187 for(int i = 0; i < numElements; i++){
158 wavetableUpdate[i] = lumpPath[i]->scanLumpSpeed(); 188 if(pathElements[i].eLump->isInScanPath){
189 wavetableUpdate[i] = pathElements[i].eLump->scanLumpSpeed();
190 }
159 } 191 }
160 break; 192 break;
161 case SPRING_FORCE: 193 case SPRING_FORCE:
162 for(int i = 0; i < numElements; i++){ 194 for(int i = 0; i < numElements; i++){
163 wavetableUpdate[i] = springPath[i]->getForceMag(); 195 if(pathElements[i].eSpring->isInScanPath){
196 wavetableUpdate[i] = pathElements[i].eSpring->getForceMag();
197 }
164 } 198 }
165 break; 199 break;
166 case YPOS: 200 case YPOS:
167 for(int i = 0; i < numElements; i++){ 201 for(int i = 0; i < numElements; i++){
168 wavetableUpdate[i] = lumpPath[i]->scanYPos(); 202 if(pathElements[i].eLump->isInScanPath){
203 wavetableUpdate[i] = pathElements[i].eLump->scanYPos();
204 }
169 } 205 }
170 break; 206 break;
171 default: 207 default:
172 break; 208 break;
173 209
174 210
175 } 211 }
176 212
177 // END THREAD MUTEX 213
214
178 215
179 // reset the interp between frames
180 if(audioAccessing){
181 cout << "buffers swapped while update!\n";
182 }
183 updateAccessing = true;
184 temp = wavetableOld; 216 temp = wavetableOld;
185 wavetableOld = wavetableNew; 217 wavetableOld = wavetableNew;
186 wavetableNew = wavetableUpdate; 218 wavetableNew = wavetableUpdate;
187 wavetableUpdate = temp; 219 wavetableUpdate = temp;
220
221 // END THREAD MUTEX
188 updateAccessing = false; 222 updateAccessing = false;
189 223
190 frameInterpolator = 0.0; 224 frameInterpolator = 0.0;
191 framesPerSample = 2.0*ofGetFrameRate()/SAMPLE_RATE; // attempt to get a reasonable est. of how fast to interp 225 framesPerSample = 2.0*ofGetFrameRate()/SAMPLE_RATE; // attempt to get a reasonable est. of how fast to interp
192 226
193 } 227 }
194 //---------------------------------------------------------------- 228 //----------------------------------------------------------------
229 //----------------------------------------------------------------
230 // AUDIO THREAD STUFF
231 //----------------------------------------------------------------
195 // get next sample 232 // get next sample
233
196 double ScanPath::getNextSample(double aPhasor){ 234 double ScanPath::getNextSample(double aPhasor){
197 // move along path, interpolating between points 235 // move along path, interpolating between points
198 // move between frames too 236 // move between frames too
199 double alongPath = aPhasor*double(numElements); 237 double alongPath = aPhasor*double(numElements);
200 238
205 n1 = 0; 243 n1 = 0;
206 } 244 }
207 245
208 double frac = alongPath - double(n0); 246 double frac = alongPath - double(n0);
209 247
248 // TODO THRED MUTEX HERE!??
249 // this is called from audio thread
250 /*
251 int i = 0;
252 while(updateAccessing){
253 i++;
254 }
210 audioAccessing = true; 255 audioAccessing = true;
211 if (updateAccessing){ 256 cout << "update is accessing while audio is" << i << "times\n";
212 cout << "update is accessing while audio is\n"; 257
213 } 258 */
214
215 // TODO THRED MUTEX HERE!??
216 259
217 double oldsample = (1 - frac) * wavetableOld[n0] + frac * wavetableOld[n1]; 260 double oldsample = (1 - frac) * wavetableOld[n0] + frac * wavetableOld[n1];
218 261
219 double newsample = (1 - frac) * wavetableNew[n0] + frac * wavetableNew[n1]; 262 double newsample = (1 - frac) * wavetableNew[n0] + frac * wavetableNew[n1];
220 263