import ddf.minim.*; import ddf.minim.analysis.*; // Declare Minim and AudioInput objects Minim minim; AudioInput in; // Declare FFT object FFT fft; // Variables for average volume and smoothed volume float rms; float smoothedRMS = 0; // Variables for spectral centroid float spectralCentroid; float smoothedSpectralCentroid = 0; void setup() { // Set up the canvas size size(800, 600); // Initialize Minim minim = new Minim(this); // Get a stereo audio input from the microphone in = minim.getLineIn(Minim.STEREO, 1024); // Create an FFT object for the audio input fft = new FFT(in.bufferSize(), in.sampleRate()); // Set the analysis window to Hamming fft.window(FFT.HAMMING); } void draw() { // Clear the background background(0); // Perform the FFT on the audio input fft.forward(in.mix); // Draw the FFT spectrum stroke(255); for (int i = 0; i < fft.specSize(); i++) { // Calculate the height of the line for this frequency bin float amplitude = fft.getBand(i) * 4; // Draw a line for each frequency bin line(i * (width / fft.specSize()), height, i * (width / fft.specSize()), height - amplitude); } // Calculate RMS (average volume) rms = in.mix.level(); // Smooth the RMS value using lerp smoothedRMS = lerp(smoothedRMS, rms, 0.1); // Display the smoothed RMS value fill(255); textSize(16); text("Smoothed RMS: " + nf(smoothedRMS, 1, 3), 10, height - 40); // Calculate spectral centroid float weightedSum = 0; float totalSum = 0; for (int i = 0; i < fft.specSize(); i++) { weightedSum += fft.getBand(i) * i; totalSum += fft.getBand(i); } if (totalSum > 0) { spectralCentroid = weightedSum / totalSum; } else { spectralCentroid = 0; } // Smooth the spectral centroid using lerp smoothedSpectralCentroid = lerp(smoothedSpectralCentroid, spectralCentroid, 0.1); // Display the smoothed spectral centroid text("Spectral Centroid: " + nf(smoothedSpectralCentroid, 1, 3), 10, height - 20); // Display a message on the canvas text("Audio FFT Spectrum", 10, 20); } void stop() { // Close the audio input when the sketch is stopped in.close(); minim.stop(); super.stop(); }