用Java读取wav文件的问题
我使用该 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:我之前的问题:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你想要这样的东西:
这将为你留下一个 1 秒
double[]
样本数组的List
(实际上,最后一个数组将略小于整整一秒)。然后,您可以迭代该数组集合并将每个数组转换为单独的 WAV 文件。I think you want something like this:
This will leave you with a
List
of 1-seconddouble[]
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.从缓冲区复制到列表时,您需要考虑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).