-
-
Save jamesdavidson/380e97cce7b6dc8297b96eaced5bf548 to your computer and use it in GitHub Desktop.
Microphone Clojure test
This file contains 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 | |
[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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See instead: https://github.com/candera/dynne