【问题标题】:TarsosDSP Pitch Detection from .wav file. And the result frequency is always less than half来自 .wav 文件的 TarsosDSP 音高检测。结果频率总是不到一半
【发布时间】:2015-07-12 06:48:02
【问题描述】:

我正在尝试使用 TarsosDSP 库从 .wav 文件中检测音高,但频率的结果总是小于一半。

这是我的代码。

    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。我的代码有问题吗?

【问题讨论】:

  • 这是一个 octave 错误,这个 YIN Pitch detection 的实现导致了这个问题,我没有时间更深入地看到 YIN 代码,但是原始论文中的一些步骤被忘记了,我做了一个音调跟踪 Tarsos 的 AMDF 代码,您可以使用 PitchEstimationAlgorithm.AMDF 对其进行测试

标签: audio pitch-detection tarsosdsp


【解决方案1】:

好吧,我不知道您使用的是什么方法,但是通过查看频率是如何精确减半的,这可能是设置错误的采样率的问题?

大多数操作在对信号进行采样时假定初始采样率,也许您已将其作为参数(或其默认值)传递了一半?

【讨论】:

    【解决方案2】:

    我刚刚在 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2010-09-06
      • 2023-04-07
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多