PageRenderTime 47ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/src/testApp.cpp

http://github.com/mazbox/Speakotron
C++ | 186 lines | 111 code | 61 blank | 14 comment | 15 complexity | 80bf36651289c49a6e4bc41930a4ca30 MD5 | raw file
  1#include "testApp.h"
  2#include "ofxSimpleGuiToo.h"
  3#include "ofxCv.h"
  4int preset = 0;
  5int lastPreset = 0;
  6ofImage *Particle::img = NULL;
  7
  8
  9
 10
 11//--------------------------------------------------------------
 12void testApp::setup(){
 13
 14
 15	pd.load("patches/mouth1.pd", 0, 2, 44100, 256);
 16	
 17	
 18	ofSetVerticalSync(true);
 19	ofSetFrameRate(60.f);
 20
 21	WIDTH = 320;
 22	HEIGHT = 240;
 23
 24	int sourceWidth = 320;
 25	int sourceHeight = 240;
 26
 27	vidGrabber.setVerbose(true);
 28
 29	sourceWidth = WIDTH;
 30	sourceHeight = HEIGHT;
 31
 32	vidGrabber.initGrabber(WIDTH, HEIGHT);
 33
 34
 35
 36
 37    tracker.setup();
 38    tracker.setScale(0.5);
 39    
 40    mouths.push_back(Mouth());
 41	ofSoundStreamSetup(2,0,this, 44100,256, 1);
 42    
 43    
 44}
 45
 46//--------------------------------------------------------------
 47void testApp::update(){
 48	ofBackground(0,0,0);
 49
 50   vidGrabber.grabFrame();
 51   bool bNewFrame = vidGrabber.isFrameNew();
 52    
 53	
 54	if (bNewFrame){
 55        tracker.update(ofxCv::toCv(vidGrabber.getPixelsRef()));
 56
 57        ofRectangle rect(10, 10, 100, 100);
 58        mouths[0].update(rect);
 59        
 60		float pitch = 0;
 61		float filter = 0;
 62        
 63		for(int i = 0; i < mouths.size(); i++) {
 64			
 65			pitch = mouths[i].getPitch();
 66			filter = mouths[i].getFilter();
 67			
 68			pd.sendFloat("volume"+ofToString(i), mouths[i].isOpen()?1:0);
 69			if(mouths[i].isOpen()) {
 70				pd.sendFloat("pitch"+ofToString(i), pitch);
 71				pd.sendFloat("filter"+ofToString(i), filter);
 72				pd.sendFloat("pan"+ofToString(i), mouths[i].getPan());
 73			}
 74		}
 75		for(int i = 0; i < mouths.size(); i++) {
 76			if(mouths[i].isOpen() && ofRandom(0, 1)>0.5) { //mouthJustOpened()) {
 77				printf("Just opened\n");
 78				particles.push_back(Particle(mouths[i].getPos(), mouths[i].getFilter()));
 79			}
 80		}
 81		
 82		
 83	}
 84	
 85	for(int i = 0; i < particles.size(); i++) {
 86		if(particles[i].isAlive()) {
 87			particles[i].update();
 88		
 89		} else {
 90			particles.erase(particles.begin()+i);
 91			i--;
 92		}
 93
 94	}
 95
 96}
 97
 98//--------------------------------------------------------------
 99void testApp::draw(){
100
101	ofEnableAlphaBlending();
102	
103    // draw the incoming, the grayscale, the bg and the thresholded difference
104    ofSetHexColor(0xffffff);
105    glPushMatrix();
106    glScalef(ofGetWidth()/vidGrabber.getWidth(), ofGetHeight()/vidGrabber.getHeight(), 1);
107    vidGrabber.draw(0, 0);
108    for(int i = 0; i < particles.size(); i++) {
109        particles[i].draw();
110    }
111    ofSetHexColor(0xFFFFFF);
112    tracker.getImageMesh().drawWireframe();
113    
114    for(int i = 0; i < mouths.size(); i++) {
115        mouths[i].draw();
116    }
117
118    glPopMatrix();
119    // gui.draw();
120}
121
122
123//--------------------------------------------------------------
124void testApp::keyPressed  (int key){
125
126	switch (key){
127		case ' ':
128			//gui.toggleDraw();
129			//bLearnBakground = true;
130			break;
131		case 'v':
132			vidGrabber.videoSettings();
133
134            break;
135        case 'f':
136			ofToggleFullscreen();
137			break;
138		
139	}
140	if(key>='1' && key <= '4') {
141		preset = key - '1' + 1;		
142	}
143}
144
145//--------------------------------------------------------------
146void testApp::mouseMoved(int x, int y ){
147}
148
149//--------------------------------------------------------------
150void testApp::mouseDragged(int x, int y, int button){
151}
152
153//--------------------------------------------------------------
154void testApp::mousePressed(int x, int y, int button){
155}
156
157//--------------------------------------------------------------
158void testApp::mouseReleased(int x, int y, int button){
159
160}
161
162//--------------------------------------------------------------
163void testApp::windowResized(int w, int h){
164
165}
166
167float stuff[256];
168
169//--------------------------------------------------------------
170void testApp::audioRequested 	(float * output, int bufferSize, int nChannels){
171	if(lastPreset!=preset) {
172		string theFile = "patches/mouth";
173		theFile += ofToString(preset);
174		theFile += ".pd";
175		pd.load(theFile, 0, 2, 44100, 256);
176
177	}
178	lastPreset = preset;
179	
180
181
182	pd.process(stuff, output, bufferSize);
183	
184	
185}
186