Weird line on the middle of the wave, how do i make it smooth (Real Time Audio Spectrum for Java)

-5

yow please help me smooth the fft analyzer, i think my math was off, i have a byte[] pcmData that holds audio buffers based on the code from xt-audio, please give me a code that converts that data into my desired data i want, see pic below

what i get: what i get

i need this data: i need this data lol

here is my code from github, please read README

here is the video of it working

Here is an image working in real time, it captures basically every sound from the computer, like youtube: Here is an image working in real time, it captures basically every sound from the computer, like youtube

Here is also an image working with vlc: Here is also an image working with vlc

i want it to become like this: i want it to become like this

edit: i figured it out, left and right channels returns data that ranges from -1 to 1, and bytes literally returns the range of bytes, i watched the coding train's fft analyzer, and that's why the graph was off

edit #2: i kinda did it, see pic below, how do i split it to stereo though? see code below

pic here

static void processAudio(short[] audio, int frames) {
    // convert from short[] to byte[]
    for (int frame = 0; frame < frames; frame++) {
        // for 2 channels
        for (int channel = 0; channel < 2; channel++) {
            // 2 = channels again
            int sampleIndex = frame * 2 + channel;
            // 2 = 2 bytes for each short
            // int byteIndex0 = sampleIndex * 2;
            int byteIndex1 = sampleIndex * 2 + 1;
            // probably some library method for this, somewhere
            //BYTES[byteIndex0] = (byte)(audio[sampleIndex] & 0x000000FF);
            BYTES[byteIndex1] = (byte) ((audio[sampleIndex] & 0x0000FF00) >> 8);
        }
    }
}

my line rendering:

public void render(Graphics gc){
    System.out.println("\nBYTES (" + BYTES.length + "): " + Arrays.toString(BYTES));
    System.out.println("bytesToFloats (" + bytesToFloats(BYTES).length + "): " + Arrays.toString(bytesToFloats(BYTES)));

    float[] pSample1 = bytesToFloats(BYTES);

    colorIndex = (colorIndex == colorSize - 1) ? 0 : colorIndex + 1;
    gc.setColor(Color.getHSBColor((float) colorIndex / 360f, 1.0f, 1.0f));

    int yLast1 = (int) (((pSample1[0] / 32768f)) * (float) halfCanvasHeight)
            + halfCanvasHeight;
    int samIncrement1 = 1;
    for (int a = samIncrement1, c = 0; c < canvasWidth; a += samIncrement1, c++) {
        int yNow = (int) (((pSample1[a] / 32768f)) * (float) halfCanvasHeight)
                + halfCanvasHeight;
        gc.drawLine(c, yLast1, c + 1, yNow);
        yLast1 = yNow;
    }
}

bytes to floats (stolen from Correct way to Convert 16bit PCM Wave data to float)

public static float[] bytesToFloats(byte[] bytes){
    float[] floats = new float[bytes.length / 2];
    for (int i = 0; i < bytes.length; i += 2){
        floats[i/2] = bytes[i] | (bytes[i+1] << 8);
    }
    return floats;
}
java
fft
real-time
wasapi
oscilloscope
asked on Stack Overflow Feb 8, 2021 by GDjkhp • edited Feb 9, 2021 by GDjkhp

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0