commit ba61264bf4ae9a7deb465d4f514a5693280d1b16
parent d6bedb0265b7f3d6f91962cf145414315dcd22e6
Author: TacoTheDank <SkytkRSfan3895@gmail.com>
Date: Sun, 14 Jun 2020 16:20:13 -0400
Update TarsosDSP to d958352
Diffstat:
3 files changed, 22 insertions(+), 36 deletions(-)
diff --git a/app/src/main/java/be/tarsos/dsp/AudioEvent.java b/app/src/main/java/be/tarsos/dsp/AudioEvent.java
@@ -203,25 +203,9 @@ public class AudioEvent {
* The buffer with audio information.
* @return The dBSPL level for the buffer.
*/
- private double soundPressureLevel(final float[] buffer) {
- double value = Math.pow(localEnergy(buffer), 0.5);
- value = value / buffer.length;
- return linearToDecibel(value);
- }
-
- /**
- * Calculates the local (linear) energy of an audio buffer.
- *
- * @param buffer
- * The audio buffer.
- * @return The local (linear) energy of an audio buffer.
- */
- private double localEnergy(final float[] buffer) {
- double power = 0.0D;
- for (float element : buffer) {
- power += element * element;
- }
- return power;
+ private static double soundPressureLevel(final float[] buffer) {
+ double rms = calculateRMS(buffer);
+ return linearToDecibel(rms);
}
/**
@@ -231,7 +215,7 @@ public class AudioEvent {
* The value to convert.
* @return The converted value.
*/
- private double linearToDecibel(final double value) {
+ private static double linearToDecibel(final double value) {
return 20.0 * Math.log10(value);
}
diff --git a/app/src/main/java/be/tarsos/dsp/SilenceDetector.java b/app/src/main/java/be/tarsos/dsp/SilenceDetector.java
@@ -57,18 +57,21 @@ public class SilenceDetector implements AudioProcessor {
}
/**
- * Calculates the local (linear) energy of an audio buffer.
- *
- * @param buffer
- * The audio buffer.
- * @return The local (linear) energy of an audio buffer.
+ * Calculates and returns the root mean square of the signal. Please
+ * cache the result since it is calculated every time.
+ * @param floatBuffer The audio buffer to calculate the RMS for.
+ * @return The <a
+ * href="http://en.wikipedia.org/wiki/Root_mean_square">RMS</a> of
+ * the signal present in the current buffer.
*/
- private double localEnergy(final float[] buffer) {
- double power = 0.0D;
- for (float element : buffer) {
- power += element * element;
+ public static double calculateRMS(float[] floatBuffer){
+ double rms = 0.0;
+ for (int i = 0; i < floatBuffer.length; i++) {
+ rms += floatBuffer[i] * floatBuffer[i];
}
- return power;
+ rms = rms / Double.valueOf(floatBuffer.length);
+ rms = Math.sqrt(rms);
+ return rms;
}
/**
@@ -78,10 +81,9 @@ public class SilenceDetector implements AudioProcessor {
* The buffer with audio information.
* @return The dBSPL level for the buffer.
*/
- private double soundPressureLevel(final float[] buffer) {
- double value = Math.pow(localEnergy(buffer), 0.5);
- value = value / buffer.length;
- return linearToDecibel(value);
+ private static double soundPressureLevel(final float[] buffer) {
+ double rms = calculateRMS(buffer);
+ return linearToDecibel(rms);
}
/**
@@ -91,7 +93,7 @@ public class SilenceDetector implements AudioProcessor {
* The value to convert.
* @return The converted value.
*/
- private double linearToDecibel(final double value) {
+ private static double linearToDecibel(final double value) {
return 20.0 * Math.log10(value);
}
diff --git a/app/src/main/java/be/tarsos/dsp/mfcc/MFCC.java b/app/src/main/java/be/tarsos/dsp/mfcc/MFCC.java
@@ -127,7 +127,7 @@ public class MFCC implements AudioProcessor {
//Calculates te centerfrequencies.
for (int i = 1; i <= amountOfMelFilters; i++) {
float fc = (inverseMel(mel[0] + factor * i) / sampleRate) * samplesPerFrame;
- centerFrequencies[i - 1] = Math.round(fc);
+ centerFrequencies[i] = Math.round(fc);
}
}