/** _______ _____ _____ _____
* |__ __| | __ \ / ____| __ \
* | | __ _ _ __ ___ ___ ___| | | | (___ | |__) |
* | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/
* | | (_| | | \__ \ (_) \__ \ |__| |____) | |
* |_|\__,_|_| |___/\___/|___/_____/|_____/|_|
*
* -------------------------------------------------------------
*
* TarsosDSP is developed by Joren Six at IPEM, University Ghent
*
* -------------------------------------------------------------
*
* Info:http://0110.be/tag/TarsosDSP* Github:https://github.com/JorenSix/TarsosDSP* Releases:http://0110.be/releases/TarsosDSP/*
* TarsosDSP includes modified source code by various authors,
* for credits and info, see README.
**/
packagebe.tarsos.dsp;/*** The continuing silence detector does not break the audio processing pipeline when silence is detected.*/
public class SilenceDetector implementsAudioProcessor {public static final double DEFAULT_SILENCE_THRESHOLD = -70.0;//db
private final double threshold;//db
private final booleanbreakProcessingQueueOnSilence;/*** Create a new silence detector with a default threshold.*/
publicSilenceDetector(){this(DEFAULT_SILENCE_THRESHOLD,false);
}/*** Create a new silence detector with a defined threshold.
*
*@paramsilenceThreshold
* The threshold which defines when a buffer is silent (in dB).
* Normal values are [-70.0,-30.0] dB SPL.
*@parambreakProcessingQueueOnSilence*/
public SilenceDetector(final double silenceThreshold,booleanbreakProcessingQueueOnSilence){this.threshold =silenceThreshold;this.breakProcessingQueueOnSilence =breakProcessingQueueOnSilence;
}/*** Calculates the local (linear) energy of an audio buffer.
*
*@parambuffer
* The audio buffer.
*@returnThe local (linear) energy of an audio buffer.*/
private double localEnergy(final float[] buffer) {double power = 0.0D;for (floatelement : buffer) {
power+= element *element;
}returnpower;
}/*** Returns the dBSPL for a buffer.
*
*@parambuffer
* The buffer with audio information.
*@returnThe dBSPL level for the buffer.*/
private double soundPressureLevel(final float[] buffer) {double value = Math.pow(localEnergy(buffer), 0.5);
value= value /buffer.length;returnlinearToDecibel(value);
}/*** Converts a linear to a dB value.
*
*@paramvalue
* The value to convert.
*@returnThe converted value.*/
private double linearToDecibel(final doublevalue) {return 20.0 *Math.log10(value);
}double currentSPL = 0;public doublecurrentSPL(){returncurrentSPL;
}/*** Checks if the dBSPL level in the buffer falls below a certain threshold.
*
*@parambuffer
* The buffer with audio information.
*@paramsilenceThreshold
* The threshold in dBSPL
*@returnTrue if the audio information in buffer corresponds with silence,
* false otherwise.*/
public boolean isSilence(final float[] buffer, final doublesilenceThreshold) {
currentSPL=soundPressureLevel(buffer);return currentSPL
}public boolean isSilence(final float[] buffer) {returnisSilence(buffer, threshold);
}
@Overridepublic booleanprocess(AudioEvent audioEvent) {boolean isSilence =isSilence(audioEvent.getFloatBuffer());//break processing chain on silence?
if(breakProcessingQueueOnSilence){//break if silent
return !isSilence;
}else{//never break the chain
return true;
}
}
@Overridepublic voidprocessingFinished() {
}
}