Skip to content

Commit

Permalink
whisper.android : support decode wav file has 2 channels (ggerganov#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
geniusnut authored May 31, 2023
1 parent a4b726b commit 77f214e
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -73,4 +77,4 @@ private fun headerBytes(totalLength: Int): ByteArray {
it.get(bytes)
return bytes
}
}
}

0 comments on commit 77f214e

Please sign in to comment.