PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/prototype2/src/testApp.cpp

https://github.com/bayks475/Final-Project
C++ | 321 lines | 155 code | 98 blank | 68 comment | 21 complexity | 4d5ab282d9aca7c43202ba9008012fe0 MD5 | raw file
  1. #include "testApp.h"
  2. //float [tones] = {12.70, 16.71, 21.20, 23.65, 29.00, 35.00, 41.74, 45.41};
  3. //--------------------------------------------------------------
  4. void testApp::setup(){
  5. video.initGrabber(320, 240);
  6. videoW=video.getWidth();
  7. videoH=video.getHeight();
  8. t= 60;
  9. //allocate memory for the texture
  10. colorVideo.allocate(320, 240);
  11. grayVideo.allocate(320, 240);
  12. background.allocate(320, 240);
  13. diffVideo.allocate(320, 240);
  14. //background
  15. bTakeSnapshot = true;
  16. int delayIncrement = 5; // How much should the delay increase per strip?
  17. // Set the frame delay for each strip
  18. for(int i=0; i<NUM_STRIPS; i++) {
  19. frameDelay[i] = i * delayIncrement; // It will be 50 delay at the top, and 0 frame delay at the bottom
  20. }
  21. // Calculate how many old frames from the camera we should keep around
  22. maxBufferFrames = NUM_STRIPS * delayIncrement;
  23. // How tall should each strip be?
  24. stripHeight = video.getHeight() / NUM_STRIPS;
  25. //sound
  26. int bufferSize = 512;
  27. sampleRate = 44100;
  28. phase = 0;
  29. phaseAdder = 0.0f;
  30. phaseAdderTarget = 0.0f;
  31. volume = 0.1f;
  32. bNoise = false;
  33. lAudio.assign(bufferSize, 0.0);
  34. rAudio.assign(bufferSize, 0.0);
  35. soundStream.setup(this, 2, 0, sampleRate, bufferSize, 4);
  36. ofSetFrameRate(60);
  37. duration = 2;
  38. startTime = ofGetElapsedTimef();
  39. }
  40. //--------------------------------------------------------------
  41. void testApp::update(){
  42. video.update();
  43. if(video.isFrameNew()){
  44. colorVideo.setFromPixels(video.getPixels(), videoW, videoH);
  45. // If we have a new frame, add it to the end of the buffer
  46. ofTexture frame;
  47. frame.allocate(320, 240, GL_RGB);
  48. frame.loadData(video.getPixels(), 320, 240, GL_RGB);
  49. frameBuffers.push_back( frame );
  50. // If we have more than "maxBufferFrames" in the buffer, get rid of the oldest one
  51. if(frameBuffers.size() > maxBufferFrames) {
  52. frameBuffers.erase(frameBuffers.begin());
  53. }
  54. //background subtractions
  55. colorVideo.mirror(false, true);
  56. grayVideo=colorVideo;
  57. if(bTakeSnapshot) {
  58. background = grayVideo;
  59. bTakeSnapshot = false;
  60. }
  61. diffVideo.absDiff(background, grayVideo);
  62. diffVideo.threshold(t);
  63. diffVideo.dilate();
  64. diffVideo.dilate();
  65. //contourVideo.findContours(grayVideo, 0, videoW*videoH, 1, true); //find contours
  66. contourVideo.findContours(diffVideo, 0, videoW*videoH, 1, false); //find contours
  67. //add treshold
  68. grayVideo.threshold(t);
  69. //white pixels
  70. twp = grayVideo.countNonZeroInRegion(0, 0, videoW , videoH); // get the number of non-black pixels
  71. wpp = twp / (videoW*videoH); // get the percentage
  72. //map the percentage
  73. targetFrequency = ofMap(wpp, 0, 1, 50, 2000);
  74. phaseAdderTarget = (targetFrequency / (float) sampleRate) * TWO_PI;
  75. }
  76. }
  77. //--------------------------------------------------------------
  78. void testApp::draw(){
  79. // Draw all of the stips
  80. for(int i=0; i<NUM_STRIPS; i++)
  81. {
  82. if(frameDelay[i] >= frameBuffers.size()) continue; // if we don't have enough buffered frames yet, skip drawing it.
  83. int n = frameBuffers.size()-frameDelay[i]-1; // Which buffered frame should we draw?
  84. int y = i * stripHeight;
  85. ofSetColor(255);
  86. frameBuffers[n].drawSubsection(0, y, frameBuffers[n].getWidth(), stripHeight, 0, y);
  87. }
  88. //draw it
  89. //video.draw(0, 0, videoW, videoH);
  90. //textures
  91. colorVideo.draw(videoW,0);
  92. grayVideo.draw(videoW*2, 0);
  93. //blob
  94. contourVideo.draw(videoW*3,0,videoW,videoH);
  95. for(int i=0; i<contourVideo.nBlobs; i++)
  96. {
  97. ofxCvBlob blob = contourVideo.blobs[i];
  98. vector<ofPoint> contour = blob.pts;
  99. for(int j=0; j<contour.size(); j++)
  100. {
  101. float x = ofMap(contour[j].x, 0, videoW, 0, videoH/2);
  102. float y = ofMap(contour[j].y, 0, videoW, 0, videoH/2);
  103. }
  104. }
  105. //check it
  106. ofDrawBitmapString("totalWhitePixels: " + ofToString(twp), 500, 500);
  107. ofDrawBitmapString("whitePixels Pct: " + ofToString(wpp), 500, 600);
  108. //ofRect(0, videoH, 100*wpp, 100*wpp);//use the pct to resize a square
  109. //sound check
  110. ofSetColor(225);
  111. string reportString = "volume: ("+ofToString(volume, 2)+") modify with -/+ keys\npan: ("+ofToString(pan, 2)+") modify with mouse x\nsynthesis: ";
  112. if( !bNoise ){
  113. reportString += "sine wave (" + ofToString(targetFrequency, 2) + "hz) modify with mouse y";
  114. }else{
  115. reportString += "noise";
  116. }
  117. ofDrawBitmapString(reportString, 32, 579);
  118. }
  119. //--------------------------------------------------------------
  120. void testApp::keyPressed(int key){
  121. }
  122. //--------------------------------------------------------------
  123. void testApp::keyReleased(int key){
  124. if(key== ' ') {
  125. bTakeSnapshot = true;
  126. }
  127. }
  128. //--------------------------------------------------------------
  129. void testApp::mouseMoved(int x, int y ){
  130. }
  131. //--------------------------------------------------------------
  132. void testApp::audioOut(float * output, int bufferSize, int nChannels) {
  133. //pan = 0.5f;
  134. float leftScale = 1 - pan;
  135. float rightScale = pan;
  136. // sin (n) seems to have trouble when n is very large, so we
  137. // keep phase in the range of 0-TWO_PI like this:
  138. while (phase > TWO_PI){
  139. phase -= TWO_PI;
  140. }
  141. for (int i = 0; i < phases.size(); i++){
  142. while (phases[i] > TWO_PI){
  143. phases[i] -= TWO_PI;
  144. }
  145. }
  146. for (int i = 0; i < phaseAdders.size(); i++){
  147. float pct = (ofGetElapsedTimef() - startTime) / duration;
  148. int sample = (int)ofMap(pct, 0, 1, 0, recordings[i].size()-1, true);
  149. targetFrequency = recordings[i][ sample ]; //// ofMap(wpp, 0, 1, 50, 2000);
  150. phaseAdderTargets[i] = (targetFrequency / (float) sampleRate) * TWO_PI;
  151. }
  152. if ( bNoise == true){
  153. // ---------------------- noise --------------
  154. for (int i = 0; i < bufferSize; i++){
  155. lAudio[i] = output[i*nChannels ] = ofRandom(0, 1) * volume * leftScale;
  156. rAudio[i] = output[i*nChannels + 1] = ofRandom(0, 1) * volume * rightScale;
  157. }
  158. } else {
  159. phaseAdder = 0.95f * phaseAdder + 0.07f * phaseAdderTarget;
  160. for (int i = 0; i < bufferSize; i++){
  161. phase += phaseAdder;
  162. float sample = sin(phase);
  163. lAudio[i] = output[i*nChannels ] = sample * volume * leftScale;
  164. rAudio[i] = output[i*nChannels + 1] = sample * volume * rightScale;
  165. }
  166. // add all the other video sin waves....
  167. for (int i = 0; i < phaseAdders.size(); i++){
  168. phaseAdders[i] = 0.95f * phaseAdders[i] + 0.07f * phaseAdderTargets[i];
  169. for (int j = 0; j < bufferSize; j++){
  170. phases[i] += phaseAdders[i];
  171. float sample = sin(phases[i]);
  172. output[j*nChannels ] += sample * volume * leftScale;
  173. output[j*nChannels + 1] += sample * volume * rightScale;
  174. }
  175. }
  176. }
  177. /*
  178. if (ofGetElapsedTimef() - startTime > duration){
  179. startTime = ofGetElapsedTimef();
  180. printf("new sound recording \n");
  181. recordings.push_back(curRecording);
  182. phases.push_back(0);
  183. phaseAdders.push_back(0);
  184. phaseAdderTargets.push_back(0);
  185. curRecording.clear();
  186. if (recordings.size() > 6){
  187. recordings.erase(recordings.begin());
  188. phases.erase(phases.begin());
  189. phaseAdders.erase(phaseAdders.begin());
  190. phaseAdderTargets.erase(phaseAdderTargets.begin());
  191. }
  192. } else {
  193. curRecording.push_back(targetFrequency);
  194. }*/
  195. }
  196. //--------------------------------------------------------------
  197. void testApp::mouseDragged(int x, int y, int button){
  198. }
  199. //--------------------------------------------------------------
  200. void testApp::mousePressed(int x, int y, int button){
  201. }
  202. //--------------------------------------------------------------
  203. void testApp::mouseReleased(int x, int y, int button){
  204. }
  205. //--------------------------------------------------------------
  206. void testApp::windowResized(int w, int h){
  207. }
  208. //--------------------------------------------------------------
  209. void testApp::gotMessage(ofMessage msg){
  210. }
  211. //--------------------------------------------------------------
  212. void testApp::dragEvent(ofDragInfo dragInfo){
  213. }