comparison clojure/com/aurellem/run/sound.clj @ 525:fa7676dbf6f2

sound recording test now works.
author Robert McIntyre <rlm@mit.edu>
date Sun, 24 Jun 2012 13:12:51 -0500
parents 7ef5c73ea8fa
children 2620d6318e8d
comparison
equal deleted inserted replaced
524:7ef5c73ea8fa 525:fa7676dbf6f2
5 (:use (com.aurellem.run util music title save-corruption 5 (:use (com.aurellem.run util music title save-corruption
6 bootstrap-0 bootstrap-1)) 6 bootstrap-0 bootstrap-1))
7 (:require clojure.string) 7 (:require clojure.string)
8 (:import [com.aurellem.gb.gb_driver SaveState]) 8 (:import [com.aurellem.gb.gb_driver SaveState])
9 (:import java.awt.image.BufferedImage) 9 (:import java.awt.image.BufferedImage)
10 (:import java.io.File)) 10 (:import java.io.File)
11 11 (:import javax.sound.sampled.AudioFormat)
12 (:import com.aurellem.gb.WaveWriter))
12 13
13 (defn sound-test [] 14 (defn sound-test []
14 (step (mid-game)) 15 (step (mid-game))
15 (println (frequencies (sound-data))) 16 (println (frequencies (sound-data)))
16 (run-moves @current-state (repeat 10 [])) 17 (run-moves @current-state (repeat 10 []))
17 (println (frequencies (sound-data)))) 18 (println (frequencies (sound-data))))
18
19
20 (import javax.sound.sampled.AudioFormat)
21 (import com.aurellem.gb.WaveWriter)
22 19
23 (def probable-format (AudioFormat. 44100 16 2 true false)) 20 (def probable-format (AudioFormat. 44100 16 2 true false))
24 21
25 (defn test-writing-file! [n] 22 (defn test-writing-file! [n]
26 (set-state! (play-midi pony-csv)) 23 (set-state! (play-midi pony-csv))
28 (File. "/home/r/proj/vba-clojure/test-sound.wav") 25 (File. "/home/r/proj/vba-clojure/test-sound.wav")
29 writer (WaveWriter. target-file)] 26 writer (WaveWriter. target-file)]
30 (dorun 27 (dorun
31 (for [y (range n)] 28 (for [y (range n)]
32 (do 29 (do
33 (let [quanta 30] 30 (let [quanta 1]
34 (run-moves @current-state (repeat quanta [])) 31 (run-moves @current-state (repeat quanta []))
35 (let [data (sound-data) 32 (let [
33
34 data (sound-data)
35 bytes (* 2 (com.aurellem.gb.Gb/getSoundFrameWritten))
36 step-section 36 step-section
37 (byte-array (take (* 2940 quanta) data))] 37 (byte-array
38 38 (take (* bytes quanta) data))
39 (.process writer step-section probable-format)))))) 39
40
41 ]
42 (com.aurellem.gb.Gb/setSoundFrameWritten 0)
43 (.process writer
44 ;;data
45 step-section
46 probable-format))))))
40 (.cleanup writer) 47 (.cleanup writer)
41 (Thread/sleep 1000) 48 (Thread/sleep 1000)
42 (clojure.java.shell/sh 49 (clojure.java.shell/sh
43 "aplay" 50 "aplay"
44 (.getCanonicalPath target-file)))) 51 (.getCanonicalPath target-file))))
45 52