<commit_before>/*
 *  InteractionPerformance.cpp
 *  Render
 *
 *  Created by <NAME> on 3/7/11.
 *  Copyright 2011 Interactive Design. All rights reserved.
 *
 */

#include "InteractionPerformance.h"
#include "testApp.h"

//--------------------------------------------------------
void InteractionPerformance::setup() {
	
	lineAlpha = 0;
	lineAlphaDes = 255;
	
    
    
	audio = Donk::AudioData::getInstance();	
	nBands = audio->getNumChannels();
    
	freq = new float[nBands];
    colors = new ofColor[nBands];
    ribbonCounts = new int[nBands];
    lastFreq = new float[nBands];
	for(int i=0; i<nBands; i++){
        freq[i] = 0.0;   
        lastFreq[i] = 0.0;
        ribbonCounts[i] = 0;
    }
	
	
	// load all the images
	ofxDirList			lister;
	lister.setVerbose(false);
	int nFiles = lister.listDir("images/performance");
	printf("found %i files for performance\n", nFiles);
	for (int i=0; i<nFiles; i++) {
		images.push_back(ofImage());
		if(!images.back().loadImage(lister.getPath(i))) {
			printf("error loaded image in performance\n");
		}
	}
	
	//now lets have 36 bubbles in total, in random positions
	// addBubbles();
	
	
    
    maxRibbonsPerChannel = 100;
    //add uggo-matic colors on purpose for debug
    colors[0] = ofColor(1.0, 0.0, 0.0);
    colors[1] = ofColor(1.0, 1.0, 0.0);
    colors[2] = ofColor(0.0, 1.0, 1.0);
    colors[3] = ofColor(1.0, 0.0, 1.0);
    colors[4] = ofColor(0.0, 1.0, 0.0);
    colors[5] = ofColor(1.0, 1.0, 1.0);
}

//--------------------------------------------------------
void InteractionPerformance::addBubbles() {
	for(int j=0; j<nBands; j++){
		for(int i=0; i<nBands; i++) {
			
			ofVec3f center(interactiveRect.width/2, 0, 0);
			float radius = ofRandom(70, 120);
			
			ofVec3f startPos;
			startPos.x = (int)ofRandom(0,2) ? -100 : interactiveRect.width+100;
			startPos.y = (int)ofRandom(0,2) ? -100 : interactiveRect.height+100;		
			startPos.z = ofRandom(-500, 500);
			
			ContentBubble * bubble = new ContentBubble();
			
			bubble->startRadius = radius;
			bubble->maxRadius = radius; // not sure, maye set to larger for audio manipulation?
			bubble->data	  = NULL;
			bubble->radius    = radius;
			bubble->rigidBody = bullet->createSphere(startPos, radius, 1);
			bubble->createContentBubble();
			bubble->setTarget(center.x + ofRandom(-300, 300), ofRandom(500, interactiveRect.height-300), 0);			
			
			bubble->performceImageID   = (int)ofRandom(0, images.size());//(j*6)+i; //now we are making 36, so go deeper into the image array
			bubble->performanceChannel = i;
			
			bubble->performanceStartTarget = bubble->target;
			bubble->offScreenTaget = bubble->performanceStartTarget;
			bubble->offScreenTaget.y = -100;
			
			bubbles.push_back(bubble);        
		}		
	}
}

//--------------------------------------------------------
void InteractionPerformance::newBubbleRecieved(Donk::BubbleData * data) { 
};

