view clojure/com/aurellem/run/final_cut.clj @ 599:4503899241e6

going to render the advanced choreo.
author Robert McIntyre <rlm@mit.edu>
date Sun, 02 Sep 2012 09:18:43 -0500
parents 6ce1d5fd4911
children 31128a10ee28
line wrap: on
line source
1 (ns com.aurellem.run.final-cut
2 (:use (com.aurellem.gb saves gb-driver util constants
3 items vbm characters money
4 rlm-assembly))
5 (:use (com.aurellem.run util sound music title save-corruption
6 bootstrap-0 bootstrap-1
7 ram-display
8 ))
9 (:require clojure.string)
10 (:import [com.aurellem.gb.gb_driver SaveState])
11 (:import java.awt.image.BufferedImage)
12 (:import java.io.File)
13 (:import com.aurellem.gb.WaveWriter))
16 (def render-dir (File. user-home "proj/vba-clojure/render/test"))
19 (defn render-files!
20 ([^File target-dir
21 moves-render-fn
22 render-ram-map?
23 initial-state
24 moves
26 ]
27 (let [ram-map-dir (File. target-dir "ram-map")
28 frames-dir (File. target-dir "frames")
29 moves-dir (File. target-dir "moves")
30 audio-file (File. target-dir "audio.wav")
31 _ (.mkdir target-dir)
32 _ (.mkdir ram-map-dir)
33 _ (.mkdir frames-dir)
34 _ (.mkdir moves-dir)
35 wave-writer (WaveWriter. audio-file)
36 moves (vec moves)
37 desired-fps 60
38 seconds-per-frame (/ 1.0 desired-fps)]
39 (set-state! initial-state)
40 ;; clear sound buffer
41 (sound-bytes)
42 (try
43 (dorun
44 (reduce
45 (fn [[total-audio-samples
46 total-video-samples] move]
47 (run-moves @current-state (vector move))
48 (let [sound (sound-bytes)
49 total-audio-samples (+ total-audio-samples
50 (count sound))
51 total-audio-time
52 (*
53 (/ total-audio-samples 4)
54 (/ 44100))
56 total-video-time (* total-video-samples
57 seconds-per-frame)
59 av-diff (- total-audio-time
60 total-video-time)
61 write-video!
62 (fn [index]
63 ;; write screenshot
64 (moves-render-fn
65 move
66 (File. moves-dir (format "%07d.png" index)))
68 (write-png!
69 @current-state
70 (File. frames-dir (format "%07d.png" index)))
72 ;; write ram-image
73 (if render-ram-map?
74 (write-ram-image!
75 @current-state
76 (File. ram-map-dir (format "%07d.png" index)))))]
77 ;;(println "audio-samples:" (count sound))
79 ;; record audio
80 (.process wave-writer sound gb-sound-format)
82 ;; duplicate or drop frames depending on
83 ;; desired-fps
84 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))
85 (if (< 0 av-diff)
86 ;; audio has gone past video, duplicate video.
87 (do (println
88 "duplicating frame, av-diff is" av-diff)
89 (write-video! (+ 1 total-video-samples))
90 (write-video! (+ 2 total-video-samples))
91 [total-audio-samples
92 (+ 2 total-video-samples)])
93 ;; video has gone past audio, drop frame.
94 (do (println
95 "dropping frame, av-diff is" av-diff)
96 [total-audio-samples
97 total-video-samples]))
98 ;; no frame dropping or duplication required.
99 (do ;;(println "all normal")
100 (write-video! (+ 1 total-video-samples))
101 [total-audio-samples
102 (+ 1 total-video-samples)]))))
103 [0 0 0] moves))
104 (finally
105 (do
106 (println "cleanup audio.")
107 (.cleanup wave-writer))))))
108 ([initial-state moves]
109 (render-files! render-dir initial-state moves))
110 ([moves]
111 (render-files! (root) moves)))
113 (defn file-names [#^File dir]
114 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
118 ;; (defn final-cut! [^File render-dir]
119 ;; (let [movie (File. render-dir "final.ogg")
120 ;; final-audio (File. render-dir "final.wav")
121 ;; final (File. render-dir "final")]
122 ;; (.delete movie)
123 ;; (.delete final-audio)
124 ;; (clojure.java.shell/sh
125 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
126 ;; (.getCanonicalPath final-audio))
127 ;; (clojure.java.shell/sh
128 ;; "ffmpeg" "-r" "60" ;; maybe 59.7 ???!
129 ;; "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
130 ;; "-i" (.getCanonicalPath final-audio)
131 ;; "-b:a" "128k"
132 ;; "-b:v" "9000k"
133 ;; "-c:a" "libvorbis"
134 ;; "-f" "webm"
135 ;; "-g" "200"
136 ;; (.getCanonicalPath movie)) nil))
138 (defn final-cut! [^File render-dir]
139 (let [movie (File. render-dir "final.ogg")
140 final-audio (File. render-dir "final.wav")
141 final (File. render-dir "final")]
142 (.delete movie)
143 (.delete final-audio)
144 (clojure.java.shell/sh
145 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
146 (.getCanonicalPath final-audio))
147 (clojure.java.shell/sh
148 "ffmpeg"
149 "-framerate" "60"
150 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
151 "-i" (.getCanonicalPath final-audio)
152 "-b:a" "128k"
153 "-b:v" "9000k"
154 "-c:a" "libvorbis"
155 "-c:v" "libtheora"
156 "-r" "60"
157 (.getCanonicalPath movie)) nil))
160 (comment
162 ;; step 1
164 (render-files!
165 render-dir (root)
166 (take 9000 (first (control-checkpoint))))
168 ;; step 2
170 (generate-composite-frames!
171 render-dir
172 (take 9000 (first (control-checkpoint))))
174 ;; step 3
175 (final-cut! render-dir)