Mercurial > cortex
comparison org/test-creature.org @ 159:75b6c2ebbf8e
refactored audio code
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 03 Feb 2012 06:41:16 -0700 |
parents | 811127d79d24 |
children | 33278bf028e7 |
comparison
equal
deleted
inserted
replaced
158:811127d79d24 | 159:75b6c2ebbf8e |
---|---|
257 (.getChild (worm-model) "eyes") | 257 (.getChild (worm-model) "eyes") |
258 "eye")) | 258 "eye")) |
259 | 259 |
260 | 260 |
261 | 261 |
262 ;; Ears work the same way as vision. | |
263 | |
264 ;; (hearing creature) will return [init-functions | |
265 ;; sensor-functions]. The init functions each take the world and | |
266 ;; register a SoundProcessor that does foureier transforms on the | |
267 ;; incommong sound data, making it available to each sensor function. | |
268 | |
269 (defn creature-ears | |
270 "Return the children of the creature's \"ears\" node." | |
271 ;;dylan | |
272 ;;"The ear nodes which are children of the \"ears\" node in the | |
273 ;;creature." | |
274 [#^Node creature] | |
275 (if-let [ear-node (.getChild creature "ears")] | |
276 (seq (.getChildren ear-node)) | |
277 (do (println-repl "could not find ears node") []))) | |
278 | |
279 | |
280 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli, | |
281 ;;anchor-qualia, augment-organ, with-organ | |
282 | |
283 | |
284 (defn update-listener-velocity | |
285 "Update the listener's velocity every update loop." | |
286 [#^Spatial obj #^Listener lis] | |
287 (let [old-position (atom (.getLocation lis))] | |
288 (.addControl | |
289 obj | |
290 (proxy [AbstractControl] [] | |
291 (controlUpdate [tpf] | |
292 (let [new-position (.getLocation lis)] | |
293 (.setVelocity | |
294 lis | |
295 (.mult (.subtract new-position @old-position) | |
296 (float (/ tpf)))) | |
297 (reset! old-position new-position))) | |
298 (controlRender [_ _]))))) | |
299 | |
300 (import com.aurellem.capture.audio.AudioSendRenderer) | |
301 | |
302 (defn attach-ear | |
303 [#^Application world #^Node creature #^Spatial ear continuation] | |
304 (let [target (closest-node creature ear) | |
305 lis (Listener.) | |
306 audio-renderer (.getAudioRenderer world) | |
307 sp (sound-processor continuation)] | |
308 (.setLocation lis (.getWorldTranslation ear)) | |
309 (.setRotation lis (.getWorldRotation ear)) | |
310 (bind-sense target lis) | |
311 (update-listener-velocity target lis) | |
312 (.addListener audio-renderer lis) | |
313 (.registerSoundProcessor audio-renderer lis sp))) | |
314 | |
315 (defn enable-hearing | |
316 [#^Node creature #^Spatial ear] | |
317 (let [hearing-data (atom [])] | |
318 [(fn [world] | |
319 (attach-ear world creature ear | |
320 (fn [data] | |
321 (reset! hearing-data (vec data))))) | |
322 [(fn [] | |
323 (let [data @hearing-data | |
324 topology | |
325 (vec (map #(vector % 0) (range 0 (count data)))) | |
326 scaled-data | |
327 (vec | |
328 (map | |
329 #(rem (int (* 255 (/ (+ 1 %) 2))) 256) | |
330 data))] | |
331 [topology scaled-data])) | |
332 ]])) | |
333 | |
334 (defn hearing | |
335 [#^Node creature] | |
336 (reduce | |
337 (fn [[init-a senses-a] | |
338 [init-b senses-b]] | |
339 [(conj init-a init-b) | |
340 (into senses-a senses-b)]) | |
341 [[][]] | |
342 (for [ear (creature-ears creature)] | |
343 (enable-hearing creature ear)))) | |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | |
350 ;; lower level --- nodes | 262 ;; lower level --- nodes |
351 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair | 263 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair |
352 | 264 |
353 ;; higher level -- organs | 265 ;; higher level -- organs |
354 ;; | 266 ;; |
413 (let [raw-sensor (sensor-data x)] | 325 (let [raw-sensor (sensor-data x)] |
414 (.setRGB image x y (gray-scale raw-sensor))))))) | 326 (.setRGB image x y (gray-scale raw-sensor))))))) |
415 | 327 |
416 (vi image))))) | 328 (vi image))))) |
417 | 329 |
418 | |
419 | |
420 ;;(defn test-touch [world creature] | |
421 | |
422 | |
423 | |
424 | |
425 | |
426 (defn test-creature [thing] | 330 (defn test-creature [thing] |
427 (let [x-axis | 331 (let [x-axis |
428 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red) | 332 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red) |
429 y-axis | 333 y-axis |
430 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) | 334 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) |