Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.sound.*;
- import ddf.minim.*;
- Minim minim;
- AudioInput minimIn;
- float[] smoothL, smoothR;
- float smoothF = 0.15;
- FFT fft;
- AudioIn in;
- int bands = 256;
- float[] spectrum = new float[bands];
- float smooth_factor = 0.2;
- int cols, rows;
- int scale = 15;
- int w = 1600;
- int h = 1200;
- float[][] terrain;
- void setup() {
- size(1600,900,P3D);
- cols = w / scale;
- rows = h / scale;
- terrain = new float[cols][rows+1];
- // Create an Input stream which is routed into the Amplitude analyzer
- fft = new FFT(this, bands);
- in = new AudioIn(this, 0);
- // start the Audio Input
- in.start();
- // patch the AudioIn
- fft.input(in);
- minim = new Minim(this);
- // use the getLineIn method of the Minim object to get an AudioInput
- minimIn = minim.getLineIn();
- smoothL = new float[minimIn.bufferSize()];
- smoothR = new float[minimIn.bufferSize()];
- }
- void draw() {
- //println(frameRate);
- background(0);
- fft.analyze();
- for (int i = 0; i < bands; i++)
- spectrum[i] += (fft.spectrum[i] - spectrum[i]) * smooth_factor;
- for(int i = 0; i < minimIn.bufferSize(); i++)
- {
- smoothL[i] += (minimIn.left.get(i) - smoothL[i]) * smoothF;
- smoothR[i] += (minimIn.right.get(i) - smoothR[i]) * smoothF;
- }
- for (int y=rows; y>0; y--) {
- for (int x=0; x<cols; x++) {
- terrain[x][y] = terrain[x][y-1];
- }
- }
- // Block start for FFT terrain. Comment if using waveform terrain.
- for (int x=0; x<cols; x++) {
- //int s=int(map(x,0,width,10,bands));
- terrain[x][0] = fft.spectrum[x]*1000;
- }
- int i=0;
- for (int x=cols-1; x>=0; x--) {
- //int s=int(map(x,0,width,10,bands));
- terrain[x][0] += spectrum[i]*1000;
- i++;
- }
- // FFT terrain end.
- int buffS = minimIn.bufferSize();
- // Block start for waveform terrain. Comment out if using FFT terrain.
- /* for(int x = 0; x < cols; x++)
- terrain[x][0] = 0;
- for (int i = 0; i < buffS - 1; i++) {
- int xl = (int)map(i, 0, buffS, 0, cols/2);
- int xr = ((int)map(i, 0, buffS, 0, cols/2)) + (cols/2);
- float s = sin(map(i, 0, buffS, 0, 1)*PI);
- terrain[xl][0] = (terrain[xl][0]/2) + ((smoothL[i]*s)/2);
- terrain[xr][0] = (terrain[xr][0]/2) + ((smoothR[i]*s)/2);
- }
- for(int x = 0; x < cols; x++)
- terrain[x][0] *= 400;
- */
- // Waveform terrain end.
- stroke(255,255,128,128);
- for(i = 0; i < buffS - 1; i++)
- {
- float s = sin(map(i, 0, buffS, 0, 1)*PI);
- 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) );
- line( (width/2)+(i/3), 70 + smoothR[i]*(s*150), (width/2)+((i+1)/3), 70 + smoothR[i+1]*(s*100) );
- }
- pushMatrix();
- lights();
- noStroke();
- fill(100,100,128,210);
- translate(width/2,height/2);
- rotateX(PI/3);
- translate(-w/2,-h/2);
- for (int y=0; y<rows; y++) {
- fill(100-y,100-(y*1.2),128,210);
- beginShape(TRIANGLE_STRIP);
- for (int x=0; x<cols; x++) {
- vertex(x*scale, y*scale, terrain[x][y]);
- vertex(x*scale, (y+1)*scale, terrain[x][y+1]);
- //rect(x*scale, y*scale, scale, scale);
- }
- endShape();
- }
- popMatrix();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement