【发布时间】:2015-09-09 18:45:24
【问题描述】:
我使用Ogg Vorbis SPI 作为解码.ogg 文件的一种方式。
我用于加载.ogg 文件并转换为 PCM 的代码 sn-p 与文档中建议的代码没有太大不同:
// The streams are valid at this point.
try (InputStream bin = new BufferedInputStream(file.getInputStream(entry) );
AudioInputStream in = AudioSystem.getAudioInputStream(bin) ) {
AudioFormat baseFormat = in.getFormat();
// At this point the format is valid:
// VORBISENC 44100.0 Hz, unknown bits per sample, mono, 1 bytes/frame, 12000.0 frames/second,
// I can't make a Clip directly from this or it will be all buzzy, so I convert to PCM
// Decoding to PCM
AudioFormat decodedFormat =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
// This is where the issues appear...
AudioInputStream decodedInputStream = AudioSystem.getAudioInputStream(decodedFormat, in);
Clip clip = AudioSystem.getClip();
clip.open(decodedInputStream);
// entry.getName is basically the filename. Any files with '0' frame length didn't get converted properly.
System.out.println("Clip " + entry.getName() + " loaded at " + clip.getFrameLength() + " frame length");
return clip;
} catch { ... }
正在发生的事情是我正在加载几个小的声音文件,范围从 5K 到 32K 的高(ogg 格式)。但是,一些文件无法正确转换为 PCM。我注意到他们都有一些共同点。它们的大小范围为 5K - 6K。声音文件似乎在 7K 左右开始正确转换。我测试了一个 11K 的声音并将其剪掉,直到它不再转换 (~6K)
我认为解决它的一种方法是添加一些听不见的噪音来填充大小,但我更喜欢正确转换声音文件而不管大小。除了 API 本身之外,我对声音表示的概念完全陌生,我不知道从哪里开始寻找为什么会发生这种情况。
【问题讨论】:
-
我想知道 AudioInputStream.getFormat() 是否移动了流中的位置并且不重置它。也许尝试关闭并重新打开流?
-
感谢您的关注;在我使用 getFormat() 之后,我刚刚尝试了 reset() 并关闭/打开了一个新流。不幸的是,这个问题仍然存在。