Advertisement
Galbi3000

TerrainAudio

Dec 17th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import processing.sound.*;
  2. import ddf.minim.*;
  3.  
  4. Minim minim;
  5. AudioInput minimIn;
  6. float[] smoothL, smoothR;
  7. float smoothF = 0.15;
  8.  
  9. FFT fft;
  10. AudioIn in;
  11. int bands = 256;
  12. float[] spectrum = new float[bands];
  13. float smooth_factor = 0.2;
  14.  
  15. int cols, rows;
  16. int scale = 15;
  17. int w = 1600;
  18. int h = 1200;
  19.  
  20. float[][] terrain;
  21.  
  22. void setup() {
  23.   size(1600,900,P3D);
  24.   cols = w / scale;
  25.   rows = h / scale;
  26.   terrain = new float[cols][rows+1];
  27.    
  28.   // Create an Input stream which is routed into the Amplitude analyzer
  29.   fft = new FFT(this, bands);
  30.   in = new AudioIn(this, 0);
  31.  
  32.   // start the Audio Input
  33.   in.start();
  34.  
  35.   // patch the AudioIn
  36.   fft.input(in);
  37.  
  38.   minim = new Minim(this);
  39.   // use the getLineIn method of the Minim object to get an AudioInput
  40.   minimIn = minim.getLineIn();
  41.  
  42.   smoothL = new float[minimIn.bufferSize()];
  43.   smoothR = new float[minimIn.bufferSize()];
  44. }
  45.  
  46. void draw() {
  47.   //println(frameRate);
  48.   background(0);
  49.   fft.analyze();
  50.  
  51.   for (int i = 0; i < bands; i++)
  52.     spectrum[i] += (fft.spectrum[i] - spectrum[i]) * smooth_factor;
  53.   for(int i = 0; i < minimIn.bufferSize(); i++)
  54.   {
  55.     smoothL[i] += (minimIn.left.get(i) - smoothL[i]) * smoothF;
  56.     smoothR[i] += (minimIn.right.get(i) - smoothR[i]) * smoothF;
  57.   }
  58.  
  59.   for (int y=rows; y>0; y--) {
  60.     for (int x=0; x<cols; x++) {
  61.       terrain[x][y] = terrain[x][y-1];
  62.     }
  63.   }
  64.  
  65.   // Block start for FFT terrain. Comment if using waveform terrain.
  66.   for (int x=0; x<cols; x++) {
  67.     //int s=int(map(x,0,width,10,bands));
  68.     terrain[x][0] = fft.spectrum[x]*1000;
  69.   }
  70.   int i=0;
  71.   for (int x=cols-1; x>=0; x--) {
  72.     //int s=int(map(x,0,width,10,bands));
  73.     terrain[x][0] += spectrum[i]*1000;
  74.     i++;
  75.   }
  76.   // FFT terrain end.
  77.  
  78.   int buffS = minimIn.bufferSize();
  79.  
  80.   // Block start for waveform terrain. Comment out if using FFT terrain.
  81. /*  for(int x = 0; x < cols; x++)
  82.     terrain[x][0] = 0;
  83.    
  84.   for (int i = 0; i < buffS - 1; i++) {
  85.     int xl = (int)map(i, 0, buffS, 0, cols/2);
  86.     int xr = ((int)map(i, 0, buffS, 0, cols/2)) + (cols/2);
  87.     float s = sin(map(i, 0, buffS, 0, 1)*PI);
  88.     terrain[xl][0] = (terrain[xl][0]/2) + ((smoothL[i]*s)/2);
  89.     terrain[xr][0] = (terrain[xr][0]/2) + ((smoothR[i]*s)/2);
  90.   }
  91.   for(int x = 0; x < cols; x++)
  92.     terrain[x][0] *= 400;
  93. */
  94.   // Waveform terrain end.
  95.  
  96.   stroke(255,255,128,128);
  97.   for(i = 0; i < buffS - 1; i++)
  98.   {
  99.     float s = sin(map(i, 0, buffS, 0, 1)*PI);
  100.     line( (width/2-buffS/3)+(i/3), 70 + smoothL[i]*(s*150), (width/2-buffS/3)+((i+1)/3), 70 + smoothL[i+1]*(s*100) );
  101.     line( (width/2)+(i/3), 70 + smoothR[i]*(s*150), (width/2)+((i+1)/3), 70 + smoothR[i+1]*(s*100) );
  102.   }
  103.   pushMatrix();
  104.  
  105.   lights();
  106.   noStroke();
  107.   fill(100,100,128,210);
  108.  
  109.   translate(width/2,height/2);
  110.   rotateX(PI/3);
  111.   translate(-w/2,-h/2);
  112.  
  113.   for (int y=0; y<rows; y++) {
  114.     fill(100-y,100-(y*1.2),128,210);
  115.     beginShape(TRIANGLE_STRIP);
  116.     for (int x=0; x<cols; x++) {
  117.       vertex(x*scale, y*scale, terrain[x][y]);
  118.       vertex(x*scale, (y+1)*scale, terrain[x][y+1]);
  119.       //rect(x*scale, y*scale, scale, scale);
  120.     }
  121.     endShape();
  122.   }
  123.   popMatrix();
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement