rlm@530
|
1 (ns com.aurellem.run.final-cut
|
rlm@530
|
2 (:use (com.aurellem.gb saves gb-driver util constants
|
rlm@530
|
3 items vbm characters money
|
rlm@530
|
4 rlm-assembly))
|
rlm@530
|
5 (:use (com.aurellem.run util sound music title save-corruption
|
rlm@530
|
6 bootstrap-0 bootstrap-1
|
rlm@530
|
7 ram-display
|
rlm@530
|
8 ))
|
rlm@530
|
9 (:require clojure.string)
|
rlm@530
|
10 (:import [com.aurellem.gb.gb_driver SaveState])
|
rlm@530
|
11 (:import java.awt.image.BufferedImage)
|
rlm@530
|
12 (:import java.io.File)
|
rlm@530
|
13 (:import com.aurellem.gb.WaveWriter))
|
rlm@530
|
14
|
rlm@530
|
15
|
rlm@542
|
16 (def render-dir (File. user-home "proj/vba-clojure/render/test"))
|
rlm@530
|
17
|
rlm@544
|
18 (defn force-sync
|
rlm@544
|
19 "keep the entire movie to a certain fps"
|
rlm@544
|
20 [fps]
|
rlm@544
|
21
|
rlm@544
|
22
|
rlm@544
|
23 )
|
rlm@544
|
24
|
rlm@544
|
25
|
rlm@530
|
26 (defn render-files!
|
rlm@530
|
27 ([^File target-dir initial-state moves]
|
rlm@530
|
28 (let [ram-map-dir (File. target-dir "ram-map")
|
rlm@530
|
29 frames-dir (File. target-dir "frames")
|
rlm@530
|
30 audio-file (File. target-dir "audio.wav")
|
rlm@533
|
31 _ (.mkdir target-dir)
|
rlm@533
|
32 _ (.mkdir ram-map-dir)
|
rlm@533
|
33 _ (.mkdir frames-dir)
|
rlm@530
|
34 wave-writer (WaveWriter. audio-file)
|
rlm@544
|
35 moves (vec moves)
|
rlm@544
|
36 desired-fps 60
|
rlm@544
|
37 seconds-per-frame (/ 1.0 desired-fps)
|
rlm@544
|
38
|
rlm@544
|
39 ]
|
rlm@530
|
40 (set-state! initial-state)
|
rlm@544
|
41 ;; clear sound buffer
|
rlm@544
|
42 (sound-bytes)
|
rlm@544
|
43 (try
|
rlm@544
|
44 (dorun
|
rlm@544
|
45 (reduce
|
rlm@544
|
46 (fn [[total-audio-samples
|
rlm@544
|
47 total-video-samples] move]
|
rlm@544
|
48 (run-moves @current-state (vector move))
|
rlm@544
|
49 (let [sound (sound-bytes)
|
rlm@544
|
50 total-audio-samples (+ total-audio-samples
|
rlm@544
|
51 (count sound))
|
rlm@544
|
52 total-audio-time
|
rlm@544
|
53 (*
|
rlm@544
|
54 (/ total-audio-samples 4)
|
rlm@544
|
55 (/ 44100))
|
rlm@544
|
56
|
rlm@544
|
57 total-video-time (* total-video-samples
|
rlm@544
|
58 seconds-per-frame)
|
rlm@530
|
59
|
rlm@544
|
60 av-diff (- total-audio-time
|
rlm@544
|
61 total-video-time)
|
rlm@544
|
62 write-video!
|
rlm@544
|
63 (fn [index]
|
rlm@544
|
64 ;; write screenshot
|
rlm@544
|
65
|
rlm@544
|
66
|
rlm@544
|
67 (write-png!
|
rlm@544
|
68 @current-state
|
rlm@544
|
69 (File. frames-dir (format "%07d.png" index)))
|
rlm@544
|
70
|
rlm@544
|
71 ;; write ram-image
|
rlm@544
|
72 (write-ram-image!
|
rlm@544
|
73 @current-state
|
rlm@544
|
74 (File. ram-map-dir (format "%07d.png" index))))]
|
rlm@544
|
75 ;;(println "audio-samples:" (count sound))
|
rlm@544
|
76
|
rlm@544
|
77 ;; record audio
|
rlm@544
|
78 (.process wave-writer sound gb-sound-format)
|
rlm@544
|
79
|
rlm@544
|
80 ;; duplicate or drop frames depending on
|
rlm@544
|
81 ;; desired-fps
|
rlm@544
|
82 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))
|
rlm@544
|
83 (if (< 0 av-diff)
|
rlm@544
|
84 ;; audio has gone past video, duplicate video.
|
rlm@544
|
85 (do (println
|
rlm@544
|
86 "duplicating frame, av-diff is" av-diff)
|
rlm@544
|
87 (write-video! (+ 1 total-video-samples))
|
rlm@544
|
88 (write-video! (+ 2 total-video-samples))
|
rlm@544
|
89 [total-audio-samples
|
rlm@544
|
90 (+ 2 total-video-samples)])
|
rlm@544
|
91 ;; video has gone past audio, drop frame.
|
rlm@544
|
92 (do (println
|
rlm@544
|
93 "dropping frame, av-diff is" av-diff)
|
rlm@544
|
94 [total-audio-samples
|
rlm@544
|
95 total-video-samples]))
|
rlm@544
|
96 ;; no frame dropping or duplication required.
|
rlm@544
|
97 (do ;;(println "all normal")
|
rlm@544
|
98 (write-video! (+ 1 total-video-samples))
|
rlm@544
|
99 [total-audio-samples
|
rlm@544
|
100 (+ 1 total-video-samples)]))))
|
rlm@544
|
101 [0 0 0] moves))
|
rlm@544
|
102 (finally
|
rlm@544
|
103 (do
|
rlm@544
|
104 (println "cleanup audio.")
|
rlm@544
|
105 (.cleanup wave-writer))))))
|
rlm@544
|
106 ([initial-state moves]
|
rlm@544
|
107 (render-files! render-dir initial-state moves))
|
rlm@544
|
108 ([moves]
|
rlm@544
|
109 (render-files! (root) moves)))
|
rlm@530
|
110
|
rlm@531
|
111 (defn file-names [#^File dir]
|
rlm@531
|
112 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
|
rlm@531
|
113
|
rlm@531
|
114 (defn composite-frames-command
|
rlm@532
|
115 [screenshot ram-map target move]
|
rlm@532
|
116 (let [actual-keys (set move)
|
rlm@532
|
117 keys [:a :b :start :select :u :d :l :r]
|
rlm@532
|
118 button-on-name ["-size" "5x5" "xc:red"]
|
rlm@534
|
119 button-off-name ["-size" "5x5" "xc:white"]]
|
rlm@532
|
120 ["convert"
|
rlm@532
|
121 "-size" "318x276" "xc:white"
|
rlm@532
|
122 (map
|
rlm@532
|
123 (fn [index]
|
rlm@532
|
124 (let [position ["-geometry"
|
rlm@532
|
125 (str "+" (+ 30 (* index 8))
|
rlm@532
|
126 "+" "225") "-composite"]]
|
rlm@532
|
127 (if (actual-keys (keys index))
|
rlm@532
|
128 [button-on-name position]
|
rlm@532
|
129 [button-off-name position])))
|
rlm@532
|
130 (range (count keys)))
|
rlm@532
|
131 screenshot "-geometry" "+10+10" "-composite"
|
rlm@532
|
132 ram-map "-geometry" "+180+10" "-composite"
|
rlm@532
|
133 target]))
|
rlm@531
|
134
|
rlm@531
|
135 (defn generate-composite-frames!
|
rlm@532
|
136 [^File rendered-dir moves]
|
rlm@532
|
137 (let [final-frames (File. rendered-dir "final")
|
rlm@532
|
138 _ (.mkdir final-frames)
|
rlm@532
|
139 screenshots (file-names (File. rendered-dir "frames"))
|
rlm@532
|
140 ram-maps (file-names (File. rendered-dir "ram-map"))
|
rlm@532
|
141 targets (map #(.getCanonicalPath
|
rlm@532
|
142 (File. final-frames (format "%07d.bmp" %)))
|
rlm@532
|
143 (range (count screenshots)))]
|
rlm@532
|
144 (dorun
|
rlm@532
|
145 (map
|
rlm@532
|
146 (comp
|
rlm@532
|
147 (partial apply clojure.java.shell/sh)
|
rlm@532
|
148 flatten
|
rlm@532
|
149 (partial apply composite-frames-command)
|
rlm@532
|
150 (fn [a b c d] (println c) [a b c d]))
|
rlm@532
|
151 screenshots ram-maps targets moves))))
|
rlm@531
|
152
|
rlm@532
|
153
|
rlm@532
|
154 (defn final-cut! [^File render-dir]
|
rlm@532
|
155 (let [movie (File. render-dir "final.ogg")
|
rlm@544
|
156 final-audio (File. render-dir "final.wav")
|
rlm@532
|
157 final (File. render-dir "final")]
|
rlm@532
|
158 (.delete movie)
|
rlm@532
|
159 (clojure.java.shell/sh
|
rlm@544
|
160 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
|
rlm@544
|
161 (.getCanonicalPath final-audio))
|
rlm@544
|
162 (clojure.java.shell/sh
|
rlm@543
|
163 "ffmpeg" "-r" "60" ;; maybe 59.7 ???!
|
rlm@532
|
164 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
|
rlm@544
|
165 "-i" (.getCanonicalPath final-audio)
|
rlm@532
|
166 "-b:a" "128k"
|
rlm@532
|
167 "-b:v" "9000k"
|
rlm@532
|
168 "-c:a" "libvorbis"
|
rlm@532
|
169 "-c:v" "libtheora"
|
rlm@532
|
170 (.getCanonicalPath movie)) nil))
|
rlm@532
|
171
|
rlm@531
|
172
|
rlm@532
|
173 (comment
|
rlm@531
|
174
|
rlm@532
|
175 ;; step 1
|
rlm@532
|
176
|
rlm@532
|
177 (render-files!
|
rlm@532
|
178 render-dir (root)
|
rlm@532
|
179 (take 9000 (first (control-checkpoint))))
|
rlm@531
|
180
|
rlm@532
|
181 ;; step 2
|
rlm@532
|
182
|
rlm@532
|
183 (generate-composite-frames!
|
rlm@532
|
184 render-dir
|
rlm@532
|
185 (take 9000 (first (control-checkpoint))))
|
rlm@532
|
186
|
rlm@532
|
187 ;; step 3
|
rlm@532
|
188 (final-cut! render-dir)
|
rlm@532
|
189 ) |