Skip to content

Commit cefe97b

Browse files
authored
whisper.android : support decode wav file has 2 channels (ggml-org#972)
1 parent e2bf27e commit cefe97b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ fun decodeWaveFile(file: File): FloatArray {
1010
file.inputStream().use { it.copyTo(baos) }
1111
val buffer = ByteBuffer.wrap(baos.toByteArray())
1212
buffer.order(ByteOrder.LITTLE_ENDIAN)
13+
val channel = buffer.getShort(22).toInt()
1314
buffer.position(44)
1415
val shortBuffer = buffer.asShortBuffer()
1516
val shortArray = ShortArray(shortBuffer.limit())
1617
shortBuffer.get(shortArray)
17-
return FloatArray(shortArray.size) { index ->
18-
(shortArray[index] / 32767.0f).coerceIn(-1f..1f)
18+
return FloatArray(shortArray.size / channel) { index ->
19+
when (channel) {
20+
1 -> (shortArray[index] / 32767.0f).coerceIn(-1f..1f)
21+
else -> ((shortArray[2*index] + shortArray[2*index + 1])/ 32767.0f / 2.0f).coerceIn(-1f..1f)
22+
}
1923
}
2024
}
2125

@@ -73,4 +77,4 @@ private fun headerBytes(totalLength: Int): ByteArray {
7377
it.get(bytes)
7478
return bytes
7579
}
76-
}
80+
}

0 commit comments

Comments
 (0)