Spaces:
Sleeping
Sleeping
whisper.android : support decode wav file has 2 channels (#972)
Browse files
examples/whisper.android/app/src/main/java/com/whispercppdemo/media/RiffWaveHelper.kt
CHANGED
|
@@ -10,12 +10,16 @@ fun decodeWaveFile(file: File): FloatArray {
|
|
| 10 |
file.inputStream().use { it.copyTo(baos) }
|
| 11 |
val buffer = ByteBuffer.wrap(baos.toByteArray())
|
| 12 |
buffer.order(ByteOrder.LITTLE_ENDIAN)
|
|
|
|
| 13 |
buffer.position(44)
|
| 14 |
val shortBuffer = buffer.asShortBuffer()
|
| 15 |
val shortArray = ShortArray(shortBuffer.limit())
|
| 16 |
shortBuffer.get(shortArray)
|
| 17 |
-
return FloatArray(shortArray.size) { index ->
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
|
@@ -73,4 +77,4 @@ private fun headerBytes(totalLength: Int): ByteArray {
|
|
| 73 |
it.get(bytes)
|
| 74 |
return bytes
|
| 75 |
}
|
| 76 |
-
}
|
|
|
|
| 10 |
file.inputStream().use { it.copyTo(baos) }
|
| 11 |
val buffer = ByteBuffer.wrap(baos.toByteArray())
|
| 12 |
buffer.order(ByteOrder.LITTLE_ENDIAN)
|
| 13 |
+
val channel = buffer.getShort(22).toInt()
|
| 14 |
buffer.position(44)
|
| 15 |
val shortBuffer = buffer.asShortBuffer()
|
| 16 |
val shortArray = ShortArray(shortBuffer.limit())
|
| 17 |
shortBuffer.get(shortArray)
|
| 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 |
+
}
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
|
|
|
| 77 |
it.get(bytes)
|
| 78 |
return bytes
|
| 79 |
}
|
| 80 |
+
}
|