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 @@ -8,20 +8,21 @@ class AudioUtils {
88 dst : ByteArray
99 ) {
1010 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- }
1711 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()
12+ val sample1 = ((buffer[i + 1 ].toInt() shl 8 ) or (buffer[i].toInt() and 0xFF ))
2013 val sample2 = ((buffer2[i + 1 ].toInt() shl 8 ) or (buffer2[i].toInt() and 0xFF ))
21- val adjustedSample2 = (sample2 * volume2).toInt()
2214
23- dst[i] = (adjustedSample.toByte() + adjustedSample2.toByte()).toByte()
24- dst[i + 1 ] = ((adjustedSample shr 8 ).toByte() + (adjustedSample2 shr 8 ).toByte()).toByte()
15+ val signedSample1 = if (sample1 > 32767 ) sample1 - 65536 else sample1
16+ val signedSample2 = if (sample2 > 32767 ) sample2 - 65536 else sample2
17+
18+ val adjustedSample1 = (signedSample1 * volume).toInt()
19+ val adjustedSample2 = (signedSample2 * volume2).toInt()
20+ var mixedSample = adjustedSample1 + adjustedSample2
21+
22+ mixedSample = mixedSample.coerceIn(- 32768 , 32767 )
23+ val unsignedSample = if (mixedSample < 0 ) mixedSample + 65536 else mixedSample
24+ dst[i] = (unsignedSample and 0xFF ).toByte()
25+ dst[i + 1 ] = ((unsignedSample shr 8 ) and 0xFF ).toByte()
2526 }
2627 }
2728
You can’t perform that action at this time.
0 commit comments