xxxxxxxxxx
let cols, rows;
let scl = 20; // Scale of the terrain
let w = 1400;
let h = 1400;
let flying = 1;
let terrain = [];
let fft;
let audio;
let spectrum;
let volume;
function preload() {
soundFormats('mp3', 'ogg');
audio = loadSound('daftpunk1111.mp3'); // Load your audio file
}
function setup() {
createCanvas(750, 1000, WEBGL);
frameRate(60);
fft = new p5.FFT();
audio.loop(); // Play the audio in a loop
cols = w / scl;
rows = h / scl;
for (let x = 0; x < cols; x++) {
terrain[x] = [];
for (let y = 0; y < rows; y++) {
terrain[x][y] = 0; // Specify a default value for now
}
}
}
function draw() {
flying -= 0.02;
let yoff = flying;
spectrum = fft.analyze(); // Get the frequency spectrum
volume = fft.getEnergy("bass"); // Get the overall volume
let bass = fft.getEnergy("bass");
let treble = fft.getEnergy("treble");
for (let y = 0; y < rows; y++) {
let xoff = 0;
for (let x = 0; x < cols; x++) {
let heightFactor = map(volume, 0, 255, 0.5, 2); // Map volume to height factor
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100) * heightFactor;
xoff += 0.1; // Adjusted for smoother noise calculation
}
yoff += 0.1; // Adjusted for smoother noise calculation
}
background(0);
noStroke(); // Disable mesh lines
// Adjust camera view
translate(0, 100, -300);
rotateX(PI / 10); // Adjusted angle for better view
translate(-w / 2, -h / 2);
scale(1.5); // Adjust scale to fill the canvas
for (let y = 0; y < rows - 1; y++) {
beginShape(TRIANGLE_STRIP);
for (let x = 0; x < cols; x++) {
let z1 = terrain[x][y];
let z2 = terrain[x][y + 1];
let bassFactor = map(bass, 0, 255, 0, 1); // Map bass to blue color
let trebleFactor = map(treble, 0, 255, 0, 1); // Map treble to green color
let col1 = lerpColor(color(0, 0, 255 * bassFactor), color(0, 255 * trebleFactor, 0), map(z1, -100, 100, 0, 1));
let col2 = lerpColor(color(0, 0, 255 * bassFactor), color(0, 255 * trebleFactor, 0), map(z2, -100, 100, 0, 1));
fill(col1);
vertex(x * scl, y * scl, z1);
fill(col2);
vertex(x * scl, (y + 1) * scl, z2);
}
endShape();
}
}