ofApp.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "ofApp.h"
  2. //--------------------------------------------------------------
  3. void ofApp::setup(){
  4. ofSetFrameRate(24);
  5. // ofEnableAntiAliasing();
  6. ofEnableBlendMode(OF_BLENDMODE_ALPHA);
  7. // ofEnableSmoothing();
  8. // voronoi.setMinDist(std::epsilon<float>());
  9. ofSetVerticalSync(true);
  10. ofSetCircleResolution(80);
  11. ofBackground(54, 54, 54);
  12. soundStream.printDeviceList();
  13. int bufferSize = 256;
  14. left.assign(bufferSize, 0.0);
  15. right.assign(bufferSize, 0.0);
  16. volHistory.assign(400, 0.0);
  17. bufferCounter = 0;
  18. drawCounter = 0;
  19. smoothedVol = 0.0;
  20. scaledVol = 0.0;
  21. ofSoundStreamSettings settings;
  22. // // if you want to set the device id to be different than the default
  23. auto devices = soundStream.getDeviceList(ofSoundDevice::Api::JACK);
  24. // // settings.device = devices[4];
  25. // // you can also get devices for an specific api
  26. // // auto devices = soundStream.getDevicesByApi(ofSoundDevice::Api::PULSE);
  27. // // settings.device = devices[0];
  28. // // or get the default device for an specific api:
  29. // // settings.api = ofSoundDevice::Api::PULSE;
  30. // // or by name
  31. //// auto devices = soundStream.getMatchingDevices("default");
  32. //auto devices = soundStream.getDevicesByApi(ofSoundDevice::Api::JACK);
  33. // settings.device = devices[0];
  34. if(!devices.empty()){
  35. settings.setInDevice(devices[0]);
  36. }
  37. settings.setInListener(this);
  38. settings.sampleRate = 48000;
  39. settings.numOutputChannels = 0;
  40. settings.numInputChannels = 2;
  41. settings.bufferSize = bufferSize;
  42. soundStream.setup(settings);
  43. ///preparing visula glitch
  44. Line l=Line(10.0f,10.0f);
  45. l.setX(12.0f);
  46. glitches.push_back(new Line(300.0f,300.0f));
  47. // audioAnalyzer.setup(48000, bufferSize, 2);
  48. }
  49. //--------------------------------------------------------------
  50. void ofApp::update(){
  51. // //lets scale the vol up to a 0-1 range
  52. // scaledVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 1.0, true);
  53. // //lets record the volume into an array
  54. // volHistory.push_back( scaledVol );
  55. // //if we are bigger the the size we want to record - lets drop the oldest value
  56. // if( volHistory.size() >= 400 ){
  57. // volHistory.erase(volHistory.begin(), volHistory.begin()+1);
  58. // }
  59. coinToss= ofRandom(0, 3.0);
  60. }
  61. //--------------------------------------------------------------
  62. void ofApp::draw(){
  63. //---------- VONOROI -----------
  64. // draw background
  65. ofColor centerColor = ofColor(85, 78, 68);
  66. ofColor edgeColor(0, 0, 0);
  67. ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);
  68. ofSetColor(255,255,255);
  69. if(coinToss <= 1){
  70. ofSetLineWidth( map(coinToss,0,3,1,5));
  71. //ofDrawLine(glm::vec2(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight())),glm::vec2(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight())));
  72. for(auto & g : glitches)
  73. {
  74. g->draw();
  75. }
  76. }
  77. }
  78. //--------------------------------------------------------------
  79. void ofApp::audioIn(ofSoundBuffer & input){
  80. float curVol = 0.0;
  81. // samples are "interleaved"
  82. int numCounted = 0;
  83. //lets go through each sample and calculate the root mean square which is a rough way to calculate volume
  84. for (size_t i = 0; i < input.getNumFrames(); i++){
  85. left[i] = input[i*2]*0.5;
  86. right[i] = input[i*2+1]*0.5;
  87. curVol += left[i] * left[i];
  88. curVol += right[i] * right[i];
  89. numCounted+=2;
  90. }
  91. //this is how we get the mean of rms :)
  92. curVol /= (float)numCounted;
  93. // this is how we get the root of rms :)
  94. curVol = sqrt( curVol );
  95. smoothedVol *= 0.93;
  96. smoothedVol += 0.07 * curVol;
  97. bufferCounter++;
  98. }
  99. //--------------------------------------------------------------
  100. void ofApp::audioReceived(float* input, int bufferSize, int nChannels) {
  101. float maxValue = 0;
  102. for(int i = 0; i < bufferSize; i++) {
  103. if(abs(input[i]) > maxValue) {
  104. maxValue = abs(input[i]);
  105. }
  106. }
  107. for(int i = 0; i < bufferSize; i++) {
  108. input[i] /= maxValue;
  109. }
  110. fft->setSignal(input);
  111. float* curFft = fft->getAmplitude();
  112. memcpy(&audioBins[0], curFft, sizeof(float) * fft->getBinSize());
  113. maxValue = 0;
  114. for(int i = 0; i < fft->getBinSize(); i++) {
  115. if(abs(audioBins[i]) > maxValue) {
  116. maxValue = abs(audioBins[i]);
  117. }
  118. }
  119. for(int i = 0; i < fft->getBinSize(); i++) {
  120. audioBins[i] /= maxValue;
  121. }
  122. soundMutex.lock();
  123. middleBins = audioBins;
  124. soundMutex.unlock();
  125. }
  126. //--------------------------------------------------------------
  127. void ofApp::keyPressed (int key){
  128. std::cout << "Pressed " << char(key) << " (int=" << key << ")\n";
  129. switch(key){
  130. case 'f':
  131. ofToggleFullscreen();
  132. break;
  133. case 's':
  134. soundStream.start();
  135. break;
  136. case 'e':
  137. soundStream.stop();
  138. break;
  139. }
  140. }
  141. //--------------------------------------------------------------
  142. void ofApp::keyReleased(int key){
  143. }
  144. //--------------------------------------------------------------
  145. void ofApp::mouseMoved(int x, int y ){
  146. }
  147. //--------------------------------------------------------------
  148. void ofApp::mouseDragged(int x, int y, int button){
  149. }
  150. //--------------------------------------------------------------
  151. void ofApp::mousePressed(int x, int y, int button){
  152. }
  153. //--------------------------------------------------------------
  154. void ofApp::mouseReleased(int x, int y, int button){
  155. }
  156. //--------------------------------------------------------------
  157. void ofApp::mouseEntered(int x, int y){
  158. }
  159. //--------------------------------------------------------------
  160. void ofApp::mouseExited(int x, int y){
  161. }
  162. //--------------------------------------------------------------
  163. void ofApp::windowResized(int w, int h){
  164. }
  165. //--------------------------------------------------------------
  166. void ofApp::gotMessage(ofMessage msg){
  167. }
  168. //--------------------------------------------------------------
  169. void ofApp::dragEvent(ofDragInfo dragInfo){
  170. }
  171. float ofApp::map(float value, float min1, float max1, float min2, float max2) {
  172. return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
  173. }