Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2023
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. import ddf.minim.analysis.*;
  2. import ddf.minim.*;
  3. import ch.bildspur.postfx.builder.*;
  4. import ch.bildspur.postfx.pass.*;
  5. import ch.bildspur.postfx.*;
  6. import peasy.*;
  7.  
  8. Minim       minim;
  9. AudioPlayer jingle;
  10. FFT         fft;
  11. PeasyCam    cam;
  12.  
  13. boolean fill = true;
  14.  
  15. int scl;
  16. int cols = 16;
  17. int rows = 32;
  18. int multiplier = 2;
  19. int fftSize = 1024;
  20. String songname = "../../data/theme11.mp3";
  21. float skip = -1;
  22. float maxwidth = 64;
  23. float[][] terrain;
  24. float maxheight;
  25. float cMod = 0.0;
  26. float desiredcMod = 0.0;
  27.  
  28. float fillFramerate = 144;
  29. float noFillFramerate = 144;
  30.  
  31. float backgroundHue = 0.0;
  32. float backgroundBrightness = 0.0;
  33. float backgroundSaturation = 0;
  34. float backgroundChangePercentage = 50;
  35. float backgroundChangeMin = 0;
  36. float backgroundChangeMax = 100;
  37. float backgroundIntensityDecay = 1;
  38. float dropheightpercentage = 100;
  39.  
  40. float lowHeightTicker = 0;
  41. float lowHeightTime = 30;
  42. float lowHeightThreshold = 10;
  43.  
  44. float alphaDecay = .33;
  45. float alphaMin = -270;
  46. float alphaMax = 360;
  47. float alphaCurrent = alphaMin;
  48.  
  49. float zEnableMin = -0.33;
  50. float zEnableMax = 1;
  51. float zEnable = zEnableMax;
  52. float zEnableSpeed = 0.001;
  53.  
  54. // PostFX fx;
  55. PostFXSupervisor supervisor;
  56. Pass[] fillPasses;
  57. Pass[] noFillPasses;
  58.  
  59. void setup () {
  60.   // cam = new PeasyCam(this, 100);
  61.   // cam.setMinimumDistance(50);
  62.   // cam.setMaximumDistance(500);
  63.  
  64.   minim = new Minim(this);
  65.   setMaximumHeight(songname);
  66.   jingle = minim.loadFile(songname, fftSize);
  67.   jingle.play();
  68.   fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
  69.   // fx = new PostFX(this);
  70.   supervisor = new PostFXSupervisor(this);
  71.   fillPasses = new Pass[] {
  72.     new PixelatePass(this, 400f),
  73.     new ChromaticAberrationPass(this),
  74.     new BloomPass(this, 0.4, 40, 40),
  75.     // new BloomPass(this, 0.1, 300, 300),
  76.     new VignettePass(this, 0.8, 0.3),
  77.    
  78.   };
  79.  
  80.   // noFillPasses = fillPasses;
  81.  
  82.   noFillPasses = new Pass[] {
  83.     new PixelatePass(this, 400f),
  84.     new ChromaticAberrationPass(this),
  85.     new BloomPass(this, 0.4, 40, 40),
  86.     // new BloomPass(this, 0.1, 300, 300),
  87.     new VignettePass(this, 0.8, 0.3),
  88.   };
  89.  
  90.   // size(1280, 720, P3D);
  91.   fullScreen(P3D, 1);
  92.   strokeJoin(ROUND);
  93.   strokeCap(ROUND);
  94.   colorMode(HSB, 360);
  95.   smooth();
  96.   frameRate(fillFramerate);
  97.   noStroke();
  98.  
  99.   scl = width / cols;
  100.  
  101.   terrain = new float[cols][rows];
  102.  
  103.   // jingle.cue(120500);
  104. }
  105.  
  106. void setFill(boolean fillValue) {
  107.   if (fillValue == fill) return;
  108.  
  109.   if (fillValue) {
  110.     println("fill");
  111.     println(jingle.position());
  112.     fill = true;
  113.     frameRate(fillFramerate);
  114.   } else {
  115.     backgroundBrightness = 360;
  116.     fill = false;
  117.     println("nofill");
  118.     println(jingle.position());
  119.     // println(heightpercentage);
  120.     frameRate(noFillFramerate);
  121.   }
  122. }
  123.  
  124. boolean hardcodedDrop = false;
  125. void draw () {
  126.   if (jingle.position() >= 132350 && !hardcodedDrop) {
  127.     setFill(false);
  128.     hardcodedDrop = true;
  129.   }
  130.  
  131.   noStroke();
  132.   setTitle();
  133.  
  134.   background(backgroundHue, backgroundSaturation, backgroundBrightness);
  135.   fft.forward(jingle.mix);
  136.  
  137.   float maxfromband = 0;
  138.   // Loop through the entire band
  139.   for(int i = 0; i < fft.specSize() / 2; i++)
  140.   {
  141.     // todo scale cols to specsize
  142.     if (i >= cols) {
  143.       break;
  144.     }
  145.    
  146.     for (int row = rows-1; row > 0; row--) {
  147.       terrain[i][row] = terrain[i][row-1];
  148.     }
  149.      
  150.     terrain[i][0] = fft.getBand(i) * multiplier;
  151.     if (terrain[i][0] > maxfromband) {
  152.       maxfromband = terrain[i][0];
  153.     }
  154.   }
  155.  
  156.  
  157.   translate(width/2, height/2, map(jingle.position(), 0, jingle.length(), -1000, -500));
  158.   // translate(width/2, height/2, -2000);
  159.   rotateX(PI/2);
  160.  
  161.   float heightpercentage = maxfromband * 100 / maxheight;
  162.   float c = map(heightpercentage, 0, 100, 0, 40);
  163.   //float xPow = map(jingle.position(), 0, jingle.length(), 6, 10);
  164.   //float xPow = map(jingle.position(), 0, jingle.length(), -2, 10);
  165.   float xPow = 0;
  166.   float yPow = 2;
  167.  
  168.   // frameRate(map(heightpercentage, 0, 100, 60, 144));
  169.  
  170.   // Change color when nothing is happening
  171.   if (heightpercentage < lowHeightThreshold) {
  172.     lowHeightTicker++;
  173.     if (lowHeightTicker > lowHeightTime && !fill) {
  174.       setFill(true);
  175.     }
  176.     desiredcMod = map(jingle.position(), 0 , jingle.length(), 0, 360);
  177.   } else {
  178.     lowHeightTicker = 0;
  179.   }
  180.  
  181.   if (heightpercentage > dropheightpercentage) {
  182.     if (fill) {
  183.       setFill(false);
  184.     }
  185.   }
  186.  
  187.   if (cMod != desiredcMod) {
  188.     if (cMod > desiredcMod) {
  189.       cMod -= 0.1;
  190.     } else {
  191.       cMod += 0.1;
  192.     }
  193.   }
  194.  
  195.   strip(c+cMod, 360-cMod, 1, 1, 1, xPow, yPow, heightpercentage);
  196.   strip(c+cMod, 360-cMod, -1, 1, 1, xPow, yPow, heightpercentage);
  197.   strip(c+cMod, 360-cMod, 1, 1, -1, xPow, yPow, heightpercentage);
  198.   strip(c+cMod, 360-cMod, -1, 1, -1, xPow, yPow, heightpercentage);
  199.  
  200.   if (heightpercentage > backgroundChangePercentage) {
  201.     backgroundHue = c+cMod;
  202.     if (map(heightpercentage, backgroundChangePercentage, 100, backgroundChangeMin, backgroundChangeMax) > backgroundBrightness) {
  203.       backgroundBrightness = map(heightpercentage, backgroundChangePercentage, 100, backgroundChangeMin, backgroundChangeMax);
  204.     }
  205.   } else if (backgroundBrightness > 0) {
  206.     backgroundBrightness -= map(backgroundBrightness, 0, 360, backgroundIntensityDecay, backgroundIntensityDecay*2);
  207.   }
  208.  
  209.   if (fill) {
  210.     zEnable += zEnableSpeed;
  211.   } else {
  212.     zEnable -= zEnableSpeed;
  213.   }
  214.   zEnable = min(max(zEnable, zEnableMin), zEnableMax);
  215.  
  216.   supervisor.render();
  217.  
  218.  
  219.  
  220.   for (Pass pass : fill ? fillPasses : noFillPasses) {
  221.     supervisor.pass(pass);
  222.   }
  223.   supervisor.compose();
  224.  
  225.   // saveFrame("exports/image" + frameCount + ".jpg");
  226. }
  227.  
  228. void strip(float colorMin, float colorMax, float xMod, float yMod, float zMod, float xPow, float yPow, float heightpercentage) {
  229.   for (int y = 0; y < rows-1; y++) {
  230.     beginShape(TRIANGLE_STRIP);
  231.     for (int x = 0; x < cols; x++) {
  232.       float h = terrain[x][y];
  233.       //float c = map(h, 0, maxheight, colorMin, colorMax);
  234.       float c = map(h, 0, maxheight, 0, 360);
  235.       float sw = map(heightpercentage, 0, 100, -maxwidth/8, maxwidth)-pow(y,2);
  236.       if (sw < 1) sw = 1;
  237.       strokeWeight(sw);
  238.       if (fill) {
  239.         float alphaValue = map(y, 0, rows, alphaMax, alphaMin) + map(heightpercentage, 0, 100, alphaMin, alphaMax);
  240.         if (alphaValue > alphaCurrent) {
  241.           alphaCurrent = alphaValue;
  242.         } else {
  243.           alphaCurrent -= alphaDecay;
  244.         }
  245.         alphaCurrent = max(min(alphaCurrent, alphaMax, 360), alphaMin, 0);
  246.  
  247.         fill(color(c, 360, 360, alphaCurrent));
  248.         stroke(color(c, 360, 360, alphaCurrent));
  249.       } else {
  250.         // fill(c, 360, map(heightpercentage, 0, 100, 0, 360));
  251.         stroke(c, 360, 360);
  252.         noFill();
  253.       }
  254.  
  255.  
  256.       float ySine = sin(map(x, 0, cols, 0, HALF_PI));
  257.       float xSine = tan(map(y, 0, rows-1, 0, HALF_PI));
  258.  
  259.      
  260.       vertex(x*scl*xMod*xSine, y*yMod-x*ySine*scl+scl, zMod*terrain[x][y]+zMod*pow(y,yPow)*pow(x,xPow)*zEnable);
  261.       vertex(x*scl*xMod*xSine, (y+1)*yMod-x*ySine*scl+scl, zMod*terrain[x][y+1]+zMod*pow(y,yPow)*pow(x,xPow)*zEnable);
  262.  
  263.       // if (fill) {
  264.       // } else {
  265.       //   vertex(x*scl*xMod*xSine, y*yMod-x*ySine*scl, zMod*terrain[x][y]/*+zMod*pow(y,yPow)*pow(x,xPow)*/);
  266.       //   vertex(x*scl*xMod*xSine, (y+1)*yMod-x*ySine*scl, zMod*terrain[x][y+1]/*+zMod*pow(y,yPow)*pow(x,xPow)*/);
  267.       // }
  268.     }
  269.     endShape();
  270.   }
  271. }
  272.  
  273.  
  274. String prepad(int num, int amount) {
  275.   String unpadded = "" + num;
  276.   String zeroes = new String(new char[amount]).replace("\0", "0");
  277.   return zeroes.substring(unpadded.length()) + unpadded;
  278. }
  279.  
  280. void setTitle() {
  281.   float position = map(jingle.position(), 0, jingle.length(), 0, 100);
  282.   String[] songParts = songname.split("/");
  283.   String songTitle = songParts[songParts.length - 1];
  284.   surface.setTitle("song: " + songTitle + " | progress: " + prepad(round(position), 3) + "/100% | fps: " + prepad(round(frameRate), 3));
  285. }
  286.  
  287. void setMaximumHeight(String name) {
  288.   AudioSample jingle = minim.loadSample(name, fftSize);
  289.  
  290.   float[] leftChannel = jingle.getChannel(AudioSample.LEFT);
  291.   float[] fftSamples = new float[fftSize];
  292.   FFT fft = new FFT( fftSize, jingle.sampleRate());
  293.  
  294.   int totalChunks = (leftChannel.length / fftSize) + 1;
  295.   for(int chunkIdx = 0; chunkIdx < totalChunks; ++chunkIdx)
  296.   {
  297.     int chunkStartIndex = chunkIdx * fftSize;
  298.     int chunkSize = min( leftChannel.length - chunkStartIndex, fftSize );
  299.     System.arraycopy( leftChannel, chunkStartIndex, fftSamples, 0, chunkSize);    
  300.     if ( chunkSize < fftSize )
  301.     {
  302.       java.util.Arrays.fill( fftSamples, chunkSize, fftSamples.length - 1, 0.0 );
  303.     }
  304.     fft.forward( fftSamples );
  305.    
  306.     float[] heights = new float[fftSize/2];
  307.     for(int i = 0; i < fftSize/2; ++i)
  308.     {
  309.       heights[i] = fft.getBand(i);
  310.      
  311.       if (fft.getBand(i) * multiplier > maxheight)
  312.         maxheight = fft.getBand(i) * multiplier;
  313.     }
  314.   }
  315.   jingle.close();
  316. }
  317.  
  318. void keyPressed() {
  319.   if (key == 'f' || key == 'F') {
  320.     setFill(!fill);
  321.   }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement