I'm trying to output my computer microphone input to the speakers output using Minim in Processing. The aim of the project is to analyze an input (the microphone, a sound file, etc), make some alterations to its frequency spectrum, and finally output it to the speakers or save the stuff on a file (I still don't know how to implement this last feature).
I referred to mots' answer in this post https://forum.processing.org/beta/num_1256413038.html to make it, but I can't let it work: nothing is being played through the speakers.
This is the code I'm using:
import ddf.minim.*;
import ddf.minim.analysis.*;
private FFT fftIn, fftOut;
private AudioInput input;
private AudioOutput output;
private Minim minim;
void setup() {
size(300, 200);
minim = new Minim(this);
input = minim.getLineIn();
input.addListener(new Listener());
output = minim.getLineOut();
output.addSignal(new Signal());
fftOut = fftIn = new FFT(input.bufferSize(), input.sampleRate());
}
class Listener implements AudioListener {
public void samples(float[] sample) {
fftIn.forward(sample);
}
public void samples(float[] left, float[] right) {
samples(left);
}
}
private class Signal implements AudioSignal {
public void generate(float[] sample) {
boolean mode = false;
if (mode) {
fftIn.inverse(sample);
} else {
for (int i = 0; i < input.bufferSize() / 2; i++) {
fftOut.setBand(i, fftIn.getBand(i));
}
fftOut.inverse(sample);
}
}
public void generate(float[] left, float[] right) {
generate(right);
}
}
EDIT 31 December 2021
This is the code I have written down: it runs with no errors, but as you can see in the OUT
section a lot of frequencies along the entire spectrum bring up and make noise to the output. I think it's caused by the discontinuities between the samples.
The code:
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioInput mic;
AudioOutput out;
FFT drawfft, testfft;
PGraphics IN, OUT;
float scale = 8, barWidth;
public void settings() {
size(1200, 960);
}
void setup() {
surface.setResizable(false);
init();
IN = createGraphics(1200, 480);
OUT = createGraphics(1200, 480);
}
void draw() {
drawSpectrum(IN, -1);
image(IN, 0, 0);
drawSpectrum(OUT, 1);
image(OUT, 0, height - OUT.height);
}
void drawSpectrum(PGraphics pg, int channel) {
if(channel == -1) drawfft.forward(mic.mix);
else if(channel == 1) drawfft.forward(out.mix);
int W = width;
int H = (height - 5) / 2;
pg.beginDraw();
pg.background(0);
barWidth = W / float(drawfft.specSize());
pg.fill(255);
pg.textAlign(RIGHT, TOP);
pg.textSize(13);
if(channel == -1) pg.text("IN", pg.width - 8, 8);
else if(channel == 1) pg.text("OUT", pg.width - 8, 8);
for(int i = 0; i < drawfft.specSize(); i++) {
pg.strokeWeight(1);
pg.fill(255, 0, 0);
pg.stroke(255, 0, 0);
pg.rect(i * barWidth, H, barWidth, -constrain(dB(drawfft.getBand(i)) * scale, 0, H));
}
pg.endDraw();
}
void init() {
minim = new Minim(this);
minim.debugOn();
mic = minim.getLineIn(Minim.MONO);
out = minim.getLineOut(Minim.MONO);
testfft = new FFT(mic.bufferSize(), mic.sampleRate());
mic.addListener(new InEvent());
out.addSignal(new OutEvent());
drawfft = new FFT(mic.bufferSize(), mic.sampleRate());
}
float dB(float amp) { return 20 * (log(amp) / log(10)); }
class InEvent implements AudioListener {
synchronized void samples(float[] samp) {
testfft.forward(samp);
}
synchronized void samples(float[] sampL, float[] sampR) {
samples(sampL);
}
}
class OutEvent implements AudioSignal {
synchronized void generate(float[] samp) {
testfft.inverse(samp);
}
synchronized void generate(float[] left, float[] right) {
generate(left);
}
}
Any reply is accepted,
Simone