首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.wav文件中的TarsosDSP基音检测。产生的频率总是小于一半

.wav文件中的TarsosDSP基音检测。产生的频率总是小于一半
EN

Stack Overflow用户
提问于 2015-07-12 14:48:02
回答 2查看 1.7K关注 0票数 0

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

这是我的代码。

代码语言:javascript
复制
    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/网站生成的,频率为329 The。我不知道为什么结果音高总是164.5赫兹。我的代码中有什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2016-04-05 00:22:34

我不知道你使用的是什么方法,但是通过观察频率是如何减半的,这可能是一个错误的采样率设置的问题?

大多数操作假设信号采样时的初始采样率,也许您已经将其作为参数(或其默认值)传递为该速率的一半?

票数 0
EN

Stack Overflow用户

发布于 2017-08-14 18:45:58

我在安卓系统上的TarsosDSP也遇到了同样的问题。对我来说,答案是来自http://onlinetonegenerator.com/的文件有32位样本,而不是默认的16位样本。相关代码:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31365172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档