annotate testApp.mm @ 27:ae4d2c3ce5e0

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