I wanted to share a program here, but I’m having trouble making it behave well.
The idea is to visualize (FFT) whatever music you are playing on your system, so you don’t need to load a track into Processing, but assume you are already playing music from soundcloud, mixcloud, youtube, etc.
It works already, but there is a ridiculous latency, and it seems to change from time to time. Sometimes it’s 2 seconds, sometimes it’s 20 seconds, and it also seems to drift. I was wondering if it was something about 44100 vs 48000 hz, or two buffer sizes that do not match…
The latency & drift are not present when working with an audio file.
Any ideas?
import ddf.minim.*;
import ddf.minim.analysis.*;
import javax.sound.sampled.*;
Minim minim;
AudioInput audio;
FFT fft;
// This affects latency, and in theory lower = less latency
int bufferSize = 512; // try 128 256 512 1024
void setup() {
size(800, 100, P2D);
// here you configure your audio device. Run it once and look in the console
// to see available names. In my system one of the lines looks like
// [2] default [default], version 4.18.0-12-generic
// so I grab the part between [num] and the comma.
startAudioInput("default [default]");
noStroke();
}
void draw() {
background(0);
if (fft != null) {
fft.forward(audio.mix);
int bands = fft.avgSize();
for (int i = 0; i < bands; i++) {
float val = fft.getAvg(i);
float x = map(i, 0, bands, 0, width);
rect( x, height, 2, -val);
}
}
}
private void startAudioInput(String deviceName) {
minim = new Minim(this);
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for (Mixer.Info info : mixerInfo) {
if (info.getName().equals(deviceName)) {
minim.setInputMixer(AudioSystem.getMixer(info));
audio = minim.getLineIn(Minim.STEREO, bufferSize);
fft = new FFT(audio.bufferSize(), audio.sampleRate());
fft.logAverages(22, 3);
return;
}
}
println(deviceName, "not found! Use one of these:");
printArray(mixerInfo);
}
I also opened an issue on GitHub.
Forgot to mention: Ubuntu 18.10