Created
March 7, 2015 02:28
-
-
Save marianogappa/c7a9887f6fe5dfce12fb to your computer and use it in GitHub Desktop.
Scala script to solve this Quora question: http://www.quora.com/How-do-I-convert-a-wav-file-to-character-ASCII-file-Say-we-have-5-bit-converter-Every-5-bit-to-be-used-to-identify-a-character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io.Source | |
/* Boilerplate to make Java not throw Exceptions on non UTF-8 characters */ | |
import java.nio.charset.CodingErrorAction | |
import scala.io.Codec | |
implicit val codec = Codec("UTF-8") | |
codec.onMalformedInput(CodingErrorAction.REPLACE) | |
codec.onUnmappableCharacter(CodingErrorAction.REPLACE) | |
/* Put your WAV file here. You might want to slice it; a full WAV file will exceed JVM memory limit */ | |
val filename = "test.wav" // TODO change me to an actual file path | |
/* A 5-bit charset to interpret the bit stream */ | |
val charset = "abcdefghijklmnopqrstuvwxyz. !,?:" | |
/* Parse the file */ | |
val hiddenMessage = ((Source.fromFile(filename) map(_.toInt) flatMap(_.toString) flatMap (Integer.toBinaryString(_)) toList) grouped(5) toList) map (_.mkString) map (Integer.parseInt(_, 2)) map (charset(_)) mkString | |
println(hiddenMessage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment