0

I am using the tarsos DSP java API. Can someone tell me why the float pitchInHz does not get updated by the method freq() in the following code? Can someone tell me what to change to get it to work because I cant see why its not.Thanks

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();
            }
        }
    });
}

}

G. Mcc
  • 1
  • 1

1 Answers1

0

Presumably because pitchInHz = result.getPitch(); has not been executed by the time you execute pitch.getPitch(). This is entirely possible since that piece of code is concurrently executed in a new Thread and pitch.getPitch() is called immediately after that thread is started, not giving the library any time to execute the callback.

The method returned 0 since float pitchInHz; is the same as float pitchInHz = 0.0f;

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • How would I change calling the methods to ensure that pitchInHz does get changed before printing its value? – G. Mcc Mar 29 '16 at 13:08
  • @G.Mcc Instead of creating a thread (`new Thread(dispatcher,...).start()`) call the dispatcher directly `dispatcher.run()`. Although I do not know if that will ever return since I don't know the library. Or do whatever you want to do in the callback instead of passing it through a variable. – Kiskae Mar 29 '16 at 15:10
  • Ye I tried that before and it just sets the value to -1 which is the default if no pitch is detected so it never does run. I'll hopefully solve it. Thanks for your help – G. Mcc Mar 29 '16 at 18:21