Skip to content

Commit 6b29e1f

Browse files
committed
2 parents 37d68a3 + 69ee33a commit 6b29e1f

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

encoder/src/main/java/com/pedro/encoder/input/audio/AudioUtils.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pedro.encoder.input.audio
22

3+
import kotlin.math.abs
4+
35
class AudioUtils {
46

57
fun applyVolumeAndMix(
@@ -8,44 +10,40 @@ class AudioUtils {
810
dst: ByteArray
911
) {
1012
if (buffer.size != buffer2.size) return
11-
if (volume == 1f && volume2 == 1f) {
12-
for (i in buffer.indices) {
13-
dst[i] = (buffer[i] + buffer2[i]).toByte()
14-
}
15-
return
16-
}
1713
for (i in buffer.indices step 2) {
18-
val sample = ((buffer[i + 1].toInt() shl 8) or (buffer[i].toInt() and 0xFF))
19-
val adjustedSample = (sample * volume).toInt()
20-
val sample2 = ((buffer2[i + 1].toInt() shl 8) or (buffer2[i].toInt() and 0xFF))
21-
val adjustedSample2 = (sample2 * volume2).toInt()
14+
val sample1 = ((buffer[i + 1].toInt() shl 8) or (buffer[i].toInt() and 0xFF)).toShort().toInt()
15+
val sample2 = ((buffer2[i + 1].toInt() shl 8) or (buffer2[i].toInt() and 0xFF)).toShort().toInt()
2216

23-
dst[i] = (adjustedSample.toByte() + adjustedSample2.toByte()).toByte()
24-
dst[i + 1] = ((adjustedSample shr 8).toByte() + (adjustedSample2 shr 8).toByte()).toByte()
17+
val adjustedSample1 = (sample1 * volume).toInt()
18+
val adjustedSample2 = (sample2 * volume2).toInt()
19+
val mixedSample = (adjustedSample1 + adjustedSample2).coerceIn(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()) and 0xFFFF
20+
dst[i] = (mixedSample and 0xFF).toByte()
21+
dst[i + 1] = ((mixedSample shr 8) and 0xFF).toByte()
2522
}
2623
}
2724

2825
fun applyVolume(buffer: ByteArray, volume: Float) {
2926
if (volume == 1f) return
3027

3128
for (i in buffer.indices step 2) {
32-
val sample = ((buffer[i + 1].toInt() shl 8) or (buffer[i].toInt() and 0xFF))
33-
val adjustedSample = (sample * volume).toInt()
34-
buffer[i] = adjustedSample.toByte()
35-
buffer[i + 1] = (adjustedSample shr 8).toByte()
29+
val sample = ((buffer[i + 1].toInt() shl 8) or (buffer[i].toInt() and 0xFF)).toShort().toInt()
30+
val adjustedSample = (sample * volume).toInt().coerceIn(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()) and 0xFFFF
31+
buffer[i] = (adjustedSample and 0xFF).toByte()
32+
buffer[i + 1] = ((adjustedSample shr 8) and 0xFF).toByte()
3633
}
3734
}
3835

3936
/**
40-
* assume always pcm 16bit
37+
* Calculate amplitude peaks. Assume always pcm 16bit
4138
* @return value from 0f to 100f
4239
*/
4340
fun calculateAmplitude(buffer: ByteArray): Float {
4441
if (buffer.size % 2 != 0) return 0f //invalid buffer
4542
var amplitude = 0
4643
for (i in buffer.indices step 2) {
47-
val sample = ((buffer[i + 1].toInt() shl 8) or (buffer[i].toInt() and 0xFF))
48-
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
4947
}
5048
return (amplitude / Short.MAX_VALUE.toFloat()) * 100
5149
}

0 commit comments

Comments
 (0)