-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.java
260 lines (237 loc) · 10.2 KB
/
driver.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//import javax.sound.sampled.AudioFormat;
//import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
//import javax.sound.sampled.TargetDataLine;
import javax.swing.JTextPane;
import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
//import be.tarsos.dsp.io.TarsosDSPAudioFormat;
import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.*;
import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm;
public class driver {
//pitches of the Ukulele Strings.
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 A2s= (float) 116.5409;
static float B2= (float) 123.4708;
static float C3= (float) 130.8128;
static float C3s=(float) 138.5913;
static float D3= (float) 146.8324;
static float D3s= (float) 155.5635;
static float E3= (float) 164.8138;
static float F3= (float) 174.6141;
static float F3s= (float) 184.9972;
static float G3= (float) 195.9977;
static float G3s= (float) 207.6523;
static float A3 = (float) 220.0000;
static float A3s= (float) 233.0819;
static float B3= (float) 246.9417;
static float C4s= (float) 277.1826;
static float D4= (float) 293.6648;
static float D4s= (float) 311.1270;
static float F4 = (float) 349.2282;
static float F4s= (float) 369.9944;
static float G4s = (float) 415.3047;
static float A4s= (float) 466.1638;
static float B4= (float) 493.8833;
static float C5= (float) 523.2511;
static float C5s= (float) 554.3652;
static float D5= (float) 587.329;
static float D5s= (float) 622.2540;
static float E5= (float) 659.2551;
static float F5= (float) 698.4564;
static float F5s=(float)739.9888;
static float G5=(float)783.9908;
static float G5s=(float)830.6093;
static float A5=(float)880.0000;
static float A5s=(float)932.3275;
static float B5=(float)987.7666;
static float C6=(float)1046.5023;
static float C6s=(float)1108.7304;
static float D6=(float)1174.6590;
//static float E4= (float) 329.6276;
public static void main(String[] args) throws LineUnavailableException {
//tuneUke();
tuneGuitar();
}
public static void tuneUke() 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());
float pitch=pitchDetectionResult.getPitch();
if(pitch<(G4+A4)/2&&pitch>(E4+G4)/2){
if(pitch<G4-(G4*.01)){
System.out.println("tune G string up: frequency is: "+pitch+", should be: "+G4);
}
if(pitch>(G4*.01)+G4){
System.out.println("tune G string down: frequency is: "+pitch+", should be: "+G4);
}
if(pitch<(G4*.01)+G4&&pitch>G4-(G4*.01)){
System.out.println("G string is tuned");
}
}
if (pitch<(C4+E4)/2){
if(pitch<C4-(C4*.01)){
System.out.println("tune C string up: frequency is: "+pitch+", should be: "+C4);
}
if(pitch>(C4*.01)+C4){
System.out.println("tune C string down: frequency is: "+pitch+", should be: "+C4);
}
if(pitch<(C4*.01)+C4&&pitch>C4-(C4*.01)){
System.out.println("C string is tuned");
}
}
if (pitch<(E4+G4)/2&&pitch>(C4+E4)/2){
if(pitch<E4-(E4*.01)){
System.out.println("tune E string up: frequency is: "+pitch+", should be: "+E4);
}
if(pitch>(E4*.01)+E4){
System.out.println("tune E string down: frequency is: "+pitch+", should be: "+E4);
}
if(pitch<(E4*.01)+E4&&pitch>E4-(E4*.01)){
System.out.println("E string is tuned");
}
}
if(pitch>(G4+A4)/2){
if(pitch<A4-(A4*.01)){
System.out.println("tune A string up: frequency is: "+pitch+", should be: "+A4);
}
if(pitch>(A4*.01)+A4){
System.out.println("tune A string down: frequency is: "+pitch+", should be: "+A4);
}
if(pitch<(A4*.01)+A4&&pitch>A4-(A4*.01)){
System.out.println("A string is tuned");
}
//if(atuned==true){
//System.out.println("instrument is tuned");
//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();
}
public static void tuneGuitar() 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());
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);
}
if(pitch>(E2*.01)+E2){
System.out.println("tune low E string down: frequency is: "+pitch+", should be: "+E2);
}
if(pitch<(E2*.01)+E2&&pitch>E2-(E2*.01)){
System.out.println("low E string is tuned");
}
}
if (pitch>=(A2+E2)/2&&pitch<(A2+D3)/2){
if(pitch<A2-(A2*.01)){
System.out.println("tune A string up: frequency is: "+pitch+", should be: "+A2);
}
if(pitch>(A2*.01)+A2){
System.out.println("tune A string down: frequency is: "+pitch+", should be: "+A2);
}
if(pitch<(A2*.01)+A2&&pitch>A2-(A2*.01)){
System.out.println("A string is tuned");
}
}
if (pitch<(D3+G3)/2&&pitch>=(A2+D3)/2){
if(pitch<D3-(D3*.01)){
System.out.println("tune D string up: frequency is: "+pitch+", should be: "+D3);
}
if(pitch>(D3*.01)+D3){
System.out.println("tune D string down: frequency is: "+pitch+", should be: "+D3);
}
if(pitch<(D3*.01)+D3&&pitch>D3-(D3*.01)){
System.out.println("D string is tuned");
}
}
if(pitch<(B3+G3)/2&&pitch>=(G3+D3)/2){
if(pitch<G3-(G3*.01)){
System.out.println("tune G string up: frequency is: "+pitch+", should be: "+G3);
}
if(pitch>(G3*.01)+G3){
System.out.println("tune G string down: frequency is: "+pitch+", should be: "+G3);
}
if(pitch<(G3*.01)+G3&&pitch>G3-(G3*.01)){
System.out.println("G string is tuned");
}
//if(atuned==true){
//System.out.println("instrument is tuned");
//return;
//}
}
if (pitch<(E4+B3)/2&&pitch>=(B3+G3)/2){
if(pitch<B3-(B3*.01)){
System.out.println("tune B string up: frequency is: "+pitch+", should be: "+B3);
}
if(pitch>(B3*.01)+B3){
System.out.println("tune B string down: frequency is: "+pitch+", should be: "+B3);
}
if(pitch<(B3*.01)+B3&&pitch>B3-(B3*.01)){
System.out.println("B string is tuned");
}
}
if(pitch>(B3+E4)/2){
if(pitch<E4-(E4*.01)){
System.out.println("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);
}
if(pitch<(E4*.01)+E4&&pitch>E4-(E4*.01)){
System.out.println("high E string is tuned");
}
}
}
//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();
}
public static void tuneLowE(final JTextPane txtpnTestText) 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());
float pitch=pitchDetectionResult.getPitch();
if(pitch<(E2+A2)/2){
if(pitch<E2-(E2*.01)){
txtpnTestText.setText("tune low E string up: frequency is: "+pitch+", should be: "+E2);
}
if(pitch>(E2*.01)+E2){
txtpnTestText.setText("tune low E string down: frequency is: "+pitch+", should be: "+E2);
}
if(pitch<(E2*.01)+E2&&pitch>E2-(E2*.01)){
txtpnTestText.setText("low E string is tuned");
}
}
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();
}
}