2015-07-12 7 views
0

我正在嘗試使用TarsosDSP庫檢測.wav文件中的音高,並且頻率結果始終小於一半。來自.wav文件的TarsosDSP Pitch Detection。並且結果頻率始終低於一半

這是我的代碼。

public class Main { 

public static void main(String[] args){ 
    try{ 
     float sampleRate = 44100; 
     int audioBufferSize = 2048; 
     int bufferOverlap = 0; 

     //Create an AudioInputStream from my .wav file 
     URL soundURL = Main.class.getResource("/DetectPicthFromWav/329.wav"); 
     AudioInputStream stream = AudioSystem.getAudioInputStream(soundURL); 

     //Convert into TarsosDSP API 
     JVMAudioInputStream audioStream = new JVMAudioInputStream(stream); 
     AudioDispatcher dispatcher = new AudioDispatcher(audioStream, audioBufferSize, bufferOverlap); 
     MyPitchDetector myPitchDetector = new MyPitchDetector(); 
     dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.YIN, sampleRate, audioBufferSize, myPitchDetector)); 
     dispatcher.run(); 


    } 
    catch(FileNotFoundException fne){fne.printStackTrace();} 
    catch(UnsupportedAudioFileException uafe){uafe.printStackTrace();} 
    catch(IOException ie){ie.printStackTrace();} 
} 
} 

    class MyPitchDetector implements PitchDetectionHandler{ 

//Here the result of pitch is always less than half. 
@Override 
public void handlePitch(PitchDetectionResult pitchDetectionResult, 
     AudioEvent audioEvent) { 
    if(pitchDetectionResult.getPitch() != -1){ 
     double timeStamp = audioEvent.getTimeStamp(); 
     float pitch = pitchDetectionResult.getPitch(); 
     float probability = pitchDetectionResult.getProbability(); 
     double rms = audioEvent.getRMS() * 100; 
     String message = String.format("Pitch detected at %.2fs: %.2fHz (%.2f probability, RMS: %.5f)\n", timeStamp,pitch,probability,rms); 
     System.out.println(message); 
    } 
} 
} 

329.wav文件從http://onlinetonegenerator.com/網站生成329Hz。 我不知道爲什麼結果音高始終是164.5Hz。我的代碼有問題嗎?

+1

這是一個八度的誤差,這種實現尹基音檢測導致此問題,我從來沒有一次看到更深的尹代碼,但是從原來的紙一些步驟被遺忘了,我做了一個跟蹤Tarsos的AMDF代碼,你可以使用'PitchEstimationAlgorithm.AMDF'來測試他 – ederwander

回答

0

嗯,我不知道你使用的是什麼方法,但通過查看頻率如何精確地減半,這可能是一個錯誤的採樣率設置的問題?

當信號被採樣時,大多數操作都假定一個初始採樣率,也許你已經把它作爲參數傳遞(或者它的默認值)是它的一半?

0

我剛剛在Android上遇到與TarsosDSP相同的問題。對我來說,答案是從http://onlinetonegenerator.com/的文件有32位樣本,而不是16位,這似乎是默認值。相關代碼:

AssetFileDescriptor afd = getAssets().openFd("440.wav"); // 440Hz sine wave 
InputStream is = afd.createInputStream(); 
TarsosDSPAudioFormat audioFormat = new TarsosDSPAudioFormat(
    /* sample rate */ 44100, 
    /* HERE sample size in bits */ 32, 
    /* number of channels */ 1, 
    /* signed/unsigned data */ true, 
    /* big-endian byte order */ false 
); 
AudioDispatcher dispatcher = new AudioDispatcher(new UniversalAudioInputStream(is, audioFormat), 2048, 0); 
PitchDetectionHandler pdh = ... 
AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 44100, 2048, pdh); 
dispatcher.addAudioProcessor(p); 
new Thread(dispatcher, "Audio Dispatcher").start();