Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Forked from BurkeB/microphone.clj
Last active April 13, 2020 16:20
Show Gist options
  • Save jamesdavidson/380e97cce7b6dc8297b96eaced5bf548 to your computer and use it in GitHub Desktop.
Save jamesdavidson/380e97cce7b6dc8297b96eaced5bf548 to your computer and use it in GitHub Desktop.
Microphone Clojure test
(import
[javax.sound.sampled TargetDataLine AudioFormat AudioSystem AudioInputStream AudioFileFormat$Type DataLine$Info]
[java.io File ByteArrayInputStream])
(def audio-format (new AudioFormat 44100 16 2 true true))
(def info (new DataLine$Info TargetDataLine audio-format))
(assert (AudioSystem/isLineSupported info))
(def line (AudioSystem/getTargetDataLine audio-format))
(.open line)
(.start line)
(def duration (* 5 (.getSampleRate audio-format)))
(def size (* duration (.getFrameSize audio-format)))
(def buf (byte-array size))
(loop [offset 0]
(if (< offset size)
; read more data and recur
(let [delta (.read line buf offset (- size offset))]
(recur (+ offset delta)))
; write to file
(let [f (new File "test.wav")
s (new AudioInputStream (new ByteArrayInputStream buf) audio-format duration)]
(AudioSystem/write s AudioFileFormat$Type/WAVE f))))
(.close line)
@jamesdavidson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment