我正在尝试使用tarsos dsp库从WAV文件中提取MFCC值,然后使用DTW计算它们之间的距离。
不幸的是,我在wav文件上如何使用MFCC类中的代码如何遇到困难。
我不确定是否需要首先将WAV文件转换为某种数组缓冲区。
请在此链接中查看MFCC类的库中的代码。
https://github.com/jorensix/tarsosdsp/blob/master/src/src/core/be/tarsos/dsp/mfcc/mfcc/mfcc.java
如果我可以获取有关如何正确使用此代码从WAV文件获得MFCC值的建议,或者也许对另一种方法进行预定,我将非常感谢。
这是示例代码应该为小文件完成工作。它将整个.wav文件加载到字节数组中,因此对于大文件来说,这不是正确的方法。最终变量可能应根据您的用例更改。我仍然是Java的新手,所以不能保证这是最好的方法。
public class App {
private final static String pathToFile = "D:\TarsosWavTest\wavs\1000HzTone.wav";
private final static int audioBufferSize = 2048;
private final static int bufferOverlap = 1024;
private final static int amountOfMelFilters = 20;
private final static int amountOfCepstrumCoef = 30;
private final static float lowerFilterFreq = 133.33f;
private final static float upperFilterFreq = 8000f;
public static void main(String[] args) {
File file = new File(pathToFile);
AudioInputStream audioInputStream;
byte[] byteAudioArray;
AudioDispatcher audioDispatcher;
try {
audioInputStream = AudioSystem.getAudioInputStream(file);
byteAudioArray = audioInputStream.readAllBytes();
} catch (Exception e) {
System.out.println("Exception occured");
e.printStackTrace();
return;
}
try {
audioDispatcher = AudioDispatcherFactory.fromByteArray(byteAudioArray, audioInputStream.getFormat(),
audioBufferSize, bufferOverlap);
} catch (Exception e) {
e.printStackTrace();
return;
}
final MFCC mfccProcessor = new MFCC(audioBufferSize, audioInputStream.getFormat().getSampleRate(),
amountOfCepstrumCoef, amountOfMelFilters, lowerFilterFreq, upperFilterFreq);
audioDispatcher.addAudioProcessor(mfccProcessor);
audioDispatcher.addAudioProcessor(new AudioProcessor() {
@Override // gets called on each audio frame
public boolean process(AudioEvent audioEvent) {
float[] mfccs = mfccProcessor.getMFCC();
/* do whatever necessary with the mfcc elements here
e.g print them */
//System.out.println(Arrays.toString(mfccs));
return true;
}
@Override // gets called when end of the audio file was reached
public void processingFinished() {
System.out.println("end of file reached");
}
});
audioDispatcher.run();// starts a new thread
}}
请注意,即使使用相同的输入参数,也不能保证不同的库(例如Libersa(计算相同的MFCC。