#include "testApp.h" /* maybe draw pixel lines to a range of buffers and then play them in various 3D directions pixel lines can have nice, missing corners and be nicely smoothed is it insane not to do this in vvvv can there be an editing interface have a play with vvvv/ max and try to replicate this idea QUICKLY? */ //-------------------------------------------------------------- void testApp::setup(){ midiIn.listPorts(); midiIn.openPort(1); midiIn.addListener(this); midiOut.listPorts(); midiOut.openPort(1); // 0 output channeLs, // 2 input channels // 44100 samples per second // BUFFER_SIZE samples per buffer // 4 num buffers (latency) soundStream.listDevices(); left = new float[BUFFER_SIZE]; right = new float[BUFFER_SIZE]; soundStream.setup(this, 0, 2, 44100, BUFFER_SIZE, 4); ofSetHexColor(0x666666); lFFTanalyzer.setup(44100, BUFFER_SIZE/2,32); lFFTanalyzer.peakHoldTime = 15; // hold longer lFFTanalyzer.peakDecayRate = 0.95f; // decay slower lFFTanalyzer.linearEQIntercept = 0.9f; // reduced gain at lowest frequency lFFTanalyzer.linearEQSlope = 0.01f; // increasing gain at higher frequencies rFFTanalyzer.setup(44100, BUFFER_SIZE/2,32); rFFTanalyzer.peakHoldTime = 15; // hold longer rFFTanalyzer.peakDecayRate = 0.95f; // decay slower rFFTanalyzer.linearEQIntercept = 0.9f; // reduced gain at lowest frequency rFFTanalyzer.linearEQSlope = 0.01f; // increasing gain at higher frequencies ofSetFrameRate(60); ofBackground(0,0,0); F_movieSpeed=0.0; //blendImage.loadImage("blend01.png"); blendImage.loadMovie("blend01.mov"); blendImage.play(); blendImage.setLoopState(OF_LOOP_NORMAL); blendImage.setSpeed(F_movieSpeed); I_movieSource=0; I_moviePlaying=0; renderImage.allocate(ofGetWidth(),ofGetHeight(),GL_RGB); maskShader.load("composite"); //testImage.loadImage("mask.png"); maskShader.begin(); maskShader.setUniformTexture("Tex0", blendImage.getTextureReference(), 0); maskShader.setUniformTexture("Tex1", renderImage.getTextureReference(), 1); maskShader.end(); showFPS=false; //defaults F_scale=0; F_drawFrames=10; F_drawStep=0; F_drawDecay=0.95; F_lineWidth=1.0; F_drawAxis=90; //Y F_xRotate=0; F_yRotate=0; F_zRotate=0; F_xRotation=0; F_yRotation=180; F_zRotation=0; I_fade1=0; I_fade2=0; thisFFTbuffer=0; lastFrameTime=ofGetElapsedTimef(); draworder=true; //forwards; visMode=WAVEFORM; B_vSync=true; B_fill=true; inputMode=PICTURE; camWidth=720; camHeight=576; grabber.initGrabber(camWidth,camHeight); gammamap=new unsigned char[256]; line1=new unsigned char[camWidth]; line2=new unsigned char[camWidth]; outBuffer=new unsigned char[camWidth*camHeight*3]; whitePt=0.9; blackPt=0.3; gamma=1.0; //calc gamma map for (int i=0;i<256;i++){ float ewp=max(whitePt,blackPt+0.1f); //limit range to 0.1 gammamap[i]=(unsigned char)(pow(min(1.0f,max(0.0f,(((float(i)/255.0f)-blackPt)/(ewp-blackPt)))),gamma)*255.0); } outTexture.allocate(camWidth,camHeight, GL_RGB); deInterlace=true; B_glitch=false; blanker.allocate(64,64,OF_IMAGE_COLOR); // blanker.loadImage("black.png"); setMidiState(); } //-------------------------------------------------------------- void testApp::update(){ if (inputMode==GRABBER) grabber.grabFrame(); if (grabber.isFrameNew()){ int time=ofGetElapsedTimeMillis(); frameTime=time-grabTime; grabTime=time; unsigned char * pixels = grabber.getPixels(); if(!deInterlace) { int totalPixels = camWidth*camHeight; for (int i = 0; i < totalPixels; i++){ unsigned char gs=(unsigned char)((int(pixels[i*3])+int(pixels[i*3+1])+int(pixels[i*3+2]))/3); outBuffer[i*3] = gammamap[gs]; outBuffer[i*3+1] = gammamap[gs]; outBuffer[i*3+2] = gammamap[gs]; } } else { field2=true; unsigned char* lp1=line1; unsigned char* lp2=line2; unsigned char* tp; int j = 0; for (int i=0;i=(frameTime/2))){ field2=false; unsigned char * pixels = grabber.getPixels(); unsigned char* lp1=line1; unsigned char* lp2=line2; unsigned char* tp; int j = 1; for (int i=0;i270) F_yseg-=360; if (F_yseg>90) { jStart=jEnd; jEnd=0; jStep=-1; if (draworder) { draworder=false; //printf("switched to reverse order\n"); } } else if (!draworder) { draworder=true; //printf("switched to normal order\n"); } for (int j=jStart;j!=jEnd;j+=jStep) { int fB=(thisFFTbuffer+j)%BUFFER_FRAMES; ofPushMatrix(); //coordinate transform for FFT draw direction ofTranslate(0,0,(j*zStep)-hw); ofRotateX(F_drawAxis); ofFill(); ofSetColor(0,0,0); if (B_fill) { glBegin(GL_QUAD_STRIP); for (int i = 0; i < lFFTanalyzer.nAverages-1; i++){ glVertex3f((i*xStep)-hw,-(FFTbuffer[0][i][fB] * F_scale),0); glVertex3f((i*xStep)-hw,0,0); } for (int i =lFFTanalyzer.nAverages, k=lFFTanalyzer.nAverages-1; i < lFFTanalyzer.nAverages+rFFTanalyzer.nAverages; i++, k--){ glVertex3f((i*xStep)-hw,-(FFTbuffer[1][(lFFTanalyzer.nAverages+rFFTanalyzer.nAverages)-(i+1)][fB] * F_scale),0); glVertex3f((i*xStep)-hw,0,0); } glEnd(); } ofNoFill(); ofSetLineWidth(F_lineWidth); ofSetColor(I_fade1,I_fade1,I_fade1); glBegin(GL_LINE_STRIP); for (int i = 0; i < lFFTanalyzer.nAverages; i++){ glVertex3f((i*xStep)-hw,-(FFTbuffer[0][i][fB] * F_scale),0); } for (int i =lFFTanalyzer.nAverages, k=lFFTanalyzer.nAverages-1; i < lFFTanalyzer.nAverages+rFFTanalyzer.nAverages; i++, k--){ glVertex3f((i*xStep)-hw,-(FFTbuffer[1][(lFFTanalyzer.nAverages+rFFTanalyzer.nAverages)-(i+1)][fB] * F_scale),0); } glEnd(); ofPopMatrix(); } ofPopMatrix(); renderImage.end(); //ofEndShape(false); //fbImage.grabScreen(0,0,ofGetWidth(),ofGetHeight()); /* ofSetHexColor(0xaaaaaa); int width=2048/FFTanalyzer.nAverages; for (int i = 0; i < FFTanalyzer.nAverages; i++){ ofRect(i*width,768,width,-FFTanalyzer.averages[i] * 20); } ofSetHexColor(0xffffff); for (int i = 0; i < (int)(BUFFER_SIZE/2 - 1); i++){ ofRect(i,768-freq[i]*10.0f,1,1); } ofSetHexColor(0xff0000); for (int i = 0; i < FFTanalyzer.nAverages; i++){ ofRect(i*width,768-FFTanalyzer.peaks[i] * 20,width,-1); } float avgStep = 1024/FFTanalyzer.nAverages; ofNoFill(); ofSetLineWidth(1); ofSetColor(223, 218, 218); ofDrawBitmapString("FFT average", 4, 18); ofBeginShape(); for (int i = 0; i < FFTanalyzer.nAverages; i++){ ofVertex(i*avgStep, 284 -FFTanalyzer.averages[i] * 20); } ofEndShape(false); ofSetColor(97,181,243); ofDrawBitmapString("FFT magnitude", 4, 38); ofBeginShape(); for (int i = 0; i < BUFFER_SIZE; i++){ ofVertex(i*avgStep, 384 -magnitude[i] * 20); } ofEndShape(false); ofSetColor(97,243,174); ofDrawBitmapString("FFT phase", 4, 58); ofBeginShape(); for (int i = 0; i < BUFFER_SIZE; i++){ ofVertex(i*avgStep, 484 -phase[i] * 20); } ofEndShape(false); ofSetColor(243,174,94); ofDrawBitmapString("FFT power", 4, 78); ofBeginShape(); for (int i = 0; i < BUFFER_SIZE; i++){ ofVertex(i*avgStep, 584 -power[i] * 20); } ofEndShape(false); //float * averages; // the actual averages //float * peaks; // peaks of the averages, aka "maxAverages" in other implementations */ ofEnableAlphaBlending(); maskShader.begin(); glActiveTexture(GL_TEXTURE0_ARB); blendImage.getTextureReference().bind(); glActiveTexture(GL_TEXTURE1_ARB); renderImage.getTextureReference().bind(); glBegin(GL_QUADS); glMultiTexCoord2d(GL_TEXTURE0_ARB, 0, 0); glMultiTexCoord2d(GL_TEXTURE1_ARB, 0, 0); glVertex2f( 0, 0); glMultiTexCoord2d(GL_TEXTURE0_ARB, blendImage.getWidth(), 0); glMultiTexCoord2d(GL_TEXTURE1_ARB, renderImage.getWidth(), 0); glVertex2f( ofGetWidth(), 0); glMultiTexCoord2d(GL_TEXTURE0_ARB, blendImage.getWidth(), blendImage.getHeight()); glMultiTexCoord2d(GL_TEXTURE1_ARB, renderImage.getWidth(), renderImage.getHeight() ); glVertex2f( ofGetWidth(), ofGetHeight()); glMultiTexCoord2d(GL_TEXTURE0_ARB, 0, blendImage.getHeight()); glMultiTexCoord2d(GL_TEXTURE1_ARB, 0, renderImage.getHeight() ); glVertex2f( 0, ofGetHeight() ); glEnd(); glActiveTexture(GL_TEXTURE1_ARB); renderImage.getTextureReference().unbind(); glActiveTexture(GL_TEXTURE0_ARB); blendImage.getTextureReference().unbind(); maskShader.end(); /* glPushMatrix(); glTranslatef(ofGetWidth()*0.5,ofGetHeight()*0.5,0); glRotatef((float)ofGetFrameNum(),0,1.0,0); renderImage.draw(ofGetWidth()*-0.5,ofGetHeight()*-0.5); glPopMatrix(); */ if (showFPS) { ofDrawBitmapString(ofToString(ofGetFrameRate(), 2),20,20); ofDrawBitmapString(ofToString(F_xRotation, 4)+" "+ofToString(F_yRotation, 4)+" "+ofToString(F_zRotation, 4),20,30); ofDrawBitmapString(ofToString(F_yseg, 4),20,40); } } //-------------------------------------------------------------- void testApp::keyPressed (int key){ if(key == 'f'){ showFPS=!showFPS; } } //-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mouseReleased(){ } //-------------------------------------------------------------- void testApp::audioIn(float * input, int bufferSize, int nChannels){ /*if (sampnum<10) { printf("size %i, channels %i\n",bufferSize,nChannels); sampnum++; } */ // samples are "interleaved" //if (nChannels==2) { for (int i = 0; i < bufferSize; i++){ left[i] = input[i*2]; right[i] = input[i*2+1]; } //} } void testApp::newMidiMessage(ofxMidiEventArgs& eventArgs){ //newMessage(eventArgs.port, eventArgs.channel, eventArgs.byteTwo, eventArgs.timestamp); //byteOne : message type /* int port; int channel; int status; int byteOne; int byteTwo; double timestamp; */ //printf("%d %d %d %d %d\n",eventArgs.port,eventArgs.channel,eventArgs.status,eventArgs.byteOne,eventArgs.byteTwo); bool noteOn; //this old thing! switch(eventArgs.status) { case 144: //noteon-off break; case 176: //control change 1-8 x 4 banks switch(eventArgs.byteOne) { case 1: F_scale = ((float)eventArgs.byteTwo)*.2; break; case 2: F_lineWidth=(float)eventArgs.byteTwo*0.1; break; case 3: F_drawAxis=((float) eventArgs.byteTwo-64)*(360.0/127); break; case 4: break; case 5: break; //65-80 buttons //radio button set case 65: if (eventArgs.byteTwo==127) { visMode=FFT_AVG; midiOut.sendControlChange(1, 66, 0); midiOut.sendControlChange(1, 67, 0); } else midiOut.sendControlChange(1, 65, 127); break; case 66: if (eventArgs.byteTwo==127) { visMode=FFT_RAW; midiOut.sendControlChange(1, 65, 0); midiOut.sendControlChange(1, 67, 0); } else midiOut.sendControlChange(1, 66, 127); break; case 67: if (eventArgs.byteTwo==127) { visMode=WAVEFORM; midiOut.sendControlChange(1, 65, 0); midiOut.sendControlChange(1, 66, 0); } else midiOut.sendControlChange(1, 67, 127); break; //radio button set case 68: if (eventArgs.byteTwo==127) { inputMode=PICTURE; midiOut.sendControlChange(1, 69, 0); } else midiOut.sendControlChange(1, 68, 127); break; case 69: if (eventArgs.byteTwo==127) { inputMode=GRABBER; midiOut.sendControlChange(1, 68, 0); } else midiOut.sendControlChange(1, 69, 127); break; case 70: B_glitch=(eventArgs.byteTwo==127); break; case 71: B_fill=(eventArgs.byteTwo==127); break; case 72: B_vSync=(eventArgs.byteTwo==127); break; //buttons to zero rotations case 73: F_xRotate=0; midiOut.sendControlChange(1, 73, 0); midiOut.sendControlChange(1, 81, 64); break; case 74: F_yRotate=0; midiOut.sendControlChange(1, 74, 0); midiOut.sendControlChange(1, 82, 64); break; case 75: F_zRotate=0; midiOut.sendControlChange(1, 75, 0); midiOut.sendControlChange(1, 83, 64); break; case 76: F_drawStep=0; midiOut.sendControlChange(1, 76, 0); midiOut.sendControlChange(1, 84, 64); break; case 77: if (eventArgs.byteTwo==127) { I_movieSource=0; midiOut.sendControlChange(1, 78, 0); midiOut.sendControlChange(1, 79, 0); midiOut.sendControlChange(1, 80, 0); } else midiOut.sendControlChange(1, 77, 127); break; case 78: if (eventArgs.byteTwo==127) { I_movieSource=1; midiOut.sendControlChange(1, 77, 0); midiOut.sendControlChange(1, 79, 0); midiOut.sendControlChange(1, 80, 0); } else midiOut.sendControlChange(1, 78, 127); break; case 79: if (eventArgs.byteTwo==127) { I_movieSource=2; midiOut.sendControlChange(1, 77, 0); midiOut.sendControlChange(1, 78, 0); midiOut.sendControlChange(1, 80, 0); } else midiOut.sendControlChange(1, 79, 127); break; case 80: if (eventArgs.byteTwo==127) { I_movieSource=3; midiOut.sendControlChange(1, 77, 0); midiOut.sendControlChange(1, 78, 0); midiOut.sendControlChange(1, 79, 0); } else midiOut.sendControlChange(1, 80, 127); break; // probably make controls logarithmic case 81: F_xRotate=(((float) eventArgs.byteTwo)-64)*.1; break; case 82: F_yRotate=(((float) eventArgs.byteTwo)-64)*.1; break; case 83: F_zRotate=(((float) eventArgs.byteTwo)-64)*.1; break; case 84: F_drawStep=(((float) eventArgs.byteTwo)-64)*.1; break; case 85: F_movieSpeed=((float)eventArgs.byteTwo)/64.0; break; case 86: F_drawDecay=((float)eventArgs.byteTwo)/127.0; break; case 87: I_fade2=((float)eventArgs.byteTwo)*2; break; case 88: I_fade1=((float)eventArgs.byteTwo)*2; //printf("fog- %f\n", F_drawDecay); break; case 91: //top F_xRotation=90; F_yRotation=90; F_zRotation=0; midiOut.sendControlChange(1, 91, 0); break; case 92: //front F_xRotation=0; F_yRotation=180; F_zRotation=0; midiOut.sendControlChange(1, 92, 0); break; //89-92 bottom 4 } //printf("rotation- %f %f %f\n",F_xRotate,F_yRotate,F_zRotate); //printf(inputMode==PICTURE?"PICTURE mode\n":"GRABBER mode\n"); } } void testApp::setMidiState(){ int knobs[32]; int buttons[16]; int faders[8]; for (int i=0;i<32;i++) knobs[i]=0; for (int i=0;i<16;i++) buttons[i]=0; for (int i=0;i<8;i++) faders[i]=0; knobs[0] = (F_scale * 5); knobs[1]=(F_lineWidth*10); knobs[2]=(F_drawAxis/(360.0/127))+64; //radio button set buttons[0]=(visMode==FFT_AVG?127:0); buttons[1]=(visMode==FFT_RAW?127:0); buttons[2]=(visMode==WAVEFORM?127:0); //radio button set buttons[3]=(inputMode==PICTURE?127:0); buttons[4]=(inputMode==GRABBER?127:0); buttons[5]=B_glitch?127:0; buttons[6]=B_fill?127:0; buttons[7]=B_vSync?127:0; buttons[12]=I_movieSource==0?127:0; buttons[13]=I_movieSource==1?127:0; buttons[14]=I_movieSource==2?127:0; buttons[15]=I_movieSource==3?127:0; faders[0]=(F_xRotate*10)+64; faders[1]=(F_yRotate*10)+64; faders[2]=(F_zRotate*10)+64; faders[3]=(F_drawStep*10)+64; faders[4]=F_movieSpeed*127; faders[5]=(F_drawDecay*127); faders[6]=I_fade1/2; faders[7]=I_fade1/2; for (int i=0;i<32;i++) midiOut.sendControlChange(1, i+1, knobs[i]); for (int i=0;i<16;i++) midiOut.sendControlChange(1, i+65, buttons[i]); for (int i=0;i<8;i++) midiOut.sendControlChange(1, i+81, faders[i]); } /* void sendNoteOn(int channel, int id, int value); void sendNoteOff(int channel, int id, int value); void sendControlChange(int channel, int id, int value); */