使用TARSOS DSP从WavFiles JAVA中提取MFCC的难题

时间:2019-06-24 18:20:11

标签: java wav mfcc dtw tarsosdsp

在使用DTW计算它们之间的距离之前,我试图使用TARSOS DSP库从wav文件中提取MFCC值。

不幸的是,我难以理解如何在wav文件中使用MFCC类的代码。

我不确定是否需要先将wav文件转换为某种数组缓冲区。

请在此链接上从库中获取MFCC类的代码。

https://github.com/JorenSix/TarsosDSP/blob/master/src/core/be/tarsos/dsp/mfcc/MFCC.java

如果我能获得有关如何正确使用此代码从wav文件中获取MFCC值的建议,或者对其他方法的建议,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是示例代码,应该可以处理小文件。它将整个.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。