Created
October 4, 2022 04:13
-
-
Save greed9/880ee8de8110ae3cefdaedbb16306572 to your computer and use it in GitHub Desktop.
FFT-based spectrum display from audio input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scrolling spectral display ala SpectrumLab | |
import processing.sound.*; | |
FFT fft; | |
AudioIn in; | |
final int bands = 512; | |
float[] spectrum = new float[bands]; | |
int leftEdge = 70 ; | |
int sampleRate = 44100 ; | |
HighPass highPass; | |
void setup() { | |
colorMode( HSB, 100, 100, 100 ) ; | |
size(1024, 600); | |
background(0, 0, 0); | |
// show available sound devices | |
Sound.list() ; | |
// Create an Input stream which is routed into the Amplitude analyzer | |
fft = new FFT(this, bands); | |
//highPass = new HighPass(this); | |
// https://processing.github.io/processing-sound/processing/sound/Sound.html | |
// https://processing.github.io/processing-sound/processing/sound/AudioDevice.html | |
// get the audio input on Powerbook | |
Sound s = new Sound(this); | |
// Sabrent usb dongle | |
s.inputDevice(3); | |
// see if we can set the samplerate here | |
s.sampleRate( sampleRate ) ; | |
in = new AudioIn(this, 0); | |
// start the Audio Input | |
in.start(); | |
in.amp(1.0) ; | |
//highPass.freq(1000); | |
//highPass.process(in); | |
// patch the AudioIn | |
fft.input(in); | |
//fft.analyze(spectrum); | |
} | |
void draw() { | |
//background(0, 0, 0); | |
// The result of the FFT is normalized | |
fft.analyze(spectrum); | |
// add to the display window | |
renderSpectrum( ) ; | |
// scroll window of previous spectra right (off screen) | |
scrollRight( ) ; | |
// (re)draw scale | |
drawScale( 512, 100 ) ; | |
} // end draw | |
/* | |
void scrollRight( ) | |
{ | |
loadPixels(); | |
for (int r = 0; r < height; r++) | |
{ | |
arrayCopy(pixels, (width * r), pixels, width * r + 1, width - 1); | |
} | |
updatePixels(); | |
} | |
*/ | |
void scrollRight( ) | |
{ | |
loadPixels(); | |
for (int r = 0; r < height; r++) | |
{ | |
int start = ( width * r ) + leftEdge ; | |
int dest = ( width * r ) + 1 + leftEdge ; | |
//println ( start + "," + dest ) ; | |
arrayCopy(pixels, start, pixels, dest, width - leftEdge - 1); | |
} | |
updatePixels(); | |
} | |
void drawScale( int bins, int step ) | |
{ | |
stroke( 57, 100, 100 ) ; | |
for( int i = step ; i < bins ; i+= step ) | |
{ | |
float freq = (i * sampleRate) / bins / 2; | |
freq = round( freq ) ; | |
String yLabel = str( int( freq )) ; | |
text( yLabel, 5, height - i ); | |
} | |
} | |
void renderSpectrum( ) { | |
// Now loop through the bins, rendering | |
//for ( int y = 0; y < spectrum.length / 2; y ++ ) { | |
for ( int y = 0; y < bands ; y ++ ) { | |
float binValue = spectrum[y] * 10000000 ; | |
//println( binValue ) ; | |
float hue = map( binValue, 0, 300,0, 100 ) ; | |
stroke( hue, 100, binValue ) ; | |
point( leftEdge, height - y ) ; | |
} // end loop on bins | |
} // end render method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment