使用 TARSOS DSP 从 WavFiles 中提取 MFCC 的困难 JAVA
Difficulties using TARSOS DSP to extract MFCC from WavFiles JAVA
我正在尝试使用 TARSOS DSP 库从 wav 文件中提取 MFCC 值,然后再使用 DTW 计算它们之间的距离。
不幸的是,我无法理解如何将来自 MFCC class 的代码用于 wav 文件。
我不确定是否需要先将 wav 文件转换为某种数组缓冲区。
请在此处 link 查看 MFCC class 库中的代码。
https://github.com/JorenSix/TarsosDSP/blob/master/src/core/be/tarsos/dsp/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
}}
请注意,即使使用相同的输入参数,也不能保证不同的库(例如 librosa)计算出相同的 MFCC。
我正在尝试使用 TARSOS DSP 库从 wav 文件中提取 MFCC 值,然后再使用 DTW 计算它们之间的距离。
不幸的是,我无法理解如何将来自 MFCC class 的代码用于 wav 文件。
我不确定是否需要先将 wav 文件转换为某种数组缓冲区。
请在此处 link 查看 MFCC class 库中的代码。
https://github.com/JorenSix/TarsosDSP/blob/master/src/core/be/tarsos/dsp/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
}}
请注意,即使使用相同的输入参数,也不能保证不同的库(例如 librosa)计算出相同的 MFCC。