使用Java将读取WAV文件时的采样率转换为采样数组

我收集了一些短WAV文件,我想用Java处理各种数字信号处理algorithm。 为了达到这个目的,我需要得到一个int值的采样数组,编码频率为11025Hz。

源文件有几个不同的采样率,包括11025赫兹和44100赫兹。 以下是我正在尝试使用的代码来读取它们:

// read the WAV file FileInputStream fileInputStream = new FileInputStream(new File("test.wav")); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fileInputStream ); // copy the AudioInputStream to a byte array called buffer ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] data = new byte[4096]; int tempBytesRead = 0; int byteCounter = 0; while ((tempBytesRead = audioInputStream.read(data, 0, data.length)) != -1) { bos.write(data, 0, tempBytesRead); byteCounter += tempBytesRead; } bos.close(); byte[] buffer = bos.toByteArray(); AudioFileFormat audioFileFormat = new AudioFileFormat(AudioFileFormat.Type.WAVE, audioInputStream.getFormat(), (int)audioInputStream.getFrameLength()); // get the resulting sample array int[] samples = new int[audioFileFormat.getFrameLength()]; for (int i = 0; i < samples.length; i++) { samples[i] = getSampleValue(i); // the getSampleValue method reads the sample values from the "buffer" array, handling different encoding types like PCM unsigned/signed, mono/stereo, 8 bit/16 bit } // RESULT: the "samples" array 

问题是,代码不能正确处理不同的采样率。 所以对于44100赫兹的帧速率,我得到了4倍于11025赫兹帧速率的样本。 我希望得到的采样数组使用11025赫兹的帧速率,而不pipe源文件的帧频如何。 我试图强制Java读取AudioInputStream时转换帧率,但我得到一个exception类似于以下一个:

 java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 11025.0 Hz, 16 bit, mono, 2 bytes/frame, 44100.0 frames/second, little-endian from PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:955) 

我阅读了Java Sound API教程: http : //java.sun.com/docs/books/tutorial/sound/converters.html 。 Java Sound API似乎不支持我的操作系统(Windows 7)的这种转换。 我想避免依赖任何外部库。 有没有办法自己进行采样率转换?

对于> 11025 Hz的采样率,您需要下采样,这是一个两阶段的过程。 首先,您需要低通滤波器来满足奈奎斯特准则,然后您可以抽取,例如对于44.1 kHz采样率数据,您需要使用截止频率为5.5 kHz的低通滤波器,然后可以丢弃3个每4个采样中的4:1下采样比率。 您需要为每个要支持的下采样比率使用不同的滤波器。

我相信接受的答案回答了另一个问题 – 它解决了同样的问题(下采样音频),但以另一种方式(手动而不是使用Java声音API)。 我有同样的事情,并挖掘它。

正确的方法(或Java声音API方式)确实(正如http://docs.oracle.com/javase/tutorial/sound/converters.html中所建议的那样&#xFF09;

 AudioFormat outDataFormat = new AudioFormat((float) 8000.0, (int) 8, (int) 1, true, false); AudioInputStream lowResAIS = AudioSystem.getAudioInputStream(outDataFormat, inFileAIS); 

问题是标准的java没有重新采样(甚至是立体声单声道转换)代码(或者至少不在代码的那部分 – 参见http://www.jsresources.org/faq_audio.html#convert_sample_rate ) 。

jsresources页面也指出了答案:只需安装2个插件就可以了。 最简单的方法就是将这些插件安装在扩展目录中,在OSX Lion上,这样做就可以实现(假设你有wget):

 wget http://www.tritonus.org/tritonus_share-0.3.6.jar -O /Library/Java/Extensions/tritonus_share-0.3.6.jar wget http://www.tritonus.org/tritonus_remaining-0.3.6.jar -O /Library/Java/Extensions/tritonus_remaining-0.3.6.jar 

添加这两个jar文件后,一切正常(只是一个额外的警告:如果你想改变通道的数量和采样率,它仍然不支持作为一个步骤)。