andrew@3: #include "testApp.h" andrew@3: //#include "math.h" andrew@3: andrew@3: #define SAMPLING_FREQUENCY 44100 andrew@3: #define FOURIER_LENGTH 2048 andrew@3: #define TEXT_HEIGHT 10 andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::setup(){ andrew@3: // listen on the given port andrew@3: // cout << "listening for osc messages on port " << PORT << "\n"; andrew@3: receiver.setup( PORT ); andrew@3: andrew@3: current_msg_string = 0; andrew@3: mouseX = 0; andrew@3: mouseY = 0; andrew@3: mouseButtonState = ""; andrew@3: andrew@3: ofBackground( 30, 30, 130 ); andrew@3: andrew@3: outputGraphics = false; andrew@3: andrew@3: maximumDetectionFunction = 20; andrew@3: minimumDetectionFunction = -20; andrew@3: andrew@3: screenWidth = (float) ofGetWidth(); andrew@3: screenHeight = (float) ofGetHeight(); andrew@3: andrew@3: mouseDownX = 0; andrew@3: mouseDownY = 0; andrew@3: andrew@3: amplitudeNumber = 256;//number of amplitudes shown on screen andrew@3: andrew@3: maxValue = 1.0; andrew@3: andrew@3: detectionType = "complex"; andrew@3: lastOnsetDetectionValue; andrew@3: andrew@3: logMode = false; andrew@3: andrew@3: midiMode = true; andrew@3: andrew@3: resetMaxima = false; andrew@3: andrew@3: reIndexFlag = false; andrew@3: andrew@3: ofBackground(0,0,0); andrew@3: andrew@3: rawOnsetIndex = 0; andrew@3: } andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::update(){ andrew@5: maxValue *= 0.995; andrew@3: andrew@3: // check for waiting messages andrew@3: while( receiver.hasWaitingMessages() ) andrew@3: { andrew@3: // get the next message andrew@3: ofxOscMessage m; andrew@3: receiver.getNextMessage( &m ); andrew@3: andrew@3: if (m.getAddress() == "/aubioData" ){ andrew@3: andrew@3: if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){ andrew@3: andrew@3: onsetIndex++; andrew@3: onsetRecorded[onsetIndex] = false; andrew@3: andrew@3: if (onsetIndex >= NUM_DETECTION_SAMPLES) andrew@3: onsetIndex = 0; andrew@3: andrew@3: onsetFunction[onsetIndex] = m.getArgAsFloat(0); andrew@3: andrew@3: checkMaxima(m.getArgAsFloat(0)); andrew@3: andrew@3: }//end if type FLOAT andrew@3: }//end if aubioData andrew@3: andrew@3: andrew@3: if (m.getAddress() == "/rawAubioData" ){ andrew@3: andrew@3: if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){ andrew@3: andrew@3: rawOnsetIndex++; andrew@3: andrew@3: if (rawOnsetIndex >= NUM_DETECTION_SAMPLES) andrew@3: rawOnsetIndex = 0; andrew@3: andrew@3: rawOnsetFunction[rawOnsetIndex] = m.getArgAsFloat(0); andrew@3: checkRawMaxima(m.getArgAsFloat(0)); andrew@3: andrew@3: }//end if type FLOAT andrew@3: }//end spec diff message andrew@3: andrew@3: andrew@3: if (m.getAddress() == "/medianAubioData" ){ andrew@3: andrew@3: if( m.getArgType( 0 ) == OFXOSC_TYPE_FLOAT ){ andrew@3: andrew@3: medianOnsetIndex++; andrew@3: andrew@3: if (medianOnsetIndex >= NUM_DETECTION_SAMPLES){ andrew@3: medianOnsetIndex = 0; andrew@3: } andrew@3: andrew@3: if(medianOnsetIndex > 0) andrew@3: medianOnsetRecorded[medianOnsetIndex] = false;//but how do we know this happens first! andrew@3: andrew@3: medianOnsetFunction[medianOnsetIndex] = m.getArgAsFloat(0); andrew@3: andrew@3: }//end if type FLOAT andrew@3: }//end spec diff message andrew@3: andrew@3: andrew@3: andrew@3: andrew@3: if (m.getAddress() == "/onset" ){ andrew@3: onsetRecorded[onsetIndex] = true; andrew@3: lastOnsetDetectionValue = onsetFunction[onsetIndex]; andrew@3: } andrew@3: andrew@3: if (m.getAddress() == "/medianOnset" ){ andrew@3: medianOnsetRecorded[onsetIndex] = true; andrew@3: } andrew@3: andrew@3: andrew@3: andrew@3: if (m.getAddress() == "/rawOnset" ){ andrew@3: rawOnsetRecorded[rawOnsetIndex] = true; andrew@3: andrew@3: } andrew@3: andrew@3: if (m.getAddress() == "/mode" ){ andrew@3: resetMaxima = true; andrew@3: detectionType = m.getArgAsString(0); andrew@3: } andrew@3: andrew@3: }//end while andrew@3: andrew@3: } andrew@3: andrew@3: andrew@3: void testApp::checkMaxima(float f){ andrew@3: andrew@3: //maximumDetectionFunction *= 0.99999; andrew@3: //minimumDetectionFunction += (maximumDetectionFunction - minimumDetectionFunction)*0.00001; andrew@3: andrew@3: if (maximumDetectionFunction * 1.08 < f){ andrew@3: maximumDetectionFunction = 1.08*f; andrew@3: } andrew@3: andrew@3: if (minimumDetectionFunction + (fabs(minimumDetectionFunction * 0.08)) > f){ andrew@3: minimumDetectionFunction = f - (0.08 * fabs(f)); andrew@3: } andrew@3: andrew@3: if (resetMaxima == true){ andrew@3: maximumDetectionFunction = 30; andrew@3: minimumDetectionFunction = 0; andrew@3: resetMaxima = false; andrew@3: } andrew@3: andrew@3: } andrew@3: andrew@3: andrew@3: void testApp::checkRawMaxima(float f){ andrew@3: andrew@3: //maximumDetectionFunction *= 0.99999; andrew@3: //minimumDetectionFunction += (maximumDetectionFunction - minimumDetectionFunction)*0.00001; andrew@3: andrew@3: if (maximumDetectionFunction * 1.08 < f){ andrew@3: maximumDetectionFunction = 1.08*f; andrew@3: } andrew@3: andrew@3: if (minimumDetectionFunction + (fabs(minimumDetectionFunction * 0.08)) > f){ andrew@3: minimumDetectionFunction = f - (0.08 * fabs(f)); andrew@3: } andrew@3: andrew@3: } andrew@3: andrew@3: andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::draw(){ andrew@3: andrew@3: drawOnsetFunction(); andrew@3: printInfo(); andrew@3: andrew@3: } andrew@3: andrew@3: void testApp::printMessages(){ andrew@3: string buf; andrew@3: buf = "listening for osc messages on port" + ofToString( PORT ); andrew@3: ofDrawBitmapString( buf, 10, 20 ); andrew@3: andrew@3: for ( int i=0; i 12){ andrew@3: amplitudeNumber -= 8; andrew@3: } andrew@3: andrew@3: } andrew@3: andrew@3: andrew@3: andrew@3: void testApp::drawOnsetFunction(){ andrew@3: int tmpIndex = onsetIndex; andrew@3: float width = screenWidth / (float) amplitudeNumber; andrew@3: float maximumValue = maximumDetectionFunction ; andrew@3: float minimumValue = minimumDetectionFunction ; andrew@3: float difference = maximumValue - minimumValue; andrew@3: float scale_factor = screenHeight/ difference; andrew@3: andrew@3: //draw axis andrew@3: ofSetColor(255,255,255); andrew@3: ofLine(0, screenHeight - (scale_factor*(0 - minimumValue)), andrew@3: (int) (width*(amplitudeNumber)), screenHeight - (scale_factor*(0 - minimumValue)) ); andrew@3: andrew@3: andrew@3: for (int Xvalue = 0;Xvalue < amplitudeNumber; Xvalue++){ andrew@3: andrew@3: int Xindex = (onsetIndex-Xvalue) ; andrew@3: int previousIndex = (Xindex-1); andrew@3: andrew@5: //below - Paul's processed onsets andrew@3: ofSetColor(55,100,255); andrew@3: andrew@3: ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(onsetFunction[previousIndex]- minimumValue)), andrew@3: (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(onsetFunction[Xindex]- minimumValue)) ); andrew@3: andrew@3: if (onsetRecorded[Xindex] == true){ andrew@3: ofSetColor(255,100,255); andrew@3: ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(onsetFunction[Xindex]- minimumValue)) , 3); andrew@3: } andrew@3: andrew@5: //specDiffOnsets andrew@3: ofSetColor(55,100,55); andrew@5: Xindex = (rawOnsetIndex-Xvalue); andrew@5: previousIndex = (Xindex-1); andrew@3: andrew@3: ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(rawOnsetFunction[previousIndex]- minimumValue)), andrew@3: (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(rawOnsetFunction[Xindex]- minimumValue)) ); andrew@3: andrew@3: //median of Onset fn andrew@3: ofSetColor(205,0,0); andrew@3: Xindex = (medianOnsetIndex-Xvalue) ; andrew@3: previousIndex = (Xindex-1); andrew@3: andrew@3: ofLine((int) (width*(amplitudeNumber - Xvalue - 1)), screenHeight - (scale_factor*(medianOnsetFunction[previousIndex]- minimumValue)), andrew@3: (int) (width*(amplitudeNumber - Xvalue)), screenHeight - (scale_factor*(medianOnsetFunction[Xindex]- minimumValue)) ); andrew@3: andrew@3: andrew@4: andrew@5: if (rawOnsetRecorded[Xindex] == true){ andrew@5: ofSetColor(255,100,0); andrew@5: ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(rawOnsetFunction[Xindex]- minimumValue)) , 3); andrew@5: } andrew@3: andrew@4: //median onsets in red andrew@3: if (medianOnsetRecorded[Xindex] == true){ andrew@3: ofSetColor(255,0,0); andrew@3: ofCircle(width*(amplitudeNumber - Xvalue), screenHeight - (scale_factor*(medianOnsetFunction[Xindex]- minimumValue)) , 3); andrew@3: } andrew@3: andrew@3: ofSetColor(255,100,0); andrew@3: andrew@3: }//end for Xvalue (across the recent observations of osc data) andrew@3: andrew@3: andrew@3: //label y axis andrew@3: int axisHeight, stepSize; andrew@3: ofSetColor(255,255,255); andrew@3: stepSize = 1000; andrew@3: andrew@3: while((difference / stepSize) < 3) andrew@3: stepSize /= 2; andrew@3: andrew@3: while ((difference / stepSize) > 7)// maximum 6 numbers to display andrew@3: stepSize *= 2; andrew@3: andrew@3: andrew@3: for (axisHeight = 0; axisHeight < maximumDetectionFunction; axisHeight += stepSize){ andrew@3: ofDrawBitmapString( ofToString((int)axisHeight), ofGetWidth()-50, andrew@3: (int) ((TEXT_HEIGHT/2) +(screenHeight - (scale_factor*(axisHeight- minimumValue)))) ); andrew@3: } andrew@3: andrew@3: for (axisHeight = max(0, (int)minimumDetectionFunction); axisHeight > min(0, (int)minimumDetectionFunction); axisHeight -= stepSize){ andrew@3: ofDrawBitmapString( ofToString((int)axisHeight), ofGetWidth()-50, andrew@3: (int) ((TEXT_HEIGHT/2) +(screenHeight - (scale_factor*(axisHeight- minimumValue)))) ); andrew@3: } andrew@3: andrew@3: //label x axis andrew@3: stepSize = 20;//need to make sure not too many of these: andrew@3: andrew@3: while((amplitudeNumber / stepSize) < 4) andrew@3: stepSize /= 2; andrew@3: andrew@3: while ((amplitudeNumber / stepSize) > 8) andrew@3: stepSize *= 2; andrew@3: andrew@3: int labelIndex = onsetIndex - (onsetIndex % stepSize); andrew@3: for (int y = labelIndex; y > onsetIndex - amplitudeNumber; y -= stepSize){ andrew@3: ofDrawBitmapString( ofToString((int)y), (int) (width*(amplitudeNumber - (onsetIndex - y))), andrew@3: (int) ((TEXT_HEIGHT+2) + (screenHeight - (scale_factor*(0 - minimumValue)))) ); andrew@3: } andrew@3: andrew@3: }//end label andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::mouseMoved(int x, int y ){ andrew@3: andrew@3: andrew@3: andrew@3: } andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::mouseDragged(int x, int y, int button){ andrew@3: if ((x - mouseDownX) > 50 ){ andrew@3: mouseDownX = x; andrew@3: } andrew@3: andrew@3: if ((x - mouseDownX) < -50){ andrew@3: andrew@3: mouseDownX = x; andrew@3: } andrew@3: andrew@3: andrew@3: } andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::mousePressed(int x, int y, int button){ andrew@3: andrew@3: mouseDownX = x; andrew@3: mouseDownY = y; andrew@3: andrew@3: } andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::mouseReleased(int x, int y, int button){ andrew@3: andrew@3: } andrew@3: andrew@3: //-------------------------------------------------------------- andrew@3: void testApp::windowResized(int w, int h){ andrew@3: andrew@3: screenWidth = (float) ofGetWidth(); andrew@3: screenHeight = (float) ofGetHeight(); andrew@3: andrew@3: } andrew@3: