有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组Java输出值随时间变化

我有一个音频文件,我正在将其转换为字节数组,但你无法判断该字节值在歌曲中实际播放的时间。所以我试着把它延伸到歌曲的长度上

因此,当歌曲播放时,它会输出字节值。这怎么可能

以下是我目前的代码:

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();
    }

}


}

如何随时间而不是立即打印字节数组(endResult[i])的值


共 (1) 个答案

  1. # 1 楼答案

    无论何时打印值,请执行以下操作:

    Thread.sleep(100);
    

    在打印下一个值之前等待100毫秒(0.1秒)。这当然是可调的