1
\$\begingroup\$

I have a MP3Player Class that does the Fast Fourier Transform and puts the values in a float array (called bands). Then every 20ms it notifys the Observer (VisualizerController) which then uses getBands() to get the float array and use it to draw the values on a canvas.

I feel like this is an ugly way to do this but I don't know how to do to it better.

Here is the whole source on github

This is the update function in the VisualizerController handling the drawing:

@Override
public void update(Observable o, Object arg) {
    if (saturation > 0.5) saturation -= 0.02;
    if (brightness > 0.8) brightness -= 0.02;

    bands = player.getBands();

    if (circle) {
        if (bars) drawCircleBars();
        else drawCircleLine();
    } else {
        if (bars) drawStraightBars();
        else drawStraightLine();
    }

    if (player.isBeat()) {
        saturation = 1.0;
        brightness = 1.0;
    }
}

And here is what it looks like in MP3Player where I get the frequencies and notify the Observers:

private class FFTThread extends Thread {

    @Override
    public void run() {

        while (!isInterrupted()) {

            fft.forward(audioPlayer.getMix());
            for (int i = 0; i < bands.length; i++) bands[i] = fft.getBand(i);

            beatDetect.detect(audioPlayer.getMix());
            beat = beatDetect.isOnset();

            progess = audioPlayer.position() * 1.0 / audioPlayer.length();

            setChanged();
            notifyObservers();

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                interrupt();
            }
        }
    }
}

public float[] getBands() {
    return bands;
}

public boolean isBeat() {
    return beat;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.