//--------------------------------------------------------
void InteractionPerformance::update() {
	
	bool bAllOffScreen = true;
	
	for(int i=0; i<bubbles.size(); i++) {
		
		if(bubbles[i]->bAnimateOut) {
			bubbles[i]->goOffScreen();
			
			if(bubbles[i]->getPosition().y > 0) {
				bAllOffScreen = false;
			}
			
		}
		else {		
			ofVec3f target = bubbles[i]->target;
			target.y = (bubbles[i]->performanceStartTarget.y) - (audio->getVolume(bubbles[i]->performceImageID) * 320.0);
			
			bubbles[i]->rigidBody->body->setDamping(0.99, 0.99);
			bubbles[i]->addAtrractionForce(target.x, target.y, target.z, 30.0);
		}	
		
		bubbles[i]->update();
		
        float newRad = freq[ bubbles[i]->performanceChannel ] * 100.0;
		bubbles[i]->lerpRadius(bubbles[i]->startRadius + newRad, 0.94);
		
		champagne(bubbles[i]->pos);
		
        
        //check for ribbon emmitting 
        float dFreq = freq[ bubbles[i]->performanceChannel ] - lastFreq[ bubbles[i]->performanceChannel ];
        int numRibbonsToEmit = ofMap(dFreq, .05, .2, 0, 20, true);
        
        //cout << " channel " << bubbles[i]->performanceChannel << " freq " << dFreq << endl;
        
        //clamp to max
        numRibbonsToEmit = MIN(numRibbonsToEmit, maxRibbonsPerChannel - (ribbonCounts[bubbles[i]->performanceChannel] + numRibbonsToEmit) );
        for(int j = 0; j < numRibbonsToEmit; j++){
            addRibbonForIndex(i);
        }
        //cache freq
        lastFreq[ bubbles[i]->performanceChannel ] = freq[ bubbles[i]->performanceChannel ];
	}	
	
	if(bAnimateOut) {
		float time = (ofGetElapsedTimeMillis()-animatedOutTimer) / 1000.0;
		if(bAllOffScreen && !bDoneAnimatingOut || time > 3.0) {
			bDoneAnimatingOut = true;
			killallBubbles();
			printf("-- killed all performance bubbles --\n");
		}
	}
	
	lineAlpha += (lineAlphaDes-lineAlpha) * 0.02;

 
    updateRibbons();
}


