Skip to content

Commit 10063c9

Browse files
committed
Perform refactoring: proper names, avoid extra new, extract constants etc
1 parent 004365b commit 10063c9

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

sound-recorder-n-spectrum-analyzer/src/main/java/by/andd3dfx/capturesound/ShowRealTimeSpectrumApp.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
import javax.sound.sampled.LineUnavailableException;
1313
import javax.sound.sampled.TargetDataLine;
1414
import java.io.ByteArrayOutputStream;
15+
import java.util.Arrays;
1516

1617
/**
1718
* Application that shows real-time spectrum
1819
*/
1920
public class ShowRealTimeSpectrumApp {
2021

22+
private final static int BUCKETS_AMOUNT = 1_000;
23+
2124
public static void main(String[] args) throws LineUnavailableException {
2225
FrequencyScanner frequencyScanner = new FrequencyScanner();
23-
AudioFormat audioFormat = getAudioFormat();
26+
AudioFormat audioFormat = buildAudioFormat();
2427
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
2528

2629
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@@ -31,6 +34,8 @@ public static void main(String[] args) throws LineUnavailableException {
3134
chartContainer.show();
3235

3336
byte tempBuffer[] = new byte[10_000];
37+
double[] xData = new double[BUCKETS_AMOUNT];
38+
double[] yData = new double[BUCKETS_AMOUNT];
3439
while (true) {
3540
targetDataLine.start();
3641
int count = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
@@ -45,24 +50,25 @@ public static void main(String[] args) throws LineUnavailableException {
4550
double[] frequencies = frequencyInfoContainer.frequencies();
4651
double[] magnitudes = frequencyInfoContainer.magnitudes();
4752

48-
int N = 1_000;
49-
double[] xData = new double[N];
50-
double[] yData = new double[N];
51-
for (int bucketIndex = 0; bucketIndex < N; bucketIndex++) {
52-
xData[bucketIndex] = frequencies[(int) ((bucketIndex + 0.5) * magnitudes.length / N)];
53+
Arrays.fill(xData, 0);
54+
Arrays.fill(yData, 0);
55+
final int magnitudesPerBucket = magnitudes.length / BUCKETS_AMOUNT;
56+
for (int bucket = 0; bucket < BUCKETS_AMOUNT; bucket++) {
57+
xData[bucket] = frequencies[(int) ((bucket + 0.5) * magnitudesPerBucket)];
5358

54-
for (int i = 0; i < magnitudes.length / N; i++) {
55-
yData[bucketIndex] += magnitudes[bucketIndex * magnitudes.length / N + i];
59+
for (int i = 0; i < magnitudesPerBucket; i++) {
60+
yData[bucket] += magnitudes[bucket * magnitudesPerBucket + i];
5661
}
5762
}
58-
var maxFrequency = frequencyInfoContainer.maxFrequency();
5963

64+
var maxFrequency = frequencyInfoContainer.maxFrequency();
6065
String title = String.format("%1$.0f Hz", maxFrequency);
66+
6167
chartContainer.update(xData, yData, title);
6268
}
6369
}
6470

65-
private static AudioFormat getAudioFormat() {
71+
private static AudioFormat buildAudioFormat() {
6672
return new AudioFormat(44_100.0F, 16, 1, true, false);
6773
}
6874

sound-recorder-n-spectrum-analyzer/src/main/java/by/andd3dfx/capturesound/fft/FrequencyScanner.java

+17-21
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*/
1414
public class FrequencyScanner {
1515

16-
private double[] windowFilter;
17-
1816
public FrequencyInfoContainer detectFrequency(byte[] audioData, int sampleRate) {
1917
short[] sampleData = new short[audioData.length / 2];
2018
ByteBuffer.wrap(audioData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(sampleData);
@@ -62,24 +60,7 @@ public FrequencyInfoContainer extractFrequencyInfo(short[] sampleData, int sampl
6260
}
6361

6462
/**
65-
* build a Hamming window filter for samples of a given size
66-
* <p>
67-
* See http://www.labbookpages.co.uk/audio/firWindowing.html#windows
68-
*
69-
* @param size the sample size for which the filter will be created
70-
*/
71-
private void buildHammingWindow(int size) {
72-
if (windowFilter != null && windowFilter.length == size) {
73-
return;
74-
}
75-
windowFilter = new double[size];
76-
for (int i = 0; i < size; ++i) {
77-
windowFilter[i] = .54 - .46 * Math.cos(2 * Math.PI * i / (size - 1.0));
78-
}
79-
}
80-
81-
/**
82-
* apply a Hamming window filter to raw input data
63+
* Apply a Hamming window filter to raw input data
8364
*
8465
* @param input an array containing unfiltered input data
8566
* @return a double array containing the filtered data
@@ -88,10 +69,25 @@ private double[] applyWindow(short[] input) {
8869
var len = input.length;
8970
double[] res = new double[len];
9071

91-
buildHammingWindow(len);
72+
double[] windowFilter = buildHammingWindow(len);
9273
for (int i = 0; i < len; ++i) {
9374
res[i] = (double) input[i] * windowFilter[i];
9475
}
9576
return res;
9677
}
78+
79+
/**
80+
* Build a Hamming window filter for samples of a given size
81+
* <p>
82+
* See <a href="http://www.labbookpages.co.uk/audio/firWindowing.html#windows">link</a> for details
83+
*
84+
* @param size the sample size for which the filter will be created
85+
*/
86+
private double[] buildHammingWindow(int size) {
87+
var res = new double[size];
88+
for (int i = 0; i < size; ++i) {
89+
res[i] = 0.54 - 0.46 * Math.cos(2 * Math.PI * i / (size - 1.0));
90+
}
91+
return res;
92+
}
9793
}

0 commit comments

Comments
 (0)