Question

I have an audio file that I am converting into a byte array, but then you cannot tell when that byte value is actually played in the song. So I am trying to stretch it out over the length of the song.

So while the song is playing, it outputs the byte value. How is this possible?

Here is my code so far:

public class Main {

private static final String FILENAME = "assets/pf.wav";
private static double[] endResult = null;

public static void convert() throws IOException{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(FILENAME));

    int read;
    byte[] buff = new byte[1024];
    while ((read = in.read(buff)) > 0)
    {
        out.write(buff, 0, read);
    }
    out.flush();
    byte[] audioBytes = out.toByteArray();
    endResult = calculateFFT(audioBytes);
}

public static double[] calculateFFT(byte[] signal)
{           
    final int mNumberOfFFTPoints =1024;
    double mMaxFFTSample;
    double temp;
    Complex[] y;
    Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
    double[] absSignal = new double[mNumberOfFFTPoints/2];

    for(int i = 0; i < mNumberOfFFTPoints; i++){
        temp = (double)((signal[2*i] & 0xFF) | (signal[2*i+1] << 8)) / 32768.0F;
        complexSignal[i] = new Complex(temp,0.0);
    }

    y = FFT.fft(complexSignal);

    mMaxFFTSample = 0.0;
    int mPeakPos = 0;
    for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
    {
         absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
         if(absSignal[i] > mMaxFFTSample)
         {
             mMaxFFTSample = absSignal[i];
             mPeakPos = i;
         } 
    }

    return absSignal;

}

public static void main(String[] args) throws UnsupportedAudioFileException, IOException     {
    File file = new File(FILENAME);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
    AudioFormat format = audioInputStream.getFormat();
    long frames = audioInputStream.getFrameLength();
    final double durationInSeconds = (frames+0.0) / format.getFrameRate();  
    try {
        convert();
        for(int i = 0; i < endResult.length; i++) {
            System.out.println(endResult[i]);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


}

How can I print out the value of the byte array (endResult[i]) over time and not immediately?

Was it helpful?

Solution

Whenever you print out a value, do:

Thread.sleep(100);

To wait 100 milliseconds (0.1 seconds) before printing the next value. This is adjustable of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top