annotate testApp.mm @ 40:af06bb942d58

Commit Before reverting to beta release to generate dSYM.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 12 Apr 2013 16:13:42 +0100
parents df7c08faf541
children 3d627dce8bf0
rev   line source
rt300@0 1 #include "testApp.h"
rt300@2 2
rt300@39 3
rt300@0 4 extern Grid theGridView;
rt300@0 5 extern PresetManager presetManager;
rt300@1 6 extern EventLogger eventLogger;
rt300@3 7 extern Frequencer frequencer;
rt300@35 8 extern Hilbert hilbert;
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@35 16
rt300@7 17 //DeviceID3523537000
rt300@0 18 //--------------------------------------------------------------
rt300@0 19 void testApp::setup(){
rt300@16 20 paused = true;
rt300@39 21 sendMIDIAndOSC = false;
rt300@37 22
rt300@16 23 ofBackground( 0, 0, 0 );
rt300@0 24 ofEnableAlphaBlending();
rt300@36 25 currentSequence = 0;
rt300@40 26 //ofEnableSmoothing();
rt300@37 27
rt300@29 28 // open an outgoing connection to HOST:PORT for OSC
rt300@29 29
rt300@29 30 sender.setup( OSC_HOST, OSC_PORT );
rt300@28 31 ofSetFrameRate(50);
rt300@0 32 // reciever
rt300@6 33 lastMoveTime = ofGetSystemTimeMicros();
rt300@0 34 prevTouchX = 0;
rt300@0 35 prevTouchY = 0;
rt300@0 36
rt300@0 37 xLocked = false;
rt300@0 38 yLocked = false;
rt300@0 39
rt300@0 40 numActiveTouches = 0;
rt300@3 41 touch0.setCoord(17./7., 2./3.);
rt300@0 42 touch1.setCoord(10,20);
rt300@3 43
rt300@0 44 prevTouch0.setCoord(1,2);
rt300@0 45 prevTouch1.setCoord(10,20);
rt300@0 46 prevDist = 10;
rt300@16 47 slowFactor = 0.98;
rt300@27 48 preventingMovePostScroll = false;
rt300@16 49
rt300@16 50 // the 5 harmonics for the frequencer
rt300@16 51 freqIndexes.push_back(0);
rt300@16 52 freqIndexes.push_back(4);
rt300@16 53 freqIndexes.push_back(6);
rt300@16 54 freqIndexes.push_back(7);
rt300@16 55 freqIndexes.push_back(8);
rt300@16 56
rt300@16 57
rt300@16 58 ofxiPhoneSetOrientation( OFXIPHONE_ORIENTATION_PORTRAIT ); // do this before setting up all the other objects
rt300@37 59 /*
rt300@37 60 sliderViewController = [[SliderViewController alloc] initWithNibName:@"SliderViewController" bundle:nil];
rt300@37 61 [ofxiPhoneGetGLParentView() addSubview:sliderViewController.view];
rt300@37 62 [sliderViewController setAppRef:(id)this];
rt300@37 63 sliderViewController.view.frame = CGRectMake(0,getHeight()-43 - 363,getWidth(),363);
rt300@37 64 */
rt300@24 65 // initialise the interfaces
rt300@0 66 theGridView.init();
rt300@0 67
rt300@24 68 setupSliderGui();
rt300@24 69 sliderGUI->setVisible(true);
rt300@0 70
rt300@0 71 // initial slider vals
rt300@0 72 for(int i=0; i<10;i++){
rt300@0 73 sliderVals.push_back(64);
rt300@0 74 }
rt300@37 75
rt300@24 76 // initialise PD
rt300@24 77
rt300@2 78 int ticksPerBuffer = 8; // 8 * 64 = buffer len of 512
rt300@2 79
rt300@2 80 // setup the app core
rt300@2 81 core.setup(2, 1, 44100, ticksPerBuffer);
rt300@2 82
rt300@2 83 // setup OF sound stream
rt300@2 84 ofSoundStreamSetup(2, 1, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 3);
rt300@1 85
rt300@37 86
rt300@37 87
rt300@24 88 // set up iOS gui stuff
rt300@24 89 bottomTabViewController = [[BottomTabViewController alloc] initWithNibName:@"BottomTabViewController" bundle:nil];
rt300@24 90 [ofxiPhoneGetGLParentView() addSubview:bottomTabViewController.view];
rt300@24 91
rt300@24 92 [bottomTabViewController setAppRef:(id)this];
rt300@24 93 bottomTabViewController.view.frame = CGRectMake(0,getHeight()-44,getWidth(),44);
rt300@29 94 bottomTabViewController.view.hidden = YES;
rt300@37 95
rt300@24 96 /////
rt300@24 97
rt300@24 98 topButtonViewController = [[TopButtonViewController alloc] initWithNibName:@"TopButtonViewController" bundle:nil];
rt300@24 99 [ofxiPhoneGetGLParentView() addSubview:topButtonViewController.view];
rt300@24 100 [topButtonViewController setAppRef:(id)this];
rt300@24 101 [topButtonViewController show:(id)this];
rt300@24 102 topButtonViewController.view.frame = CGRectMake(0,0,getWidth(),44);
rt300@24 103
rt300@25 104 usernameAlertViewController = [[UsernameAlertViewController alloc] init];
rt300@26 105
rt300@27 106 helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil];
rt300@27 107 [ofxiPhoneGetGLParentView() addSubview:helpViewController.view];
rt300@27 108 [helpViewController setAppRef:(id)this];
rt300@27 109 helpViewController.view.hidden = YES;
rt300@27 110
rt300@36 111
rt300@25 112
rt300@36 113 // load preset sequences
rt300@36 114 loadSequences();
rt300@36 115
rt300@24 116 // initialise user logging stuff
rt300@30 117 presetManager.startLoadAll();
rt300@29 118 tsc = [[TimedSessionController alloc] init];
rt300@29 119 [tsc setAppRef:(id)this];
rt300@7 120 eventLogger.init();
rt300@24 121
rt300@28 122 // timer.init();
rt300@29 123 whichInterfaceShowing = BOTH; // but timer will change this
rt300@37 124
rt300@28 125
rt300@29 126 if(eventLogger.questionnaireCompleted){
rt300@29 127 [bottomTabViewController show:(id)this withSelection:1];
rt300@29 128 }
rt300@27 129 setAllGUISliders(theGridView.getParams());
rt300@24 130 // GO
rt300@16 131 paused = false;
rt300@37 132
rt300@36 133 randomise(); // everyone starts from random position (or where they left off??)
rt300@32 134
rt300@32 135 ofxiPhoneExternalDisplay::mirrorOn();
rt300@37 136
rt300@37 137 //[sliderViewController show:(id)this];
rt300@36 138
rt300@39 139 /////////////////////////
rt300@39 140 // MIDI
rt300@39 141
rt300@39 142 midiChannel = 7;
rt300@39 143 midiOffset = 0;
rt300@39 144
rt300@39 145 // enables the network midi session between iOS and Mac OSX on a
rt300@39 146 // local wifi network
rt300@39 147 //
rt300@39 148 // in ofxMidi: open the input/outport network ports named "Session 1"
rt300@39 149 //
rt300@39 150 // on OSX: use the Audio MIDI Setup Utility to connect to the iOS device
rt300@39 151 //
rt300@39 152 ofxMidi::enableNetworking();
rt300@39 153
rt300@39 154 // list the number of available input & output ports
rt300@39 155 ofxMidiIn::listPorts();
rt300@39 156 ofxMidiOut::listPorts();
rt300@39 157
rt300@39 158 // create and open input ports
rt300@39 159 for(int i = 0; i < ofxMidiIn::getNumPorts(); ++i) {
rt300@39 160
rt300@39 161 // new object
rt300@39 162 inputs.push_back(new ofxMidiIn);
rt300@39 163
rt300@39 164 // set this class to receive incoming midi events
rt300@39 165 inputs[i]->addListener(this);
rt300@39 166
rt300@39 167 // open input port via port number
rt300@39 168 inputs[i]->openPort(i);
rt300@39 169 }
rt300@39 170
rt300@39 171 // create and open output ports
rt300@39 172 for(int i = 0; i < ofxMidiOut::getNumPorts(); ++i) {
rt300@39 173
rt300@39 174 // new object
rt300@39 175 outputs.push_back(new ofxMidiOut);
rt300@39 176
rt300@39 177 // open input port via port number
rt300@39 178 outputs[i]->openPort(i);
rt300@39 179 }
rt300@39 180
rt300@39 181 // set this class to receieve midi device (dis)connection events
rt300@39 182 ofxMidi::setConnectionListener(this);
rt300@39 183
rt300@39 184 // END MIDI
rt300@39 185
rt300@24 186 }
rt300@24 187
rt300@24 188
rt300@24 189 //--------------------------------------------------------------
rt300@24 190 void testApp::exit(){
rt300@37 191
rt300@25 192 presetManager.exitAndSaveAll();
rt300@25 193 eventLogger.exitAndSave();
rt300@37 194
rt300@24 195 core.exit();
rt300@24 196
rt300@25 197 // are these handled automatically?
rt300@25 198 //[introViewController release];
rt300@25 199 //[topButtonViewController release];
rt300@25 200 //[bottomTabViewController release];
rt300@24 201
rt300@39 202 // clean up MIDI
rt300@39 203 for(int i = 0; i < inputs.size(); ++i) {
rt300@39 204 inputs[i]->closePort();
rt300@39 205 inputs[i]->removeListener(this);
rt300@39 206 delete inputs[i];
rt300@39 207 }
rt300@39 208
rt300@39 209 for(int i = 0; i < outputs.size(); ++i) {
rt300@39 210 outputs[i]->closePort();
rt300@39 211 delete outputs[i];
rt300@39 212 }
rt300@39 213
rt300@24 214 delete sliderGUI;
rt300@37 215
rt300@24 216 cout << "exit done \n";
rt300@0 217 }
rt300@24 218
rt300@24 219 //--------------------------------------------------------------
rt300@3 220 #pragma mark GUI
rt300@16 221 //--
rt300@16 222 float testApp::getWidth(){
rt300@16 223 // depends on orientation
rt300@16 224 return ofGetWidth();
rt300@16 225
rt300@16 226 }
rt300@16 227 float testApp::getHeight(){
rt300@16 228 // depends on orientation
rt300@16 229 return ofGetHeight();
rt300@16 230
rt300@16 231 }
rt300@24 232 #pragma mark GUI
rt300@24 233 ////////////////////////////
rt300@24 234 // These functions called from iOS toolbars
rt300@24 235 //--------------------------------------------------------------
rt300@24 236 void testApp::lockSequencerPressed(bool locked){
rt300@24 237 theGridView.shiftCentreToSnapped();
rt300@24 238 xLocked = locked;
rt300@39 239 eventLogger.logEvent(SEQ_LOCKED); // TODO whatabout unlock?
rt300@25 240
rt300@24 241
rt300@24 242 }
rt300@24 243 //--------------------------------------------------------------
rt300@24 244 void testApp::lockSynthPressed(bool locked){
rt300@24 245 theGridView.shiftCentreToSnapped();
rt300@24 246 yLocked = locked;
rt300@25 247 eventLogger.logEvent(SYNTH_LOCKED);
rt300@24 248
rt300@24 249 }
rt300@24 250 //--------------------------------------------------------------
rt300@24 251 void testApp::seqStartStop(bool go){
rt300@24 252 if(!go){ //stop
rt300@24 253 core.pd.startMessage();
rt300@24 254 core.pd.addFloat(0);
rt300@24 255 core.pd.finishMessage("fromOF", "seqStartStop");
rt300@25 256 eventLogger.logEvent(PAUSE_PRESSED);
rt300@24 257 }else { // play
rt300@24 258 //stopSequencer();
rt300@24 259 core.pd.startMessage();
rt300@24 260 core.pd.addFloat(1);
rt300@24 261 core.pd.finishMessage("fromOF", "seqStartStop");
rt300@25 262 eventLogger.logEvent(PLAY_PRESSED);
rt300@24 263 }
rt300@24 264 }
rt300@24 265 //--------------------------------------------------------------
rt300@24 266 void testApp::showQuestionnaire(){
rt300@24 267 // stop updating / drawing
rt300@37 268
rt300@24 269 // if(eventLogger.questionnaireCompleted) return;
rt300@37 270
rt300@27 271 [topButtonViewController pausePressed:(id)this];
rt300@24 272
rt300@24 273 //stopSequencer
rt300@27 274 seqStartStop(false);
rt300@24 275
rt300@24 276 questionnaireViewController = [[QuestionnaireViewController alloc] initWithNibName:@"QuestionnaireViewController" bundle:nil];
rt300@24 277 [ofxiPhoneGetGLParentView() addSubview:questionnaireViewController.view];
rt300@24 278
rt300@24 279 [questionnaireViewController setAppRef:(id)this];
rt300@24 280 [questionnaireViewController show:(id)this];
rt300@24 281
rt300@24 282 whichInterfaceShowing = QUESTIONNAIRE;
rt300@24 283
rt300@37 284
rt300@24 285 }
rt300@24 286 //--------------------------------------------------------------
rt300@28 287 void testApp::questionnaireHidden(vector<int> answers, const char* userComments){
rt300@24 288 // send answers to server as json
rt300@28 289 eventLogger.questionnaireAnswersObtained(answers, userComments);
rt300@39 290
rt300@39 291 freeUseMode();
rt300@37 292
rt300@39 293 }
rt300@39 294 //--------------------------------------------------------------
rt300@39 295 // shortcut function for testing
rt300@39 296 void testApp::justStart(){
rt300@39 297 freeUseMode();
rt300@39 298 }
rt300@39 299 //--------------------------------------------------------------
rt300@39 300 void testApp::freeUseMode(){
rt300@38 301 interfaceSelected(1);
rt300@32 302 [bottomTabViewController show:(id)this withSelection:1];
rt300@24 303
rt300@38 304 [topButtonViewController enableSmoothSwitch:(id)this];
rt300@37 305
rt300@39 306 sendMIDIAndOSC = true;
rt300@38 307 }
rt300@38 308 //--------------------------------------------------------------
rt300@38 309 void testApp::setInterp(int state){
rt300@38 310 if(state == 0){
rt300@38 311 theGridView.setInterpolation(Grid::NO_INTERPOLATION);
rt300@39 312 eventLogger.logEvent(SMOOTHING_OFF);
rt300@38 313 }else if(state == 1){
rt300@38 314 theGridView.setInterpolation(Grid::INTERPOLATE_GRID);
rt300@39 315 eventLogger.logEvent(SMOOTHING_ON);
rt300@38 316 }
rt300@38 317 // tell sliders and PD
rt300@38 318 setAllGUISliders(theGridView.getParams());
rt300@39 319
rt300@33 320 }
rt300@33 321 //--------------------------------------------------------------
rt300@24 322 void testApp::showIntro(){
rt300@37 323
rt300@24 324 cout << "SHOW INTRO\n";
rt300@37 325
rt300@29 326 [tsc cancelTimers];
rt300@29 327 bottomTabViewController.view.hidden = YES;
rt300@24 328 introViewController = [[IntroViewController alloc] initWithNibName:@"IntroViewController" bundle:nil];
rt300@24 329 [ofxiPhoneGetGLParentView() addSubview:introViewController.view];
rt300@24 330
rt300@24 331 [introViewController setAppRef:(id)this];
rt300@24 332 [introViewController show:(id)this];
rt300@24 333
rt300@24 334
rt300@24 335 whichInterfaceShowing = INTRO;
rt300@24 336
rt300@24 337 }
rt300@24 338 //--------------------------------------------------------------
rt300@24 339 void testApp::introHidden(bool OK){
rt300@24 340 if(OK){
rt300@25 341 eventLogger.consentGiven = true;
rt300@29 342 //
rt300@29 343 // clear presets?
rt300@29 344 presetManager.clearAll();
rt300@25 345 // show username prompt
rt300@25 346 [usernameAlertViewController showUserNamePrompt];
rt300@32 347 // reset top buttons
rt300@32 348 [topButtonViewController unlockAll];
rt300@32 349 randomise();
rt300@24 350 }
rt300@24 351 // no unOK
rt300@24 352 }
rt300@24 353 //--------------------------------------------------------------
rt300@39 354 // called from BottomTabViewController iOS segmented thing, also timed session controller
rt300@24 355 void testApp::interfaceSelected(int which){
rt300@24 356 switch (which){
rt300@39 357 case 0: // slider
rt300@37 358
rt300@24 359 whichInterfaceShowing = SLIDERS;
rt300@24 360 sliderGUI->setVisible(true);
rt300@24 361 // set the slider values to stuff got from zoomer
rt300@24 362 sliderVals = theGridView.getParams();
rt300@24 363 setAllGUISliders(sliderVals);
rt300@25 364
rt300@24 365 break;
rt300@39 366 case 1: // both
rt300@37 367
rt300@24 368 whichInterfaceShowing = BOTH;
rt300@24 369 sliderGUI->setVisible(true);
rt300@24 370 // set the slider values to stuff got from zoomer
rt300@24 371 sliderVals = theGridView.getParams();
rt300@24 372 setAllGUISliders(sliderVals);
rt300@39 373
rt300@24 374 break;
rt300@39 375 case 2: // zoomer
rt300@37 376
rt300@24 377 sliderGUI->setVisible(false);
rt300@24 378 whichInterfaceShowing = ZOOMER;
rt300@24 379 break;
rt300@24 380 }
rt300@39 381 eventLogger.logEvent(SWAP_VIEW,theGridView.getCoord(),theGridView.getScale(), which);
rt300@24 382 }
rt300@24 383 //--------------------------------------------------------------
rt300@24 384 //--------------------------------------------------------------
rt300@24 385 void testApp::setupSliderGui(){
rt300@39 386
rt300@24 387 float length = SLIDER_GUI_WIDTH - (OFX_UI_GLOBAL_WIDGET_SPACING*2);
rt300@24 388
rt300@24 389
rt300@24 390 //float dim = 42;
rt300@24 391
rt300@24 392 // make this iphone size...?
rt300@24 393 int height = 480;
rt300@39 394
rt300@24 395 float dim = (height-10.0*OFX_UI_GLOBAL_WIDGET_SPACING)/10.0;
rt300@24 396 // LEFT GUI
rt300@30 397 sliderGUI = new ofxUICanvas(0,160,SLIDER_GUI_WIDTH,getHeight());
rt300@37 398
rt300@24 399 // Uh.. loop this
rt300@24 400 for(int i = 1; i<=10;i++){
rt300@37 401
rt300@24 402 ofxUISlider *slider;
rt300@25 403 slider = (ofxUISlider *)sliderGUI->addWidgetDown(new ofxUISlider(length,dim,0.0,127,64,sliderParamNames[i-1]));
rt300@24 404 slider->setDrawPadding(true);
rt300@28 405 slider->setDrawPaddingOutline(true);
rt300@24 406 if(i <= 5){
rt300@24 407 slider->setColorFill(ofColor(0,0,255));
rt300@24 408 slider->setColorFillHighlight(ofColor(0,0,255));
rt300@24 409 }else{
rt300@24 410 slider->setColorFill(ofColor(255,0,0));
rt300@24 411 slider->setColorFillHighlight(ofColor(255,0,0));
rt300@24 412 }
rt300@24 413
rt300@24 414 sliders.push_back(slider);
rt300@24 415 }
rt300@24 416
rt300@37 417
rt300@24 418
rt300@24 419 ofAddListener(sliderGUI->newGUIEvent, this, &testApp::sliderGUIEvent);
rt300@37 420
rt300@24 421 }
rt300@24 422 //--------------------------------------------------------------
rt300@24 423 void testApp::sliderGUIEvent(ofxUIEventArgs &e){
rt300@24 424 if(whichInterfaceShowing == ZOOMER){
rt300@24 425 cout << "GUI ERROR";
rt300@24 426 return;
rt300@24 427 }
rt300@37 428
rt300@0 429 // "normal" parameter changes
rt300@0 430 for(int i = 1; i<=10;i++){
rt300@0 431
rt300@24 432 if(e.widget->getName() == sliderParamNames[i-1])
rt300@0 433 {
rt300@0 434 //cout << "param change: " << p;
rt300@0 435 ofxUISlider *slider = (ofxUISlider *) e.widget;
rt300@3 436 sliderMoved(i-1,slider->getScaledValue()); // internal array 0 indexed
rt300@0 437 }
rt300@0 438 }
rt300@37 439
rt300@0 440 }
rt300@0 441 //--------------------------------------------------------------
rt300@3 442 void testApp::sliderMoved(int which, float value){
rt300@3 443 // an update caused by slider view being touched
rt300@0 444 sliderVals[which] = (int)value;
rt300@0 445 theGridView.setParams(sliderVals);
rt300@37 446
rt300@3 447 sendParametersToPD();
rt300@39 448 if(sendMIDIAndOSC){
rt300@39 449
rt300@39 450 sendMidiParam(which);
rt300@39 451 sendOSCParams();
rt300@39 452 }
rt300@5 453
rt300@9 454 eventLogger.logEvent(CHANGE_SLIDER, TwoVector(),0.0,which , value);
rt300@5 455
rt300@0 456 }
rt300@0 457 //--------------------------------------------------------------
rt300@3 458 void testApp::setAllGUISliders(vector<int> vals){
rt300@3 459 // an update caused by zoomer view being moved
rt300@25 460 for(int i = 0; i<NUM_PARAMS;i++){
rt300@0 461 sliders[i]->setValue(vals[i]);
rt300@1 462 sliderVals[i] = vals[i];
rt300@0 463 }
rt300@3 464
rt300@39 465 sendParametersToPD();
rt300@39 466 if(sendMIDIAndOSC){
rt300@39 467
rt300@39 468 sendMidiParams();
rt300@39 469 sendOSCParams();
rt300@39 470 }
rt300@39 471
rt300@0 472 }
rt300@0 473 //--------------------------------------------------------------
rt300@25 474 void testApp::randomise(){
rt300@25 475 // pick random settings for all params
rt300@39 476
rt300@39 477 // random zoom? no.
rt300@25 478 for(int i=0; i < NUM_PARAMS ; i++){
rt300@25 479 sliderVals[i] = ofRandom(0, 127);
rt300@25 480
rt300@25 481 }
rt300@37 482 currentSequence = ofRandom(0, sequences.size()-1);
rt300@25 483 // send to grid, sliders and PD
rt300@25 484 theGridView.setParams(sliderVals);
rt300@25 485 setAllGUISliders(sliderVals);
rt300@39 486
rt300@39 487
rt300@25 488 eventLogger.logEvent(RANDOMISE, theGridView.getCoord() ,0.0);
rt300@25 489
rt300@25 490 }
rt300@39 491 //--------------------------------------------------------------
rt300@27 492 void testApp::showHelp(){
rt300@29 493 whichInterfaceShowing = HELP;
rt300@27 494 seqStartStop(false);
rt300@27 495 [topButtonViewController pausePressed:(id)this];
rt300@27 496 helpViewController.view.hidden = NO;
rt300@27 497 eventLogger.logEvent(HELP_PRESSED);
rt300@27 498 }
rt300@27 499 void testApp::helpHidden(){
rt300@29 500 whichInterfaceShowing = BOTH;
rt300@27 501 // start seq?
rt300@27 502
rt300@27 503 }
rt300@25 504 //--------------------------------------------------------------
rt300@36 505 void testApp::nextSequence(){
rt300@36 506 currentSequence++;
rt300@36 507 if(currentSequence >= sequences.size()){
rt300@36 508 currentSequence = 0;
rt300@36 509 }
rt300@36 510 sendParametersToPD();
rt300@36 511 }
rt300@36 512 //--------------------------------------------------------------
rt300@39 513 //--------------------------------------------------------------
rt300@39 514 #pragma mark sending to pd and midi
rt300@3 515 void testApp::sendParametersToPD(){
rt300@37 516 static int previousSequence;
rt300@37 517 // frequencer stuff to get 16 steps
rt300@36 518
rt300@37 519 vector<double> vals;
rt300@37 520
rt300@37 521
rt300@37 522 vals.push_back((sliderVals[0]+32)*8.); // DC offset
rt300@37 523 for(int i=1; i<5;i++){
rt300@37 524 vals.push_back((sliderVals[i] - 64)*2.);
rt300@37 525 }
rt300@37 526
rt300@37 527 vector<double> steps = frequencer.freqMagEdit(freqIndexes, vals);
rt300@37 528 List seqSteps;
rt300@37 529 seqSteps.addSymbol("seqSteps");
rt300@37 530 for(int i=0; i < 16; i++){
rt300@37 531 seqSteps.addFloat(round(steps[i]));
rt300@37 532 }
rt300@37 533 core.pd.sendList("fromOF", seqSteps);
rt300@37 534
rt300@36 535 /*
rt300@37 536 if(currentSequence != previousSequence){
rt300@37 537 List seqSteps;
rt300@37 538
rt300@37 539 seqSteps.addSymbol("seqSteps");
rt300@37 540 if(currentSequence >= sequences.size() || currentSequence < 0){
rt300@37 541 cout << "ERROR: not a valid sequence index\n";
rt300@37 542 for(int i=0; i < 16; i++){
rt300@37 543 seqSteps.addFloat(50);
rt300@37 544 }
rt300@37 545 }else{
rt300@37 546 for(int i=0; i < 16; i++){
rt300@37 547 seqSteps.addFloat(round(sequences[currentSequence][i]));
rt300@37 548 }
rt300@37 549 }
rt300@37 550
rt300@37 551 core.pd.sendList("fromOF", seqSteps);
rt300@37 552 previousSequence = currentSequence;
rt300@3 553 }
rt300@36 554 */
rt300@36 555
rt300@37 556 // send synth params
rt300@3 557 sendOscShape(sliderVals[5]);
rt300@3 558 sendFiltType(sliderVals[6]);
rt300@3 559 sendFiltFreq(sliderVals[7]);
rt300@3 560 sendEnvShape(sliderVals[8]);
rt300@3 561 sendModFreq(sliderVals[9]);
rt300@39 562
rt300@3 563
rt300@3 564 }
rt300@39 565 //--------------------------------------------------------------
rt300@39 566 void testApp::sendMidiParam(int which){
rt300@39 567 int midiChannel = 7;
rt300@39 568 int offset = 0;
rt300@39 569
rt300@39 570 for(int i = 0; i < outputs.size(); ++i) {
rt300@39 571 outputs[i]->sendControlChange(midiChannel, offset+which, sliderVals[which]);
rt300@39 572 }
rt300@39 573
rt300@39 574
rt300@39 575 }
rt300@39 576 //--------------------------------------------------------------
rt300@39 577 void testApp::sendMidiParams(){
rt300@39 578
rt300@39 579 for(int i = 0; i< sliderVals.size(); i++){
rt300@39 580 for(int j = 0; j < outputs.size(); ++j) {
rt300@39 581 outputs[j]->sendControlChange(midiChannel, midiOffset+i, sliderVals[i]);
rt300@39 582 }
rt300@39 583 }
rt300@39 584
rt300@39 585 }
rt300@39 586 //--------------------------------------------------------------
rt300@39 587 void testApp::sendOSCParams(){
rt300@39 588
rt300@39 589 ofxOscMessage m;
rt300@39 590 m.setAddress( "sonicZoom" );
rt300@39 591
rt300@39 592 for(int i = 0; i< sliderVals.size(); i++){
rt300@39 593
rt300@39 594 m.addFloatArg(sliderVals[i]);
rt300@39 595
rt300@39 596 }
rt300@39 597 sender.sendMessage( m );
rt300@39 598 }
rt300@39 599 //--------------------------------------------------------------
rt300@39 600
rt300@27 601 void testApp::setupNewUser(){
rt300@27 602 // this function is for supervised trials with my ipad
rt300@27 603 eventLogger.newUser();
rt300@27 604 }
rt300@8 605 //--------------------------------------------------------------
rt300@3 606 #pragma mark STANDARD OF FUNCTIONS
rt300@3 607 //--------------------------------------------------------------
rt300@0 608 void testApp::update(){
rt300@37 609
rt300@16 610 if(paused) return;
rt300@16 611
rt300@32 612 if(ofxiPhoneExternalDisplay::isExternalScreenConnected()){
rt300@32 613 if(!ofxiPhoneExternalDisplay::isMirroring()){
rt300@32 614 ofxiPhoneExternalDisplay::mirrorOn();
rt300@32 615 printf("turned on Mirroring!\n");
rt300@32 616 }
rt300@32 617 }
rt300@32 618
rt300@27 619 // continiue to move or zoom at velocity, unless snapped
rt300@3 620
rt300@3 621 if (numActiveTouches == 0){ // no touches, use momentum
rt300@32 622 // ZOOM MOMENTUM
rt300@32 623 // continiue to zoom at velocity
rt300@32 624 if (numActiveTouches < 2 && abs(zoomVel)>0.001){
rt300@32 625 theGridView.zoom(zoomVel + 1.0); // +1 because zoomVel factor is + or - , wheras zoom is a multiplier near 1
rt300@32 626 zoomVel = zoomVel*slowFactor;
rt300@32 627 moveVel.setCoord(0.0,0.0);; // don't move if zooming! Too many events!
rt300@35 628
rt300@37 629 setAllGUISliders(theGridView.getParams());
rt300@39 630
rt300@32 631 }
rt300@32 632
rt300@22 633 if(moveVel.norm() > 0.3){
rt300@22 634 if(theGridView.snapped){
rt300@28 635 // stop it (snap check sends snap event)
rt300@22 636 moveVel.setCoord(0.0,0.0);
rt300@22 637 }else{
rt300@22 638 theGridView.move(moveVel);
rt300@22 639 moveVel = moveVel*slowFactor;
rt300@22 640 }
rt300@37 641 // and get new parameter values
rt300@5 642 setAllGUISliders(theGridView.getParams());
rt300@39 643
rt300@22 644 }else if(moveVel.norm() > 0.01){ // and less than 0.3
rt300@3 645 // stop it
rt300@3 646 moveVel.setCoord(0.0,0.0);
rt300@5 647 setAllGUISliders(theGridView.getParams());
rt300@39 648
rt300@5 649 eventLogger.logEvent(SCROLL_STOPPED, theGridView.getCoord() );
rt300@22 650
rt300@3 651 }else{
rt300@3 652 // stopped - do nothing
rt300@3 653 }
rt300@37 654
rt300@0 655 }
rt300@37 656
rt300@0 657 }
rt300@39 658
rt300@0 659 //--------------------------------------------------------------
rt300@39 660
rt300@0 661 void testApp::draw(){
rt300@37 662
rt300@24 663 switch (whichInterfaceShowing){
rt300@24 664 case SLIDERS:
rt300@24 665 break;
rt300@24 666 case ZOOMER:
rt300@24 667 theGridView.draw();
rt300@37 668
rt300@24 669 break;
rt300@24 670 case BOTH:
rt300@33 671
rt300@33 672 theGridView.draw();
rt300@37 673
rt300@24 674 break;
rt300@24 675 case INTRO:
rt300@24 676 break;
rt300@24 677
rt300@24 678 case QUESTIONNAIRE:
rt300@24 679 break;
rt300@24 680
rt300@0 681 }
rt300@4 682
rt300@0 683 }
rt300@0 684
rt300@0 685 //--------------------------------------------------------------
rt300@0 686 void testApp::touchDown(ofTouchEventArgs &touch){
rt300@27 687 // in slider gui area?
rt300@24 688 if(whichInterfaceShowing == SLIDERS){
rt300@24 689 return;
rt300@24 690 }else if (whichInterfaceShowing == BOTH && touch.x < (SLIDER_GUI_WIDTH+10)){
rt300@24 691 return;
rt300@24 692
rt300@24 693 }// otherwise we're good to let the zoomer handle touch
rt300@0 694
rt300@0 695 numActiveTouches++;
rt300@27 696 preventingMovePostScroll = false;
rt300@37 697
rt300@0 698 if(touch.id == 0){
rt300@0 699 touch0.setCoord(touch.x,touch.y);
rt300@0 700 prevTouch0 = touch0;
rt300@32 701 // stop zoom
rt300@32 702 zoomVel = 0.0;
rt300@0 703 }else if(touch.id == 1){
rt300@0 704
rt300@0 705 touch1.setCoord(touch.x,touch.y);
rt300@0 706 prevTouch1 = touch1;
rt300@0 707
rt300@0 708 }
rt300@0 709 if(numActiveTouches == 1){
rt300@0 710 moveVel.setCoord(0.0, 0.0);
rt300@0 711 prevMove.setCoord(0.0, 0.0);
rt300@0 712 prevMove2.setCoord(0.0, 0.0);
rt300@27 713
rt300@0 714 }else if(numActiveTouches == 2){
rt300@0 715 zoomVel = 0.0;
rt300@0 716 prevZoom = 0.0;
rt300@0 717 prevZoom2 = 0.0;
rt300@0 718 double dist = touch1.distanceTo(touch0);
rt300@0 719 prevDist = dist;
rt300@0 720 }
rt300@37 721
rt300@0 722 }
rt300@0 723
rt300@0 724 //--------------------------------------------------------------
rt300@0 725 void testApp::touchMoved(ofTouchEventArgs &touch){
rt300@1 726
rt300@24 727 // TODO check if in gui area!!!
rt300@24 728 if(whichInterfaceShowing == SLIDERS){
rt300@24 729 return;
rt300@24 730 }else if (whichInterfaceShowing == BOTH && touch.x < (SLIDER_GUI_WIDTH+10)){
rt300@24 731 return;
rt300@24 732
rt300@27 733 }
rt300@24 734
rt300@27 735 // otherwise we're good to let the zoomer handle touch
rt300@37 736
rt300@1 737 // which one? keep track of each touch point
rt300@0 738 if(touch.id == 0){
rt300@0 739 touch0.setCoord(touch.x,touch.y);
rt300@0 740
rt300@0 741 }else if(touch.id == 1){
rt300@0 742
rt300@0 743 touch1.setCoord(touch.x,touch.y);
rt300@0 744 }
rt300@37 745
rt300@1 746 if(numActiveTouches == 1){
rt300@27 747 if(preventingMovePostScroll) return;
rt300@1 748 handleScroll();
rt300@0 749 }else if(numActiveTouches == 2){
rt300@1 750 handleZoom();
rt300@0 751
rt300@0 752 }
rt300@0 753 prevTouch0 = touch0;
rt300@37 754
rt300@0 755
rt300@0 756 }
rt300@5 757
rt300@1 758 //--------------------------------------------------------------
rt300@27 759 // handle a finger being dragged
rt300@1 760 void testApp::handleScroll(){
rt300@37 761
rt300@1 762 TwoVector move = touch0 - prevTouch0;
rt300@5 763 if(yLocked){
rt300@5 764 move.y = 0.0;
rt300@5 765 }
rt300@5 766 if(xLocked){
rt300@5 767 move.x = 0.0;
rt300@5 768 }
rt300@37 769
rt300@6 770 // check time since last move - if
rt300@6 771 unsigned int moveTime = ofGetSystemTimeMicros();
rt300@6 772 if(moveTime - lastMoveTime > 100000){
rt300@6 773 moveVel = TwoVector(); // zero
rt300@6 774 }else{
rt300@6 775 moveVel = (move*0.3 + prevMove*0.34 + prevMove2*0.38); // use the time
rt300@6 776
rt300@6 777 }
rt300@6 778 lastMoveTime = moveTime;
rt300@6 779
rt300@37 780
rt300@1 781 prevMove2 = prevMove;
rt300@1 782 prevMove = move;
rt300@1 783
rt300@37 784
rt300@3 785 theGridView.move(move);
rt300@3 786
rt300@3 787 // and get new parameter values
rt300@3 788 setAllGUISliders(theGridView.getParams());
rt300@39 789
rt300@1 790 }
rt300@1 791 //--------------------------------------------------------------
rt300@27 792 // handle pinch movememnt
rt300@1 793 void testApp::handleZoom(){
rt300@1 794 // work out change in difference
rt300@1 795 double dist = touch1.distanceTo(touch0);
rt300@1 796 double zoomFactor = prevDist/dist;
rt300@1 797
rt300@1 798 //TODO check for sensible maximums, e.g. spurious touch data
rt300@1 799 if(zoomFactor > 2.0 || zoomFactor < 0.5){
rt300@1 800 cout << "Zoom too much!!!!" << zoomFactor;
rt300@1 801 zoomFactor = 1.0;
rt300@1 802 }
rt300@1 803
rt300@1 804 zoomVel = (zoomFactor-1)*0.3 + prevZoom*0.34 + prevZoom2*0.38;
rt300@1 805 prevZoom2 = prevZoom;
rt300@1 806 prevZoom = (zoomFactor-1);
rt300@1 807
rt300@1 808 theGridView.zoom(zoomFactor);
rt300@1 809
rt300@1 810 prevDist = dist;
rt300@35 811
rt300@3 812
rt300@37 813 setAllGUISliders(theGridView.getParams());
rt300@39 814
rt300@37 815
rt300@37 816
rt300@1 817 }
rt300@0 818 //--------------------------------------------------------------
rt300@0 819 void testApp::touchUp(ofTouchEventArgs &touch){
rt300@1 820 if(numActiveTouches > 0) numActiveTouches--; // dirty
rt300@27 821 preventingMovePostScroll = false;
rt300@24 822 // TODO check if in gui area!!!
rt300@24 823 if(whichInterfaceShowing == SLIDERS){
rt300@24 824 return;
rt300@24 825 }else if (whichInterfaceShowing == BOTH && touch.x < 256){
rt300@24 826 return;
rt300@1 827
rt300@24 828 }// otherwise we're good to let the zoomer handle touch
rt300@24 829
rt300@37 830
rt300@0 831 // which one?
rt300@0 832 if(touch.id == 0){
rt300@0 833 // tricky situation - we tried to zoom but may have left non-move finger on
rt300@0 834 prevTouch0.setCoord(touch.x,touch.y);
rt300@0 835
rt300@0 836 }else if(touch.id == 1){
rt300@0 837
rt300@37 838
rt300@0 839 prevTouch1.setCoord(0,0);
rt300@6 840
rt300@6 841 }
rt300@6 842 if(numActiveTouches == 0){
rt300@6 843 // check time since last move
rt300@6 844 // check time since last move - if
rt300@6 845 unsigned int moveTime = ofGetSystemTimeMicros();
rt300@6 846 if(moveTime - lastMoveTime > 100000){
rt300@6 847 moveVel = TwoVector(); // zero
rt300@6 848 }else{
rt300@6 849 moveVel = (move*0.3 + prevMove*0.34 + prevMove2*0.38); // use the time
rt300@6 850
rt300@6 851 }
rt300@6 852 lastMoveTime = moveTime;
rt300@27 853 }else if (numActiveTouches == 1){
rt300@27 854 // just zoomed , but now lifted one of the fingers
rt300@27 855 // can be bad if moved so create special mode to stop scroll (special modes bad!)
rt300@27 856 preventingMovePostScroll = true;
rt300@0 857 }
rt300@37 858
rt300@0 859 }
rt300@0 860
rt300@0 861 //--------------------------------------------------------------
rt300@0 862 void testApp::touchDoubleTap(ofTouchEventArgs &touch){
rt300@0 863 // preset?
rt300@25 864
rt300@25 865 /* ballses everything for some reason
rt300@37 866 TwoVector centre = TwoVector(getWidth()*0.5,getHeight()*0.5);
rt300@37 867 // if near centre
rt300@37 868 if((touch.x < centre.x+10) && (touch.x > centre.x-10) && (touch.y < centre.y+10) && (touch.y > centre.y-10)){
rt300@37 869 numActiveTouches = 0; // dirty
rt300@37 870 presetManager.showNameDialog();
rt300@37 871
rt300@37 872 }
rt300@25 873 */
rt300@25 874
rt300@0 875 }
rt300@0 876
rt300@0 877 //--------------------------------------------------------------
rt300@0 878 void testApp::lostFocus(){
rt300@37 879
rt300@0 880 }
rt300@0 881
rt300@0 882 //--------------------------------------------------------------
rt300@0 883 void testApp::gotFocus(){
rt300@37 884
rt300@0 885 }
rt300@0 886
rt300@0 887 //--------------------------------------------------------------
rt300@0 888 void testApp::gotMemoryWarning(){
rt300@37 889
rt300@0 890 }
rt300@0 891
rt300@0 892 //--------------------------------------------------------------
rt300@0 893 void testApp::deviceOrientationChanged(int newOrientation){
rt300@37 894 /*
rt300@37 895 cout << "orientation: " << newOrientation;
rt300@37 896
rt300@37 897 if(newOrientation == 4){
rt300@37 898 ofxiPhoneSetOrientation( OF_ORIENTATION_DEFAULT );
rt300@37 899
rt300@37 900 }else if(newOrientation == 3){
rt300@37 901 ofxiPhoneSetOrientation( OF_ORIENTATION_90_LEFT );
rt300@37 902 }else if(newOrientation == 3){
rt300@37 903 ofxiPhoneSetOrientation( OF_ORIENTATION_90_LEFT );
rt300@37 904 }
rt300@37 905
rt300@37 906
rt300@37 907 [ofxiPhoneGetGLView() updateDimensions];
rt300@37 908 */
rt300@0 909 }
rt300@0 910
rt300@0 911
rt300@0 912 //--------------------------------------------------------------
rt300@0 913 void testApp::touchCancelled(ofTouchEventArgs& args){
rt300@37 914
rt300@0 915 }
rt300@3 916 //---------------------------------------------------------------
rt300@3 917 // AUDIO STUFF
rt300@3 918 //---------------------------------------------------------------
rt300@3 919
rt300@3 920 #pragma mark AUDIO STREAMS
rt300@3 921 //--------------------------------------------------------------
rt300@3 922 void testApp::audioReceived(float * input, int bufferSize, int nChannels) {
rt300@3 923 core.audioReceived(input, bufferSize, nChannels);
rt300@3 924 }
rt300@3 925
rt300@3 926 void testApp::audioRequested(float * output, int bufferSize, int nChannels) {
rt300@3 927 core.audioRequested(output, bufferSize, nChannels);
rt300@3 928 }
rt300@3 929 //---------------------------------------------------------------
rt300@3 930 #pragma mark UTILITIES
rt300@0 931
rt300@0 932 // 5hz cut off
rt300@0 933 const double fB[3] = {0.049489956268677, 0.098979912537354, 0.049489956268677};
rt300@0 934
rt300@0 935 const double fA[3] = {1.000000000000000, -1.279632424997809, 0.477592250072517};
rt300@0 936
rt300@0 937 // 1hz cut off
rt300@0 938 //const double fB[3] = {0.002550535158536, 0.005101070317073, 0.002550535158536};
rt300@0 939
rt300@0 940 //const double fA[3] = {1.000000000000000, -1.852146485395936, 0.862348626030081};
rt300@0 941
rt300@0 942
rt300@0 943 //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 944 //---------------------------------------------------------------
rt300@0 945 vector<float> testApp::vectorFilter(vector<float> newVec){
rt300@0 946 static vector<float> x0(10,0);
rt300@0 947 static vector<float> x1(10,0);
rt300@0 948 static vector<float> x2(10,0);
rt300@0 949 static vector<float> y0(10,0);
rt300@0 950 static vector<float> y1(10,0);
rt300@0 951 static vector<float> y2(10,0);
rt300@0 952
rt300@0 953 x0 = newVec;
rt300@0 954
rt300@0 955 // this low passes a bunch of params values all at once
rt300@0 956 int sz = newVec.size();
rt300@0 957 for(int i=0; i<sz; i++){
rt300@0 958 y0[i] = fB[0]*x0[i] + fB[1]*x1[i] + fB[2]*x2[i] - fA[1]*y1[i] - fA[2]*y2[i];
rt300@0 959 }
rt300@0 960 // shift
rt300@0 961 x2 = x1;
rt300@0 962 x1 = x0;
rt300@0 963 y2 = y1;
rt300@0 964 y1 = y0;
rt300@0 965
rt300@0 966 return y0;
rt300@2 967 }
rt300@35 968 //---------------------------------------------------------------
rt300@24 969 float ctrlSmoother(float newsamp){
rt300@24 970 static float x1,x2,y1,y2;
rt300@24 971 float x0, y0;
rt300@37 972
rt300@24 973 x0 = newsamp;
rt300@37 974
rt300@24 975 y0 = fB[0]*x0 + fB[1]*x1 + fB[2]*x2 - fA[1]*y1 - fA[2]*y2;
rt300@37 976
rt300@24 977 // shift
rt300@24 978 x2 = x1;
rt300@24 979 x1 = x0;
rt300@24 980 y2 = y1;
rt300@24 981 y1 = y0;
rt300@24 982
rt300@24 983 return y0;
rt300@24 984 }
rt300@8 985 //---------------------------------------------------------------
rt300@3 986 void testApp::sendOscShape(int ctrlin){
rt300@35 987 if(ctrlin < 0 || ctrlin > 127){
rt300@35 988 cout << "ERROR: bad slider value!";
rt300@35 989 return;
rt300@35 990 }
rt300@3 991
rt300@3 992 static int numpoints = 5;
rt300@3 993 static int numcontrols = 5;
rt300@3 994 //float values[points][controls] =
rt300@3 995 float ctrlout[numcontrols];
rt300@3 996 string ctrlName[5] = {"pWidth" , "sqVol", "sawVol", "sineVol", "FMAmt"};
rt300@3 997 float values[5][5] =
rt300@37 998 {{0.5, 0., 0., 1., 1.}, // 0
rt300@37 999 {0.5, 0., 0., 1., 0.}, // 32
rt300@3 1000 {0.5, 0., 1., 0., 0.}, // 64
rt300@3 1001 {0.5, 1., 1., 0., 0.}, // 96
rt300@3 1002 {0.01,1., 1., 0., 0.}}; // 127
rt300@37 1003
rt300@3 1004 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 1005 int idx = floor(fidx);
rt300@3 1006 float frac = fidx - idx;
rt300@3 1007 for(int i=0; i < numcontrols; i++){
rt300@3 1008 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 1009 // send to PD
rt300@3 1010 List toPD;
rt300@3 1011
rt300@3 1012 toPD.addSymbol(ctrlName[i]);
rt300@3 1013 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@37 1014
rt300@3 1015 core.pd.sendList("fromOF", toPD);
rt300@3 1016 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 1017 }
rt300@3 1018
rt300@2 1019 }
rt300@8 1020 //---------------------------------------------------------------
rt300@3 1021 void testApp::sendFiltType(int ctrlin){
rt300@35 1022 if(ctrlin < 0 || ctrlin > 127){
rt300@35 1023 cout << "ERROR: bad slider value!";
rt300@35 1024 return;
rt300@35 1025 }
rt300@3 1026 static int numpoints = 3;
rt300@3 1027 static int numcontrols = 4;
rt300@3 1028 //float values[points][controls] =
rt300@3 1029 float ctrlout[numcontrols];
rt300@3 1030 string ctrlName[4] = {"lpLev" , "bpLev", "hpLev", "reson"};
rt300@3 1031 float values[3][4] =
rt300@37 1032 {{2., 0., 0., 1.}, // 0
rt300@24 1033 {0., 10., 0., 10.}, // 64
rt300@24 1034 {0., 0., 1., 1.}}; // 127
rt300@3 1035
rt300@3 1036 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 1037 int idx = floor(fidx);
rt300@3 1038 float frac = fidx - idx;
rt300@3 1039 for(int i=0; i < numcontrols; i++){
rt300@3 1040 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 1041 // send to PD
rt300@3 1042 List toPD;
rt300@3 1043
rt300@3 1044 toPD.addSymbol(ctrlName[i]);
rt300@3 1045 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@3 1046
rt300@3 1047 core.pd.sendList("fromOF", toPD);
rt300@3 1048 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 1049 }
rt300@2 1050 }
rt300@8 1051 //---------------------------------------------------------------
rt300@3 1052 void testApp::sendFiltFreq(int ctrlin){
rt300@35 1053 if(ctrlin < 0 || ctrlin > 127){
rt300@35 1054 cout << "ERROR: bad slider value!";
rt300@35 1055 return;
rt300@35 1056 }
rt300@24 1057 // smooth this
rt300@24 1058 float fin = ctrlin;
rt300@24 1059 float fout;
rt300@24 1060 fout = (int)ctrlSmoother(fin);
rt300@3 1061 List toPD;
rt300@3 1062
rt300@3 1063 toPD.addSymbol("filtFreq");
rt300@37 1064 toPD.addFloat(fout);
rt300@3 1065
rt300@3 1066 core.pd.sendList("fromOF", toPD);
rt300@3 1067 }
rt300@8 1068 //---------------------------------------------------------------
rt300@3 1069 void testApp::sendEnvShape(int ctrlin){
rt300@35 1070 if(ctrlin < 0 || ctrlin > 127){
rt300@35 1071 cout << "ERROR: bad slider value!";
rt300@35 1072 return;
rt300@35 1073 }
rt300@3 1074 static int numpoints = 5;
rt300@3 1075 static int numcontrols = 3;
rt300@3 1076 //float values[points][controls] =
rt300@3 1077 float ctrlout[numcontrols];
rt300@3 1078 string ctrlName[3] = {"attack" , "decay", "sustain"};
rt300@3 1079 float values[5][3] =
rt300@37 1080 {{0., 0., 0.}, // 0
rt300@3 1081 {0., 0.5, 0.}, // 32
rt300@3 1082 {0.0, 1., 0.8}, // 64
rt300@3 1083 {0.99, 0.3, 0.}, // 96
rt300@3 1084 {0.3, 0.1, 0.}}; // 127
rt300@3 1085
rt300@3 1086 float fidx = (numpoints-1)*ctrlin/128.;
rt300@3 1087 int idx = floor(fidx);
rt300@3 1088 float frac = fidx - idx;
rt300@3 1089 for(int i=0; i < numcontrols; i++){
rt300@3 1090 ctrlout[i] = (1 - frac)*values[idx][i] + (frac)*values[idx+1][i];
rt300@3 1091 // send to PD
rt300@3 1092 List toPD;
rt300@3 1093
rt300@3 1094 toPD.addSymbol(ctrlName[i]);
rt300@3 1095 toPD.addFloat(ctrlout[i]); // rounding here??
rt300@3 1096
rt300@3 1097 core.pd.sendList("fromOF", toPD);
rt300@3 1098 //cout << ctrlName[i] << "sending" << ctrlout[i] << "\n";
rt300@3 1099 }
rt300@3 1100 }
rt300@8 1101 //---------------------------------------------------------------
rt300@3 1102 void testApp::sendModFreq(int ctrlin){
rt300@35 1103 if(ctrlin < 0 || ctrlin > 127){
rt300@35 1104 cout << "ERROR: bad slider value!";
rt300@35 1105 return;
rt300@35 1106 }
rt300@3 1107 float fm = ctrlin/127.;
rt300@3 1108 List toPD;
rt300@3 1109
rt300@3 1110 toPD.addSymbol("FMFreq");
rt300@3 1111 toPD.addFloat(fm); // rounding here??
rt300@3 1112
rt300@3 1113 core.pd.sendList("fromOF", toPD);
rt300@8 1114 }
rt300@35 1115 //---------------------------------------------------------------
rt300@35 1116
rt300@35 1117
rt300@35 1118
rt300@35 1119 //---------------------------------------------------------------------------
rt300@35 1120 void saveSequences(){
rt300@35 1121 ofFile sequenceFile(ofxiPhoneGetDocumentsDirectory() + "pilot_sequences.json" ,ofFile::WriteOnly);
rt300@35 1122
rt300@35 1123 // the 5 harmonics for the frequencer
rt300@35 1124 vector<int> freqIndexes;
rt300@35 1125 freqIndexes.push_back(0);
rt300@35 1126 freqIndexes.push_back(4);
rt300@35 1127 freqIndexes.push_back(6);
rt300@35 1128 freqIndexes.push_back(7);
rt300@35 1129 freqIndexes.push_back(8);
rt300@35 1130
rt300@35 1131 // loop thru all presets
rt300@35 1132 int N = presetManager.thePresets.size();
rt300@35 1133
rt300@35 1134 vector<int> sliderVals;
rt300@35 1135
rt300@35 1136 Json::Value root;
rt300@35 1137
rt300@35 1138 vector<double> vals;
rt300@35 1139
rt300@35 1140 for(int i = 0; i< N ; i++){
rt300@35 1141 sliderVals = theGridView.calculateParamsFromCoord(presetManager.thePresets[i]->coordinates);
rt300@35 1142
rt300@35 1143 vals.push_back((sliderVals[0]+32)*8.); // DC offset
rt300@35 1144 for(int i=1; i<5;i++){
rt300@35 1145 vals.push_back((sliderVals[i] - 64)*2.);
rt300@35 1146 }
rt300@35 1147
rt300@35 1148
rt300@35 1149 vector<double> steps = frequencer.freqMagEdit(freqIndexes, vals);
rt300@35 1150
rt300@35 1151 for(int j = 0; j<16;j++){
rt300@35 1152 root["sequences"][i][j] = int(steps[j]);
rt300@35 1153 }
rt300@35 1154 vals.clear();
rt300@35 1155 }
rt300@35 1156
rt300@35 1157 //cout << root;
rt300@35 1158 sequenceFile << root;
rt300@35 1159 }
rt300@35 1160 //=--------------------------------------------------------------------
rt300@36 1161 void testApp::loadSequences(){
rt300@36 1162
rt300@36 1163 // read in sequence preset file
rt300@36 1164 string jsonFile = ofxiPhoneGetDocumentsDirectory() + "pilot_sequences.json";
rt300@37 1165
rt300@36 1166 Json::Value root;
rt300@36 1167 Json::Reader reader;
rt300@37 1168
rt300@36 1169 ifstream theFile(jsonFile.c_str());
rt300@36 1170 stringstream fileText;
rt300@36 1171 string line;
rt300@36 1172 if(!theFile){
rt300@36 1173 cout<<"can't find sequence file: " << jsonFile.c_str() << "\n";
rt300@36 1174 return;
rt300@36 1175 }else{
rt300@36 1176
rt300@36 1177 while(theFile){
rt300@36 1178 theFile >> line;
rt300@36 1179 // cout << line << "\n"; // lots?
rt300@37 1180 fileText << line;
rt300@36 1181 }
rt300@36 1182
rt300@36 1183 theFile.close();
rt300@36 1184 }
rt300@36 1185
rt300@36 1186 bool parsingSuccessful = reader.parse( fileText.str(), root );
rt300@36 1187
rt300@36 1188 if ( !parsingSuccessful )
rt300@36 1189 {
rt300@36 1190 // report to the user the failure and their locations in the document.
rt300@36 1191 std::cout << "Failed to parse sequence JSON: \n"
rt300@36 1192 << reader.getFormattedErrorMessages();
rt300@36 1193 return;
rt300@36 1194 }
rt300@36 1195
rt300@36 1196 // now put into variables
rt300@36 1197 const Json::Value jseqs = root["sequences"];
rt300@36 1198 int N = jseqs.size();
rt300@36 1199 for(int i=0; i<N; i++){
rt300@36 1200 sequences.push_back(vector<int>());
rt300@36 1201 for(int j=0; j<jseqs[i].size(); j++){
rt300@36 1202 sequences.back().push_back(jseqs[i][j].asInt());
rt300@36 1203 }
rt300@36 1204 }
rt300@36 1205 // currentSequence
rt300@37 1206
rt300@37 1207 currentSequence = ofRandom(0,N-1);
rt300@36 1208
rt300@36 1209 }
rt300@39 1210 //-------------------------------------------------------------------------
rt300@39 1211 //--------------------------------------------------------------
rt300@39 1212
rt300@39 1213 #pragma mark MIDI
rt300@39 1214 void testApp::addMessage(string msg) {
rt300@39 1215 cout << msg << endl;
rt300@39 1216 messages.push_back(msg);
rt300@39 1217 while(messages.size() > maxMessages)
rt300@39 1218 messages.pop_front();
rt300@39 1219 }
rt300@39 1220
rt300@39 1221 //--------------------------------------------------------------
rt300@39 1222 void testApp::newMidiMessage(ofxMidiMessage& msg) {
rt300@39 1223 //addMessage(msg.toString());
rt300@39 1224
rt300@39 1225 // look at what it is
rt300@39 1226
rt300@39 1227 if(msg.channel == midiChannel && msg.status == MIDI_CONTROL_CHANGE){
rt300@39 1228 int ctl_num = msg.control;
rt300@39 1229 int ctl_val = msg.value;
rt300@39 1230 if(ctl_num - midiOffset >= 0 && ctl_num - midiOffset < sliderVals.size()){
rt300@39 1231 sliderVals[ctl_num] = (int)ctl_val;
rt300@39 1232 sliders[ctl_num]->setValue(ctl_val);
rt300@39 1233 theGridView.setParams(sliderVals);
rt300@39 1234 }
rt300@39 1235 }
rt300@39 1236
rt300@39 1237 }
rt300@39 1238
rt300@39 1239 //--------------------------------------------------------------
rt300@39 1240 void testApp::midiInputAdded(string name, bool isNetwork) {
rt300@39 1241 stringstream msg;
rt300@39 1242 msg << "ofxMidi: input added: " << name << " network: " << isNetwork;
rt300@39 1243 cout << msg.str();
rt300@39 1244 addMessage(msg.str());
rt300@39 1245
rt300@39 1246 // create and open a new input port
rt300@39 1247 ofxMidiIn * newInput = new ofxMidiIn;
rt300@39 1248 newInput->openPort(name);
rt300@39 1249 newInput->addListener(this);
rt300@39 1250 inputs.push_back(newInput);
rt300@39 1251 }
rt300@39 1252
rt300@39 1253 //--------------------------------------------------------------
rt300@39 1254 void testApp::midiInputRemoved(string name, bool isNetwork) {
rt300@39 1255 stringstream msg;
rt300@39 1256 msg << "ofxMidi: input removed: " << name << " network: " << isNetwork << endl;
rt300@39 1257 cout << msg.str();
rt300@39 1258 addMessage(msg.str());
rt300@39 1259
rt300@39 1260 // close and remove input port
rt300@39 1261 vector<ofxMidiIn*>::iterator iter;
rt300@39 1262 for(iter = inputs.begin(); iter != inputs.end(); ++iter) {
rt300@39 1263 ofxMidiIn * input = (*iter);
rt300@39 1264 if(input->getName() == name) {
rt300@39 1265 input->closePort();
rt300@39 1266 input->removeListener(this);
rt300@39 1267 delete input;
rt300@39 1268 inputs.erase(iter);
rt300@39 1269 break;
rt300@39 1270 }
rt300@39 1271 }
rt300@39 1272 }
rt300@39 1273
rt300@39 1274 //--------------------------------------------------------------
rt300@39 1275 void testApp::midiOutputAdded(string name, bool isNetwork) {
rt300@39 1276 stringstream msg;
rt300@39 1277 msg << "ofxMidi: output added: " << name << " network: " << isNetwork << endl;
rt300@39 1278 cout << msg.str();
rt300@39 1279 addMessage(msg.str());
rt300@39 1280
rt300@39 1281 // create and open new output port
rt300@39 1282 ofxMidiOut * newOutput = new ofxMidiOut;
rt300@39 1283 newOutput->openPort(name);
rt300@39 1284 outputs.push_back(newOutput);
rt300@39 1285 }
rt300@39 1286
rt300@39 1287 //--------------------------------------------------------------
rt300@39 1288 void testApp::midiOutputRemoved(string name, bool isNetwork) {
rt300@39 1289 stringstream msg;
rt300@39 1290 msg << "ofxMidi: output removed: " << name << " network: " << isNetwork << endl;
rt300@39 1291 cout << msg.str();
rt300@39 1292 addMessage(msg.str());
rt300@39 1293
rt300@39 1294 // close and remove output port
rt300@39 1295 vector<ofxMidiOut*>::iterator iter;
rt300@39 1296 for(iter = outputs.begin(); iter != outputs.end(); ++iter) {
rt300@39 1297 ofxMidiOut * output = (*iter);
rt300@39 1298 if(output->getName() == name) {
rt300@39 1299 output->closePort();
rt300@39 1300 delete output;
rt300@39 1301 outputs.erase(iter);
rt300@39 1302 break;
rt300@39 1303 }
rt300@39 1304 }
rt300@39 1305 }
rt300@39 1306 //--------------------------------------------------------------