annotate aubioOnsetDetectorOFvisualiser/src/testApp.cpp @ 3:979125db34ab

added OF visualiser src code. Added a long term median trigger threshold. New method is working very well on onsets
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 21 Nov 2011 23:22:40 +0000
parents
children fb106f14e0a4
rev   line source
andrew@3 1 #include "testApp.h"
andrew@3 2 //#include "math.h"
andrew@3 3
andrew@3 4 #define SAMPLING_FREQUENCY 44100
andrew@3 5 #define FOURIER_LENGTH 2048
andrew@3 6 #define TEXT_HEIGHT 10
andrew@3 7 //--------------------------------------------------------------
andrew@3 8 void testApp::setup(){
andrew@3 9 // listen on the given port
andrew@3 10 // cout << "listening for osc messages on port " << PORT << "\n";
andrew@3 11 receiver.setup( PORT );
andrew@3 12
andrew@3 13 current_msg_string = 0;
andrew@3 14 mouseX = 0;
andrew@3 15 mouseY = 0;
andrew@3 16 mouseButtonState = "";
andrew@3 17
andrew@3 18 ofBackground( 30, 30, 130 );
andrew@3 19
andrew@3 20
andrew@3 21 outputGraphics = false;
andrew@3 22
andrew@3 23 maximumDetectionFunction = 20;
andrew@3 24 minimumDetectionFunction = -20;
andrew@3 25
andrew@3 26 screenWidth = (float) ofGetWidth();
andrew@3 27 screenHeight = (float) ofGetHeight();
andrew@3 28
andrew@3 29 mouseDownX = 0;
andrew@3 30 mouseDownY = 0;
andrew@3 31
andrew@3 32 amplitudeNumber = 256;//number of amplitudes shown on screen
andrew@3 33
andrew@3 34 maxValue = 1.0;
andrew@3 35
andrew@3 36 detectionType = "complex";
andrew@3 37 lastOnsetDetectionValue;
andrew@3 38
andrew@3 39 logMode = false;
andrew@3 40
andrew@3 41 midiMode = true;
andrew@3 42
andrew@3 43 resetMaxima = false;
andrew@3 44
andrew@3 45 reIndexFlag = false;
andrew@3 46
andrew@3 47 ofBackground(0,0,0);
andrew@3 48
andrew@3 49 rawOnsetIndex = 0;
andrew@3 50 }
andrew@3 51
andrew@3 52 //--------------------------------------------------------------
andrew@3 53 void testApp::update(){
andrew@3 54 maxValue *= 0.995;
andrew@3 55 // hide old messages
andrew@3 56
andrew@3 57 // check for waiting messages
andrew@3 58 while( receiver.hasWaitingMessages() )
andrew@3 59 {
andrew@3 60 // get the next message
andrew@3 61 ofxOscMessage m;
andrew@3 62 receiver.getNextMessage( &m );
andrew@3 63
andrew@3 64 // unrecognized message: display on the bottom of the screen
andrew@3 65 // string msg_string;
andrew@3 66 // msg_string = m.getAddress();
andrew@3 67 if (m.getAddress() == "/aubioData" ){
andrew@3 68
andrew@3 69 if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){
andrew@3 70
andrew@3 71 onsetIndex++;
andrew@3 72 onsetRecorded[onsetIndex] = false;
andrew@3 73
andrew@3 74 if (onsetIndex >= NUM_DETECTION_SAMPLES)
andrew@3 75 onsetIndex = 0;
andrew@3 76
andrew@3 77 onsetFunction[onsetIndex] = m.getArgAsFloat(0);
andrew@3 78
andrew@3 79 checkMaxima(m.getArgAsFloat(0));
andrew@3 80
andrew@3 81 }//end if type FLOAT
andrew@3 82 }//end if aubioData
andrew@3 83
andrew@3 84
andrew@3 85 if (m.getAddress() == "/rawAubioData" ){
andrew@3 86
andrew@3 87 if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){
andrew@3 88
andrew@3 89 rawOnsetIndex++;
andrew@3 90
andrew@3 91 if (rawOnsetIndex >= NUM_DETECTION_SAMPLES)
andrew@3 92 rawOnsetIndex = 0;
andrew@3 93
andrew@3 94 rawOnsetFunction[rawOnsetIndex] = m.getArgAsFloat(0);
andrew@3 95 checkRawMaxima(m.getArgAsFloat(0));
andrew@3 96
andrew@3 97 }//end if type FLOAT
andrew@3 98 }//end spec diff message
andrew@3 99
andrew@3 100
andrew@3 101 if (m.getAddress() == "/medianAubioData" ){
andrew@3 102
andrew@3 103 if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){
andrew@3 104
andrew@3 105 medianOnsetIndex++;
andrew@3 106
andrew@3 107 if (medianOnsetIndex >= NUM_DETECTION_SAMPLES){
andrew@3 108 medianOnsetIndex = 0;
andrew@3 109 }
andrew@3 110
andrew@3 111 if(medianOnsetIndex > 0)
andrew@3 112 medianOnsetRecorded[medianOnsetIndex] = false;//but how do we know this happens first!
andrew@3 113
andrew@3 114 medianOnsetFunction[medianOnsetIndex] = m.getArgAsFloat(0);
andrew@3 115
andrew@3 116 }//end if type FLOAT
andrew@3 117 }//end spec diff message
andrew@3 118
andrew@3 119
andrew@3 120
andrew@3 121
andrew@3 122 if (m.getAddress() == "/onset" ){
andrew@3 123 onsetRecorded[onsetIndex] = true;
andrew@3 124 lastOnsetDetectionValue = onsetFunction[onsetIndex];
andrew@3 125 }
andrew@3 126
andrew@3 127 if (m.getAddress() == "/medianOnset" ){
andrew@3 128 medianOnsetRecorded[onsetIndex] = true;
andrew@3 129 }
andrew@3 130
andrew@3 131
andrew@3 132
andrew@3 133 if (m.getAddress() == "/rawOnset" ){
andrew@3 134 rawOnsetRecorded[rawOnsetIndex] = true;
andrew@3 135
andrew@3 136 }
andrew@3 137
andrew@3 138 if (m.getAddress() == "/mode" ){
andrew@3 139 resetMaxima = true;
andrew@3 140 detectionType = m.getArgAsString(0);
andrew@3 141 }
andrew@3 142
andrew@3 143 }//end while
andrew@3 144
andrew@3 145 }
andrew@3 146
andrew@3 147
andrew@3 148 void testApp::checkMaxima(float f){
andrew@3 149
andrew@3 150 //maximumDetectionFunction *= 0.99999;
andrew@3 151 //minimumDetectionFunction += (maximumDetectionFunction - minimumDetectionFunction)*0.00001;
andrew@3 152
andrew@3 153 if (maximumDetectionFunction * 1.08 < f){
andrew@3 154 maximumDetectionFunction = 1.08*f;
andrew@3 155 }
andrew@3 156
andrew@3 157 if (minimumDetectionFunction + (fabs(minimumDetectionFunction * 0.08)) > f){
andrew@3 158 minimumDetectionFunction = f - (0.08 * fabs(f));
andrew@3 159 }
andrew@3 160
andrew@3 161 if (resetMaxima == true){
andrew@3 162 maximumDetectionFunction = 30;
andrew@3 163 minimumDetectionFunction = 0;
andrew@3 164 resetMaxima = false;
andrew@3 165 }
andrew@3 166
andrew@3 167 }
andrew@3 168
andrew@3 169
andrew@3 170 void testApp::checkRawMaxima(float f){
andrew@3 171
andrew@3 172 //maximumDetectionFunction *= 0.99999;
andrew@3 173 //minimumDetectionFunction += (maximumDetectionFunction - minimumDetectionFunction)*0.00001;
andrew@3 174
andrew@3 175 if (maximumDetectionFunction * 1.08 < f){
andrew@3 176 maximumDetectionFunction = 1.08*f;
andrew@3 177 }
andrew@3 178
andrew@3 179 if (minimumDetectionFunction + (fabs(minimumDetectionFunction * 0.08)) > f){
andrew@3 180 minimumDetectionFunction = f - (0.08 * fabs(f));
andrew@3 181 }
andrew@3 182
andrew@3 183 }
andrew@3 184
andrew@3 185
andrew@3 186
andrew@3 187 //--------------------------------------------------------------
andrew@3 188 void testApp::draw(){
andrew@3 189
andrew@3 190 drawOnsetFunction();
andrew@3 191 printInfo();
andrew@3 192
andrew@3 193 }
andrew@3 194
andrew@3 195 void testApp::printMessages(){
andrew@3 196 string buf;
andrew@3 197 buf = "listening for osc messages on port" + ofToString( PORT );
andrew@3 198 ofDrawBitmapString( buf, 10, 20 );
andrew@3 199
andrew@3 200 for ( int i=0; i<NUM_MSG_STRINGS; i++ )
andrew@3 201 {
andrew@3 202 ofDrawBitmapString( msg_strings[i], 10, 40+15*i );
andrew@3 203 }
andrew@3 204
andrew@3 205 }
andrew@3 206
andrew@3 207 void testApp::printInfo(){
andrew@3 208 ofSetColor(255,255,255);
andrew@3 209 string printString;
andrew@3 210
andrew@3 211 /* printString = "Max ";
andrew@3 212 printString += ofToString(maximumDetectionFunction);
andrew@3 213 printString += " Min ";
andrew@3 214 printString += ofToString(minimumDetectionFunction);
andrew@3 215 */
andrew@3 216
andrew@3 217 printString += detectionType;
andrew@3 218 printString +=" ";
andrew@3 219 printString += ofToString(lastOnsetDetectionValue, 1);
andrew@3 220 ofDrawBitmapString( printString , 10, ofGetHeight() - 20);
andrew@3 221 }
andrew@3 222
andrew@3 223 //--------------------------------------------------------------
andrew@3 224 void testApp::keyPressed (int key){
andrew@3 225
andrew@3 226 if (key == OF_KEY_UP){
andrew@3 227
andrew@3 228 }
andrew@3 229
andrew@3 230 if (key == OF_KEY_DOWN ){
andrew@3 231
andrew@3 232 }
andrew@3 233
andrew@3 234
andrew@3 235 if (key == OF_KEY_RIGHT ){
andrew@3 236
andrew@3 237 }
andrew@3 238
andrew@3 239 if (key == OF_KEY_LEFT ){
andrew@3 240
andrew@3 241 }
andrew@3 242
andrew@3 243 if (key == 'L' || key == 'l'){
andrew@3 244 logMode = !logMode;
andrew@3 245 }
andrew@3 246
andrew@3 247 if (key == 'p' || key == 'P'){
andrew@3 248
andrew@3 249 }
andrew@3 250
andrew@3 251 if (key == 'm' || key == 'M'){
andrew@3 252 midiMode = !midiMode;
andrew@3 253 }
andrew@3 254
andrew@3 255 if (key == 'x' || key == 'X'){
andrew@3 256 amplitudeNumber *= 2;
andrew@3 257 }
andrew@3 258
andrew@3 259 if (key == 'z' || key == 'Z'){
andrew@3 260 amplitudeNumber /= 2;
andrew@3 261 }
andrew@3 262
andrew@3 263 if ((key == '=' || key == '+') && amplitudeNumber < 120){
andrew@3 264 amplitudeNumber += 8;
andrew@3 265 }
andrew@3 266
andrew@3 267 if ((key == '-' || key == '_') && amplitudeNumber > 12){
andrew@3 268 amplitudeNumber -= 8;
andrew@3 269 }
andrew@3 270
andrew@3 271 }
andrew@3 272
andrew@3 273
andrew@3 274
andrew@3 275 void testApp::drawOnsetFunction(){
andrew@3 276 int tmpIndex = onsetIndex;
andrew@3 277 float width = screenWidth / (float) amplitudeNumber;
andrew@3 278 float maximumValue = maximumDetectionFunction ;
andrew@3 279 float minimumValue = minimumDetectionFunction ;
andrew@3 280 float difference = maximumValue - minimumValue;
andrew@3 281 float scale_factor = screenHeight/ difference;
andrew@3 282
andrew@3 283 //draw axis
andrew@3 284 ofSetColor(255,255,255);
andrew@3 285 ofLine(0, screenHeight - (scale_factor*(0 - minimumValue)),
andrew@3 286 (int) (width*(amplitudeNumber)), screenHeight - (scale_factor*(0 - minimumValue)) );
andrew@3 287
andrew@3 288
andrew@3 289 for (int Xvalue = 0;Xvalue < amplitudeNumber; Xvalue++){
andrew@3 290
andrew@3 291 int Xindex = (onsetIndex-Xvalue) ;
andrew@3 292 int previousIndex = (Xindex-1);
andrew@3 293
andrew@3 294 ofSetColor(55,100,255);
andrew@3 295
andrew@3 296 ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(onsetFunction[previousIndex]- minimumValue)),
andrew@3 297 (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(onsetFunction[Xindex]- minimumValue)) );
andrew@3 298
andrew@3 299 if (onsetRecorded[Xindex] == true){
andrew@3 300 ofSetColor(255,100,255);
andrew@3 301 ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(onsetFunction[Xindex]- minimumValue)) , 3);
andrew@3 302 }
andrew@3 303
andrew@3 304 //specDiffOnsets
andrew@3 305 ofSetColor(55,100,55);
andrew@3 306 Xindex = (rawOnsetIndex-Xvalue) ;
andrew@3 307 previousIndex = (Xindex-1);
andrew@3 308
andrew@3 309 ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(rawOnsetFunction[previousIndex]- minimumValue)),
andrew@3 310 (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(rawOnsetFunction[Xindex]- minimumValue)) );
andrew@3 311
andrew@3 312 //median of Onset fn
andrew@3 313 ofSetColor(205,0,0);
andrew@3 314 Xindex = (medianOnsetIndex-Xvalue) ;
andrew@3 315 previousIndex = (Xindex-1);
andrew@3 316
andrew@3 317 ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(medianOnsetFunction[previousIndex]- minimumValue)),
andrew@3 318 (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(medianOnsetFunction[Xindex]- minimumValue)) );
andrew@3 319
andrew@3 320
andrew@3 321
andrew@3 322 if (rawOnsetRecorded[Xindex] == true){
andrew@3 323 ofSetColor(255,100,0);
andrew@3 324 ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(rawOnsetFunction[Xindex]- minimumValue)) , 3);
andrew@3 325 }
andrew@3 326
andrew@3 327 if (medianOnsetRecorded[Xindex] == true){
andrew@3 328 ofSetColor(255,0,0);
andrew@3 329 ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(medianOnsetFunction[Xindex]- minimumValue)) , 3);
andrew@3 330 }
andrew@3 331
andrew@3 332
andrew@3 333 /*
andrew@3 334 if (medianOnsetRecorded[Xindex] == true){
andrew@3 335 ofSetColor(255,0,0);
andrew@3 336 ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(medianOnsetFunction[Xindex]- minimumValue)) , 3);
andrew@3 337 }
andrew@3 338 */
andrew@3 339 ofSetColor(255,100,0);
andrew@3 340
andrew@3 341 }//end for Xvalue (across the recent observations of osc data)
andrew@3 342
andrew@3 343
andrew@3 344 //label y axis
andrew@3 345 int axisHeight, stepSize;
andrew@3 346 ofSetColor(255,255,255);
andrew@3 347 stepSize = 1000;
andrew@3 348
andrew@3 349 while((difference / stepSize) < 3)
andrew@3 350 stepSize /= 2;
andrew@3 351
andrew@3 352 while ((difference / stepSize) > 7)// maximum 6 numbers to display
andrew@3 353 stepSize *= 2;
andrew@3 354
andrew@3 355
andrew@3 356 for (axisHeight = 0; axisHeight < maximumDetectionFunction; axisHeight += stepSize){
andrew@3 357 ofDrawBitmapString( ofToString((int)axisHeight), ofGetWidth()-50,
andrew@3 358 (int) ((TEXT_HEIGHT/2) +(screenHeight - (scale_factor*(axisHeight- minimumValue)))) );
andrew@3 359 }
andrew@3 360
andrew@3 361 for (axisHeight = max(0, (int)minimumDetectionFunction); axisHeight > min(0, (int)minimumDetectionFunction); axisHeight -= stepSize){
andrew@3 362 ofDrawBitmapString( ofToString((int)axisHeight), ofGetWidth()-50,
andrew@3 363 (int) ((TEXT_HEIGHT/2) +(screenHeight - (scale_factor*(axisHeight- minimumValue)))) );
andrew@3 364 }
andrew@3 365
andrew@3 366 //label x axis
andrew@3 367 stepSize = 20;//need to make sure not too many of these:
andrew@3 368
andrew@3 369 while((amplitudeNumber / stepSize) < 4)
andrew@3 370 stepSize /= 2;
andrew@3 371
andrew@3 372 while ((amplitudeNumber / stepSize) > 8)
andrew@3 373 stepSize *= 2;
andrew@3 374
andrew@3 375 int labelIndex = onsetIndex - (onsetIndex % stepSize);
andrew@3 376 for (int y = labelIndex; y > onsetIndex - amplitudeNumber; y -= stepSize){
andrew@3 377 ofDrawBitmapString( ofToString((int)y), (int) (width*(amplitudeNumber - (onsetIndex - y))),
andrew@3 378 (int) ((TEXT_HEIGHT+2) + (screenHeight - (scale_factor*(0 - minimumValue)))) );
andrew@3 379 }
andrew@3 380
andrew@3 381 }//end label
andrew@3 382 //--------------------------------------------------------------
andrew@3 383 void testApp::mouseMoved(int x, int y ){
andrew@3 384
andrew@3 385
andrew@3 386
andrew@3 387 }
andrew@3 388
andrew@3 389 //--------------------------------------------------------------
andrew@3 390 void testApp::mouseDragged(int x, int y, int button){
andrew@3 391 if ((x - mouseDownX) > 50 ){
andrew@3 392 mouseDownX = x;
andrew@3 393 }
andrew@3 394
andrew@3 395 if ((x - mouseDownX) < -50){
andrew@3 396
andrew@3 397 mouseDownX = x;
andrew@3 398 }
andrew@3 399
andrew@3 400
andrew@3 401 }
andrew@3 402
andrew@3 403 //--------------------------------------------------------------
andrew@3 404 void testApp::mousePressed(int x, int y, int button){
andrew@3 405
andrew@3 406 mouseDownX = x;
andrew@3 407 mouseDownY = y;
andrew@3 408
andrew@3 409 }
andrew@3 410
andrew@3 411 //--------------------------------------------------------------
andrew@3 412 void testApp::mouseReleased(int x, int y, int button){
andrew@3 413
andrew@3 414 }
andrew@3 415
andrew@3 416 //--------------------------------------------------------------
andrew@3 417 void testApp::windowResized(int w, int h){
andrew@3 418
andrew@3 419 screenWidth = (float) ofGetWidth();
andrew@3 420 screenHeight = (float) ofGetHeight();
andrew@3 421
andrew@3 422 }
andrew@3 423