-
Notifications
You must be signed in to change notification settings - Fork 1
/
TuneHighE.java
167 lines (148 loc) · 5.32 KB
/
TuneHighE.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.awt.EventQueue;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
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;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TuneHighE {
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;
JFrame frame;
static AudioDispatcher adp;
/**
* Launch the application.
*/
public static void NewScreen() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TuneHighE window = new TuneHighE();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @throws LineUnavailableException
*/
public TuneHighE() throws LineUnavailableException {
final JTextPane textPane =initialize();
//tunee(textPane);
JButton btnReturn = new JButton("Return");
btnReturn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
adp.stop();
frame.dispose();
try {
new Guituner();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.dispose();
}
});
btnReturn.setBounds(27, 215, 97, 25);
frame.getContentPane().add(btnReturn);
JButton btnTune = new JButton("Tune");
btnTune.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//tuneE(textPane);
textPane.setText("tuning");
//TuneThread thread = new TuneThread(textPane);
//thread.start();
new Thread(new Runnable(){
public void run(){
try {
tunee(textPane);
//System.out.println("fnr");
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
btnTune.setBounds(146, 57, 97, 25);
frame.getContentPane().add(btnTune);
}
/**
* Initialize the contents of the frame.
* @throws LineUnavailableException
*/
private static void tunee(final JTextPane textPane) 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>(B3+E4)/2){
if(pitch<E4-(E4*.01)){
//System.out.println("tune high E string up: frequency is: "+pitch+", should be: "+E4);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textPane.setText("tune high E string up: frequency is: "+pitch+", should be: "+E4);
}
});
}
if(pitch>(E4*.01)+E4){
//System.out.println("tune high E string down: frequency is: "+pitch+", should be: "+E4);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textPane.setText("tune high E string down: frequency is: "+pitch+", should be: "+E4);
}
});
}
if(pitch<(E4*.01)+E4&&pitch>E4-(E4*.01)){
//System.out.println("high E string is tuned");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textPane.setText("high E string is tuned");
}
});
}
//}
}
//Handle pitch here, tell user to tune up or tune down
}
};
adp = AudioDispatcherFactory.fromDefaultMicrophone(2048, 0);
adp.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.YIN, 44100, 2048, handler));
adp.run();
}
private JTextPane initialize() throws LineUnavailableException {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblTuningLowE = new JLabel("Tuning High E String:");
lblTuningLowE.setBounds(125, 13, 186, 16);
frame.getContentPane().add(lblTuningLowE);
final JTextPane textPane = new JTextPane();
textPane.setBounds(27, 95, 380, 73);
frame.getContentPane().add(textPane);
return textPane;
}
}