diff org/ear.org @ 164:c33a8e5fe7bc

removed hearing-init-fns requirement, changed names
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 03:29:59 -0700
parents 985c73659923
children 362bc30a3d41
line wrap: on
line diff
     1.1 --- a/org/ear.org	Sat Feb 04 02:15:32 2012 -0700
     1.2 +++ b/org/ear.org	Sat Feb 04 03:29:59 2012 -0700
     1.3 @@ -749,13 +749,22 @@
     1.4  
     1.5  #+include "../../jmeCapture/src/com/aurellem/capture/audio/SoundProcessor.java" src java 
     1.6  
     1.7 +
     1.8 +Ears work the same way as vision.
     1.9 +
    1.10 +(hearing creature) will return [init-functions sensor-functions]. The
    1.11 +init functions each take the world and register a SoundProcessor that
    1.12 +does foureier transforms on the incommong sound data, making it
    1.13 +available to each sensor function.
    1.14 +
    1.15 +
    1.16  #+name: ears
    1.17  #+begin_src clojure
    1.18  (ns cortex.hearing
    1.19    "Simulate the sense of hearing in jMonkeyEngine3. Enables multiple
    1.20 -  listeners at different positions in the same world. Passes vectors
    1.21 -  of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary
    1.22 -  function."
    1.23 +  listeners at different positions in the same world. Automatically
    1.24 +  reads ear-nodes from specially prepared blender files and
    1.25 +  instantiates them in the world as actual ears."
    1.26    {:author "Robert McIntyre"}
    1.27    (:use (cortex world util sense))
    1.28    (:import java.nio.ByteBuffer)
    1.29 @@ -763,7 +772,8 @@
    1.30    (:import com.aurellem.capture.audio.SoundProcessor)
    1.31    (:import javax.sound.sampled.AudioFormat))
    1.32  
    1.33 -(cortex.import/mega-import-jme3)  
    1.34 +(cortex.import/mega-import-jme3)
    1.35 +(use 'clojure.contrib.def)
    1.36  
    1.37  (defn sound-processor
    1.38    "Deals with converting ByteBuffers into Vectors of floats so that
    1.39 @@ -783,12 +793,11 @@
    1.40          (continuation
    1.41           (vec floats))))))
    1.42  
    1.43 -(defn ears
    1.44 -  "Return the children of the creature's \"ears\" node."
    1.45 -  [#^Node creature]
    1.46 -  (if-let [ear-node (.getChild creature "ears")]
    1.47 -    (seq (.getChildren ear-node))
    1.48 -    (do (println-repl "could not find ears node") [])))
    1.49 +(defvar 
    1.50 +  ^{:arglists '([creature])}
    1.51 +  ears
    1.52 +  (sense-nodes "ears")
    1.53 +  "Return the children of the creature's \"ears\" node.")
    1.54  
    1.55  (defn update-listener-velocity!
    1.56    "Update the listener's velocity every update loop."
    1.57 @@ -808,7 +817,10 @@
    1.58  
    1.59  (import com.aurellem.capture.audio.AudioSendRenderer)
    1.60  
    1.61 -(defn create-listener!
    1.62 +(defn create-listener!  
    1.63 +  "Create a Listener centered on the current position of 'ear 
    1.64 +   which follows the closest physical node in 'creature and 
    1.65 +   sends sound data to 'continuation."
    1.66    [#^Application world #^Node creature #^Spatial ear continuation]
    1.67    (let [target (closest-node creature ear)
    1.68          lis (Listener.)
    1.69 @@ -822,34 +834,36 @@
    1.70      (.registerSoundProcessor audio-renderer lis sp)))
    1.71  
    1.72  (defn hearing-fn
    1.73 +  "Returns a functon which returns auditory sensory data when called
    1.74 +   inside a running simulation."
    1.75    [#^Node creature #^Spatial ear]
    1.76 -  (let [hearing-data (atom [])]
    1.77 -    [(fn [world]
    1.78 -       (create-listener! world creature ear
    1.79 -                   (fn [data]
    1.80 -                     (reset! hearing-data (vec data)))))
    1.81 -     [(fn []
    1.82 -        (let [data @hearing-data
    1.83 -              topology              
    1.84 -              (vec (map #(vector % 0) (range 0 (count data))))
    1.85 -              scaled-data
    1.86 -              (vec
    1.87 -               (map
    1.88 -                #(rem (int (* 255 (/ (+ 1 %) 2)))  256)
    1.89 -                   data))]
    1.90 -          [topology scaled-data]))
    1.91 -        ]]))
    1.92 -
    1.93 +  (let [hearing-data (atom [])
    1.94 +        register-listener!
    1.95 +        (runonce 
    1.96 +         (fn [#^Application world]
    1.97 +           (create-listener!
    1.98 +            world creature ear
    1.99 +            (fn [data]
   1.100 +              (reset! hearing-data (vec data))))))]
   1.101 +    (fn [#^Application world]
   1.102 +      (register-listener! world)
   1.103 +      (let [data @hearing-data
   1.104 +            topology              
   1.105 +            (vec (map #(vector % 0) (range 0 (count data))))
   1.106 +            scaled-data
   1.107 +            (vec
   1.108 +             (map
   1.109 +              #(rem (int (* 255 (/ (+ 1 %) 2)))  256)
   1.110 +              data))]
   1.111 +        [topology scaled-data]))))
   1.112 +    
   1.113  (defn hearing!
   1.114 +  "Endow the creature in a particular world with the sense of
   1.115 +   hearing. Will return a sequence of functions, one for each ear,
   1.116 +   which when called will return the auditory data from that ear."
   1.117    [#^Node creature]
   1.118 -  (reduce
   1.119 -   (fn [[init-a senses-a]
   1.120 -        [init-b senses-b]]
   1.121 -     [(conj init-a init-b)
   1.122 -      (into senses-a senses-b)])
   1.123 -   [[][]]      
   1.124 -   (for [ear (ears creature)]
   1.125 -     (hearing-fn creature ear))))
   1.126 +  (for [ear (ears creature)]
   1.127 +    (hearing-fn creature ear)))
   1.128  
   1.129  
   1.130  #+end_src