This example shares the specific code for Android to implement mute detection for your reference. The specific content is as follows
1. Background
When doing voice evaluation, you need to automatically stop when the user is not speaking. At this time, you need to determine when not speaking is in a silent state.
II. Principle
Each time you record, you can calculate the tone strength based on the recorded data, set a tone strength value to the upper limit, and judge it as non-silent when the tone strength exceeds this value. When it is less than this value, it is judged as mute. When the number of mutes reaches a certain number of times in succession, the recording stops.
3. Code implementation
1. Calculate the sound strength
/** * Calculate volume * * @param buffer Recording data * @return */ public static double calculateVolume(byte[] buffer) { double sumVolume = 0.0; double avgVolume = 0.0; double volume = 0.0; for (int i = 0; i < ; i += 2) { int v1 = buffer[i] & 0xFF; int v2 = buffer[i + 1] & 0xFF; int temp = v1 + (v2 << 8); if (temp >= 0x8000) { temp = 0xffff - temp; } sumVolume += (temp); } avgVolume = sumVolume / / 2; volume = Math.log10(1 + avgVolume) * 10; return volume; }
2. Silent detection
class VadManager : VadInterface { /** * vad configuration */ private var mConfig: Config? = null /** * Number of times recording and data collection */ private var mCount = 0 /** * Number of mutes at the front end point */ private var mBeginMuteCount = 0 /** * Maximum number of muted front endpoints */ private var mBeginMuteMaxCount = 9 /** * Number of mutes on the back endpoint */ private var mEndMuteCount = 0 /** * Maximum number of muted times of the back endpoint */ private var mEndMuteMaxCount = 9 companion object { /** * The recording time is 200 milliseconds */ const val RECORD_DATA_TIME = 200 /** * Silent value */ const val MUTE_VOLUME = 18.0 @Volatile private var instance: VadInterface? = null @JvmStatic fun getInstance(): VadInterface { if (instance == null) { synchronized(VadManager::) { if (instance == null) { instance = VadManager() } } } return instance!! } } override fun startVad(config: Config?) { mConfig = config mConfig?.let { if () { if ( > 0) { mBeginMuteMaxCount = / RECORD_DATA_TIME } if ( > 0) { mEndMuteMaxCount = / RECORD_DATA_TIME } } } } override fun canStopRecord(volume: Double): Boolean { ("volume", ()) mCount++ mConfig?.let { if (!) { return false } if (mCount <= mBeginMuteMaxCount) { //The front endpoint continuous mute count if (volume <= MUTE_VOLUME) { mBeginMuteCount++ } else { mBeginMuteCount = 0 } } else if (mCount == mBeginMuteMaxCount + 1) { //Judge whether the maximum number of mutes at the front end point is reached? if (mBeginMuteCount >= mBeginMuteMaxCount) { closeVad() return true } } else { //The back endpoint continuous mute count if (volume <= MUTE_VOLUME) { mEndMuteCount++ } else { mEndMuteCount = 0 } // If the maximum number of mutes is reached, stop recording if (mEndMuteCount >= mEndMuteMaxCount) { closeVad() return true } } return false } return false } private fun closeVad() { mConfig = null mCount = 0 mBeginMuteCount = 0 mBeginMuteMaxCount = 9 mEndMuteCount = 0 mEndMuteMaxCount = 9 } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.