-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPitchDetect.java
196 lines (179 loc) · 6.13 KB
/
FPitchDetect.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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.*;
public class FPitchDetect extends Compute {
private double[] spectrum, noise;
private int key;
private ArrayList<Integer> peaks;
private boolean recording, calibrating;
private long calibStartTime;
private double calibIndex;
private JButton recordBtn, calibrateBtn, saveBtn;
public FPitchDetect(String p) {
super(p);
peaks = new ArrayList<>();
spectrum = new double[AudioStream.SR / 8];
noise = new double[spectrum.length];
}
public void initializeButtonPanel() {
super.initializeButtonPanel();
calibrateBtn = new JButton("calibrate");
calibrateBtn.setBackground(Color.LIGHT_GRAY);
calibrateBtn.addActionListener(this);
super.addBtn(calibrateBtn);
recordBtn = new JButton("record");
recordBtn.setBackground(Color.LIGHT_GRAY);
recordBtn.addActionListener(this);
super.addBtn(recordBtn);
saveBtn = new JButton("save & exit");
saveBtn.setBackground(Color.LIGHT_GRAY);
saveBtn.addActionListener(this);
super.addBtn(saveBtn);
}
public int compute(byte[] sample) {
calcSpectrum(sample);
if (calibrating) {
calcNoise();
calibIndex++;
if (System.nanoTime() - calibStartTime > 2e9) {
calibrating = false;
}
}
if (recording) {
peaks = detectPeak(500);
key = calcKey(super.getKeys());
return key;
}
return NULL_KEY;
}
public void calibrate() {
calibStartTime = System.nanoTime();
calibrating = true;
calibIndex = 0;
noise = new double[spectrum.length];
}
private void calcSpectrum(byte[] sample) {
Complex[] waveC = new Complex[sample.length / 2];
for (int i = 0; i < waveC.length; i++) {
double frame = (sample[i * 2 + 1] << 8) + (sample[i * 2] & 0xFF);
frame *= 0.5 - 0.5 * Math.cos(2 * Math.PI * i / waveC.length);
waveC[i] = new Complex(frame, 0);
}
Complex[] spectrumC = Fourier.czt(waveC, AudioStream.F, AudioStream.SR);
spectrum = new double[spectrumC.length];
for (int i = 0; i < spectrum.length; i++) {
spectrum[i] = spectrumC[i].abs();
if (!calibrating) {
spectrum[i] -= noise[i];
}
}
}
private void calcNoise() {
for (int j = 0; j < spectrum.length; j++) {
noise[j] *= calibIndex / (calibIndex + 1);
noise[j] += spectrum[j] / (calibIndex + 1);
}
}
private ArrayList<Integer> detectPeak(int width) {
ArrayList<Integer> peaks = new ArrayList<>();
for (int i = 0; i < spectrum.length; i++) {
int j = 0;
while (true) {
double a = spectrum[i], b = spectrum[i];
if (i - j >= 0) {
a = spectrum[i - j];
}
if (i + j < spectrum.length) {
b = spectrum[i + j];
}
if (i - j < 0 && i + j > spectrum.length) {
break;
}
if (a > spectrum[i] || b > spectrum[i]) {
break;
}
j++;
}
if (j >= width && spectrum[i] >= 800000 && i > 200) {
peaks.add(i);
}
}
return peaks;
}
private int calcKey(double[] keys) {
double pitch;
try {
pitch = peaks.get(0) / 6.0;
} catch (IndexOutOfBoundsException e) {
return BLANK_KEY;
}
int lo = 0;
int hi = keys.length - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
assert (mid < hi);
double d1 = Math.abs(keys[mid] - pitch);
double d2 = Math.abs(keys[mid + 1] - pitch);
if (d2 <= d1) {
lo = mid + 1;
} else {
hi = mid;
}
}
return hi;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "calibrate") {
if (recording) {
JOptionPane.showMessageDialog(this, "Recording.");
} else {
calibrate();
}
}
if (e.getActionCommand() == "record") {
if (calibrating) {
JOptionPane.showMessageDialog(this, "Calibrating.");
} else {
recording = true;
((JButton)e.getSource()).setText("pause");
}
}
if (e.getActionCommand() == "pause") {
recording = false;
((JButton)e.getSource()).setText("record");
}
if (e.getActionCommand() == "save & exit") {
super.save();
System.exit(0);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (calibrating) {
g.setColor(Color.WHITE);
int[] x = new int[1000];
int[] y = new int[1000];
for (int i = 0; i < 1000; i++) {
x[i] = i;
y[i] = 500 - (int)(noise[i * 6] / 20000);
}
g.drawPolyline(x, y, x.length);
}
if (recording) {
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 32));
if (key != BLANK_KEY && key != NULL_KEY) {
g.drawString(super.getKeyNames()[key], 500, 200);
}
for (int i = 0; i < 1000; i++) {
g.fillRect(i, 500, 1, -(int)(spectrum[i * 6] / 20000));
}
g.setColor(Color.RED);
for (Integer i : (ArrayList<Integer>)peaks.clone()) {
g.fillOval(i / 6 - 4, 500 - (int)(spectrum[i] / 20000) - 4, 8, 8);
}
}
repaint();
}
}