123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include "ofApp.h"
- //--------------------------------------------------------------
- void ofApp::setup(){
- ofSetFrameRate(24);
- // ofEnableAntiAliasing();
- ofEnableBlendMode(OF_BLENDMODE_ALPHA);
- // ofEnableSmoothing();
- // voronoi.setMinDist(std::epsilon<float>());
- ofSetVerticalSync(true);
- ofSetCircleResolution(80);
- ofBackground(54, 54, 54);
-
- soundStream.printDeviceList();
-
- int bufferSize = 256;
- left.assign(bufferSize, 0.0);
- right.assign(bufferSize, 0.0);
- volHistory.assign(400, 0.0);
-
- bufferCounter = 0;
- drawCounter = 0;
- smoothedVol = 0.0;
- scaledVol = 0.0;
- ofSoundStreamSettings settings;
- // // if you want to set the device id to be different than the default
- auto devices = soundStream.getDeviceList(ofSoundDevice::Api::JACK);
- // // settings.device = devices[4];
- // // you can also get devices for an specific api
- // // auto devices = soundStream.getDevicesByApi(ofSoundDevice::Api::PULSE);
- // // settings.device = devices[0];
- // // or get the default device for an specific api:
- // // settings.api = ofSoundDevice::Api::PULSE;
- // // or by name
- //// auto devices = soundStream.getMatchingDevices("default");
- //auto devices = soundStream.getDevicesByApi(ofSoundDevice::Api::JACK);
- // settings.device = devices[0];
- if(!devices.empty()){
- settings.setInDevice(devices[0]);
- }
- settings.setInListener(this);
- settings.sampleRate = 48000;
- settings.numOutputChannels = 0;
- settings.numInputChannels = 2;
- settings.bufferSize = bufferSize;
- soundStream.setup(settings);
- ///preparing visula glitch
- Line l=Line(10.0f,10.0f);
- l.setX(12.0f);
- glitches.push_back(new Line(300.0f,300.0f));
- // audioAnalyzer.setup(48000, bufferSize, 2);
- }
- //--------------------------------------------------------------
- void ofApp::update(){
- // //lets scale the vol up to a 0-1 range
- // scaledVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 1.0, true);
- // //lets record the volume into an array
- // volHistory.push_back( scaledVol );
-
- // //if we are bigger the the size we want to record - lets drop the oldest value
- // if( volHistory.size() >= 400 ){
- // volHistory.erase(volHistory.begin(), volHistory.begin()+1);
- // }
- coinToss= ofRandom(0, 3.0);
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- //---------- VONOROI -----------
- // draw background
- ofColor centerColor = ofColor(85, 78, 68);
- ofColor edgeColor(0, 0, 0);
- ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);
- ofSetColor(255,255,255);
- if(coinToss <= 1){
- ofSetLineWidth( map(coinToss,0,3,1,5));
- //ofDrawLine(glm::vec2(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight())),glm::vec2(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight())));
- for(auto & g : glitches)
- {
- g->draw();
- }
- }
- }
- //--------------------------------------------------------------
- void ofApp::audioIn(ofSoundBuffer & input){
-
- float curVol = 0.0;
-
- // samples are "interleaved"
- int numCounted = 0;
- //lets go through each sample and calculate the root mean square which is a rough way to calculate volume
- for (size_t i = 0; i < input.getNumFrames(); i++){
- left[i] = input[i*2]*0.5;
- right[i] = input[i*2+1]*0.5;
- curVol += left[i] * left[i];
- curVol += right[i] * right[i];
- numCounted+=2;
- }
-
- //this is how we get the mean of rms :)
- curVol /= (float)numCounted;
-
- // this is how we get the root of rms :)
- curVol = sqrt( curVol );
-
- smoothedVol *= 0.93;
- smoothedVol += 0.07 * curVol;
-
- bufferCounter++;
-
- }
- //--------------------------------------------------------------
- void ofApp::audioReceived(float* input, int bufferSize, int nChannels) {
- float maxValue = 0;
- for(int i = 0; i < bufferSize; i++) {
- if(abs(input[i]) > maxValue) {
- maxValue = abs(input[i]);
- }
- }
- for(int i = 0; i < bufferSize; i++) {
- input[i] /= maxValue;
- }
- fft->setSignal(input);
- float* curFft = fft->getAmplitude();
- memcpy(&audioBins[0], curFft, sizeof(float) * fft->getBinSize());
- maxValue = 0;
- for(int i = 0; i < fft->getBinSize(); i++) {
- if(abs(audioBins[i]) > maxValue) {
- maxValue = abs(audioBins[i]);
- }
- }
- for(int i = 0; i < fft->getBinSize(); i++) {
- audioBins[i] /= maxValue;
- }
- soundMutex.lock();
- middleBins = audioBins;
- soundMutex.unlock();
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed (int key){
- std::cout << "Pressed " << char(key) << " (int=" << key << ")\n";
- switch(key){
- case 'f':
- ofToggleFullscreen();
- break;
- case 's':
- soundStream.start();
- break;
- case 'e':
- soundStream.stop();
- break;
- }
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
-
- }
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
-
- }
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseEntered(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::mouseExited(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
- float ofApp::map(float value, float min1, float max1, float min2, float max2) {
- return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
- }
|