annotate testApp.mm @ 29:fabb3a5cdfc9

Timed session improvements. Desperate pathetic attempts to send a simple HTTP POST.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 22 Feb 2013 17:41:38 +0000
parents e2c62db1e265
children c0a6f7c66719
rev   line source
rt300@0 1 #include "testApp.h"
rt300@2 2
rt300@24 3 #define SLIDER_GUI_WIDTH 256
rt300@25 4 #define NUM_PARAMS 10
rt300@0 5 extern Grid theGridView;
rt300@0 6 extern PresetManager presetManager;
rt300@1 7 extern EventLogger eventLogger;
rt300@3 8 extern Frequencer frequencer;
rt300@9 9
rt300@24 10 const vector<string> parameterNames;
rt300@24 11
rt300@24 12 const string sliderParamNames[10] = {"Transpose", "1/4 note","1/6 note","1/7 note","1/8 note","Waveform", "Filter Type","Filter Freq", "Envelope","FM amt"};
rt300@24 13 //const vector<const string> v(ra[0],ra[1]);
rt300@24 14
rt300@24 15
rt300@7 16 //DeviceID3523537000
rt300@0 17 //--------------------------------------------------------------
rt300@0 18 void testApp::setup(){
rt300@16 19 paused = true;
rt300@29 20
rt300@24 21
rt300@16 22 ofBackground( 0, 0, 0 );
rt300@0 23 ofEnableAlphaBlending();
rt300@21 24 //ofEnableSmoothing();
rt300@0 25
rt300@29 26 // open an outgoing connection to HOST:PORT for OSC
rt300@29 27
rt300@29 28 sender.setup( OSC_HOST, OSC_PORT );
rt300@28 29 ofSetFrameRate(50);
rt300@0 30 // reciever
rt300@6 31 lastMoveTime = ofGetSystemTimeMicros();
rt300@0 32 prevTouchX = 0;
rt300@0 33 prevTouchY = 0;
rt300@0 34
rt300@0 35 xLocked = false;
rt300@0 36 yLocked = false;
rt300@0 37
rt300@0 38 numActiveTouches = 0;
rt300@3 39 touch0.setCoord(17./7., 2./3.);
rt300@0 40 touch1.setCoord(10,20);
rt300@3 41
rt300@0 42 prevTouch0.setCoord(1,2);
rt300@0 43 prevTouch1.setCoord(10,20);
rt300@0 44 prevDist = 10;
rt300@16 45 slowFactor = 0.98;
rt300@27 46 preventingMovePostScroll = false;
rt300@16 47
rt300@16 48 // the 5 harmonics for the frequencer
rt300@16 49 freqIndexes.push_back(0);
rt300@16 50 freqIndexes.push_back(4);
rt300@16 51 freqIndexes.push_back(6);
rt300@16 52 freqIndexes.push_back(7);
rt300@16 53 freqIndexes.push_back(8);
rt300@16 54
rt300@16 55
rt300@16 56 ofxiPhoneSetOrientation( OFXIPHONE_ORIENTATION_PORTRAIT ); // do this before setting up all the other objects
rt300@0 57
rt300@24 58 // initialise the interfaces
rt300@0 59 theGridView.init();
rt300@0 60
rt300@24 61 setupSliderGui();
rt300@24 62 sliderGUI->setVisible(true);
rt300@0 63
rt300@0 64 // initial slider vals
rt300@0 65 for(int i=0; i<10;i++){
rt300@0 66 sliderVals.push_back(64);
rt300@0 67 }
rt300@8 68
rt300@24 69 // initialise PD
rt300@24 70
rt300@2 71 int ticksPerBuffer = 8; // 8 * 64 = buffer len of 512
rt300@2 72
rt300@2 73 // setup the app core
rt300@2 74 core.setup(2, 1, 44100, ticksPerBuffer);
rt300@2 75
rt300@2 76 // setup OF sound stream
rt300@2 77 ofSoundStreamSetup(2, 1, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 3);
rt300@1 78
rt300@24 79
rt300@24 80
rt300@24 81 // set up iOS gui stuff
rt300@24 82 bottomTabViewController = [[BottomTabViewController alloc] initWithNibName:@"BottomTabViewController" bundle:nil];
rt300@24 83 [ofxiPhoneGetGLParentView() addSubview:bottomTabViewController.view];
rt300@24 84
rt300@24 85 [bottomTabViewController setAppRef:(id)this];
rt300@24 86 bottomTabViewController.view.frame = CGRectMake(0,getHeight()-44,getWidth(),44);
rt300@29 87 bottomTabViewController.view.hidden = YES;
rt300@28 88
rt300@24 89 /////
rt300@24 90
rt300@24 91 topButtonViewController = [[TopButtonViewController alloc] initWithNibName:@"TopButtonViewController" bundle:nil];
rt300@24 92 [ofxiPhoneGetGLParentView() addSubview:topButtonViewController.view];
rt300@24 93 [topButtonViewController setAppRef:(id)this];
rt300@24 94 [topButtonViewController show:(id)this];
rt300@24 95 topButtonViewController.view.frame = CGRectMake(0,0,getWidth(),44);
rt300@24 96
rt300@25 97 usernameAlertViewController = [[UsernameAlertViewController alloc] init];
rt300@26 98
rt300@27 99 helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil];
rt300@27 100 [ofxiPhoneGetGLParentView() addSubview:helpViewController.view];
rt300@27 101 [helpViewController setAppRef:(id)this];
rt300@27 102 helpViewController.view.hidden = YES;
rt300@27 103
rt300@25 104 /*
rt300@25 105 sliderViewController = [[SliderViewController alloc] initWithNibName:@"SLiderViewController" bundle:nil];
rt300@25 106 [ofxiPhoneGetGLParentView() addSubview:sliderViewController.view];
rt300@25 107
rt300@25 108 [sliderViewController setAppRef:(id)this];
rt300@25 109 [sliderViewController show:(id)this];
rt300@25 110
rt300@25 111 sliderViewController.view.frame = CGRectMake(0,getHeight()-43 - 363,getWidth(),44);
rt300@25 112 */
rt300@25 113
rt300@24 114 // initialise user logging stuff
rt300@5 115 presetManager.startupLoadAll();
rt300@29 116 tsc = [[TimedSessionController alloc] init];
rt300@29 117 [tsc setAppRef:(id)this];
rt300@7 118 eventLogger.init();
rt300@24 119
rt300@28 120 // timer.init();
rt300@29 121 whichInterfaceShowing = BOTH; // but timer will change this
rt300@29 122
rt300@28 123
rt300@29 124 if(eventLogger.questionnaireCompleted){
rt300@29 125 [bottomTabViewController show:(id)this withSelection:1];
rt300@29 126 }
rt300@27 127 setAllGUISliders(theGridView.getParams());
rt300@24 128 // GO
rt300@16 129 paused = false;
rt300@29 130
rt300@16 131
rt300@24 132 }
rt300@24 133
rt300@24 134
rt300@24 135 //--------------------------------------------------------------
rt300@24 136 void testApp::exit(){
rt300@25 137
rt300@25 138 presetManager.exitAndSaveAll();
rt300@25 139 eventLogger.exitAndSave();
rt300@25 140
rt300@24 141 core.exit();
rt300@24 142
rt300@25 143 // are these handled automatically?
rt300@25 144 //[introViewController release];
rt300@25 145 //[topButtonViewController release];
rt300@25 146 //[bottomTabViewController release];
rt300@24 147
rt300@24 148 delete sliderGUI;
rt300@24 149
rt300@24 150 cout << "exit done \n";
rt300@0 151 }
rt300@24 152
rt300@24 153 //--------------------------------------------------------------
rt300@3 154 #pragma mark GUI
rt300@16 155 //--
rt300@16 156 float testApp::getWidth(){
rt300@16 157 // depends on orientation
rt300@16 158 return ofGetWidth();
rt300@16 159
rt300@16 160 }
rt300@16 161 float testApp::getHeight(){
rt300@16 162 // depends on orientation
rt300@16 163 return ofGetHeight();
rt300@16 164
rt300@16 165 }
rt300@24 166 #pragma mark GUI
rt300@24 167 ////////////////////////////
rt300@24 168 // These functions called from iOS toolbars
rt300@24 169 //--------------------------------------------------------------
rt300@24 170 void testApp::lockSequencerPressed(bool locked){
rt300@24 171 theGridView.shiftCentreToSnapped();
rt300@24 172 xLocked = locked;
rt300@25 173 eventLogger.logEvent(SEQ_LOCKED);
rt300@25 174
rt300@24 175
rt300@24 176 }
rt300@24 177 //--------------------------------------------------------------
rt300@24 178 void testApp::lockSynthPressed(bool locked){
rt300@24 179 theGridView.shiftCentreToSnapped();
rt300@24 180 yLocked = locked;
rt300@25 181 eventLogger.logEvent(SYNTH_LOCKED);
rt300@24 182
rt300@24 183 }
rt300@24 184 //--------------------------------------------------------------
rt300@24 185 void testApp::seqStartStop(bool go){
rt300@24 186 if(!go){ //stop
rt300@24 187 core.pd.startMessage();
rt300@24 188 core.pd.addFloat(0);
rt300@24 189 core.pd.finishMessage("fromOF", "seqStartStop");
rt300@25 190 eventLogger.logEvent(PAUSE_PRESSED);
rt300@24 191 }else { // play
rt300@24 192 //stopSequencer();
rt300@24 193 core.pd.startMessage();
rt300@24 194 core.pd.addFloat(1);
rt300@24 195 core.pd.finishMessage("fromOF", "seqStartStop");
rt300@25 196 eventLogger.logEvent(PLAY_PRESSED);
rt300@24 197 }
rt300@24 198 }
rt300@24 199 //--------------------------------------------------------------
rt300@24 200 void testApp::showQuestionnaire(){
rt300@24 201 // stop updating / drawing
rt300@24 202
rt300@24 203 // if(eventLogger.questionnaireCompleted) return;
rt300@29 204
rt300@27 205 [topButtonViewController pausePressed:(id)this];
rt300@24 206
rt300@24 207 //stopSequencer
rt300@27 208 seqStartStop(false);
rt300@24 209
rt300@24 210 questionnaireViewController = [[QuestionnaireViewController alloc] initWithNibName:@"QuestionnaireViewController" bundle:nil];
rt300@24 211 [ofxiPhoneGetGLParentView() addSubview:questionnaireViewController.view];
rt300@24 212
rt300@24 213 [questionnaireViewController setAppRef:(id)this];
rt300@24 214 [questionnaireViewController show:(id)this];
rt300@24 215
rt300@24 216 whichInterfaceShowing = QUESTIONNAIRE;
rt300@24 217
rt300@24 218
rt300@24 219 }
rt300@24 220 //--------------------------------------------------------------
rt300@28 221 void testApp::questionnaireHidden(vector<int> answers, const char* userComments){
rt300@24 222 // send answers to server as json
rt300@28 223 eventLogger.questionnaireAnswersObtained(answers, userComments);
rt300@24 224
rt300@24 225 // set "we've done questionnaire" to true in event logger
rt300@27 226
rt300@24 227 whichInterfaceShowing = BOTH;
rt300@24 228 // tell bottomtabviewcontroller
rt300@29 229 [bottomTabViewController show:(id)this withSelection:1];
rt300@29 230
rt300@24 231 }
rt300@24 232 //--------------------------------------------------------------
rt300@24 233 void testApp::showIntro(){
rt300@25 234
rt300@24 235 cout << "SHOW INTRO\n";
rt300@24 236
rt300@29 237 [tsc cancelTimers];
rt300@29 238 bottomTabViewController.view.hidden = YES;
rt300@24 239 introViewController = [[IntroViewController alloc] initWithNibName:@"IntroViewController" bundle:nil];
rt300@24 240 [ofxiPhoneGetGLParentView() addSubview:introViewController.view];
rt300@24 241
rt300@24 242 [introViewController setAppRef:(id)this];
rt300@24 243 [introViewController show:(id)this];
rt300@24 244
rt300@24 245
rt300@24 246 whichInterfaceShowing = INTRO;
rt300@24 247
rt300@24 248 }
rt300@24 249 //--------------------------------------------------------------
rt300@24 250 void testApp::introHidden(bool OK){
rt300@24 251 if(OK){
rt300@25 252 eventLogger.consentGiven = true;
rt300@29 253 //
rt300@29 254 // clear presets?
rt300@29 255 presetManager.clearAll();
rt300@25 256 // show username prompt
rt300@25 257 [usernameAlertViewController showUserNamePrompt];
rt300@24 258 }
rt300@24 259 // no unOK
rt300@24 260 }
rt300@24 261 //--------------------------------------------------------------
rt300@24 262 // called from BottomTabViewController iOS segmented thing
rt300@24 263 void testApp::interfaceSelected(int which){
rt300@24 264 switch (which){
rt300@24 265 case 0:
rt300@24 266
rt300@24 267 whichInterfaceShowing = SLIDERS;
rt300@24 268 sliderGUI->setVisible(true);
rt300@24 269 // set the slider values to stuff got from zoomer
rt300@24 270 sliderVals = theGridView.getParams();
rt300@24 271 setAllGUISliders(sliderVals);
rt300@25 272
rt300@24 273 break;
rt300@24 274 case 1:
rt300@24 275
rt300@24 276 whichInterfaceShowing = BOTH;
rt300@24 277 sliderGUI->setVisible(true);
rt300@24 278 // set the slider values to stuff got from zoomer
rt300@24 279 sliderVals = theGridView.getParams();
rt300@24 280 setAllGUISliders(sliderVals);
rt300@24 281 break;
rt300@24 282 case 2:
rt300@24 283
rt300@24 284 sliderGUI->setVisible(false);
rt300@24 285 whichInterfaceShowing = ZOOMER;
rt300@24 286 break;
rt300@24 287 }
rt300@25 288 eventLogger.logEvent(SWAP_VIEW,TwoVector(),0.0, which);
rt300@24 289 }
rt300@24 290 //--------------------------------------------------------------
rt300@24 291 //--------------------------------------------------------------
rt300@24 292 void testApp::setupSliderGui(){
rt300@24 293 float xInit = OFX_UI_GLOBAL_WIDGET_SPACING;
rt300@24 294 float length = SLIDER_GUI_WIDTH - (OFX_UI_GLOBAL_WIDGET_SPACING*2);
rt300@24 295
rt300@24 296
rt300@24 297 //float dim = 42;
rt300@24 298
rt300@24 299 // make this iphone size...?
rt300@24 300 int height = 480;
rt300@24 301 int width = 320;
rt300@24 302 float dim = (height-10.0*OFX_UI_GLOBAL_WIDGET_SPACING)/10.0;
rt300@24 303 // LEFT GUI
rt300@24 304 sliderGUI = new ofxUICanvas(0,100,SLIDER_GUI_WIDTH,getHeight());
rt300@24 305
rt300@29 306
rt300@24 307 // Uh.. loop this
rt300@24 308 for(int i = 1; i<=10;i++){
rt300@29 309
rt300@24 310 ofxUISlider *slider;
rt300@25 311 slider = (ofxUISlider *)sliderGUI->addWidgetDown(new ofxUISlider(length,dim,0.0,127,64,sliderParamNames[i-1]));
rt300@24 312 slider->setDrawPadding(true);
rt300@28 313 slider->setDrawPaddingOutline(true);
rt300@24 314 if(i <= 5){
rt300@24 315 slider->setColorFill(ofColor(0,0,255));
rt300@24 316 slider->setColorFillHighlight(ofColor(0,0,255));
rt300@24 317 }else{
rt300@24 318 slider->setColorFill(ofColor(255,0,0));
rt300@24 319 slider->setColorFillHighlight(ofColor(255,0,0));
rt300@24 320 }
rt300@24 321
rt300@24 322 sliders.push_back(slider);
rt300@24 323 }
rt300@24 324
rt300@24 325
rt300@24 326
rt300@24 327 ofAddListener(sliderGUI->newGUIEvent, this, &testApp::sliderGUIEvent);
rt300@24 328
rt300@24 329 }
rt300@24 330 //--------------------------------------------------------------
rt300@24 331 void testApp::sliderGUIEvent(ofxUIEventArgs &e){
rt300@24 332 if(whichInterfaceShowing == ZOOMER){
rt300@24 333 cout << "GUI ERROR";
rt300@24 334 return;
rt300@24 335 }
rt300@24 336
rt300@0 337 // "normal" parameter changes
rt300@0 338 for(int i = 1; i<=10;i++){
rt300@0 339
rt300@24 340 if(e.widget->getName() == sliderParamNames[i-1])
rt300@0 341 {
rt300@0 342 //cout << "param change: " << p;
rt300@0 343 ofxUISlider *slider = (ofxUISlider *) e.widget;
rt300@3 344 sliderMoved(i-1,slider->getScaledValue()); // internal array 0 indexed
rt300@0 345 }
rt300@0 346 }
rt300@24 347
rt300@0 348 }
rt300@0 349 //--------------------------------------------------------------
rt300@3 350 void testApp::sliderMoved(int which, float value){
rt300@3 351 // an update caused by slider view being touched
rt300@0 352 sliderVals[which] = (int)value;
rt300@0 353 theGridView.setParams(sliderVals);
rt300@24 354
rt300@3 355 sendParametersToPD();
rt300@5 356
rt300@9 357 eventLogger.logEvent(CHANGE_SLIDER, TwoVector(),0.0,which , value);
rt300@5 358
rt300@0 359 }
rt300@0 360 //--------------------------------------------------------------
rt300@3 361 void testApp::setAllGUISliders(vector<int> vals){
rt300@3 362 // an update caused by zoomer view being moved
rt300@25 363 for(int i = 0; i<NUM_PARAMS;i++){
rt300@0 364 sliders[i]->setValue(vals[i]);
rt300@1 365 sliderVals[i] = vals[i];
rt300@0 366 }
rt300@3 367
rt300@0 368 }
rt300@0 369 //--------------------------------------------------------------
rt300@25 370 void testApp::randomise(){
rt300@25 371 // pick random settings for all params
rt300@25 372 for(int i=0; i < NUM_PARAMS ; i++){
rt300@25 373 sliderVals[i] = ofRandom(0, 127);
rt300@25 374
rt300@25 375 }
rt300@25 376
rt300@25 377 // send to grid, sliders and PD
rt300@25 378 theGridView.setParams(sliderVals);
rt300@25 379 setAllGUISliders(sliderVals);
rt300@25 380 sendParametersToPD();
rt300@25 381
rt300@25 382 eventLogger.logEvent(RANDOMISE, theGridView.getCoord() ,0.0);
rt300@25 383
rt300@25 384 }
rt300@27 385 //-
rt300@27 386 void testApp::showHelp(){
rt300@29 387 whichInterfaceShowing = HELP;
rt300@27 388 seqStartStop(false);
rt300@27 389 [topButtonViewController pausePressed:(id)this];
rt300@27 390 helpViewController.view.hidden = NO;
rt300@27 391 eventLogger.logEvent(HELP_PRESSED);
rt300@27 392 }
rt300@27 393 void testApp::helpHidden(){
rt300@29 394 whichInterfaceShowing = BOTH;
rt300@27 395 // start seq?
rt300@27 396
rt300@27 397 }
rt300@25 398 //--------------------------------------------------------------
rt300@3 399 void testApp::sendParametersToPD(){
rt300@3 400 // frequencer stuff to get 16 steps
rt300@3 401 vector<double> vals;
rt300@3 402
rt300@3 403
rt300@3 404 vals.push_back((sliderVals[0]+32)*8.); // DC offset
rt300@3 405 for(int i=1; i<5;i++){
rt300@3 406 vals.push_back((sliderVals[i] - 64)*2.);
rt300@3 407 }
rt300@3 408
rt300@3 409 vector<double> steps = frequencer.freqMagEdit(freqIndexes, vals);
rt300@3 410 // send a list using the List object
rt300@3 411 List seqSteps;
rt300@3 412
rt300@3 413 seqSteps.addSymbol("seqSteps");
rt300@3 414 for(int i=0; i < 16; i++){
rt300@3 415 seqSteps.addFloat(round(steps[i])); // rounding here??
rt300@3 416 }
rt300@3 417
rt300@3 418 core.pd.sendList("fromOF", seqSteps);
rt300@8 419
rt300@3 420 sendOscShape(sliderVals[5]);
rt300@3 421 sendFiltType(sliderVals[6]);
rt300@3 422 sendFiltFreq(sliderVals[7]);
rt300@3 423 sendEnvShape(sliderVals[8]);
rt300@3 424 sendModFreq(sliderVals[9]);
rt300@3 425
rt300@3 426 }
rt300@27 427 void testApp::setupNewUser(){
rt300@27 428 // this function is for supervised trials with my ipad
rt300@27 429 eventLogger.newUser();
rt300@27 430 }
rt300@8 431 //--------------------------------------------------------------
rt300@3 432 #pragma mark STANDARD OF FUNCTIONS
rt300@3 433 //--------------------------------------------------------------
rt300@0 434 void testApp::update(){
rt300@27 435
rt300@16 436 if(paused) return;
rt300@16 437
rt300@27 438 // continiue to move or zoom at velocity, unless snapped
rt300@3 439
rt300@3 440 if (numActiveTouches == 0){ // no touches, use momentum
rt300@3 441
rt300@22 442 if(moveVel.norm() > 0.3){
rt300@22 443 if(theGridView.snapped){
rt300@28 444 // stop it (snap check sends snap event)
rt300@22 445 moveVel.setCoord(0.0,0.0);
rt300@22 446 }else{
rt300@22 447 theGridView.move(moveVel);
rt300@22 448 moveVel = moveVel*slowFactor;
rt300@22 449 }
rt300@3 450 // and get new parameter values
rt300@5 451 setAllGUISliders(theGridView.getParams());
rt300@3 452 sendParametersToPD();
rt300@22 453 }else if(moveVel.norm() > 0.01){ // and less than 0.3
rt300@3 454 // stop it
rt300@3 455 moveVel.setCoord(0.0,0.0);
rt300@5 456 setAllGUISliders(theGridView.getParams());
rt300@3 457 sendParametersToPD();
rt300@5 458 eventLogger.logEvent(SCROLL_STOPPED, theGridView.getCoord() );
rt300@22 459
rt300@3 460 }else{
rt300@3 461 // stopped - do nothing
rt300@3 462 }
rt300@3 463
rt300@0 464 }
rt300@28 465 // ZOOM MOMENTUM
rt300@0 466 // continiue to zoom at velocity
rt300@0 467 if (numActiveTouches < 2 && abs(zoomVel)>0.001){
rt300@0 468 theGridView.zoom(zoomVel + 1.0); // +1 because zoomVel factor is + or - , wheras zoom is a multiplier near 1
rt300@0 469 zoomVel = zoomVel*slowFactor;
rt300@0 470 }
rt300@6 471
rt300@0 472 }
rt300@0 473 //--------------------------------------------------------------
rt300@0 474 void testApp::sendOSCParams(){
rt300@0 475
rt300@0 476 vector<int> params = theGridView.getParams(); // FILTER HERE? NEED FLOATS...
rt300@0 477 vector<int>::iterator iter = params.begin();
rt300@0 478
rt300@0 479 ofxOscMessage m;
rt300@0 480 m.setAddress( "p" );
rt300@0 481
rt300@0 482 for(;iter < params.end();iter++){
rt300@0 483
rt300@0 484 m.addFloatArg( *iter );
rt300@0 485
rt300@0 486 }
rt300@0 487 sender.sendMessage( m );
rt300@0 488 }
rt300@0 489 //--------------------------------------------------------------
rt300@0 490 void testApp::draw(){
rt300@24 491
rt300@24 492 switch (whichInterfaceShowing){
rt300@24 493 case SLIDERS:
rt300@24 494 break;
rt300@24 495 case ZOOMER:
rt300@24 496 theGridView.draw();
rt300@24 497 break;
rt300@24 498 case BOTH:
rt300@24 499
rt300@24 500 theGridView.draw();
rt300@24 501 break;
rt300@24 502 case INTRO:
rt300@24 503 break;
rt300@24 504
rt300@24 505 case QUESTIONNAIRE:
rt300@24 506 break;
rt300@24 507
rt300@0 508 }
rt300@4 509
rt300@0 510 }
rt300@0 511
rt300@0 512 //--------------------------------------------------------------
rt300@0 513 void testApp::touchDown(ofTouchEventArgs &touch){
rt300@27 514 // in slider gui area?
rt300@24 515 if(whichInterfaceShowing == SLIDERS){
rt300@24 516 return;
rt300@24 517 }else if (whichInterfaceShowing == BOTH && touch.x < (SLIDER_GUI_WIDTH+10)){
rt300@24 518 return;
rt300@24 519
rt300@24 520 }// otherwise we're good to let the zoomer handle touch
rt300@0 521
rt300@0 522 numActiveTouches++;
rt300@27 523 preventingMovePostScroll = false;
rt300@27 524
rt300@0 525 if(touch.id == 0){
rt300@0 526 touch0.setCoord(touch.x,touch.y);
rt300@0 527 prevTouch0 = touch0;
rt300@0 528 }else if(touch.id == 1){
rt300@0 529
rt300@0 530 touch1.setCoord(touch.x,touch.y);
rt300@0 531 prevTouch1 = touch1;
rt300@0 532
rt300@0 533 }
rt300@0 534 if(numActiveTouches == 1){
rt300@0 535 moveVel.setCoord(0.0, 0.0);
rt300@0 536 prevMove.setCoord(0.0, 0.0);
rt300@0 537 prevMove2.setCoord(0.0, 0.0);
rt300@27 538
rt300@0 539 }else if(numActiveTouches == 2){
rt300@0 540 zoomVel = 0.0;
rt300@0 541 prevZoom = 0.0;
rt300@0 542 prevZoom2 = 0.0;
rt300@0 543 double dist = touch1.distanceTo(touch0);
rt300@0 544 prevDist = dist;
rt300@0 545 }
rt300@0 546
rt300@0 547 }
rt300@0 548
rt300@0 549 //--------------------------------------------------------------
rt300@0 550 void testApp::touchMoved(ofTouchEventArgs &touch){
rt300@1 551
rt300@24 552 // TODO check if in gui area!!!
rt300@24 553 if(whichInterfaceShowing == SLIDERS){
rt300@24 554 return;
rt300@24 555 }else if (whichInterfaceShowing == BOTH && touch.x < (SLIDER_GUI_WIDTH+10)){
rt300@24 556 return;
rt300@24 557
rt300@27 558 }
rt300@24 559
rt300@27 560 // otherwise we're good to let the zoomer handle touch
rt300@0 561
rt300@1 562 // which one? keep track of each touch point
rt300@0 563 if(touch.id == 0){
rt300@0 564 touch0.setCoord(touch.x,touch.y);
rt300@0 565
rt300@0 566 }else if(touch.id == 1){
rt300@0 567
rt300@0 568 touch1.setCoord(touch.x,touch.y);
rt300@0 569 }
rt300@0 570
rt300@1 571 if(numActiveTouches == 1){
rt300@27 572 if(preventingMovePostScroll) return;
rt300@1 573 handleScroll();
rt300@0 574 }else if(numActiveTouches == 2){
rt300@1 575 handleZoom();
rt300@0 576
rt300@0 577 }
rt300@0 578 prevTouch0 = touch0;
rt300@0 579
rt300@0 580
rt300@0 581 }
rt300@5 582
rt300@1 583 //--------------------------------------------------------------
rt300@27 584 // handle a finger being dragged
rt300@1 585 void testApp::handleScroll(){
rt300@6 586
rt300@1 587 TwoVector move = touch0 - prevTouch0;
rt300@5 588 if(yLocked){
rt300@5 589 move.y = 0.0;
rt300@5 590 }
rt300@5 591 if(xLocked){
rt300@5 592 move.x = 0.0;
rt300@5 593 }
rt300@5 594
rt300@6 595 // check time since last move - if
rt300@6 596 unsigned int moveTime = ofGetSystemTimeMicros();
rt300@6 597 if(moveTime - lastMoveTime > 100000){
rt300@6 598 moveVel = TwoVector(); // zero
rt300@6 599 }else{
rt300@6 600 moveVel = (move*0.3 + prevMove*0.34 + prevMove2*0.38); // use the time
rt300@6 601
rt300@6 602 }
rt300@6 603 lastMoveTime = moveTime;
rt300@6 604
rt300@5 605
rt300@1 606 prevMove2 = prevMove;
rt300@1 607 prevMove = move;
rt300@1 608
rt300@5 609
rt300@3 610 theGridView.move(move);
rt300@3 611
rt300@3 612 // and get new parameter values
rt300@3 613 setAllGUISliders(theGridView.getParams());
rt300@3 614 sendParametersToPD();
rt300@1 615 }
rt300@1 616 //--------------------------------------------------------------
rt300@27 617 // handle pinch movememnt
rt300@1 618 void testApp::handleZoom(){
rt300@1 619 // work out change in difference
rt300@1 620 double dist = touch1.distanceTo(touch0);
rt300@1 621 double zoomFactor = prevDist/dist;
rt300@1 622
rt300@1 623 //TODO check for sensible maximums, e.g. spurious touch data
rt300@1 624 if(zoomFactor > 2.0 || zoomFactor < 0.5){
rt300@1 625 cout << "Zoom too much!!!!" << zoomFactor;
rt300@1 626 zoomFactor = 1.0;
rt300@1 627 }
rt300@1 628
rt300@1 629 zoomVel = (zoomFactor-1)*0.3 + prevZoom*0.34 + prevZoom2*0.38;
rt300@1 630 prevZoom2 = prevZoom;
rt300@1 631 prevZoom = (zoomFactor-1);
rt300@1 632
rt300@1 633 theGridView.zoom(zoomFactor);
rt300@1 634
rt300@1 635 prevDist = dist;
rt300@3 636
rt300@1 637 }
rt300@0 638 //--------------------------------------------------------------
rt300@0 639 void testApp::touchUp(ofTouchEventArgs &touch){
rt300@1 640 if(numActiveTouches > 0) numActiveTouches--; // dirty
rt300@27 641 preventingMovePostScroll = false;
rt300@24 642 // TODO check if in gui area!!!
rt300@24 643 if(whichInterfaceShowing == SLIDERS){
rt300@24 644 return;
rt300@24 645 }else if (whichInterfaceShowing == BOTH && touch.x < 256){
rt300@24 646 return;
rt300@1 647
rt300@24 648 }// otherwise we're good to let the zoomer handle touch
rt300@24 649
rt300@1 650
rt300@0 651 // which one?
rt300@0 652 if(touch.id == 0){
rt300@0 653 // tricky situation - we tried to zoom but may have left non-move finger on
rt300@0 654 prevTouch0.setCoord(touch.x,touch.y);
rt300@0 655
rt300@0 656 }else if(touch.id == 1){
rt300@0 657
rt300@0 658
rt300@0 659 prevTouch1.setCoord(0,0);
rt300@6 660
rt300@6 661 }
rt300@6 662 if(numActiveTouches == 0){
rt300@6 663 // check time since last move
rt300@6 664 // check time since last move - if
rt300@6 665 unsigned int moveTime = ofGetSystemTimeMicros();
rt300@6 666 if(moveTime - lastMoveTime > 100000){
rt300@6 667 moveVel = TwoVector(); // zero
rt300@6 668 }else{
rt300@6 669 moveVel = (move*0.3 + prevMove*0.34 + prevMove2*0.38); // use the time
rt300@6 670
rt300@6 671 }
rt300@6 672 lastMoveTime = moveTime;
rt300@27 673 }else if (numActiveTouches == 1){
rt300@27 674 // just zoomed , but now lifted one of the fingers
rt300@27 675 // can be bad if moved so create special mode to stop scroll (special modes bad!)
rt300@27 676 preventingMovePostScroll = true;
rt300@0 677 }
rt300@0 678
rt300@0 679 }
rt300@0 680
rt300@0 681 //--------------------------------------------------------------
rt300@0 682 void testApp::touchDoubleTap(ofTouchEventArgs &touch){
rt300@0 683 // preset?
rt300@25 684
rt300@25 685 /* ballses everything for some reason
rt300@25 686 TwoVector centre = TwoVector(getWidth()*0.5,getHeight()*0.5);
rt300@25 687 // if near centre
rt300@25 688 if((touch.x < centre.x+10) && (touch.x > centre.x-10) && (touch.y < centre.y+10) && (touch.y > centre.y-10)){
rt300@25 689 numActiveTouches = 0; // dirty
rt300@25 690 presetManager.showNameDialog();
rt300@25 691
rt300@25 692 }
rt300@25 693 */
rt300@25 694
rt300@0 695 }
rt300@0 696
rt300@0 697 //--------------------------------------------------------------
rt300@0 698 void testApp::lostFocus(){
rt300@0 699
rt300@0 700 }
rt300@0 701
rt300@0 702 //--------------------------------------------------------------
rt300@0 703 void testApp::gotFocus(){
rt300@0 704
rt300@0 705 }
rt300@0 706
rt300@0 707 //--------------------------------------------------------------
rt300@0 708 void testApp::gotMemoryWarning(){
rt300@0 709
rt300@0 710 }
rt300@0 711
rt300@0 712 //--------------------------------------------------------------
rt300@0 713 void testApp::deviceOrientationChanged(int newOrientation){
rt300@16 714 /*
rt300@0 715 cout << "orientation: " << newOrientation;
rt300@15 716
rt300@0 717 if(newOrientation == 4){
rt300@16 718 ofxiPhoneSetOrientation( OF_ORIENTATION_DEFAULT );
rt300@16 719
rt300@0 720 }else if(newOrientation == 3){
rt300@0 721 ofxiPhoneSetOrientation( OF_ORIENTATION_90_LEFT );
rt300@16 722 }else if(newOrientation == 3){
rt300@16 723 ofxiPhoneSetOrientation( OF_ORIENTATION_90_LEFT );
rt300@16 724 }
rt300@16 725
rt300@16 726
rt300@16 727 [ofxiPhoneGetGLView() updateDimensions];
rt300@16 728 */
rt300@0 729 }
rt300@0 730
rt300@0 731
rt300@0 732 //--------------------------------------------------------------
rt300@0 733 void testApp::touchCancelled(ofTouchEventArgs& args){
rt300@0 734
rt300@0 735 }
rt300@3 736 //---------------------------------------------------------------
rt300@3 737 // AUDIO STUFF
rt300@3 738 //---------------------------------------------------------------
rt300@3 739
rt300@3 740 #pragma mark AUDIO STREAMS
rt300@3 741 //--------------------------------------------------------------
rt300@3 742 void testApp::audioReceived(float * input, int bufferSize, int nChannels) {
rt300@3 743 core.audioReceived(input, bufferSize, nChannels);
rt300@3 744 }
rt300@3 745
rt300@3 746 void testApp::audioRequested(float * output, int bufferSize, int nChannels) {
rt300@3 747 core.audioRequested(output, bufferSize, nChannels);
rt300@3 748 }
rt300@3 749 //---------------------------------------------------------------
rt300@3 750 #pragma mark UTILITIES
rt300@0 751
rt300@0 752 // 5hz cut off
rt300@0 753 const double fB[3] = {0.049489956268677, 0.098979912537354, 0.049489956268677};
rt300@0 754
rt300@0 755 const double fA[3] = {1.000000000000000, -1.279632424997809, 0.477592250072517};
rt300@0 756
rt300@0 757 // 1hz cut off
rt300@0 758 //const double fB[3] = {0.002550535158536, 0.005101070317073, 0.002550535158536};
rt300@0 759
rt300@0 760 //const double fA[3] = {1.000000000000000, -1.852146485395936, 0.862348626030081};
rt300@0 761
rt300@0 762
rt300@0 763 //a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)- a(2)*y(n-1) - ... - a(na+1)*y(n-na)
rt300@0 764 //---------------------------------------------------------------
rt300@0 765 vector<float> testApp::vectorFilter(vector<float> newVec){
rt300@0 766 static vector<float> x0(10,0);
rt300@0 767 static vector<float> x1(10,0);
rt300@0 768 static vector<float> x2(10,0);
rt300@0 769 static vector<float> y0(10,0);
rt300@0 770 static vector<float> y1(10,0);
rt300@0 771 static vector<float> y2(10,0);
rt300@0 772
rt300@0 773 x0 = newVec;
rt300@0 774
rt300@0 775 // this low passes a bunch of params values all at once
rt300@0 776 int sz = newVec.size();
rt300@0 777 for(int i=0; i<sz; i++){
rt300@0 778 y0[i] = fB[0]*x0[i] + fB[1]*x1[i] + fB[2]*x2[i] - fA[1]*y1[i] - fA[2]*y2[i];
rt300@0 779 }
rt300@0 780 // shift
rt300@0 781 x2 = x1;
rt300@0 782 x1 = x0;
rt300@0 783 y2 = y1;
rt300@0 784 y1 = y0;
rt300@0 785
rt300@0 786 return y0;
rt300@2 787 }
rt300@24 788 float ctrlSmoother(float newsamp){
rt300@24 789 static float x1,x2,y1,y2;
rt300@24 790 float x0, y0;
rt300@24 791
rt300@24 792 x0 = newsamp;
rt300@24 793
rt300@24 794 y0 = fB[0]*x0 + fB[1]*x1 + fB[2]*x2 - fA[1]*y1 - fA[2]*y2;
rt300@24 795
rt300@24 796 // shift
rt300@24 797 x2 = x1;
rt300@24 798 x1 = x0;
rt300@24 799 y2 = y1;
rt300@24 800 y1 = y0;
rt300@24 801
rt300@24 802 return y0;
rt300@24 803 }
rt300@8 804 //---------------------------------------------------------------
rt300@3 805 void testApp::sendOscShape(int ctrlin){
rt300@3 806
rt300@3 807 static int numpoints = 5;
rt300@3 808 static int numcontrols = 5;
rt300@3 809 //float values[points][controls] =
rt300@3 810 float ctrlout[numcontrols];
rt300@3 811 string ctrlName[5] = {"pWidth" , "sqVol", "sawVol", "sineVol", "FMAmt"};
rt300@3 812 float values[5][5] =
rt300@3 813 {{0.5, 0., 0., 1., 1.}, // 0
rt300@3 814 {0.5, 0., 0., 1., 0.}, // 32
rt300@3 815 {0.5, 0., 1., 0., 0.}, // 64
rt300@3 816 {0.5, 1., 1., 0., 0.}, // 96
rt300@3 817 {0.01,1., 1., 0., 0.}}; // 127
rt300@3 818
rt300@3 819 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 820 int idx = floor(fidx);
rt300@3 821 float frac = fidx - idx;
rt300@3 822 for(int i=0; i < numcontrols; i++){
rt300@3 823 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 824 // send to PD
rt300@3 825 List toPD;
rt300@3 826
rt300@3 827 toPD.addSymbol(ctrlName[i]);
rt300@3 828 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@3 829
rt300@3 830 core.pd.sendList("fromOF", toPD);
rt300@3 831 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 832 }
rt300@3 833
rt300@2 834 }
rt300@8 835 //---------------------------------------------------------------
rt300@3 836 void testApp::sendFiltType(int ctrlin){
rt300@3 837 static int numpoints = 3;
rt300@3 838 static int numcontrols = 4;
rt300@3 839 //float values[points][controls] =
rt300@3 840 float ctrlout[numcontrols];
rt300@3 841 string ctrlName[4] = {"lpLev" , "bpLev", "hpLev", "reson"};
rt300@3 842 float values[3][4] =
rt300@24 843 {{2., 0., 0., 1.}, // 0
rt300@24 844 {0., 10., 0., 10.}, // 64
rt300@24 845 {0., 0., 1., 1.}}; // 127
rt300@3 846
rt300@3 847 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 848 int idx = floor(fidx);
rt300@3 849 float frac = fidx - idx;
rt300@3 850 for(int i=0; i < numcontrols; i++){
rt300@3 851 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 852 // send to PD
rt300@3 853 List toPD;
rt300@3 854
rt300@3 855 toPD.addSymbol(ctrlName[i]);
rt300@3 856 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@3 857
rt300@3 858 core.pd.sendList("fromOF", toPD);
rt300@3 859 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 860 }
rt300@2 861 }
rt300@8 862 //---------------------------------------------------------------
rt300@3 863 void testApp::sendFiltFreq(int ctrlin){
rt300@24 864 // smooth this
rt300@24 865 float fin = ctrlin;
rt300@24 866 float fout;
rt300@24 867 fout = (int)ctrlSmoother(fin);
rt300@3 868 List toPD;
rt300@3 869
rt300@3 870 toPD.addSymbol("filtFreq");
rt300@24 871 toPD.addFloat(fout);
rt300@3 872
rt300@3 873 core.pd.sendList("fromOF", toPD);
rt300@3 874 }
rt300@8 875 //---------------------------------------------------------------
rt300@3 876 void testApp::sendEnvShape(int ctrlin){
rt300@3 877 static int numpoints = 5;
rt300@3 878 static int numcontrols = 3;
rt300@3 879 //float values[points][controls] =
rt300@3 880 float ctrlout[numcontrols];
rt300@3 881 string ctrlName[3] = {"attack" , "decay", "sustain"};
rt300@3 882 float values[5][3] =
rt300@3 883 {{0., 0., 0.}, // 0
rt300@3 884 {0., 0.5, 0.}, // 32
rt300@3 885 {0.0, 1., 0.8}, // 64
rt300@3 886 {0.99, 0.3, 0.}, // 96
rt300@3 887 {0.3, 0.1, 0.}}; // 127
rt300@3 888
rt300@3 889 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 890 int idx = floor(fidx);
rt300@3 891 float frac = fidx - idx;
rt300@3 892 for(int i=0; i < numcontrols; i++){
rt300@3 893 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 894 // send to PD
rt300@3 895 List toPD;
rt300@3 896
rt300@3 897 toPD.addSymbol(ctrlName[i]);
rt300@3 898 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@3 899
rt300@3 900 core.pd.sendList("fromOF", toPD);
rt300@3 901 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 902 }
rt300@3 903 }
rt300@8 904 //---------------------------------------------------------------
rt300@3 905 void testApp::sendModFreq(int ctrlin){
rt300@3 906 float fm = ctrlin/127.;
rt300@3 907 List toPD;
rt300@3 908
rt300@3 909 toPD.addSymbol("FMFreq");
rt300@3 910 toPD.addFloat(fm); // rounding here??
rt300@3 911
rt300@3 912 core.pd.sendList("fromOF", toPD);
rt300@8 913 }
rt300@8 914 //---------------------------------------------------------------