Add the contents of the folder "add_to_codeblocks_mingw_include" into "... \ CodeBlocks \ MinGW \ include"
Add the contents of the folder "add_to_codeblocks_mingw_lib" into "... \ CodeBlocks \ MinGW \ lib"
#include "ofMain.h" #include "ofApp.h" //======================================================================== int main( ){ ofSetupOpenGL(1024,768, OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp( new ofApp()); }
#pragma once #include "ofMain.h" class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); };
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ } //-------------------------------------------------------------- void ofApp::update(){ } //-------------------------------------------------------------- void ofApp::draw(){ } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ } //-------------------------------------------------------------- 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::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
void setup (); void update (); void draw ();
#include "testApp.h" #include "ofAppGlutWindow.h" //-------------------------------------------------------------- int main(){ ofAppGlutWindow window; // create a window // set width, height, mode (OF_WINDOW or OF_FULLSCREEN) ofSetupOpenGL(&window, 1024, 768, OF_WINDOW); ofRunApp(new testApp()); // start the app }
#pragma once #include "ofMain.h" /* This example draws points cloud and plays music track. Also it analyzes music spectrum and use this data for controlling the radius and shuffle of the cloud. It's the example 06-DancingCloud from the book "Mastering openFrameworks: Creative Coding Demystified", Chapter 6 - Working with Sounds Music track "surface.wav" by Ilya Orange (soundcloud.com/ilyaorange) */ class testApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void mousePressed(int x, int y, int button); ofSoundPlayer sound; //Sound sample void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y); void mouseDragged(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); };
#include "testApp.h" const int N = 256; //Number of bands in spectrum float spectrum[ N ]; //Smoothed spectrum values float Rad = 500; //Cloud raduis parameter float Vel = 0.1; //Cloud points velocity parameter int bandRad = 2; //Band index in spectrum, affecting Rad value int bandVel = 100; //Band index in spectrum, affecting Vel value const int n = 300; //Number of cloud points //Offsets for Perlin noise calculation for points float tx[n], ty[n]; ofPoint p[n]; //Cloud's points positions float time0 = 0; //Time value, used for dt computing //-------------------------------------------------------------- void testApp::setup(){ //Set up sound sample sound.loadSound( "surface.wav" ); sound.setLoop( true ); sound.play(); //Set spectrum values to 0 for (int i=0; i<N; i++) { spectrum[i] = 0.0f; } //Initialize points offsets by random numbers for ( int j=0; j<n; j++ ) { tx[j] = ofRandom( 0, 1000 ); ty[j] = ofRandom( 0, 1000 ); } } //-------------------------------------------------------------- void testApp::update(){ //Update sound engine ofSoundUpdate(); //Get current spectrum with N bands float *val = ofSoundGetSpectrum( N ); //We should not release memory of val, //because it is managed by sound engine //Update our smoothed spectrum, //by slowly decreasing its values and getting maximum with val //So we will have slowly falling peaks in spectrum for ( int i=0; i<N; i++ ) { spectrum[i] *= 0.97; //Slow decreasing spectrum[i] = max( spectrum[i], val[i] ); } //Update particles using spectrum values //Computing dt as a time between the last //and the current calling of update() float time = ofGetElapsedTimef(); float dt = time - time0; dt = ofClamp( dt, 0.0, 0.1 ); time0 = time; //Store the current time //Update Rad and Vel from spectrum //Note, the parameters in ofMap's were tuned for best result //just for current music track Rad = ofMap( spectrum[ bandRad ], 1, 3, 400, 800, true ); Vel = ofMap( spectrum[ bandVel ], 0, 0.1, 0.05, 0.5 ); //Update particles positions for (int j=0; j<n; j++) { tx[j] += Vel * dt; //move offset ty[j] += Vel * dt; //move offset //Calculate Perlin's noise in [-1, 1] and //multiply on Rad p[j].x = ofSignedNoise( tx[j] ) * Rad; p[j].y = ofSignedNoise( ty[j] ) * Rad; } } //-------------------------------------------------------------- void testApp::draw(){ ofBackground( 255, 255, 255 ); //Set up the background //Draw background rect for spectrum ofSetColor( 230, 230, 230 ); ofFill(); ofRect( 10, 700, N * 6, -100 ); //Draw spectrum ofSetColor( 0, 0, 0 ); for (int i=0; i<N; i++) { //Draw bandRad and bandVel by black color, //and other by gray color if ( i == bandRad || i == bandVel ) { ofSetColor( 0, 0, 0 ); //Black color } else { ofSetColor( 128, 128, 128 ); //Gray color } ofRect( 10 + i * 5, 700, 3, -spectrum[i] * 100 ); } //Draw cloud //Move center of coordinate system to the screen center ofPushMatrix(); ofTranslate( ofGetWidth() / 2, ofGetHeight() / 2 ); //Draw points ofSetColor( 0, 0, 0 ); ofFill(); for (int i=0; i<n; i++) { ofCircle( p[i], 2 ); } //Draw lines between near points float dist = 40; //Threshold parameter of distance for (int j=0; j<n; j++) { for (int k=j+1; k<n; k++) { if ( ofDist( p[j].x, p[j].y, p[k].x, p[k].y ) < dist ) { ofLine( p[j], p[k] ); } } } //Restore coordinate system ofPopMatrix(); } //-------------------------------------------------------------- void testApp::keyPressed(int key){ } //-------------------------------------------------------------- void testApp::keyReleased(int key){ } //-------------------------------------------------------------- void testApp::mouseMoved(int x, int y){ } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void testApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void testApp::dragEvent(ofDragInfo dragInfo){ }
300 points (const int n = 300;) move along the Perlin noise trajectories, and the adjacent points are connected by segments.
Cloud radius and speed of movement are two parameters that are taken from sound analysis.
Sound analysis is as follows: the original sound is transformed into a spectrum (using the windowed Fourier transform). Two spectrum values ​​are selected, which become the two parameters that control the movement of a cloud of points. These two frequencies are shown on the spectrum in black.
ofSoundPlayer sound; //Sound sample
const int N = 256; // float spectrum[ N ]; // float Rad = 500; // float Vel = 0.1; // int bandRad = 2; // Rad int bandVel = 100; // Vel const int n = 300; // // float tx[n], ty[n]; ofPoint p[n]; // float time0 = 0; // dt -
void testApp::setup(){ //Set up sound sample sound.loadSound( "surface.wav" ); sound.setLoop( true ); sound.play(); //Set spectrum values to 0 for (int i=0; i<N; i++) { spectrum[i] = 0.0f; } //Initialize points offsets by random numbers for ( int j=0; j<n; j++ ) { tx[j] = ofRandom( 0, 1000 ); ty[j] = ofRandom( 0, 1000 ); } }
void testApp::update(){ //Update sound engine ofSoundUpdate(); //Get current spectrum with N bands float *val = ofSoundGetSpectrum( N ); //We should not release memory of val, //because it is managed by sound engine //Update our smoothed spectrum, //by slowly decreasing its values and getting maximum with val //So we will have slowly falling peaks in spectrum for ( int i=0; i<N; i++ ) { spectrum[i] *= 0.97; //Slow decreasing spectrum[i] = max( spectrum[i], val[i] ); } //Update particles using spectrum values //Computing dt as a time between the last //and the current calling of update() float time = ofGetElapsedTimef(); float dt = time - time0; dt = ofClamp( dt, 0.0, 0.1 ); time0 = time; //Store the current time //Update Rad and Vel from spectrum //Note, the parameters in ofMap's were tuned for best result //just for current music track Rad = ofMap( spectrum[ bandRad ], 1, 3, 400, 800, true ); Vel = ofMap( spectrum[ bandVel ], 0, 0.1, 0.05, 0.5 ); //Update particles positions for (int j=0; j<n; j++) { tx[j] += Vel * dt; //move offset ty[j] += Vel * dt; //move offset //Calculate Perlin's noise in [-1, 1] and //multiply on Rad p[j].x = ofSignedNoise( tx[j] ) * Rad; p[j].y = ofSignedNoise( ty[j] ) * Rad; } }
void testApp::draw(){ ofBackground( 255, 255, 255 ); //Set up the background //Draw background rect for spectrum ofSetColor( 230, 230, 230 ); ofFill(); ofRect( 10, 700, N * 6, -100 ); //Draw spectrum ofSetColor( 0, 0, 0 ); for (int i=0; i<N; i++) { //Draw bandRad and bandVel by black color, //and other by gray color if ( i == bandRad || i == bandVel ) { ofSetColor( 0, 0, 0 ); //Black color } else { ofSetColor( 128, 128, 128 ); //Gray color } ofRect( 10 + i * 5, 700, 3, -spectrum[i] * 100 ); } //Draw cloud //Move center of coordinate system to the screen center ofPushMatrix(); ofTranslate( ofGetWidth() / 2, ofGetHeight() / 2 ); //Draw points ofSetColor( 0, 0, 0 ); ofFill(); for (int i=0; i<n; i++) { ofCircle( p[i], 2 ); } //Draw lines between near points float dist = 40; //Threshold parameter of distance for (int j=0; j<n; j++) { for (int k=j+1; k<n; k++) { if ( ofDist( p[j].x, p[j].y, p[k].x, p[k].y ) < dist ) { ofLine( p[j], p[k] ); } } } //Restore coordinate system ofPopMatrix(); }
sound.loadSound( "zvezda.mp3", true );
stars.loadImage("stars.jpg");
// , // bg_transparent ofEnableAlphaBlending(); ofSetColor(0, 0, 0, bg_transparent); ofRect(0, 0, 1000, 700); ofDisableAlphaBlending(); // , ofSetHexColor(0x606060); ofDrawBitmapString("Music by: volfworks", 800,610);
Source: https://habr.com/ru/post/244265/
All Articles