annotate 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
rev   line source
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
rlm@530 19 (defn render-files!
rlm@599 20 ([^File target-dir
rlm@599 21 moves-render-fn
rlm@599 22 render-ram-map?
rlm@599 23 initial-state
rlm@599 24 moves
rlm@599 25
rlm@599 26 ]
rlm@530 27 (let [ram-map-dir (File. target-dir "ram-map")
rlm@530 28 frames-dir (File. target-dir "frames")
rlm@546 29 moves-dir (File. target-dir "moves")
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@546 34 _ (.mkdir moves-dir)
rlm@530 35 wave-writer (WaveWriter. audio-file)
rlm@544 36 moves (vec moves)
rlm@544 37 desired-fps 60
rlm@547 38 seconds-per-frame (/ 1.0 desired-fps)]
rlm@530 39 (set-state! initial-state)
rlm@544 40 ;; clear sound buffer
rlm@544 41 (sound-bytes)
rlm@544 42 (try
rlm@544 43 (dorun
rlm@544 44 (reduce
rlm@544 45 (fn [[total-audio-samples
rlm@544 46 total-video-samples] move]
rlm@544 47 (run-moves @current-state (vector move))
rlm@544 48 (let [sound (sound-bytes)
rlm@544 49 total-audio-samples (+ total-audio-samples
rlm@544 50 (count sound))
rlm@544 51 total-audio-time
rlm@544 52 (*
rlm@544 53 (/ total-audio-samples 4)
rlm@544 54 (/ 44100))
rlm@544 55
rlm@544 56 total-video-time (* total-video-samples
rlm@544 57 seconds-per-frame)
rlm@530 58
rlm@544 59 av-diff (- total-audio-time
rlm@544 60 total-video-time)
rlm@544 61 write-video!
rlm@544 62 (fn [index]
rlm@544 63 ;; write screenshot
rlm@599 64 (moves-render-fn
rlm@546 65 move
rlm@546 66 (File. moves-dir (format "%07d.png" index)))
rlm@544 67
rlm@544 68 (write-png!
rlm@544 69 @current-state
rlm@544 70 (File. frames-dir (format "%07d.png" index)))
rlm@544 71
rlm@544 72 ;; write ram-image
rlm@599 73 (if render-ram-map?
rlm@599 74 (write-ram-image!
rlm@599 75 @current-state
rlm@599 76 (File. ram-map-dir (format "%07d.png" index)))))]
rlm@544 77 ;;(println "audio-samples:" (count sound))
rlm@544 78
rlm@544 79 ;; record audio
rlm@544 80 (.process wave-writer sound gb-sound-format)
rlm@544 81
rlm@544 82 ;; duplicate or drop frames depending on
rlm@544 83 ;; desired-fps
rlm@544 84 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))
rlm@544 85 (if (< 0 av-diff)
rlm@544 86 ;; audio has gone past video, duplicate video.
rlm@544 87 (do (println
rlm@544 88 "duplicating frame, av-diff is" av-diff)
rlm@544 89 (write-video! (+ 1 total-video-samples))
rlm@544 90 (write-video! (+ 2 total-video-samples))
rlm@544 91 [total-audio-samples
rlm@544 92 (+ 2 total-video-samples)])
rlm@544 93 ;; video has gone past audio, drop frame.
rlm@544 94 (do (println
rlm@544 95 "dropping frame, av-diff is" av-diff)
rlm@544 96 [total-audio-samples
rlm@544 97 total-video-samples]))
rlm@544 98 ;; no frame dropping or duplication required.
rlm@544 99 (do ;;(println "all normal")
rlm@544 100 (write-video! (+ 1 total-video-samples))
rlm@544 101 [total-audio-samples
rlm@544 102 (+ 1 total-video-samples)]))))
rlm@544 103 [0 0 0] moves))
rlm@544 104 (finally
rlm@544 105 (do
rlm@544 106 (println "cleanup audio.")
rlm@544 107 (.cleanup wave-writer))))))
rlm@544 108 ([initial-state moves]
rlm@544 109 (render-files! render-dir initial-state moves))
rlm@544 110 ([moves]
rlm@544 111 (render-files! (root) moves)))
rlm@530 112
rlm@531 113 (defn file-names [#^File dir]
rlm@531 114 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
rlm@531 115
rlm@531 116
rlm@532 117
rlm@549 118 ;; (defn final-cut! [^File render-dir]
rlm@549 119 ;; (let [movie (File. render-dir "final.ogg")
rlm@549 120 ;; final-audio (File. render-dir "final.wav")
rlm@549 121 ;; final (File. render-dir "final")]
rlm@549 122 ;; (.delete movie)
rlm@549 123 ;; (.delete final-audio)
rlm@549 124 ;; (clojure.java.shell/sh
rlm@549 125 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
rlm@549 126 ;; (.getCanonicalPath final-audio))
rlm@549 127 ;; (clojure.java.shell/sh
rlm@549 128 ;; "ffmpeg" "-r" "60" ;; maybe 59.7 ???!
rlm@549 129 ;; "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
rlm@549 130 ;; "-i" (.getCanonicalPath final-audio)
rlm@549 131 ;; "-b:a" "128k"
rlm@549 132 ;; "-b:v" "9000k"
rlm@549 133 ;; "-c:a" "libvorbis"
rlm@549 134 ;; "-f" "webm"
rlm@549 135 ;; "-g" "200"
rlm@549 136 ;; (.getCanonicalPath movie)) nil))
rlm@549 137
rlm@532 138 (defn final-cut! [^File render-dir]
rlm@532 139 (let [movie (File. render-dir "final.ogg")
rlm@544 140 final-audio (File. render-dir "final.wav")
rlm@532 141 final (File. render-dir "final")]
rlm@532 142 (.delete movie)
rlm@547 143 (.delete final-audio)
rlm@532 144 (clojure.java.shell/sh
rlm@544 145 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
rlm@544 146 (.getCanonicalPath final-audio))
rlm@544 147 (clojure.java.shell/sh
rlm@549 148 "ffmpeg"
rlm@549 149 "-framerate" "60"
rlm@532 150 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
rlm@544 151 "-i" (.getCanonicalPath final-audio)
rlm@532 152 "-b:a" "128k"
rlm@532 153 "-b:v" "9000k"
rlm@532 154 "-c:a" "libvorbis"
rlm@549 155 "-c:v" "libtheora"
rlm@549 156 "-r" "60"
rlm@532 157 (.getCanonicalPath movie)) nil))
rlm@549 158
rlm@531 159
rlm@532 160 (comment
rlm@531 161
rlm@532 162 ;; step 1
rlm@532 163
rlm@532 164 (render-files!
rlm@532 165 render-dir (root)
rlm@532 166 (take 9000 (first (control-checkpoint))))
rlm@531 167
rlm@532 168 ;; step 2
rlm@532 169
rlm@532 170 (generate-composite-frames!
rlm@532 171 render-dir
rlm@532 172 (take 9000 (first (control-checkpoint))))
rlm@532 173
rlm@532 174 ;; step 3
rlm@532 175 (final-cut! render-dir)
rlm@532 176 )