File tree 1 file changed +11
-5
lines changed
sound-recorder-n-spectrum-analyzer/src/main/java/by/andd3dfx/capturesound/fft
1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 13
13
*/
14
14
public class FrequencyScanner {
15
15
16
+ private double [] windowFilter = new double []{};
17
+
16
18
public FrequencyInfoContainer detectFrequency (byte [] audioData , int sampleRate ) {
17
19
short [] sampleData = new short [audioData .length / 2 ];
18
20
ByteBuffer .wrap (audioData ).order (ByteOrder .LITTLE_ENDIAN ).asShortBuffer ().get (sampleData );
@@ -69,7 +71,7 @@ private double[] applyWindow(short[] input) {
69
71
var len = input .length ;
70
72
double [] res = new double [len ];
71
73
72
- double [] windowFilter = buildHammingWindow (len );
74
+ rebuildHammingWindowIfNeeded (len );
73
75
for (int i = 0 ; i < len ; ++i ) {
74
76
res [i ] = (double ) input [i ] * windowFilter [i ];
75
77
}
@@ -83,11 +85,15 @@ private double[] applyWindow(short[] input) {
83
85
*
84
86
* @param size the sample size for which the filter will be created
85
87
*/
86
- private double [] buildHammingWindow (int size ) {
87
- var res = new double [size ];
88
+ private void rebuildHammingWindowIfNeeded (int size ) {
89
+ // Build new window only if requested size differs from size of existing window
90
+ if (windowFilter .length == size ) {
91
+ return ;
92
+ }
93
+
94
+ windowFilter = new double [size ];
88
95
for (int i = 0 ; i < size ; ++i ) {
89
- res [i ] = 0.54 - 0.46 * Math .cos (2 * Math .PI * i / (size - 1.0 ));
96
+ windowFilter [i ] = 0.54 - 0.46 * Math .cos (2 * Math .PI * i / (size - 1.0 ));
90
97
}
91
- return res ;
92
98
}
93
99
}
You can’t perform that action at this time.
0 commit comments