-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTuneThread.java
97 lines (85 loc) · 3.61 KB
/
TuneThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import javax.sound.sampled.LineUnavailableException;
import javax.swing.JTextPane;
import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;
import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm;
public class TuneThread extends Thread{
static float G4= (float) 391.9954;//4
static float C4= (float) 261.6256;//1
static float E4= (float) 329.6279;//2
static float A4= (float) 440.0000;//3
static float E2= (float) 82.4069;
static float A2= (float) 110.000;
static float D3= (float) 146.8324;
static float G3= (float) 195.9977;
static float B3= (float) 246.9417;
static JTextPane textPane;
public TuneThread(JTextPane textPane){
this.textPane=textPane;
}
public void run(){
try {
tuneE();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void tuneE() throws LineUnavailableException{
PitchDetectionHandler handler = new PitchDetectionHandler() {
public void handlePitch(PitchDetectionResult pitchDetectionResult,
AudioEvent audioEvent) {
if (pitchDetectionResult.getPitch()>70.0){
//System.out.println(audioEvent.getTimeStamp() + " " +pitchDetectionResult.getPitch());
final float pitch=pitchDetectionResult.getPitch();
if(pitch<(E2+A2)/2){
if(pitch<E2-(E2*.01)){
System.out.println("tune low E string up: frequency is: "+pitch+", should be: "+E2);
textPane.setText("tune low E string up: frequency is: "+pitch+", should be: "+E2);
//SwingUtilities
}
if(pitch>(E2*.01)+E2){
System.out.println("tune low E string down: frequency is: "+pitch+", should be: "+E2);
textPane.setText("tune low E string down: frequency is: "+pitch+", should be: "+E2);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
textPane.setText("tune low E string up: frequency is: "+pitch+", should be: "+E2);
}
});
}
if(pitch<(E2*.01)+E2&&pitch>E2-(E2*.01)){
System.out.println("low E string is tuned");
textPane.setText("low E string is tuned");
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
textPane.setText("tune low E string up: frequency is: "+pitch+", should be: "+E2);
}
});
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return;
}
//Handle pitch here, tell user to tune up or tune down
}
};
AudioDispatcher adp = AudioDispatcherFactory.fromDefaultMicrophone(2048, 0);
adp.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.YIN, 44100, 2048, handler));
adp.run();
}
}