/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"
  4. int preset = 0;
  5. int lastPreset = 0;
  6. ofImage *Particle::img = NULL;
  7. //--------------------------------------------------------------
  8. void testApp::setup(){
  9. pd.load("patches/mouth1.pd", 0, 2, 44100, 256);
  10. ofSetVerticalSync(true);
  11. ofSetFrameRate(60.f);
  12. WIDTH = 320;
  13. HEIGHT = 240;
  14. int sourceWidth = 320;
  15. int sourceHeight = 240;
  16. vidGrabber.setVerbose(true);
  17. sourceWidth = WIDTH;
  18. sourceHeight = HEIGHT;
  19. vidGrabber.initGrabber(WIDTH, HEIGHT);
  20. tracker.setup();
  21. tracker.setScale(0.5);
  22. mouths.push_back(Mouth());
  23. ofSoundStreamSetup(2,0,this, 44100,256, 1);
  24. }
  25. //--------------------------------------------------------------
  26. void testApp::update(){
  27. ofBackground(0,0,0);
  28. vidGrabber.grabFrame();
  29. bool bNewFrame = vidGrabber.isFrameNew();
  30. if (bNewFrame){
  31. tracker.update(ofxCv::toCv(vidGrabber.getPixelsRef()));
  32. ofRectangle rect(10, 10, 100, 100);
  33. mouths[0].update(rect);
  34. float pitch = 0;
  35. float filter = 0;
  36. for(int i = 0; i < mouths.size(); i++) {
  37. pitch = mouths[i].getPitch();
  38. filter = mouths[i].getFilter();
  39. pd.sendFloat("volume"+ofToString(i), mouths[i].isOpen()?1:0);
  40. if(mouths[i].isOpen()) {
  41. pd.sendFloat("pitch"+ofToString(i), pitch);
  42. pd.sendFloat("filter"+ofToString(i), filter);
  43. pd.sendFloat("pan"+ofToString(i), mouths[i].getPan());
  44. }
  45. }
  46. for(int i = 0; i < mouths.size(); i++) {
  47. if(mouths[i].isOpen() && ofRandom(0, 1)>0.5) { //mouthJustOpened()) {
  48. printf("Just opened\n");
  49. particles.push_back(Particle(mouths[i].getPos(), mouths[i].getFilter()));
  50. }
  51. }
  52. }
  53. for(int i = 0; i < particles.size(); i++) {
  54. if(particles[i].isAlive()) {
  55. particles[i].update();
  56. } else {
  57. particles.erase(particles.begin()+i);
  58. i--;
  59. }
  60. }
  61. }
  62. //--------------------------------------------------------------
  63. void testApp::draw(){
  64. ofEnableAlphaBlending();
  65. // draw the incoming, the grayscale, the bg and the thresholded difference
  66. ofSetHexColor(0xffffff);
  67. glPushMatrix();
  68. glScalef(ofGetWidth()/vidGrabber.getWidth(), ofGetHeight()/vidGrabber.getHeight(), 1);
  69. vidGrabber.draw(0, 0);
  70. for(int i = 0; i < particles.size(); i++) {
  71. particles[i].draw();
  72. }
  73. ofSetHexColor(0xFFFFFF);
  74. tracker.getImageMesh().drawWireframe();
  75. for(int i = 0; i < mouths.size(); i++) {
  76. mouths[i].draw();
  77. }
  78. glPopMatrix();
  79. // gui.draw();
  80. }
  81. //--------------------------------------------------------------
  82. void testApp::keyPressed (int key){
  83. switch (key){
  84. case ' ':
  85. //gui.toggleDraw();
  86. //bLearnBakground = true;
  87. break;
  88. case 'v':
  89. vidGrabber.videoSettings();
  90. break;
  91. case 'f':
  92. ofToggleFullscreen();
  93. break;
  94. }
  95. if(key>='1' && key <= '4') {
  96. preset = key - '1' + 1;
  97. }
  98. }
  99. //--------------------------------------------------------------
  100. void testApp::mouseMoved(int x, int y ){
  101. }
  102. //--------------------------------------------------------------
  103. void testApp::mouseDragged(int x, int y, int button){
  104. }
  105. //--------------------------------------------------------------
  106. void testApp::mousePressed(int x, int y, int button){
  107. }
  108. //--------------------------------------------------------------
  109. void testApp::mouseReleased(int x, int y, int button){
  110. }
  111. //--------------------------------------------------------------
  112. void testApp::windowResized(int w, int h){
  113. }
  114. float stuff[256];
  115. //--------------------------------------------------------------
  116. void testApp::audioRequested (float * output, int bufferSize, int nChannels){
  117. if(lastPreset!=preset) {
  118. string theFile = "patches/mouth";
  119. theFile += ofToString(preset);
  120. theFile += ".pd";
  121. pd.load(theFile, 0, 2, 44100, 256);
  122. }
  123. lastPreset = preset;
  124. pd.process(stuff, output, bufferSize);
  125. }