Advertisement
fairyburnout

Music Visualizer 1.1

Feb 26th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.53 KB | None | 0 0
  1. // Welcome! In order for this code to run, the library Minim must be added :)
  2.  
  3. // I've pre-loaded my favorite songs to be a visual jukebox
  4.   // TAB to pause / play
  5.   // LEFT / RIGHT to skip forward or backwards 15 seconds
  6.   // Keys 1-0 load new songs
  7.   // Line 84 loads the beginning song
  8.      // I'm currently working on adding some filters to be adjusted by the UP / DOWN keys but they dont work yet lol
  9.       // also cube the cube class is still under construction so bear with me
  10.  
  11.   // if my classmates see this and don't know how to add Minim, it's in tools -> add tool, search Minim and install
  12.   // 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
  13.     // 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
  14.  
  15.   //Anyways, let's get started! -em
  16.  
  17. import ddf.minim.*;
  18. import ddf.minim.analysis.*;
  19. import ddf.minim.ugens.*;
  20.  
  21. Minim minim;
  22. AudioPlayer song;
  23. FFT fft;
  24. MoogFilter moog;
  25. AudioOutput out;
  26.  
  27. // Variables which define the "zones" of the spectrum
  28. // For example, for bass, we only take the first 4% of the total spectrum
  29. float specLow = 0.03; // 3%
  30. float specMid = 0.125;  // 12.5%
  31. float specHi = 0.20;   // 20%
  32.  
  33. // So 64% of the possible spectrum remains that will not be used.
  34. // These values are generally too high for the human ear anyway.
  35.  
  36. // Score values for each zone
  37. float scoreLow = 0;
  38. float scoreMid = 0;
  39. float scoreHi = 0;
  40.  
  41. // Previous value, to soften the reduction
  42. float oldScoreLow = scoreLow;
  43. float oldScoreMid = scoreMid;
  44. float oldScoreHi = scoreHi;
  45.  
  46. // Softening value
  47. float scoreDecreaseRate = 25;
  48.  
  49. // moog has to be a global variable
  50. float moogNum;
  51.  
  52. // Spheres that appear in space
  53. int nbSpheres;
  54. Sphere[] spheres;
  55.  
  56. // Cubes which appear as well
  57. int nbCubes;
  58. Cube[] cubes;
  59.  
  60. // Lines that appear on the sides
  61. int nbWalls = 500;
  62. Wall[] walls;
  63.  
  64. String windowName;
  65.  
  66. void setup() {
  67.  
  68.   String songs[] = new String[8];
  69.   songs[0] = "rezz.mp3";
  70.   songs[1] = "GOAT.mp3";
  71.   songs[2] = "smellsBlood.mp3";
  72.   songs[3] = "Grimes Genesis.mp3";
  73.   songs[4] = "crimewave.mp3";
  74.   songs[5] = "theDare.mp3";
  75.   songs[6] = "tameImpala.mp3";
  76.   songs[7] = "finlancesub.mp3";
  77.  
  78.   // Display in 3D on the whole screen
  79.   fullScreen(P3D);
  80.  
  81.   // Load the minim library
  82.   minim = new Minim(this);
  83.   out     = minim.getLineOut();
  84.  
  85.   // Load the song by changing the number from the list!
  86.   song = minim.loadFile(songs[2]);
  87.  
  88.   // Create the FFT object to analyze the song
  89.   fft = new FFT(song.bufferSize(), song.sampleRate());
  90.  
  91.   // create moog filter (set to 0) but available for use later
  92.   moogNum = 0;
  93.   moog = new MoogFilter(1200, moogNum);
  94.  
  95.   // One cube per frequency band
  96.   nbSpheres = (int)(fft.specSize()*specHi);
  97.   spheres = new Sphere[nbSpheres];
  98.  
  99.   // As many walls as we want
  100.   walls = new Wall[nbWalls];
  101.  
  102.   // Create all the objects
  103.  
  104.  
  105.   // Create the wall objects
  106.   // Left walls
  107.   for (int i = 0; i < nbWalls; i+=4) {
  108.     walls[i] = new Wall(0, height/2, 10, height);
  109.   }
  110.  
  111.   //Right Walls
  112.   for (int i = 1; i < nbWalls; i+=4) {
  113.     walls[i] = new Wall(width, height/2, 10, height);
  114.   }
  115.  
  116.   //Bottom Wall
  117.   for (int i = 2; i < nbWalls; i+=4) {
  118.     walls[i] = new Wall(width/2, height, width, 10);
  119.   }
  120.  
  121.   //Top Wall
  122.   for (int i = 3; i < nbWalls; i+=4) {
  123.     walls[i] = new Wall(width/2, 0, width, 10);
  124.   }
  125.  
  126.   // Create the sphere objects
  127.   for (int i = 0; i < nbSpheres; i++) {
  128.     spheres[i] = new Sphere();
  129.   }
  130.  
  131.   // Create the cube objects
  132.   for (int i = 0; i < nbCubes; i++) {
  133.     cubes[i] = new Cube();
  134.   }
  135.  
  136.   //background color
  137.   background(255);
  138.  
  139.   //begin the song
  140.   song.play(0);
  141.  // song.patch( moog ).patch(out);
  142. }
  143.  
  144. void draw()
  145. {
  146.   // Advance the song. We draw () for each "frame" of the song ...
  147.   fft.forward(song.mix);
  148.  
  149.   // Calculation of the "scores" (power) for three categories of sound
  150.   // First, save the old values
  151.   oldScoreLow = scoreLow;
  152.   oldScoreMid = scoreMid;
  153.   oldScoreHi = scoreHi;
  154.  
  155.   //Reset the values
  156.   scoreLow = 0;
  157.   scoreMid = 0;
  158.   scoreHi = 0;
  159.  
  160.   //Calculate the new "scores"
  161.   for (int i = 0; i < fft.specSize()*specLow; i++) {
  162.     scoreLow += fft.getBand(i);
  163.   }
  164.  
  165.   for (int i = (int)(fft.specSize()*specLow); i < fft.specSize()*specMid; i++) {
  166.     scoreMid += fft.getBand(i);
  167.   }
  168.  
  169.   for (int i = (int)(fft.specSize()*specMid); i < fft.specSize()*specHi; i++) {
  170.     scoreHi += fft.getBand(i);
  171.   }
  172.  
  173.   // Slow down the descent.
  174.   if (oldScoreLow > scoreLow) {
  175.     scoreLow = oldScoreLow - scoreDecreaseRate;
  176.   }
  177.  
  178.   if (oldScoreMid > scoreMid) {
  179.     scoreMid = oldScoreMid - scoreDecreaseRate;
  180.   }
  181.  
  182.   if (oldScoreHi > scoreHi) {
  183.     scoreHi = oldScoreHi - scoreDecreaseRate;
  184.   }
  185.  
  186.   // Volume for all frequencies at this time, with higher sounds more prominent.
  187.   // This allows the animation to go faster for higher pitched sounds, which are more noticeable
  188.   float scoreGlobal = 0.66*scoreLow + 0.8*scoreMid + 1*scoreHi;
  189.  
  190.  
  191.   //Subtle background color
  192.   colorMode(RGB);
  193.   background(scoreLow/100, scoreMid/100, scoreHi/100);
  194.  
  195.   // Sphere for each frequency band
  196.   for (int i = 0; i < nbSpheres; i++)
  197.   {
  198.     // Value of the frequency band
  199.     float bandValue = fft.getBand(i);
  200.  
  201.     // The color is represented as: red for bass, green for mid sounds, and blue for highs.
  202.     // The opacity is determined by the volume of the tape and the overall volume.
  203.     spheres[i].display(scoreLow, scoreMid, scoreHi, bandValue, scoreGlobal);
  204.   }
  205.  
  206.   // Cubes for each frequency band
  207.   for (int i = 0; i < nbCubes; i++)
  208.   {
  209.     // Value of the frequency band
  210.     float bandValue = fft.getBand(i);
  211.  
  212.     // The color is represented as: red for bass, green for mid sounds, and blue for highs.
  213.     // The opacity is determined by the volume of the tape and the overall volume.
  214.     cubes[i].display(scoreLow, scoreMid, scoreHi, bandValue, scoreGlobal);
  215.   }
  216.  
  217.   // Line walls, here you have to keep the value of the previous strip  
  218.   // and the next one to connect them together
  219.   float previousBandValue = fft.getBand(0);
  220.  
  221.   // Distance between each line point, negative because on dimension z
  222.   float dist = -25;
  223.  
  224.   // Multiply the height by this constant
  225.   float heightMult = 2;
  226.  
  227.   // For each band
  228.   for (int i = 1; i < fft.specSize(); i++) {
  229.  
  230.     // Value of the frequency band, we multiply the bands further away so that they are more visible.
  231.     float bandValue = fft.getBand(i)*(1 + (i/50));
  232.  
  233.     // Select the color according to the strengths of the different types of sounds
  234.     colorMode(RGB);
  235.     stroke(100+scoreLow, 100+scoreMid, 100+scoreHi, 255-i);
  236.     strokeWeight(1 + (scoreGlobal/100));
  237.     strokeJoin(ROUND);
  238.  
  239.  
  240.     // lower left line
  241.     line(0, height-(previousBandValue*heightMult), dist*(i-1), 0, height-(bandValue*heightMult), dist*i);
  242.     line((previousBandValue*heightMult), height, dist*(i-1), (bandValue*heightMult), height, dist*i);
  243.     line(0, height-(previousBandValue*heightMult), dist*(i-1), (bandValue*heightMult), height, dist*i);
  244.  
  245.     //top left line
  246.     line(0, (previousBandValue*heightMult), dist*(i-1), 0, (bandValue*heightMult), dist*i);
  247.     line((previousBandValue*heightMult), 0, dist*(i-1), (bandValue*heightMult), 0, dist*i);
  248.     line(0, (previousBandValue*heightMult), dist*(i-1), (bandValue*heightMult), 0, dist*i);
  249.  
  250.     //bottom right line
  251.     line(width, height-(previousBandValue*heightMult), dist*(i-1), width, height-(bandValue*heightMult), dist*i);
  252.     line(width-(previousBandValue*heightMult), height, dist*(i-1), width-(bandValue*heightMult), height, dist*i);
  253.     line(width, height-(previousBandValue*heightMult), dist*(i-1), width-(bandValue*heightMult), height, dist*i);
  254.  
  255.     //top right line
  256.     line(width, (previousBandValue*heightMult), dist*(i-1), width, (bandValue*heightMult), dist*i);
  257.     line(width-(previousBandValue*heightMult), 0, dist*(i-1), width-(bandValue*heightMult), 0, dist*i);
  258.     line(width, (previousBandValue*heightMult), dist*(i-1), width-(bandValue*heightMult), 0, dist*i);
  259.  
  260.     // Save the value for the next loop round
  261.     previousBandValue = bandValue;
  262.   }
  263.  
  264.   //rectangular walls
  265.   for (int i = 0; i < nbWalls; i++) {
  266.     // We assign each wall a band, and we send its strength.
  267.     float intensity = fft.getBand(i%((int)(fft.specSize()*specHi)));
  268.     walls[i].display(scoreLow, scoreMid, scoreHi, intensity, scoreGlobal);
  269.   }
  270.   colorMode(HSB);
  271.   lights();
  272.   directionalLight(abs(360-scoreGlobal), 50, previousBandValue, 0, -scoreGlobal/1000, -1);
  273.   ambientLight(scoreHi, scoreGlobal, previousBandValue);
  274.   ///spotLight(
  275.   //saveFrame("line-######.png");
  276. }
  277.  
  278. // Class for the spheres which fly in space
  279. class Cube {
  280.   // Z position of "spawn" and maximum Z position
  281.   float startingZ = -11000;
  282.   float maxZ = 100;
  283.  
  284.   // Position values
  285.   float x, y, z;
  286.   float rotX, rotY, rotZ;
  287.   float sumRotX, sumRotY, sumRotZ;
  288.  
  289.   //Construction of the cubes
  290.   Cube() {
  291.     // Make the cube appear at a random location
  292.     x = random(0, width);
  293.     y = random(0, height);
  294.     z = random(startingZ, maxZ);
  295.  
  296.     // Give the cube a random rotation
  297.     rotX = random(-1, 1);
  298.     rotY = random(-1, 1);
  299.     rotZ = random(-1, 1);
  300.   }
  301.  
  302.   void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
  303.     // Select the color, opacity determined by the intensity (volume of the band)
  304.     colorMode(HSB);
  305.     color displayColor = color(1+(intensity/5)+(pow((scoreGlobal/150), 2)), 100, 100);
  306.     noFill();
  307.  
  308.     // Line color, they disappear with the individual intensity of the cube
  309.     color strokeColor = lerpColor(displayColor, 255, .9);
  310.     stroke(strokeColor);
  311.     strokeWeight(1 + (scoreGlobal/300));
  312.  
  313.     // Create a transformation matrix to perform rotations, enlargements
  314.     pushMatrix();
  315.  
  316.     //Shifting
  317.     translate(x, y, z);
  318.  
  319.     //Calculation of rotation as a function of intensity for the cube
  320.     sumRotX += intensity*(rotX/1000);
  321.     sumRotY += intensity*(rotY/1000);
  322.     sumRotZ += intensity*(rotZ/1000);
  323.  
  324.     //Applying rotation
  325.     rotateX(sumRotX);
  326.     rotateY(sumRotY);
  327.     rotateZ(sumRotZ);
  328.  
  329.     // Creation of the cube, variable size according to the intensity for the cube
  330.     box(scoreGlobal/scoreLow, scoreGlobal/scoreMid, scoreGlobal/scoreHi);
  331.  
  332.     // Apply the matrix
  333.     popMatrix();
  334.  
  335.     //Shift thru Z
  336.     z+= (1+(intensity/5)+(pow((scoreGlobal/150), 2)));
  337.  
  338.     //Replace the sphere at the back when it is no longer visible
  339.     if (z >= maxZ) {
  340.       x = random(0, width);
  341.       y = random(0, height);
  342.       z = startingZ;
  343.     }
  344.   }
  345. }
  346.  
  347.  
  348. // Class for the spheres which fly in space
  349. class Sphere {
  350.   // Z position of "spawn" and maximum Z position
  351.   float startingZ = -10000;
  352.   float maxZ = 1000;
  353.  
  354.   // Position values
  355.   float x, y, z;
  356.   float rotX, rotY, rotZ;
  357.   float sumRotX, sumRotY, sumRotZ;
  358.  
  359.   //Construction of the cubes
  360.   Sphere() {
  361.     // Make the cube appear at a random location
  362.     x = random(0, width);
  363.     y = random(0, height);
  364.     z = random(startingZ, maxZ);
  365.  
  366.     // Give the cube a random rotation
  367.     rotX = random(0, 1);
  368.     rotY = random(0, 1);
  369.     rotZ = random(0, 1);
  370.   }
  371.  
  372.   void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
  373.     // Select the color, opacity determined by the intensity (volume of the band)
  374.     color displayColor = color(scoreLow*0.67, scoreMid*0.67, scoreHi*0.67, intensity*5);
  375.     fill(displayColor, 255);
  376.  
  377.     // Line color, they disappear with the individual intensity of the cube
  378.     color strokeColor = lerpColor(displayColor, 255, .75);
  379.     stroke(strokeColor);
  380.     strokeWeight(1 + (scoreGlobal/300));
  381.  
  382.     // Create a transformation matrix to perform rotations, enlargements
  383.     pushMatrix();
  384.  
  385.     //Shifting
  386.     translate(x, y, z);
  387.  
  388.     //Calculation of rotation as a function of intensity for the cube
  389.     sumRotX += intensity*(rotX/1000);
  390.     sumRotY += intensity*(rotY/1000);
  391.     sumRotZ += intensity*(rotZ/1000);
  392.  
  393.     //Applying rotation
  394.     rotateX(sumRotX);
  395.     rotateY(sumRotY);
  396.     rotateZ(sumRotZ);
  397.  
  398.     // Creation of the sphere, variable size according to the intensity for the sphere
  399.     sphereDetail(5);
  400.     sphere(100+(intensity/2));
  401.  
  402.     // Apply the matrix
  403.     popMatrix();
  404.  
  405.     //Shift thru Z
  406.     z+= (1+(intensity/5)+(pow((scoreGlobal/150), 2)));
  407.  
  408.     //Replace the sphere at the back when it is no longer visible
  409.     if (z >= maxZ) {
  410.       x = random(0, width);
  411.       y = random(0, height);
  412.       z = startingZ;
  413.     }
  414.   }
  415. }
  416.  
  417.  
  418. // Class to display the lines on the sides
  419. class Wall {
  420.   //Minimum and maximum Z position
  421.   float startingZ = -10000;
  422.   float maxZ = 50;
  423.  
  424.   //Position values
  425.   float x, y, z;
  426.   float sizeX, sizeY;
  427.  
  428.   //Construction of walls
  429.   Wall(float x, float y, float sizeX, float sizeY) {
  430.     //Minimum and maximum Z position
  431.     this.x = x;
  432.     this.y = y;
  433.     //Random depth
  434.     this.z = random(startingZ, maxZ);  
  435.  
  436.     //We determine the size because the walls to the floors have a different size than those on the sides
  437.     this.sizeX = sizeX;
  438.     this.sizeY = sizeY;
  439.   }
  440.  
  441.   //Display function
  442.   void display(float scoreLow, float scoreMid, float scoreHi, float intensity, float scoreGlobal) {
  443.     // Color determined by low, medium and high sounds
  444.     // Opacity determined by the overall volume
  445.     colorMode(RGB);
  446.     color displayColor = color(scoreLow*0.67, scoreMid*0.67, scoreHi*0.67, scoreGlobal);
  447.  
  448.     // Make the lines disappear in the distance to give an illusion of fog
  449.     fill(displayColor, ((scoreGlobal-5)/1000)*(255+(z/25)));
  450.     noStroke();
  451.  
  452.     // First band, the one that moves according to the force
  453.     // Transformation matrix
  454.     pushMatrix();
  455.  
  456.     //Shift
  457.     translate(x, y, z);
  458.  
  459.     //Enlargement
  460.     if (intensity > 100) intensity = 100;
  461.     scale(sizeX*(intensity/100), sizeY*(intensity/100), 20);
  462.  
  463.     //Creation of the "sphere"
  464.     sphere(scoreLow);
  465.     popMatrix();
  466.  
  467.     //Second strip, the one that is always the same size
  468.     displayColor = color(scoreLow*0.5, scoreMid*0.5, scoreHi*0.5, scoreGlobal);
  469.     fill(displayColor, (scoreGlobal/5000)*(255+(z/25)));
  470.     //Transform Matrix
  471.     pushMatrix();
  472.  
  473.     //Shift
  474.     translate(x, y, z);
  475.  
  476.     //Enlarge scale
  477.     scale(sizeX, sizeY, 10);
  478.  
  479.     //Creation of "the box"
  480.     box(scoreGlobal/scoreLow);
  481.     popMatrix();
  482.  
  483.     //Z plane Enlargement
  484.     z+= (pow((scoreGlobal/150), 2));
  485.     if (z >= maxZ) {
  486.       z = startingZ;
  487.     }
  488.   }
  489. }
  490.  
  491.  
  492. void keyPressed() {
  493.  
  494.   if (keyCode == TAB) {
  495.     if (song.isPlaying()) {
  496.       song.pause();
  497.     } else {
  498.       song.play();
  499.     }
  500.   } else if (keyCode == RIGHT) {
  501.     song.skip(15000);
  502.   } else if (keyCode == LEFT) {
  503.     song.skip(-15000);
  504.   } else if (keyCode == UP) {
  505.   } else if (keyCode == DOWN) {
  506.   }
  507.  
  508.   String songs[] = new String[8];
  509.   songs[0] = "rezz.mp3";
  510.   songs[1] = "GOAT.mp3";
  511.   songs[2] = "smellsBlood.mp3";
  512.   songs[3] = "Grimes Genesis.mp3";
  513.   songs[4] = "crimewave.mp3";
  514.   songs[5] = "theDare.mp3";
  515.   songs[6] = "tameImpala.mp3";
  516.   songs[7] = "finlancesub.mp3";
  517.  
  518.   if ( key == '1' )
  519.   {
  520.     if (song.isPlaying()) {
  521.       song.pause();
  522.     }
  523.     song = minim.loadFile(songs[0]);
  524.     song.play();
  525.   } else if ( key == '2' )
  526.   {
  527.     if (song.isPlaying()) {
  528.       song.pause();
  529.     }
  530.     song = minim.loadFile(songs[1]);
  531.     song.play();
  532.   } else if ( key == '3' )
  533.   {
  534.     if (song.isPlaying()) {
  535.       song.pause();
  536.     }
  537.     song = minim.loadFile(songs[2]);
  538.     song.play();
  539.   } else if ( key == '4' )
  540.   {
  541.     if (song.isPlaying()) {
  542.       song.pause();
  543.     }
  544.     song = minim.loadFile(songs[3]);
  545.     song.play();
  546.   } else if ( key == '5' )
  547.   {
  548.     if (song.isPlaying()) {
  549.       song.pause();
  550.     }
  551.     song = minim.loadFile(songs[4]);
  552.     song.play();
  553.   } else if ( key == '6' )
  554.   {
  555.     if (song.isPlaying()) {
  556.       song.pause();
  557.     }
  558.     song = minim.loadFile(songs[5]);
  559.     song.play();
  560.   } else if ( key == '7' )
  561.   {
  562.     if (song.isPlaying()) {
  563.       song.pause();
  564.     }
  565.     song = minim.loadFile(songs[6]);
  566.     song.play();
  567.   } else if ( key == '8' )
  568.   {
  569.     if (song.isPlaying()) {
  570.       song.pause();
  571.     }
  572.     song = minim.loadFile(songs[7]);
  573.     song.play();
  574.   } else if ( key == '9' )
  575.   {
  576.     if (song.isPlaying()) {
  577.       song.pause();
  578.     }
  579.    // song = minim.loadFile(songs[8]);
  580.     song.play();
  581.   }
  582.  
  583.   if ( key == 'a' || key == 'A' ) moog.type = MoogFilter.Type.LP;
  584.   if ( key == 's' || key == 'S' ) moog.type = MoogFilter.Type.HP;
  585.   if ( key == 'd' || key == 'D' ) moog.type = MoogFilter.Type.BP;
  586. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement