0

我正在使用 tarsos DSP java API。有人能告诉我为什么 float pitchInHz 没有通过以下代码中的方法 freq() 得到更新吗?有人能告诉我要改变什么才能让它工作吗,因为我不明白为什么它不是。谢谢

public class trial extends JFrame{
File f = new File("RecordAudio.wav");
static JLabel lblNewLabel = new JLabel("New label");
float pitchInHz;
public trial(){
    getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
    getContentPane().add(lblNewLabel);
    run();
    freq();
    float values = freq();
    System.out.print(values);

}
public void run(){
    AudioDispatcher dispatcher = null;
    try {
        dispatcher = AudioDispatcherFactory.fromFile(f, 1024, 0);
    } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    AudioProcessor p = new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, 48000, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    new Thread(dispatcher,"Audio Dispatcher").start();


}
public float freq(){
    return pitchInHz;
}

PitchDetectionHandler pdh = new PitchDetectionHandler() {

    public void handlePitch(PitchDetectionResult result,AudioEvent e) {

                float pitchInHz = result.getPitch();
                lblNewLabel.setText("" + pitchInHz);
                //System.out.print(pitchInHz);  
    }

};


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                trial frame = new trial();
                frame.setVisible(true);
                frame.setSize(500, 500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

4

1 回答 1

0

大概是因为pitchInHz = result.getPitch();你执行的时候还没有执行pitch.getPitch()。这是完全可能的,因为这段代码在一个新线程中同时执行,并pitch.getPitch()在该线程启动后立即调用,而不给库任何时间来执行回调。

该方法返回 0 因为float pitchInHz;float pitchInHz = 0.0f;

于 2016-03-29T11:59:39.553 回答