java判断是否为音频_判断音频中静音的代码(没测试)

/** _______ _____ _____ _____

* |__ __| | __ \ / ____| __ \

* | | __ _ _ __ ___ ___ ___| | | | (___ | |__) |

* | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/

* | | (_| | | \__ \ (_) \__ \ |__| |____) | |

* |_|\__,_|_| |___/\___/|___/_____/|_____/|_|

*

* -------------------------------------------------------------

*

* 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() {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值