File tree Expand file tree Collapse file tree
encoder/src/main/java/com/pedro/encoder/input/audio Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com.pedro.encoder.input.audio
22
3+ import kotlin.math.abs
4+
35class AudioUtils {
46
57 fun applyVolumeAndMix (
@@ -32,15 +34,16 @@ class AudioUtils {
3234 }
3335
3436 /* *
35- * assume always pcm 16bit
37+ * Calculate amplitude peaks. Assume always pcm 16bit
3638 * @return value from 0f to 100f
3739 */
3840 fun calculateAmplitude (buffer : ByteArray ): Float {
3941 if (buffer.size % 2 != 0 ) return 0f // invalid buffer
4042 var amplitude = 0
4143 for (i in buffer.indices step 2 ) {
42- val sample = ((buffer[i + 1 ].toInt() shl 8 ) or (buffer[i].toInt() and 0xFF ))
43- if (sample > amplitude) amplitude = sample
44+ val sample = ((buffer[i + 1 ].toInt() shl 8 ) or (buffer[i].toInt() and 0xFF )).toShort().toInt()
45+ val sampleAmplitude = abs(sample)
46+ if (sampleAmplitude > amplitude) amplitude = sampleAmplitude
4447 }
4548 return (amplitude / Short .MAX_VALUE .toFloat()) * 100
4649 }
You can’t perform that action at this time.
0 commit comments