用Java读取wav文件的问题

发布于 2024-11-07 01:36:14 字数 1626 浏览 4 评论 0原文

我使用该 API 在 Java 中读取 wav 文件: Reading and Write Wav Files in Java

我用 Java 读取了一个 wav 文件,并希望将所有值放入一个数组列表中。我写道:

// Open the wav file specified as the first argument
     WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence);
     List<Double> allBuffer = new ArrayList<Double>();

 ....
     do {
        // Read frames into buffer
        framesRead = wavFile.readFrames(buffer, 50);
        for (double aBuffer : buffer) {
           allBuffer.add(aBuffer);
        }

     } while (framesRead != 0);

当我在调试器上检查 allBuffer 的大小时,它说:

74700

但是在我使用的代码中:

wavFile.display();

它的输出:

....
... Frames: 74613
....

我的 allbuffer 比该 API 显示的帧大。它是怎么来的?

PS:我之前的问题:

用Java读取wav文件

如何分割wav文件用Java分成1秒的片段?

PS2: 我修复了错误,但是当我运行程序时,有时会出现该错误:可能是什么问题?

java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
at sun.java2d.Disposer.run(Disposer.java:127)
at java.lang.Thread.run(Thread.java:662)

删除引用时出现异常:java.lang.InterruptedException

I use that API to read wav file at Java: Reading and Writing Wav Files in Java

I read a wav file with Java and want to put all the values into an arraylist. I wrote:

// Open the wav file specified as the first argument
     WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence);
     List<Double> allBuffer = new ArrayList<Double>();

 ....
     do {
        // Read frames into buffer
        framesRead = wavFile.readFrames(buffer, 50);
        for (double aBuffer : buffer) {
           allBuffer.add(aBuffer);
        }

     } while (framesRead != 0);

When I check the size of allBuffer at debugger it says:

74700

However at the code it whe I use:

wavFile.display();

the output of it:

....
... Frames: 74613
....

My allbuffer is bigger than frames as displayed by that API. How it comes?

PS: My previous questions about this:

Reading wav file in Java

How to divide a wav file into 1 second pieces with Java?

PS2:
I fixed the bugs however when I run the program I get that error sometimes: What may be the problem?

java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
at sun.java2d.Disposer.run(Disposer.java:127)
at java.lang.Thread.run(Thread.java:662)

Exception while removing reference: java.lang.InterruptedException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-11-14 01:36:14

我想你想要这样的东西:

// Open the wav file specified as the first argument
     WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence);
     List<double[]> allBuffer = new List<double[]>();

    int bufferSize = 44100; // 1 second, probably
    double[] buffer;


 ....
     do {
        // Read frames into buffer

        buffer = new double[bufferSize];
        framesRead = wavFile.readFrames(buffer, bufferSize);
        if (framesRead == bufferSize)
        {
            allBuffer.add(buffer);
        }
        else
        {
            double[] crippleBuffer = new double[framesRead];
            Array.Copy(buffer, crippleBuffer, framesRead);
            allBuffer.add(crippleBuffer);
            break;
        }

     } 

这将为你留下一个 1 秒 double[] 样本数组的 List (实际上,最后一个数组将略小于整整一秒)。然后,您可以迭代该数组集合并将每个数组转换为单独的 WAV 文件。

I think you want something like this:

// Open the wav file specified as the first argument
     WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence);
     List<double[]> allBuffer = new List<double[]>();

    int bufferSize = 44100; // 1 second, probably
    double[] buffer;


 ....
     do {
        // Read frames into buffer

        buffer = new double[bufferSize];
        framesRead = wavFile.readFrames(buffer, bufferSize);
        if (framesRead == bufferSize)
        {
            allBuffer.add(buffer);
        }
        else
        {
            double[] crippleBuffer = new double[framesRead];
            Array.Copy(buffer, crippleBuffer, framesRead);
            allBuffer.add(crippleBuffer);
            break;
        }

     } 

This will leave you with a List of 1-second double[] arrays of samples (actually, the last array will be somewhat less than a full second). You can then iterate through this collection of arrays and turn each one into a separate WAV file.

不忘初心 2024-11-14 01:36:14

从缓冲区复制到列表时,您需要考虑framesRead。在最后一次调用中,您复制的双精度数比实际从文件中读取的双精度数多。 (即与第一个链接中的示例代码完全相同)。

you need to account for framesRead when copying from the buffer into your list. on the last call, you are copying more doubles than you actually read from the file. (i.e. exactly like the example code in your first link).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文