# HG changeset patch # User Robert McIntyre # Date 1328276459 25200 # Node ID 0e794e48a0cc0d74ccd60b64b959eec937ec1d91 # Parent 32c69ba451d92788b57b2d6491dbff343b4609e4 updated clojure hearing code to work with blender diff -r 32c69ba451d9 -r 0e794e48a0cc org/ear.org --- a/org/ear.org Mon Jan 23 05:49:37 2012 -0700 +++ b/org/ear.org Fri Feb 03 06:40:59 2012 -0700 @@ -757,12 +757,14 @@ of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary function." {:author "Robert McIntyre"} - (:use (cortex world util)) + (:use (cortex world util sense)) (:import java.nio.ByteBuffer) (:import org.tritonus.share.sampled.FloatSampleTools) (:import com.aurellem.capture.audio.SoundProcessor) (:import javax.sound.sampled.AudioFormat)) - + +(cortex.import/mega-import-jme3) + (defn sound-processor "Deals with converting ByteBuffers into Vectors of floats so that the continuation functions can be defined in terms of immutable @@ -781,16 +783,93 @@ (continuation (vec floats)))))) -(defn add-ear - "Add an ear to the world. The continuation function will be called - on the FFT or the sounds which the ear hears in the given - timeframe. Sound is 3D." - [world listener continuation] - (let [renderer (.getAudioRenderer world)] - (.addListener renderer listener) - (.registerSoundProcessor renderer listener - (sound-processor continuation)) - listener)) + + + +;; Ears work the same way as vision. + +;; (hearing creature) will return [init-functions +;; sensor-functions]. The init functions each take the world and +;; register a SoundProcessor that does foureier transforms on the +;; incommong sound data, making it available to each sensor function. + +(defn creature-ears + "Return the children of the creature's \"ears\" node." + ;;dylan + ;;"The ear nodes which are children of the \"ears\" node in the + ;;creature." + [#^Node creature] + (if-let [ear-node (.getChild creature "ears")] + (seq (.getChildren ear-node)) + (do (println-repl "could not find ears node") []))) + + +;;dylan (defn follow-sense, adjoin-sense, attach-stimuli, +;;anchor-qualia, augment-organ, with-organ + + +(defn update-listener-velocity + "Update the listener's velocity every update loop." + [#^Spatial obj #^Listener lis] + (let [old-position (atom (.getLocation lis))] + (.addControl + obj + (proxy [AbstractControl] [] + (controlUpdate [tpf] + (let [new-position (.getLocation lis)] + (.setVelocity + lis + (.mult (.subtract new-position @old-position) + (float (/ tpf)))) + (reset! old-position new-position))) + (controlRender [_ _]))))) + +(import com.aurellem.capture.audio.AudioSendRenderer) + +(defn attach-ear + [#^Application world #^Node creature #^Spatial ear continuation] + (let [target (closest-node creature ear) + lis (Listener.) + audio-renderer (.getAudioRenderer world) + sp (sound-processor continuation)] + (.setLocation lis (.getWorldTranslation ear)) + (.setRotation lis (.getWorldRotation ear)) + (bind-sense target lis) + (update-listener-velocity target lis) + (.addListener audio-renderer lis) + (.registerSoundProcessor audio-renderer lis sp))) + +(defn enable-hearing + [#^Node creature #^Spatial ear] + (let [hearing-data (atom [])] + [(fn [world] + (attach-ear world creature ear + (fn [data] + (reset! hearing-data (vec data))))) + [(fn [] + (let [data @hearing-data + topology + (vec (map #(vector % 0) (range 0 (count data)))) + scaled-data + (vec + (map + #(rem (int (* 255 (/ (+ 1 %) 2))) 256) + data))] + [topology scaled-data])) + ]])) + +(defn hearing + [#^Node creature] + (reduce + (fn [[init-a senses-a] + [init-b senses-b]] + [(conj init-a init-b) + (into senses-a senses-b)]) + [[][]] + (for [ear (creature-ears creature)] + (enable-hearing creature ear)))) + + #+end_src * Example