From 77f214e644621c2a3f42454262cb64df3d43df66 Mon Sep 17 00:00:00 2001 From: geniusnut Date: Wed, 31 May 2023 15:13:14 +0800 Subject: [PATCH] whisper.android : support decode wav file has 2 channels (#972) --- .../java/com/whispercppdemo/media/RiffWaveHelper.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt b/examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt index 0947a8c5230..064ef699de1 100644 --- a/examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt +++ b/examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt @@ -10,12 +10,16 @@ fun decodeWaveFile(file: File): FloatArray { file.inputStream().use { it.copyTo(baos) } val buffer = ByteBuffer.wrap(baos.toByteArray()) buffer.order(ByteOrder.LITTLE_ENDIAN) + val channel = buffer.getShort(22).toInt() buffer.position(44) val shortBuffer = buffer.asShortBuffer() val shortArray = ShortArray(shortBuffer.limit()) shortBuffer.get(shortArray) - return FloatArray(shortArray.size) { index -> - (shortArray[index] / 32767.0f).coerceIn(-1f..1f) + return FloatArray(shortArray.size / channel) { index -> + when (channel) { + 1 -> (shortArray[index] / 32767.0f).coerceIn(-1f..1f) + else -> ((shortArray[2*index] + shortArray[2*index + 1])/ 32767.0f / 2.0f).coerceIn(-1f..1f) + } } } @@ -73,4 +77,4 @@ private fun headerBytes(totalLength: Int): ByteArray { it.get(bytes) return bytes } -} \ No newline at end of file +}