Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Welcome! In order for this code to run, the library Minim must be added :)
- // I've pre-loaded my favorite songs to be a visual jukebox
- // TAB to pause / play
- // LEFT / RIGHT to skip forward or backwards 15 seconds
- // Keys 1-0 load new songs
- // Line 84 loads the beginning song
- // I'm currently working on adding some filters to be adjusted by the UP / DOWN keys but they dont work yet lol
- // also cube the cube class is still under construction so bear with me
- // if my classmates see this and don't know how to add Minim, it's in tools -> add tool, search Minim and install
- // any song menu can be added by adding the tracks to the folder + editing the String array Songs[] on lines 56-64 AND 496-504
- // the strings MUST match the song names in the folder, so if you want to add your own music I would recomnmend renaming the file to something short
- //Anyways, let's get started! -em
- import ddf.minim.*;
- import ddf.minim.analysis.*;
- import ddf.minim.ugens.*;
- Minim minim;
- AudioPlayer song;
- FFT fft;
- MoogFilter moog;
- AudioOutput out;
- // Variables which define the "zones" of the spectrum
- // For example, for bass, we only take the first 4% of the total spectrum
- float specLow = 0.03; // 3%
- float specMid = 0.125; // 12.5%
- float specHi = 0.20; // 20%
- // So 64% of the possible spectrum remains that will not be used.
- // These values are generally too high for the human ear anyway.
- // Score values for each zone
- float scoreLow = 0;
- float scoreMid = 0;
- float scoreHi = 0;
- // Previous value, to soften the reduction
- float oldScoreLow = scoreLow;
- float oldScoreMid = scoreMid;
- float oldScoreHi = scoreHi;
- // Softening value
- float scoreDecreaseRate = 25;
- // moog has to be a global variable
- float moogNum;
- // Spheres that appear in space
- int nbSpheres;
- Sphere[] spheres;
- // Cubes which appear as well
- int nbCubes;
- Cube[] cubes;
- // Lines that appear on the sides
- int nbWalls = 500;
- Wall[] walls;
- String windowName;
- void setup() {
- String songs[] = new String[8];
- songs[0] = "rezz.mp3";
- songs[1] = "GOAT.mp3";
- songs[2] = "smellsBlood.mp3";
- songs[3] = "Grimes Genesis.mp3";
- songs[4] = "crimewave.mp3";
- songs[5] = "theDare.mp3";
- songs[6] = "tameImpala.mp3";
- songs[7] = "finlancesub.mp3";
- // Display in 3D on the whole screen
- fullScreen(P3D);
- // Load the minim library
- minim = new Minim(this);
- out = minim.getLineOut();
- // Load the song by changing the number from the list!
- song = minim.loadFile(songs[2]);
- // Create the FFT object to analyze the song
- fft = new FFT(song.bufferSize(), song.sampleRate());
- // create moog filter (set to 0) but available for use later
- moogNum = 0;
- moog = new MoogFilter(1200, moogNum);
- // One cube per frequency band
- nbSpheres = (int)(fft.specSize()*specHi);
- spheres = new Sphere[nbSpheres];
- // As many walls as we want
- walls = new Wall[nbWalls];
- // Create all the objects
- // Create the wall objects
- // Left walls
- for (int i = 0; i < nbWalls; i+=4) {
- walls[i] = new Wall(0, height/2, 10, height);
- }
- //Right Walls
- for (int i = 1; i < nbWalls; i+=4) {
- walls[i] = new Wall(width, height/2, 10, height);
- }
- //Bottom Wall
- for (int i = 2; i < nbWalls; i+=4) {
- walls[i] = new Wall(width/2, height, width, 10);
- }
- //Top Wall
- for (int i = 3; i < nbWalls; i+=4) {
- walls[i] = new Wall(width/2, 0, width, 10);
- }
- // Create the sphere objects
- for (int i = 0; i < nbSpheres; i++) {
- spheres[i] = new Sphere();
- }
- // Create the cube objects
- for (int i = 0; i < nbCubes; i++) {
- cubes[i] = new Cube();
- }
- //background color
- background(255);
- //begin the song
- song.play(0);
- // song.patch( moog ).patch(out);
- }
- void draw()
- {
- // Advance the song. We draw () for each "frame" of the song ...
- fft.forward(song.mix);
- // Calculation of the "scores" (power) for three categories of sound
- // First, save the old values
- oldScoreLow = scoreLow;
- oldScoreMid = scoreMid;
- oldScoreHi = scoreHi;
- //Reset the values
- scoreLow = 0;
- scoreMid = 0;
- scoreHi = 0;
- //Calculate the new "scores"
- for (int i = 0; i < fft.specSize()*specLow; i++) {
- scoreLow += fft.getBand(i);
- }
- for (int i = (int)(fft.specSize()*specLow); i < fft.specSize()*specMid; i++) {
- scoreMid += fft.getBand(i);
- }
- for (int i = (int)(fft.specSize()*specMid); i < fft.specSize()*specHi; i++) {
- scoreHi += fft.getBand(i);
- }
- // Slow down the descent.
- if (oldScoreLow > scoreLow) {
- scoreLow = oldScoreLow - scoreDecreaseRate;
- }
- if (oldScoreMid > scoreMid) {
- scoreMid = oldScoreMid - scoreDecreaseRate;
- }
- if (oldScoreHi > scoreHi) {
- scoreHi = oldScoreHi - scoreDecreaseRate;
- }
- // Volume for all frequencies at this time, with higher sounds more prominent.
- // This allows the animation to go faster for higher pitched sounds, which are more noticeable
- float scoreGlobal = 0.66*scoreLow + 0.8*scoreMid + 1*scoreHi;
- //Subtle background color
- colorMode(RGB);
- background(scoreLow/100, scoreMid/100, scoreHi/100);
- // Sphere for each frequency band
- for (int i = 0; i < nbSpheres; i++)
- {
- // Value of the frequency band
- float bandValue = fft.getBand(i);
- // The color is represented as: red for bass, green for mid sounds, and blue for highs.
- // The opacity is determined by the volume of the tape and the overall volume.
- spheres[i].display(scoreLow, scoreMid, scoreHi, bandValue, scoreGlobal);
- }
- // Cubes for each frequency band
- for (int i = 0; i < nbCubes; i++)
- {
- // Value of the frequency band
- float bandValue = fft.getBand(i);
- // The color is represented as: red for bass, green for mid sounds, and blue for highs.
- // The opacity is determined by the volume of the tape and the overall volume.
- cubes[i].display(scoreLow, scoreMid, scoreHi, bandValue, scoreGlobal);
- }
- // Line walls, here you have to keep the value of the previous strip
- // and the next one to connect them together
- float previousBandValue = fft.getBand(0);
- // Distance between each line point, negative because on dimension z
- float dist = -25;
- // Multiply the height by this constant
- float heightMult = 2;
- // For each band
- for (int i = 1; i < fft.specSize(); i++) {
- // Value of the frequency band, we multiply the bands further away so that they are more visible.
- float bandValue = fft.getBand(i)*(1 + (i/50));
- // Select the color according to the strengths of the different types of sounds
- colorMode(RGB);
- stroke(100+scoreLow, 100+scoreMid, 100+scoreHi, 255-i);
- strokeWeight(1 + (scoreGlobal/100));
- strokeJoin(ROUND);
- // lower left line
- line(0, height-(previousBandValue*heightMult), dist*(i-1), 0, height-(bandValue*heightMult), dist*i);
- line((previousBandValue*heightMult), height, dist*(i-1), (bandValue*heightMult), height, dist*i);
- line(0, height-(previousBandValue*heightMult), dist*(i-1), (bandValue*heightMult), height, dist*i);
- //top left line
- line(0, (previousBandValue*heightMult), dist*(i-1), 0, (bandValue*heightMult), dist*i);
- line((previousBandValue*heightMult), 0, dist*(i-1), (bandValue*heightMult), 0, dist*i);
- line(0, (previousBandValue*heightMult), dist*(i-1), (bandValue*heightMult), 0, dist*i);
- //bottom right line
- line(width, height-(previousBandValue*heightMult), dist*(i-1), width, height-(bandValue*heightMult), dist*i);
- line(width-(previousBandValue*heightMult), height, dist*(i-1), width-(bandValue*heightMult), height, dist*i);
- line(width, height-(previousBandValue*heightMult), dist*(i-1), width-(bandValue*heightMult), height, dist*i);
- //top right line
- line(width, (previousBandValue*heightMult), dist*(i-1), width, (bandValue*heightMult), dist*i);
- line(width-(previousBandValue*heightMult), 0, dist*(i-1), width-(bandValue*heightMult), 0, dist*i);
- line(width, (previousBandValue*heightMult), dist*(i-1), width-(bandValue*heightMult), 0, dist*i);
- // Save the value for the next loop round
- previousBandValue = bandValue;
- }
- //rectangular walls
- for (int i = 0; i < nbWalls; i++) {
- // We assign each wall a band, and we send its strength.
- float intensity = fft.getBand(i%((int)(fft.specSize()*specHi)));
- walls[i].display(scoreLow, scoreMid, scoreHi, intensity, scoreGlobal);
- }
- colorMode(HSB);
- lights();
- directionalLight(abs(360-scoreGlobal), 50, previousBandValue, 0, -scoreGlobal/1000, -1);
- ambientLight(scoreHi, scoreGlobal, previousBandValue);
- ///spotLight(
- //saveFrame("line-######.png");
- }
- // Class for the spheres which fly in space
- class Cube {
- // Z position of "spawn" and maximum Z position
- float startingZ = -11000;
- float maxZ = 100;
- // Position values
- float x, y, z;
- float rotX, rotY, rotZ;
- float sumRotX, sumRotY, sumRotZ;
- //Construction of the cubes
- Cube() {
- // Make the cube appear at a random location
- x = random(0, width);
- y = random(0, height);
- z = random(startingZ, maxZ);
- // Give the cube a random rotation
- rotX = random(-1, 1);
- rotY = random(-1, 1);
- rotZ = random(-1, 1);
- }
- void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
- // Select the color, opacity determined by the intensity (volume of the band)
- colorMode(HSB);
- color displayColor = color(1+(intensity/5)+(pow((scoreGlobal/150), 2)), 100, 100);
- noFill();
- // Line color, they disappear with the individual intensity of the cube
- color strokeColor = lerpColor(displayColor, 255, .9);
- stroke(strokeColor);
- strokeWeight(1 + (scoreGlobal/300));
- // Create a transformation matrix to perform rotations, enlargements
- pushMatrix();
- //Shifting
- translate(x, y, z);
- //Calculation of rotation as a function of intensity for the cube
- sumRotX += intensity*(rotX/1000);
- sumRotY += intensity*(rotY/1000);
- sumRotZ += intensity*(rotZ/1000);
- //Applying rotation
- rotateX(sumRotX);
- rotateY(sumRotY);
- rotateZ(sumRotZ);
- // Creation of the cube, variable size according to the intensity for the cube
- box(scoreGlobal/scoreLow, scoreGlobal/scoreMid, scoreGlobal/scoreHi);
- // Apply the matrix
- popMatrix();
- //Shift thru Z
- z+= (1+(intensity/5)+(pow((scoreGlobal/150), 2)));
- //Replace the sphere at the back when it is no longer visible
- if (z >= maxZ) {
- x = random(0, width);
- y = random(0, height);
- z = startingZ;
- }
- }
- }
- // Class for the spheres which fly in space
- class Sphere {
- // Z position of "spawn" and maximum Z position
- float startingZ = -10000;
- float maxZ = 1000;
- // Position values
- float x, y, z;
- float rotX, rotY, rotZ;
- float sumRotX, sumRotY, sumRotZ;
- //Construction of the cubes
- Sphere() {
- // Make the cube appear at a random location
- x = random(0, width);
- y = random(0, height);
- z = random(startingZ, maxZ);
- // Give the cube a random rotation
- rotX = random(0, 1);
- rotY = random(0, 1);
- rotZ = random(0, 1);
- }
- void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
- // Select the color, opacity determined by the intensity (volume of the band)
- color displayColor = color(scoreLow*0.67, scoreMid*0.67, scoreHi*0.67, intensity*5);
- fill(displayColor, 255);
- // Line color, they disappear with the individual intensity of the cube
- color strokeColor = lerpColor(displayColor, 255, .75);
- stroke(strokeColor);
- strokeWeight(1 + (scoreGlobal/300));
- // Create a transformation matrix to perform rotations, enlargements
- pushMatrix();
- //Shifting
- translate(x, y, z);
- //Calculation of rotation as a function of intensity for the cube
- sumRotX += intensity*(rotX/1000);
- sumRotY += intensity*(rotY/1000);
- sumRotZ += intensity*(rotZ/1000);
- //Applying rotation
- rotateX(sumRotX);
- rotateY(sumRotY);
- rotateZ(sumRotZ);
- // Creation of the sphere, variable size according to the intensity for the sphere
- sphereDetail(5);
- sphere(100+(intensity/2));
- // Apply the matrix
- popMatrix();
- //Shift thru Z
- z+= (1+(intensity/5)+(pow((scoreGlobal/150), 2)));
- //Replace the sphere at the back when it is no longer visible
- if (z >= maxZ) {
- x = random(0, width);
- y = random(0, height);
- z = startingZ;
- }
- }
- }
- // Class to display the lines on the sides
- class Wall {
- //Minimum and maximum Z position
- float startingZ = -10000;
- float maxZ = 50;
- //Position values
- float x, y, z;
- float sizeX, sizeY;
- //Construction of walls
- Wall(float x, float y, float sizeX, float sizeY) {
- //Minimum and maximum Z position
- this.x = x;
- this.y = y;
- //Random depth
- this.z = random(startingZ, maxZ);
- //We determine the size because the walls to the floors have a different size than those on the sides
- this.sizeX = sizeX;
- this.sizeY = sizeY;
- }
- //Display function
- void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
- // Color determined by low, medium and high sounds
- // Opacity determined by the overall volume
- colorMode(RGB);
- color displayColor = color(scoreLow*0.67, scoreMid*0.67, scoreHi*0.67, scoreGlobal);
- // Make the lines disappear in the distance to give an illusion of fog
- fill(displayColor, ((scoreGlobal-5)/1000)*(255+(z/25)));
- noStroke();
- // First band, the one that moves according to the force
- // Transformation matrix
- pushMatrix();
- //Shift
- translate(x, y, z);
- //Enlargement
- if (intensity > 100) intensity = 100;
- scale(sizeX*(intensity/100), sizeY*(intensity/100), 20);
- //Creation of the "sphere"
- sphere(scoreLow);
- popMatrix();
- //Second strip, the one that is always the same size
- displayColor = color(scoreLow*0.5, scoreMid*0.5, scoreHi*0.5, scoreGlobal);
- fill(displayColor, (scoreGlobal/5000)*(255+(z/25)));
- //Transform Matrix
- pushMatrix();
- //Shift
- translate(x, y, z);
- //Enlarge scale
- scale(sizeX, sizeY, 10);
- //Creation of "the box"
- box(scoreGlobal/scoreLow);
- popMatrix();
- //Z plane Enlargement
- z+= (pow((scoreGlobal/150), 2));
- if (z >= maxZ) {
- z = startingZ;
- }
- }
- }
- void keyPressed() {
- if (keyCode == TAB) {
- if (song.isPlaying()) {
- song.pause();
- } else {
- song.play();
- }
- } else if (keyCode == RIGHT) {
- song.skip(15000);
- } else if (keyCode == LEFT) {
- song.skip(-15000);
- } else if (keyCode == UP) {
- } else if (keyCode == DOWN) {
- }
- String songs[] = new String[8];
- songs[0] = "rezz.mp3";
- songs[1] = "GOAT.mp3";
- songs[2] = "smellsBlood.mp3";
- songs[3] = "Grimes Genesis.mp3";
- songs[4] = "crimewave.mp3";
- songs[5] = "theDare.mp3";
- songs[6] = "tameImpala.mp3";
- songs[7] = "finlancesub.mp3";
- if ( key == '1' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[0]);
- song.play();
- } else if ( key == '2' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[1]);
- song.play();
- } else if ( key == '3' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[2]);
- song.play();
- } else if ( key == '4' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[3]);
- song.play();
- } else if ( key == '5' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[4]);
- song.play();
- } else if ( key == '6' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[5]);
- song.play();
- } else if ( key == '7' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[6]);
- song.play();
- } else if ( key == '8' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- song = minim.loadFile(songs[7]);
- song.play();
- } else if ( key == '9' )
- {
- if (song.isPlaying()) {
- song.pause();
- }
- // song = minim.loadFile(songs[8]);
- song.play();
- }
- if ( key == 'a' || key == 'A' ) moog.type = MoogFilter.Type.LP;
- if ( key == 's' || key == 'S' ) moog.type = MoogFilter.Type.HP;
- if ( key == 'd' || key == 'D' ) moog.type = MoogFilter.Type.BP;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement