我使用的是tarsos DSP java API。有人可以告訴我爲什麼float pitch不會在下面的代碼中被freq()方法更新嗎?誰能告訴我什麼改變,以得到它的工作,因爲我看不出爲什麼它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();
}
}
});
}
}
簡短的回答是,'handlePitch'不會被調用,或者是,結果居然是0 –