//--------------------------------------------------------
void InteractionPerformance::drawContent() {
	
	ofEnableAlphaBlending();

	// fft goods
	glPushMatrix();
	ofSetColor(255, 0, 0, lineAlpha);
	ofNoFill();
	ofBeginShape();
	for (int i=0; i<nBands; i++) {
		
		freq[i] *= 0.986f;
		if(freq[i] < audio->getVolume(i)) {
			freq[i] = audio->getVolume(i);
		}
		
		float x   = ((float)i/(nBands-1)) * interactiveRect.width;
		float y   = 200 + (interactiveRect.height/2);
		float frq = freq[i] * 200.0;
		ofVertex(x, y-frq);
	}
	ofEndShape();
	glPopMatrix();
	
    
    drawRibbons();
	
	ofSetColor(255, 255, 255);
	for(int i=0; i<bubbles.size(); i++) {
		//bubbles[i]->drawHighLight();
		//bubbles[i]->pushBubble();<commit_msg>removed red line<commit_after>/*
 *  InteractionPerformance.cpp
 *  Render
 *
 *  Created by <NAME> on 3/7/11.
 *  Copyright 2011 Interactive Design. All rights reserved.
 *
 */

#include "InteractionPerformance.h"
#include "testApp.h"

//--------------------------------------------------------
void InteractionPerformance::setup() {
	
	lineAlpha = 0;
	lineAlphaDes = 255;
	
    
    
	audio = Donk::AudioData::getInstance();	
	nBands = audio->getNumChannels();
    
	freq = new float[nBands];
    colors = new ofColor[nBands];
    ribbonCounts = new int[nBands];
    lastFreq = new float[nBands];
	for(int i=0; i<nBands; i++){
        freq[i] = 0.0;   
        lastFreq[i] = 0.0;
        ribbonCounts[i] = 0;
    }
	
	
	// load all the images
	ofxDirList			lister;
	lister.setVerbose(false);
	int nFiles = lister.listDir("images/performance");
	printf("found %i files for performance\n", nFiles);
	for (int i=0; i<nFiles; i++) {
		images.push_back(ofImage());
		if(!images.back().loadImage(lister.getPath(i))) {
			printf("error loaded image in performance\n");
		}
	}
	
	//now lets have 36 bubbles in total, in random positions
	// addBubbles();
	
	
    
    maxRibbonsPerChannel = 100;
    //add uggo-matic colors on purpose for debug
    colors[0] = ofColor(1.0, 0.0, 0.0);
    colors[1] = ofColor(1.0, 1.0, 0.0);
    colors[2] = ofColor(0.0, 1.0, 1.0);
    colors[3] = ofColor(1.0, 0.0, 1.0);
    colors[4] = ofColor(0.0, 1.0, 0.0);
    colors[5] = ofColor(1.0, 1.0, 1.0);
}

//--------------------------------------------------------
void InteractionPerformance::addBubbles() {
	for(int j=0; j<nBands; j++){
		for(int i=0; i<nBands; i++) {
			
			ofVec3f center(interactiveRect.width/2, 0, 0);
			float radius = ofRandom(70, 120);
			
			ofVec3f startPos;
			startPos.x = (int)ofRandom(0,2) ? -100 : interactiveRect.width+100;
			startPos.y = (int)ofRandom(0,2) ? -100 : interactiveRect.height+100;		
			startPos.z = ofRandom(-500, 500);
			
			ContentBubble * bubble = new ContentBubble();
			
			bubble->startRadius = radius;
			bubble->maxRadius = radius; // not sure, maye set to larger for audio manipulation?
			bubble->data	  = NULL;
			bubble->radius    = radius;
			bubble->rigidBody = bullet->createSphere(startPos, radius, 1);
			bubble->createContentBubble();
			bubble->setTarget(center.x + ofRandom(-300, 300), ofRandom(500, interactiveRect.height-300), 0);			
			
			bubble->performceImageID   = (int)ofRandom(0, images.size());//(j*6)+i; //now we are making 36, so go deeper into the image array
			bubble->performanceChannel = i;
			
			bubble->performanceStartTarget = bubble->target;
			bubble->offScreenTaget = bubble->performanceStartTarget;
			bubble->offScreenTaget.y = -100;
			
			bubbles.push_back(bubble);        
		}		
	}
}

//--------------------------------------------------------
void InteractionPerformance::newBubbleRecieved(Donk::BubbleData * data) { 
};

//--------------------------------------------------------
void InteractionPerformance::update() {
	
	bool bAllOffScreen = true;
	
	for(int i=0; i<bubbles.size(); i++) {
		
		if(bubbles[i]->bAnimateOut) {
			bubbles[i]->goOffScreen();
			
			if(bubbles[i]->getPosition().y > 0) {
				bAllOffScreen = false;
			}
			
		}
		else {		
			ofVec3f target = bubbles[i]->target;
			target.y = (bubbles[i]->performanceStartTarget.y) - (audio->getVolume(bubbles[i]->performceImageID) * 320.0);
			
			bubbles[i]->rigidBody->body->setDamping(0.99, 0.99);
			bubbles[i]->addAtrractionForce(target.x, target.y, target.z, 30.0);
		}	
		
		bubbles[i]->update();
		
        float newRad = freq[ bubbles[i]->performanceChannel ] * 100.0;
		bubbles[i]->lerpRadius(bubbles[i]->startRadius + newRad, 0.94);
		
		champagne(bubbles[i]->pos);
		
        
        //check for ribbon emmitting 
        float dFreq = freq[ bubbles[i]->performanceChannel ] - lastFreq[ bubbles[i]->performanceChannel ];
        int numRibbonsToEmit = ofMap(dFreq, .05, .2, 0, 20, true);
        
        //cout << " channel " << bubbles[i]->performanceChannel << " freq " << dFreq << endl;
        
        //clamp to max
        numRibbonsToEmit = MIN(numRibbonsToEmit, maxRibbonsPerChannel - (ribbonCounts[bubbles[i]->performanceChannel] + numRibbonsToEmit) );
        for(int j = 0; j < numRibbonsToEmit; j++){
            addRibbonForIndex(i);
        }
        //cache freq
        lastFreq[ bubbles[i]->performanceChannel ] = freq[ bubbles[i]->performanceChannel ];
	}	
	
	if(bAnimateOut) {
		float time = (ofGetElapsedTimeMillis()-animatedOutTimer) / 1000.0;
		if(bAllOffScreen && !bDoneAnimatingOut || time > 3.0) {
			bDoneAnimatingOut = true;
			killallBubbles();
			printf("-- killed all performance bubbles --\n");
		}
	}
	
	lineAlpha += (lineAlphaDes-lineAlpha) * 0.02;

 
    updateRibbons();
}


//--------------------------------------------------------
void InteractionPerformance::drawContent() {
	
	ofEnableAlphaBlending();

	/*
	// fft goods
	glPushMatrix();
	ofSetColor(255, 0, 0, lineAlpha);
	ofNoFill();
	ofBeginShape();
	for (int i=0; i<nBands; i++) {
		
		freq[i] *= 0.986f;
		if(freq[i] < audio->getVolume(i)) {
			freq[i] = audio->getVolume(i);
		}
		
		float x   = ((float)i/(nBands-1)) * interactiveRect.width;
		float y   = 200 + (interactiveRect.height/2);
		float frq = freq[i] * 200.0;
		ofVertex(x, y-frq);
	}
	ofEndShape();
	glPopMatrix();
	*/
    
    drawRibbons();
	
	ofSetColor(255, 255, 255);
	for(int i=0; i<bubbles.size(); i++) {
		//bubbles[i]->drawHighLight();
		//bubbles[i]->pushBubble();
{'fraction_non_alphanumeric': 0.2073115142674811, 'fraction_numerical': 0.03464308505789042, 'mean_word_length': 2.576785132050864, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '361125', 'n_tokens_mistral': 4533, 'n_tokens_neox': 3873, 'n_words': 737}