annotate examples/10-Instruments/d-box/sensors.cpp @ 539:b486344aa796 prerelease

merge
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 24 Jun 2016 01:43:53 +0100
parents 8fcfbfb32aa0
children
rev   line source
robert@464 1 /*
robert@464 2 * sensors.cpp
robert@464 3 *
robert@464 4 * Created on: May 28, 2014
robert@464 5 * Author: Victor Zappi
robert@464 6 */
robert@464 7
robert@464 8 #include <stdio.h>
robert@464 9 #include <pthread.h>
robert@464 10 #include <unistd.h>
robert@464 11 #include <math.h>
robert@464 12 #include <vector>
robert@464 13 #include "prio.h"
robert@464 14 #include "sensors.h"
robert@464 15 #include "OscillatorBank.h"
robert@464 16 #include "DboxSensors.h"
robert@464 17
robert@464 18
robert@464 19 //----------------------------------------
robert@464 20 // main extern variables
robert@464 21 //----------------------------------------
robert@464 22 extern vector<OscillatorBank*> gOscBanks;
robert@464 23 extern int gCurrentOscBank;
robert@464 24 extern int gNextOscBank;
robert@464 25 extern int gShouldStop;
robert@464 26 extern int gVerbose;
robert@464 27
robert@464 28 float gSensor0LatestTouchPos = 0; // most recent pitch touch location [0-1] on sensor 0, used by render.cpp
robert@464 29 int gSensor0LatestTouchNum = 0; // most recent num of touches on sensor 0, used by render.cpp
robert@464 30 float gSensor1LatestTouchPos[5]; // most recent touche locations on sensor 1, used by render.cpp
robert@464 31 //float gSensor1LatestTouchSizes[5];
robert@464 32 int gSensor1LatestTouchCount; // most recent number touches on sensor 1, used by render.cpp
robert@464 33 int gSensor1LatestTouchIndex = 0; // index of last touch in gSensor1LatestTouchPos[5], used by render.cpp
robert@464 34 int gLastFSRValue = 1799; // most recent fsr value, used by render.cpp
robert@464 35
robert@464 36
robert@464 37 DboxSensors Sensors;
robert@464 38
robert@464 39
robert@464 40 //----------------------------------------
robert@464 41 // var shared with logger
robert@464 42 //----------------------------------------
robert@464 43 int s0TouchNum = 0;
robert@464 44 float s0Touches_[MAX_TOUCHES];
robert@464 45 float s0Size_[MAX_TOUCHES];
robert@464 46 int s0LastIndex;
robert@464 47
robert@464 48 int s1TouchNum = 0;
robert@464 49 float s1Touches_[MAX_TOUCHES];
robert@464 50 float s1Size_[MAX_TOUCHES];
robert@464 51 int s1LastIndex;
robert@464 52
robert@464 53 int fsr = 1799;
robert@464 54
robert@464 55
robert@464 56
robert@464 57 using namespace std;
robert@464 58
robert@464 59 int initSensorLoop(int sensorAddress0, int sensorAddress1, int sensorType)
robert@464 60 {
robert@464 61 int tk0_bus = 1;
robert@464 62 int tk0_address = sensorAddress0;
robert@464 63 int tk1_bus = 1;
robert@464 64 int tk1_address = sensorAddress1;
robert@464 65 int tk_file = 0;
robert@464 66 int fsr_max = 1799;
robert@464 67 int fsr_pinNum = 4;
robert@464 68
robert@464 69 if(gVerbose==1)
robert@464 70 cout << "---------------->Init Control Thread" << endl;
robert@464 71
robert@464 72 if(Sensors.initSensors(tk0_bus, tk0_address, tk1_bus, tk1_address, tk_file, fsr_pinNum, fsr_max, sensorType)>0)
robert@464 73 {
robert@464 74 gShouldStop = 1;
robert@464 75 cout << "control cannot start" << endl;
robert@464 76 return -1;
robert@464 77 }
robert@464 78
robert@464 79 for(int i=0; i<MAX_TOUCHES; i++)
robert@464 80 {
robert@464 81 s0Touches_[i] = 0.0;
robert@464 82 s0Size_[i] = 0.0;
robert@464 83
robert@464 84 s1Touches_[i] = 0.0;
robert@464 85 s1Size_[i] = 0.0;
robert@464 86 }
robert@464 87
robert@464 88 return 0;
robert@464 89 }
robert@464 90
robert@464 91 void sensorLoop(void *)
robert@464 92 {
robert@464 93 timeval start, end;
robert@464 94 unsigned long elapsedTime;
robert@464 95 //float touchSize = 0; // once used for timbre
robert@464 96
robert@464 97
robert@464 98
robert@464 99 float *s0Touches;
robert@464 100 float *s0Size;
robert@464 101 int s0PrevTouchNum = 0;
robert@464 102 int s0SortedTouchIndices[MAX_TOUCHES];
robert@464 103 float s0SortedTouches[MAX_TOUCHES];
robert@464 104 float s0PrevSortedTouches[MAX_TOUCHES];
robert@464 105
robert@464 106 float *s1Touches;
robert@464 107 float *s1Size;
robert@464 108 int s1PrevTouchNum = 0;
robert@464 109 int s1SortedTouchIndices[MAX_TOUCHES];
robert@464 110 float s1SortedTouches[MAX_TOUCHES];
robert@464 111 float s1PrevSortedTouches[MAX_TOUCHES];
robert@464 112
robert@464 113 float freqScaler = 0;
robert@464 114 int fsrMin = 0;//50; // was 200
robert@464 115 int fsrMax = 1799;//1300; // was 800
robert@464 116 float vel = 0;
robert@464 117 float prevVel = 0;
robert@464 118 float filterMaxF = 0;
robert@464 119 if(gVerbose==1)
robert@464 120 dbox_printf("__________set Control Thread priority\n");
robert@464 121
robert@464 122 if(gVerbose==1)
robert@464 123 dbox_printf("_________________Control Thread!\n");
robert@464 124
robert@464 125 // get freq scaler, cos freqs must be scaled according to the wavetable used in the oscillator bank
robert@464 126 freqScaler = gOscBanks[gCurrentOscBank]->getFrequencyScaler();
robert@464 127 filterMaxF = gOscBanks[gCurrentOscBank]->filterMaxF;
robert@464 128
robert@464 129 // init time vals
robert@464 130 gettimeofday(&start, NULL);
robert@464 131
robert@464 132 // here we go, sensor loop until the end of the application
robert@464 133 while(!gShouldStop)
robert@464 134 {
robert@464 135 gettimeofday(&end, NULL);
robert@464 136 elapsedTime = ( (end.tv_sec*1000000+end.tv_usec) - (start.tv_sec*1000000+start.tv_usec) );
robert@464 137 if( elapsedTime<4000 )
robert@464 138 usleep(4000-elapsedTime);
robert@464 139 else
robert@464 140 dbox_printf("%d\n", (int)elapsedTime); // this print happens when something's gone bad...
robert@464 141
robert@464 142 if(Sensors.readSensors()==0)
robert@464 143 {
robert@464 144 s0TouchNum = Sensors.getTKTouchCount(0);
robert@464 145 s0Touches = Sensors.getTKXPositions(0);
robert@464 146 s0Size = Sensors.getTKTouchSize(0);
robert@464 147
robert@464 148 s1TouchNum = Sensors.getTKTouchCount(1);
robert@464 149 s1Touches = Sensors.getTKXPositions(1);
robert@464 150 s1Size = Sensors.getTKTouchSize(1);
robert@464 151
robert@464 152 for(int i=0; i<MAX_TOUCHES; i++)
robert@464 153 {
robert@464 154 s0Touches_[i] = s0Touches[i];
robert@464 155 s0Size_[i] = s0Size[i];
robert@464 156
robert@464 157 s1Touches_[i] = s1Touches[i];
robert@464 158 s1Size_[i] = s1Size[i];
robert@464 159 }
robert@464 160
robert@464 161 gSensor0LatestTouchNum = s0TouchNum;
robert@464 162 if(s0TouchNum > 0)
robert@464 163 {
robert@464 164 //-----------------------------------------------------------------------------------
robert@464 165 // timbre, speed and pitch
robert@464 166 //touchSize = 0; \\ once used for timbre
robert@464 167
robert@464 168 // if we have a number of touches different from previous round, track their order of arrival [calculated using distance comparison]
robert@464 169 if(s0PrevTouchNum!=s0TouchNum)
robert@464 170 {
robert@464 171 float distances[MAX_TOUCHES*(MAX_TOUCHES-1)]; // maximum number of current+previous touches between rounds with different num of touches
robert@464 172 int ids[MAX_TOUCHES*(MAX_TOUCHES-1)];
robert@464 173 // calculate all distance permutations between previous and current touches
robert@464 174 for(int i=0; i<s0TouchNum; i++)
robert@464 175 {
robert@464 176 for(int p=0; p<s0PrevTouchNum; p++)
robert@464 177 {
robert@464 178 int index = i*s0PrevTouchNum+p; // permutation id [says between which touches we are calculating distance]
robert@464 179 distances[index] = fabs(s0Touches[i]-s0PrevSortedTouches[p]);
robert@464 180 ids[index] = index;
robert@464 181 if(index>0)
robert@464 182 {
robert@464 183 // sort, from min to max distance
robert@464 184 float tmp;
robert@464 185 while(distances[index]<distances[index-1])
robert@464 186 {
robert@464 187 tmp = ids[index-1];
robert@464 188 ids[index-1] = ids[index];
robert@464 189 ids[index] = tmp;
robert@464 190
robert@464 191 tmp = distances[index-1];
robert@464 192 distances[index-1] = distances[index];
robert@464 193 distances[index] = tmp;
robert@464 194
robert@464 195 index--;
robert@464 196
robert@464 197 if(index == 0)
robert@464 198 break;
robert@464 199 }
robert@464 200 }
robert@464 201 }
robert@464 202 }
robert@464 203
robert@464 204 int sorted = 0;
robert@464 205 bool currAssigned[MAX_TOUCHES] = {false};
robert@464 206 bool prevAssigned[MAX_TOUCHES] = {false};
robert@464 207
robert@464 208 // track touches assigning index according to shortest distance
robert@464 209 for(int i=0; i<s0TouchNum*s0PrevTouchNum; i++)
robert@464 210 {
robert@464 211 int currentIndex = ids[i]/s0PrevTouchNum;
robert@464 212 int prevIndex = ids[i]%s0PrevTouchNum;
robert@464 213 // avoid double assignment
robert@464 214 if(!currAssigned[currentIndex] && !prevAssigned[prevIndex])
robert@464 215 {
robert@464 216 currAssigned[currentIndex] = true;
robert@464 217 prevAssigned[prevIndex] = true;
robert@464 218 s0SortedTouchIndices[currentIndex] = prevIndex;
robert@464 219 sorted++;
robert@464 220 }
robert@464 221 }
robert@464 222 // we still have to assign a free index to new touches
robert@464 223 if(s0PrevTouchNum<s0TouchNum)
robert@464 224 {
robert@464 225 for(int i=0; i<s0TouchNum; i++)
robert@464 226 {
robert@464 227 if(!currAssigned[i])
robert@464 228 s0SortedTouchIndices[i] = sorted++; // assign next free index
robert@464 229
robert@464 230 // update tracked value
robert@464 231 s0SortedTouches[s0SortedTouchIndices[i]] = s0Touches[i];
robert@464 232 s0PrevSortedTouches[i] = s0SortedTouches[i];
robert@464 233 if(s0SortedTouchIndices[i]==s0TouchNum-1)
robert@464 234 s0LastIndex = i;
robert@464 235
robert@464 236 // accumulate sizes for timbre
robert@464 237 //touchSize += s0Size[i];
robert@464 238 }
robert@464 239 }
robert@464 240 else // some touches have disappeared...
robert@464 241 {
robert@464 242 // ...we have to shift all indices...
robert@464 243 for(int i=s0PrevTouchNum-1; i>=0; i--)
robert@464 244 {
robert@464 245 if(!prevAssigned[i])
robert@464 246 {
robert@464 247 for(int j=0; j<s0TouchNum; j++)
robert@464 248 {
robert@464 249 // ...only if touches that disappeared were before the current one
robert@464 250 if(s0SortedTouchIndices[j]>i)
robert@464 251 s0SortedTouchIndices[j]--;
robert@464 252 }
robert@464 253 }
robert@464 254 }
robert@464 255 // done! now update
robert@464 256 for(int i=0; i<s0TouchNum; i++)
robert@464 257 {
robert@464 258 // update tracked value
robert@464 259 s0SortedTouches[s0SortedTouchIndices[i]] = s0Touches[i];
robert@464 260 s0PrevSortedTouches[i] = s0SortedTouches[i];
robert@464 261 if(s0SortedTouchIndices[i]==s0TouchNum-1)
robert@464 262 s0LastIndex = i;
robert@464 263
robert@464 264 // accumulate sizes for timbre
robert@464 265 //touchSize += s0Size[i];
robert@464 266 }
robert@464 267 }
robert@464 268 }
robert@464 269 else // nothing's changed since last round
robert@464 270 {
robert@464 271 for(int i=0; i<s0TouchNum; i++)
robert@464 272 {
robert@464 273 // update tracked value
robert@464 274 s0SortedTouches[s0SortedTouchIndices[i]] = s0Touches[i];
robert@464 275 s0PrevSortedTouches[i] = s0SortedTouches[i];
robert@464 276
robert@464 277 // accumulate sizes for timbre
robert@464 278 //touchSize += s0Size[i];
robert@464 279 }
robert@464 280 }
robert@464 281
robert@464 282 if(s0TouchNum == 0)
robert@464 283 s0LastIndex = -1;
robert@464 284
robert@464 285 // timbre
robert@464 286 //touchSize = (touchSize > 0.7) ? 1 : touchSize/0.7;
robert@464 287 //gOscBanks[gCurrentOscBank]->hopNumTh = log((1-touchSize)+1)/log(2)*20000;
robert@464 288 //gOscBanks[gCurrentOscBank]->hopNumTh = 0;
robert@464 289
robert@464 290
robert@464 291 // pitch, controlled by last touch
robert@464 292 //prevTouchPos = touch[touchIndex];
robert@464 293 //touchPos = (s0SortedTouches[s0TouchNum-1]-0.5)/0.5; // from [0,1] to [-1,1]
robert@464 294 gSensor0LatestTouchPos = s0SortedTouches[s0TouchNum-1];
robert@464 295 //touchPos = s0Touches[0];
robert@464 296 //gOscBanks[gCurrentOscBank]->pitchMultiplier = pow(2, touchPos);
robert@464 297 //-----------------------------------------------------------------------------------
robert@464 298
robert@464 299
robert@464 300
robert@464 301 //-----------------------------------------------------------------------------------
robert@464 302 // note on
robert@464 303 //if(s0PrevTouchNum == 0)
robert@464 304 // gOscBanks[gCurrentOscBank]->play();
robert@464 305 // fsr = Sensors.getFSRVAlue();
robert@464 306 fsr = gLastFSRValue;
robert@464 307 //dbox_printf("fsr: %d\n", fsr);
robert@464 308 if(!gOscBanks[gCurrentOscBank]->note)
robert@464 309 {
robert@464 310 vel = fsr;
robert@464 311 vel /= (float)(fsrMax-fsrMin);
robert@464 312
robert@464 313 vel = 1-vel;
robert@464 314 dbox_printf("Attack vel: %f\n", vel);
robert@464 315 gOscBanks[gCurrentOscBank]->play(vel);
robert@464 316 prevVel = vel;
robert@464 317 }
robert@464 318 else if(gOscBanks[gCurrentOscBank]->getEnvelopeState() != env_release)
robert@464 319 {
robert@464 320 fsr = (fsr > fsrMax) ? fsrMax : fsr;
robert@464 321 vel = (fsr < fsrMin) ? fsrMin : fsr;
robert@464 322 vel -= fsrMin;
robert@464 323 vel /= (float)(fsrMax-fsrMin);
robert@464 324 vel = 1-vel;
robert@464 325 if(vel > prevVel)
robert@464 326 {
robert@464 327 gOscBanks[gCurrentOscBank]->afterTouch(vel);
robert@464 328 prevVel = vel;
robert@464 329 }
robert@464 330 }
robert@464 331 //-----------------------------------------------------------------------------------
robert@464 332 }
robert@464 333 else
robert@464 334 {
robert@464 335 //prevFsr = 1799;
robert@464 336 //prevTouchPos = -1;
robert@464 337 //-----------------------------------------------------------------------------------
robert@464 338 // note off
robert@464 339 if(s0PrevTouchNum > 0)
robert@464 340 {
robert@464 341 if(gOscBanks[gCurrentOscBank]->state==bank_playing)
robert@464 342 gOscBanks[gCurrentOscBank]->stop();
robert@464 343 }
robert@464 344 //-----------------------------------------------------------------------------------
robert@464 345 }
robert@464 346
robert@464 347
robert@464 348
robert@464 349 // sensor 2
robert@464 350 //-----------------------------------------------------------------------------------
robert@464 351 //filter - calculated even when no touches on first sensor, to filter also release tail
robert@464 352 gOscBanks[gCurrentOscBank]->filterNum = s1TouchNum;
robert@464 353
robert@464 354 gSensor1LatestTouchCount = gOscBanks[gCurrentOscBank]->filterNum;
robert@464 355 for(int i = 0; i < gSensor1LatestTouchCount; i++) {
robert@464 356 gSensor1LatestTouchPos[i] = s1Touches[i];
robert@464 357 //gSensor1LatestTouchSizes[i] = s1Size[i];
robert@464 358 }
robert@464 359
robert@464 360 /* for(int i=0; i<gOscBanks[gCurrentOscBank]->filterNum; i++)
robert@464 361 {
robert@464 362 // touch pos is linear but freqs are log
robert@464 363 gOscBanks[gCurrentOscBank]->filterFreqs[i] = ((exp(s0Touches[i]*4)-1)/(exp(4)-1))*filterMaxF*freqScaler;
robert@464 364 //gOscBanks[gCurrentOscBank]->filterQ[i] = size[i]*5*(1+touch[i]*1000)*freqScaler;
robert@464 365 gOscBanks[gCurrentOscBank]->filterQ[i] = s0Size[i];
robert@464 366 if(gOscBanks[gCurrentOscBank]->filterFreqs[i]>500*freqScaler)
robert@464 367 gOscBanks[gCurrentOscBank]->filterPadding[i] = 1+100000*( (gOscBanks[gCurrentOscBank]->filterFreqs[i]-500*freqScaler)/(filterMaxF-500)*freqScaler );
robert@464 368 else
robert@464 369 gOscBanks[gCurrentOscBank]->filterPadding[i] = 1;
robert@464 370 }*/
robert@464 371
robert@464 372 // each touch on sensor 2 is a notch filter, whose Q is determined by touch size
robert@464 373 for(int i=0; i<gOscBanks[gCurrentOscBank]->filterNum; i++)
robert@464 374 {
robert@464 375 // map touch pos [which is linear] on freqs exponentially
robert@464 376 float freq = ((exp(s1Touches[i]*4)-1)/EXP_DENOM)*filterMaxF;
robert@464 377 gOscBanks[gCurrentOscBank]->filterFreqs[i] = freq*freqScaler;
robert@464 378 // also size is mapped exponentially on Q
robert@464 379 float siz = (exp(s1Size[i])-1)/1.71828;
robert@464 380 gOscBanks[gCurrentOscBank]->filterQ[i] = siz*( (filterMaxF-freq)/filterMaxF * 0.9 + 0.1 ); // size weight on Q decreases with frequency
robert@464 381 }
robert@464 382 //-----------------------------------------------------------------------------------
robert@464 383
robert@464 384
robert@464 385
robert@464 386 //-----------------------------------------------------------------------------------
robert@464 387 // sort touches on sensor 2
robert@464 388 if(s1TouchNum > 0)
robert@464 389 {
robert@464 390 // if we have a number of touches different from previous round, track their order of arrival [calculated using distance comparison]
robert@464 391 if(s1PrevTouchNum!=s1TouchNum)
robert@464 392 {
robert@464 393 float distances[MAX_TOUCHES*(MAX_TOUCHES-1)]; // maximum number of current+previous touches between rounds with different num of touches
robert@464 394 int ids[MAX_TOUCHES*(MAX_TOUCHES-1)];
robert@464 395 // calculate all distance permutations between previous and current touches
robert@464 396 for(int i=0; i<s1TouchNum; i++)
robert@464 397 {
robert@464 398 for(int p=0; p<s1PrevTouchNum; p++)
robert@464 399 {
robert@464 400 int index = i*s1PrevTouchNum+p; // permutation id [says between which touches we are calculating distance]
robert@464 401 distances[index] = fabs(s1Touches[i]-s1PrevSortedTouches[p]);
robert@464 402 ids[index] = index;
robert@464 403 if(index>0)
robert@464 404 {
robert@464 405 // sort, from min to max distance
robert@464 406 float tmp;
robert@464 407 while(distances[index]<distances[index-1])
robert@464 408 {
robert@464 409 tmp = ids[index-1];
robert@464 410 ids[index-1] = ids[index];
robert@464 411 ids[index] = tmp;
robert@464 412
robert@464 413 tmp = distances[index-1];
robert@464 414 distances[index-1] = distances[index];
robert@464 415 distances[index] = tmp;
robert@464 416
robert@464 417 index--;
robert@464 418
robert@464 419 if(index == 0)
robert@464 420 break;
robert@464 421 }
robert@464 422 }
robert@464 423 }
robert@464 424 }
robert@464 425
robert@464 426 int sorted = 0;
robert@464 427 bool currAssigned[MAX_TOUCHES] = {false};
robert@464 428 bool prevAssigned[MAX_TOUCHES] = {false};
robert@464 429
robert@464 430 // track touches assigning index according to shortest distance
robert@464 431 for(int i=0; i<s1TouchNum*s1PrevTouchNum; i++)
robert@464 432 {
robert@464 433 int currentIndex = ids[i]/s1PrevTouchNum;
robert@464 434 int prevIndex = ids[i]%s1PrevTouchNum;
robert@464 435 // avoid double assignment
robert@464 436 if(!currAssigned[currentIndex] && !prevAssigned[prevIndex])
robert@464 437 {
robert@464 438 currAssigned[currentIndex] = true;
robert@464 439 prevAssigned[prevIndex] = true;
robert@464 440 s1SortedTouchIndices[currentIndex] = prevIndex;
robert@464 441 sorted++;
robert@464 442 }
robert@464 443 }
robert@464 444 // we still have to assign a free index to new touches
robert@464 445 if(s1PrevTouchNum<s1TouchNum)
robert@464 446 {
robert@464 447 for(int i=0; i<s1TouchNum; i++)
robert@464 448 {
robert@464 449 if(!currAssigned[i])
robert@464 450 s1SortedTouchIndices[i] = sorted++; // assign next free index
robert@464 451
robert@464 452 // update tracked value
robert@464 453 s1SortedTouches[s1SortedTouchIndices[i]] = s1Touches[i];
robert@464 454 s1PrevSortedTouches[i] = s1SortedTouches[i];
robert@464 455 if(s1SortedTouchIndices[i]==s1TouchNum-1)
robert@464 456 s1LastIndex = i;
robert@464 457 }
robert@464 458 }
robert@464 459 else // some touches have disappeared...
robert@464 460 {
robert@464 461 // ...we have to shift all indices...
robert@464 462 for(int i=s1PrevTouchNum-1; i>=0; i--)
robert@464 463 {
robert@464 464 if(!prevAssigned[i])
robert@464 465 {
robert@464 466 for(int j=0; j<s1TouchNum; j++)
robert@464 467 {
robert@464 468 // ...only if touches that disappeared were before the current one
robert@464 469 if(s1SortedTouchIndices[j]>i)
robert@464 470 s1SortedTouchIndices[j]--;
robert@464 471 }
robert@464 472 }
robert@464 473 }
robert@464 474 // done! now update
robert@464 475 for(int i=0; i<s1TouchNum; i++)
robert@464 476 {
robert@464 477 // update tracked value
robert@464 478 s1SortedTouches[s1SortedTouchIndices[i]] = s1Touches[i];
robert@464 479 s1PrevSortedTouches[i] = s1SortedTouches[i];
robert@464 480 if(s1SortedTouchIndices[i]==s1TouchNum-1)
robert@464 481 s1LastIndex = i;
robert@464 482 }
robert@464 483 }
robert@464 484 }
robert@464 485 else // nothing's changed since last round
robert@464 486 {
robert@464 487 for(int i=0; i<s1TouchNum; i++)
robert@464 488 {
robert@464 489 // update tracked value
robert@464 490 s1SortedTouches[s1SortedTouchIndices[i]] = s1Touches[i];
robert@464 491 s1PrevSortedTouches[i] = s1SortedTouches[i];
robert@464 492 }
robert@464 493 }
robert@464 494 }
robert@464 495
robert@464 496 if(s1TouchNum > 0)
robert@464 497 {
robert@464 498 gSensor1LatestTouchIndex = s1LastIndex;
robert@464 499 }
robert@464 500 else
robert@464 501 s1LastIndex = -1;
robert@464 502
robert@464 503 /* dbox_printf("-----------------------------\nnum: %d, latest: %d\n", s1TouchNum, gSensor1LatestTouchIndex);
robert@464 504 for(int i=0; i<s1TouchNum; i++)
robert@464 505 dbox_printf("\t%f\n", gSensor1LatestTouchPos[i]);
robert@464 506 dbox_printf("------\n");
robert@464 507 for(int i=0; i<s1TouchNum; i++)
robert@464 508 dbox_printf("\t%f\n", s1SortedTouches[i]);*/
robert@464 509
robert@464 510
robert@464 511
robert@464 512 // update variables for both sensors
robert@464 513 s0PrevTouchNum = s0TouchNum;
robert@464 514 s1PrevTouchNum = s1TouchNum;
robert@464 515 }
robert@464 516 else
robert@464 517 dbox_printf("Come on instrument!\n"); //break
robert@464 518
robert@464 519 gettimeofday(&start, NULL);
robert@464 520 }
robert@464 521
robert@464 522 dbox_printf("sensor thread ended\n");
robert@464 523 }
robert@464 524
robert@464 525 void *keyboardLoop(void *)
robert@464 526 {
robert@464 527 if(gVerbose==1)
robert@464 528 cout << "_________________Keyboard Control Thread!" << endl;
robert@464 529
robert@464 530 char keyStroke = '.';
robert@464 531 cout << "Press q to quit." << endl;
robert@464 532
robert@464 533 float speed;
robert@464 534
robert@464 535 do
robert@464 536 {
robert@464 537 keyStroke = getchar();
robert@464 538 while(getchar()!='\n'); // to read the first stroke
robert@464 539
robert@464 540 switch (keyStroke)
robert@464 541 {
robert@464 542 //----------------------------------------------------------------------------
robert@464 543 case 'a':
robert@464 544 gOscBanks[gCurrentOscBank]->hopNumTh = 0;
robert@464 545 gOscBanks[gCurrentOscBank]->play(1);
robert@464 546 //cout << "Note on" << endl;
robert@464 547 break;
robert@464 548 case 's':
robert@464 549 if(gOscBanks[gCurrentOscBank]->state==bank_playing)
robert@464 550 {
robert@464 551 gOscBanks[gCurrentOscBank]->stop();
robert@464 552 //cout << "Note off" << endl;
robert@464 553 }
robert@464 554 break;
robert@464 555 //----------------------------------------------------------------------------
robert@464 556 case '[':
robert@464 557 gOscBanks[gCurrentOscBank]->freqMovement-=0.05;
robert@464 558 if(gOscBanks[gCurrentOscBank]->freqMovement<0)
robert@464 559 gOscBanks[gCurrentOscBank]->freqMovement = 0;
robert@464 560 //cout << "gOscBanks[gCurrentOscBank]->FreqMov: " << gOscBanks[gCurrentOscBank]->freqMovement << endl;
robert@464 561 break;
robert@464 562 case ']':
robert@464 563 gOscBanks[gCurrentOscBank]->freqMovement+=0.05;
robert@464 564 if(gOscBanks[gCurrentOscBank]->freqMovement>1)
robert@464 565 gOscBanks[gCurrentOscBank]->freqMovement = 1;
robert@464 566 //cout << "gOscBanks[gCurrentOscBank]->FreqMov: " << gOscBanks[gCurrentOscBank]->freqMovement << endl;
robert@464 567 break;
robert@464 568 //----------------------------------------------------------------------------
robert@464 569 case '<':
robert@464 570 speed = gOscBanks[gCurrentOscBank]->getSpeed() - 0.1 ;
robert@464 571 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 572 dbox_printf("Speed: %f\n", speed);
robert@464 573
robert@464 574 break;
robert@464 575 case '>':
robert@464 576 speed = gOscBanks[gCurrentOscBank]->getSpeed() + 0.1 ;
robert@464 577 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 578 dbox_printf("Speed: %f\n", speed);
robert@464 579 break;
robert@464 580 case '0':
robert@464 581 speed = 0.1;
robert@464 582 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 583 dbox_printf("Speed: %f\n", speed);
robert@464 584 break;
robert@464 585 case '1':
robert@464 586 speed = 0.5;
robert@464 587 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 588 dbox_printf("Speed: %f\n", speed);
robert@464 589 break;
robert@464 590 case '2':
robert@464 591 speed = 1;
robert@464 592 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 593 dbox_printf("Speed: %f\n", speed);
robert@464 594 break;
robert@464 595 case '3':
robert@464 596 speed = 2;
robert@464 597 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 598 dbox_printf("Speed: %f\n", speed);
robert@464 599 break;
robert@464 600 case '4':
robert@464 601 speed = 3;
robert@464 602 gOscBanks[gCurrentOscBank]->setSpeed(speed);
robert@464 603 dbox_printf("Speed: %f\n", speed);
robert@464 604 break;
robert@464 605 //----------------------------------------------------------------------------
robert@464 606 case 'z':
robert@464 607 gOscBanks[gCurrentOscBank]->setJumpHop(0);
robert@464 608 break;
robert@464 609 case 'x':
robert@464 610 gOscBanks[gCurrentOscBank]->setJumpHop(100);
robert@464 611 break;
robert@464 612 case 'c':
robert@464 613 gOscBanks[gCurrentOscBank]->setJumpHop(600);
robert@464 614 break;
robert@464 615 case 'v':
robert@464 616 gOscBanks[gCurrentOscBank]->setJumpHop(1100);
robert@464 617 break;
robert@464 618 case 'b':
robert@464 619 gOscBanks[gCurrentOscBank]->setJumpHop(2000);
robert@464 620 break;
robert@464 621 case 'n':
robert@464 622 gOscBanks[gCurrentOscBank]->setJumpHop(gOscBanks[gCurrentOscBank]->getLastHop());
robert@464 623 break;
robert@464 624 //----------------------------------------------------------------------------
robert@464 625 case 'q':
robert@464 626 gShouldStop = true;
robert@464 627 break;
robert@464 628 case 'o':
robert@464 629 gNextOscBank = (gCurrentOscBank + 1) % gOscBanks.size();
robert@464 630 break;
robert@464 631 default:
robert@464 632 break;
robert@464 633 //----------------------------------------------------------------------------
robert@464 634 }
robert@464 635 usleep(1000); /* Wait 1ms to avoid checking too quickly */
robert@464 636 }
robert@464 637 while (keyStroke!='q');
robert@464 638
robert@464 639 cout << "keyboard thread ended" << endl;
robert@464 640
robert@464 641 return (void *)0;
robert@464 642 }