changeset 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
files org/ear.org org/ideas.org org/sense-util.org org/test-creature.org org/util.org
diffstat 5 files changed, 81 insertions(+), 40 deletions(-) [+]
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
     2.1 --- a/org/ideas.org	Sat Feb 04 02:15:32 2012 -0700
     2.2 +++ b/org/ideas.org	Sat Feb 04 03:29:59 2012 -0700
     2.3 @@ -114,7 +114,13 @@
     2.4   - [ ] joints!
     2.5  
     2.6   - [ ] touch!
     2.7 - - [ ] hearing!
     2.8 + - [X] hearing!
     2.9   - [ ] vision!
    2.10   - [ ] proprioception!
    2.11   - [ ] movement!
    2.12 +
    2.13 +
    2.14 +;;In the elder days of Art,
    2.15 +;;Builders wrought with greatest care
    2.16 +;;Each minute and unseen part;
    2.17 +;;For the Gods see everywhere.
     3.1 --- a/org/sense-util.org	Sat Feb 04 02:15:32 2012 -0700
     3.2 +++ b/org/sense-util.org	Sat Feb 04 03:29:59 2012 -0700
     3.3 @@ -174,6 +174,14 @@
     3.4     [#^Spatial object local-coordinate]
     3.5     (.localToWorld object local-coordinate nil))
     3.6  
     3.7 +
     3.8 +(defn sense-nodes [parent-name]
     3.9 +  (fn [#^Node creature]
    3.10 +    (if-let [sense-node (.getChild creature parent-name)]
    3.11 +      (seq (.getChildren sense-node))
    3.12 +      (do (println-repl "could not find" parent-name "node") []))))
    3.13 +
    3.14 +
    3.15  #+end_src
    3.16  
    3.17  #+results: sense-util
     4.1 --- a/org/test-creature.org	Sat Feb 04 02:15:32 2012 -0700
     4.2 +++ b/org/test-creature.org	Sat Feb 04 03:29:59 2012 -0700
     4.3 @@ -184,7 +184,7 @@
     4.4          [init-vision-fns vision-data] (vision creature)
     4.5          vision-debug (map (fn [_] (debug-vision-window)) vision-data)
     4.6          me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
     4.7 -        [init-hearing-fns hearing-senses] (hearing! creature)
     4.8 +        hearing-senses (hearing! creature)
     4.9          hearing-windows (map (fn [_] (debug-hearing-window 50))
    4.10                               hearing-senses)
    4.11          bell (AudioNode. (asset-manager)
    4.12 @@ -231,7 +231,7 @@
    4.13            (light-up-everything world)
    4.14            (enable-debug world)
    4.15            (dorun (map #(% world) init-vision-fns))
    4.16 -          (dorun (map #(% world) init-hearing-fns))
    4.17 +          ;;(dorun (map #(% world) init-hearing-fns))
    4.18            
    4.19            (add-eye world
    4.20                     (attach-eye creature (test-eye))
    4.21 @@ -259,7 +259,7 @@
    4.22             (map #(%1 (%2))
    4.23                  vision-debug vision-data))
    4.24            (dorun
    4.25 -           (map #(%1 (%2)) hearing-windows hearing-senses))
    4.26 +           (map #(%1 (%2 world)) hearing-windows hearing-senses))
    4.27            
    4.28            
    4.29            ;;(println-repl (vision-data))
     5.1 --- a/org/util.org	Sat Feb 04 02:15:32 2012 -0700
     5.2 +++ b/org/util.org	Sat Feb 04 03:29:59 2012 -0700
     5.3 @@ -202,6 +202,19 @@
     5.4    keeping the keys the same."  
     5.5    [f m] (zipmap (keys m) (map f (vals m))))
     5.6  
     5.7 +(defn runonce
     5.8 +  "Decorator.  returns a function which will run only once. 
     5.9 +   Inspired by Halloway's version from Lancet."
    5.10 +  {:author "Robert McIntyre"}
    5.11 +  [function]
    5.12 +  (let [sentinel (Object.) 
    5.13 +	result (atom sentinel)]
    5.14 +    (fn [& args]
    5.15 +       (locking sentinel
    5.16 +	 (if (= @result sentinel) 
    5.17 +	   (reset! result (apply function args)) 
    5.18 +	    @result)))))
    5.19 +
    5.20  
    5.21  #+end_src
    5.22