changeset 31:0e794e48a0cc

updated clojure hearing code to work with blender
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:40:59 -0700 (2012-02-03)
parents 32c69ba451d9
children b8bc24918d63
files org/ear.org
diffstat 1 files changed, 91 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/ear.org	Mon Jan 23 05:49:37 2012 -0700
     1.2 +++ b/org/ear.org	Fri Feb 03 06:40:59 2012 -0700
     1.3 @@ -757,12 +757,14 @@
     1.4    of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary
     1.5    function."
     1.6    {:author "Robert McIntyre"}
     1.7 -  (:use (cortex world util))
     1.8 +  (:use (cortex world util sense))
     1.9    (:import java.nio.ByteBuffer)
    1.10    (:import org.tritonus.share.sampled.FloatSampleTools)
    1.11    (:import com.aurellem.capture.audio.SoundProcessor)
    1.12    (:import javax.sound.sampled.AudioFormat))
    1.13 -  
    1.14 +
    1.15 +(cortex.import/mega-import-jme3)  
    1.16 +
    1.17  (defn sound-processor
    1.18    "Deals with converting ByteBuffers into Vectors of floats so that
    1.19    the continuation functions can be defined in terms of immutable
    1.20 @@ -781,16 +783,93 @@
    1.21          (continuation
    1.22           (vec floats))))))
    1.23  
    1.24 -(defn add-ear 
    1.25 -  "Add an ear to the world.  The continuation function will be called
    1.26 -  on the FFT or the sounds which the ear hears in the given
    1.27 -  timeframe. Sound is 3D."
    1.28 -  [world listener continuation]
    1.29 -  (let [renderer (.getAudioRenderer world)]
    1.30 -    (.addListener renderer listener)
    1.31 -    (.registerSoundProcessor renderer listener
    1.32 -			     (sound-processor continuation))
    1.33 -    listener))
    1.34 +
    1.35 +
    1.36 +
    1.37 +;; Ears work the same way as vision.
    1.38 +
    1.39 +;; (hearing creature) will return [init-functions
    1.40 +;; sensor-functions]. The init functions each take the world and
    1.41 +;; register a SoundProcessor that does foureier transforms on the
    1.42 +;; incommong sound data, making it available to each sensor function.
    1.43 +
    1.44 +(defn creature-ears
    1.45 +  "Return the children of the creature's \"ears\" node."
    1.46 +  ;;dylan
    1.47 +  ;;"The ear nodes which are children of the \"ears\" node in the
    1.48 +  ;;creature."
    1.49 +  [#^Node creature]
    1.50 +  (if-let [ear-node (.getChild creature "ears")]
    1.51 +    (seq (.getChildren ear-node))
    1.52 +    (do (println-repl "could not find ears node") [])))
    1.53 +
    1.54 +
    1.55 +;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
    1.56 +;;anchor-qualia, augment-organ, with-organ
    1.57 +
    1.58 +
    1.59 +(defn update-listener-velocity
    1.60 +  "Update the listener's velocity every update loop."
    1.61 +  [#^Spatial obj #^Listener lis]
    1.62 +  (let [old-position (atom (.getLocation lis))]
    1.63 +    (.addControl
    1.64 +     obj
    1.65 +     (proxy [AbstractControl] []
    1.66 +       (controlUpdate [tpf]
    1.67 +         (let [new-position (.getLocation lis)]
    1.68 +           (.setVelocity
    1.69 +            lis 
    1.70 +            (.mult (.subtract new-position @old-position)
    1.71 +                   (float (/ tpf))))
    1.72 +           (reset! old-position new-position)))
    1.73 +       (controlRender [_ _])))))
    1.74 +
    1.75 +(import com.aurellem.capture.audio.AudioSendRenderer)
    1.76 +
    1.77 +(defn attach-ear
    1.78 +  [#^Application world #^Node creature #^Spatial ear continuation]
    1.79 +  (let [target (closest-node creature ear)
    1.80 +        lis (Listener.)
    1.81 +        audio-renderer (.getAudioRenderer world)
    1.82 +        sp (sound-processor continuation)]
    1.83 +    (.setLocation lis (.getWorldTranslation ear))
    1.84 +    (.setRotation lis (.getWorldRotation ear))
    1.85 +    (bind-sense target lis)
    1.86 +    (update-listener-velocity target lis)
    1.87 +    (.addListener audio-renderer lis)
    1.88 +    (.registerSoundProcessor audio-renderer lis sp)))
    1.89 +
    1.90 +(defn enable-hearing
    1.91 +  [#^Node creature #^Spatial ear]
    1.92 +  (let [hearing-data (atom [])]
    1.93 +    [(fn [world]
    1.94 +       (attach-ear world creature ear
    1.95 +                   (fn [data]
    1.96 +                     (reset! hearing-data (vec data)))))
    1.97 +     [(fn []
    1.98 +        (let [data @hearing-data
    1.99 +              topology              
   1.100 +              (vec (map #(vector % 0) (range 0 (count data))))
   1.101 +              scaled-data
   1.102 +              (vec
   1.103 +               (map
   1.104 +                #(rem (int (* 255 (/ (+ 1 %) 2)))  256)
   1.105 +                   data))]
   1.106 +          [topology scaled-data]))
   1.107 +        ]]))
   1.108 +
   1.109 +(defn hearing
   1.110 +  [#^Node creature]
   1.111 +  (reduce
   1.112 +   (fn [[init-a senses-a]
   1.113 +        [init-b senses-b]]
   1.114 +     [(conj init-a init-b)
   1.115 +      (into senses-a senses-b)])
   1.116 +   [[][]]      
   1.117 +   (for [ear (creature-ears creature)]
   1.118 +     (enable-hearing creature ear))))
   1.119 +
   1.120 +
   1.121  #+end_src
   1.122  
   1.123  * Example