Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active July 14, 2020 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavpandeyvpz/6d7d78bf92329e6d4ea14a3e1ecd9580 to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/6d7d78bf92329e6d4ea14a3e1ecd9580 to your computer and use it in GitHub Desktop.
import java.io.IOException
import java.io.OutputStream
class WavHeader (
private val format: Int = 0,
private val channels: Int = 0,
private val hertz: Int = 0,
private val bitrate: Int = 0,
private val size: Int = 0
) {
@Throws(IOException::class)
fun write(out: OutputStream): Int {
writeId(out, "RIFF")
writeInt(out, size)
writeId(out, "WAVEfmt ")
writeInt(out, 16)
writeShort(out, format)
writeShort(out, channels)
writeInt(out, hertz)
writeInt(out, channels * hertz * bitrate / 8)
writeShort(out, channels * bitrate / 8)
writeShort(out, bitrate)
writeId(out, "data")
writeInt(out, size)
return 44
}
companion object {
@Throws(IOException::class)
private fun writeId(out: OutputStream, id: String) {
for (element in id) out.write(element.toInt())
}
@Throws(IOException::class)
private fun writeInt(out: OutputStream, v: Int) {
out.write(v shr 0)
out.write(v shr 8)
out.write(v shr 16)
out.write(v shr 24)
}
@Throws(IOException::class)
private fun writeShort(out: OutputStream, v: Int) {
out.write(v shr 0)
out.write(v shr 8)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment