comparison 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
comparison
equal deleted inserted replaced
163:985c73659923 164:c33a8e5fe7bc
747 simple. Just as there were =SceneProcessors= for vision, there are 747 simple. Just as there were =SceneProcessors= for vision, there are
748 now =SoundProcessors= for hearing. 748 now =SoundProcessors= for hearing.
749 749
750 #+include "../../jmeCapture/src/com/aurellem/capture/audio/SoundProcessor.java" src java 750 #+include "../../jmeCapture/src/com/aurellem/capture/audio/SoundProcessor.java" src java
751 751
752
753 Ears work the same way as vision.
754
755 (hearing creature) will return [init-functions sensor-functions]. The
756 init functions each take the world and register a SoundProcessor that
757 does foureier transforms on the incommong sound data, making it
758 available to each sensor function.
759
760
752 #+name: ears 761 #+name: ears
753 #+begin_src clojure 762 #+begin_src clojure
754 (ns cortex.hearing 763 (ns cortex.hearing
755 "Simulate the sense of hearing in jMonkeyEngine3. Enables multiple 764 "Simulate the sense of hearing in jMonkeyEngine3. Enables multiple
756 listeners at different positions in the same world. Passes vectors 765 listeners at different positions in the same world. Automatically
757 of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary 766 reads ear-nodes from specially prepared blender files and
758 function." 767 instantiates them in the world as actual ears."
759 {:author "Robert McIntyre"} 768 {:author "Robert McIntyre"}
760 (:use (cortex world util sense)) 769 (:use (cortex world util sense))
761 (:import java.nio.ByteBuffer) 770 (:import java.nio.ByteBuffer)
762 (:import org.tritonus.share.sampled.FloatSampleTools) 771 (:import org.tritonus.share.sampled.FloatSampleTools)
763 (:import com.aurellem.capture.audio.SoundProcessor) 772 (:import com.aurellem.capture.audio.SoundProcessor)
764 (:import javax.sound.sampled.AudioFormat)) 773 (:import javax.sound.sampled.AudioFormat))
765 774
766 (cortex.import/mega-import-jme3) 775 (cortex.import/mega-import-jme3)
776 (use 'clojure.contrib.def)
767 777
768 (defn sound-processor 778 (defn sound-processor
769 "Deals with converting ByteBuffers into Vectors of floats so that 779 "Deals with converting ByteBuffers into Vectors of floats so that
770 the continuation functions can be defined in terms of immutable 780 the continuation functions can be defined in terms of immutable
771 stuff." 781 stuff."
781 (FloatSampleTools/byte2floatInterleaved 791 (FloatSampleTools/byte2floatInterleaved
782 bytes 0 floats 0 num-floats audioFormat) 792 bytes 0 floats 0 num-floats audioFormat)
783 (continuation 793 (continuation
784 (vec floats)))))) 794 (vec floats))))))
785 795
786 (defn ears 796 (defvar
787 "Return the children of the creature's \"ears\" node." 797 ^{:arglists '([creature])}
788 [#^Node creature] 798 ears
789 (if-let [ear-node (.getChild creature "ears")] 799 (sense-nodes "ears")
790 (seq (.getChildren ear-node)) 800 "Return the children of the creature's \"ears\" node.")
791 (do (println-repl "could not find ears node") [])))
792 801
793 (defn update-listener-velocity! 802 (defn update-listener-velocity!
794 "Update the listener's velocity every update loop." 803 "Update the listener's velocity every update loop."
795 [#^Spatial obj #^Listener lis] 804 [#^Spatial obj #^Listener lis]
796 (let [old-position (atom (.getLocation lis))] 805 (let [old-position (atom (.getLocation lis))]
806 (reset! old-position new-position))) 815 (reset! old-position new-position)))
807 (controlRender [_ _]))))) 816 (controlRender [_ _])))))
808 817
809 (import com.aurellem.capture.audio.AudioSendRenderer) 818 (import com.aurellem.capture.audio.AudioSendRenderer)
810 819
811 (defn create-listener! 820 (defn create-listener!
821 "Create a Listener centered on the current position of 'ear
822 which follows the closest physical node in 'creature and
823 sends sound data to 'continuation."
812 [#^Application world #^Node creature #^Spatial ear continuation] 824 [#^Application world #^Node creature #^Spatial ear continuation]
813 (let [target (closest-node creature ear) 825 (let [target (closest-node creature ear)
814 lis (Listener.) 826 lis (Listener.)
815 audio-renderer (.getAudioRenderer world) 827 audio-renderer (.getAudioRenderer world)
816 sp (sound-processor continuation)] 828 sp (sound-processor continuation)]
820 (update-listener-velocity! target lis) 832 (update-listener-velocity! target lis)
821 (.addListener audio-renderer lis) 833 (.addListener audio-renderer lis)
822 (.registerSoundProcessor audio-renderer lis sp))) 834 (.registerSoundProcessor audio-renderer lis sp)))
823 835
824 (defn hearing-fn 836 (defn hearing-fn
837 "Returns a functon which returns auditory sensory data when called
838 inside a running simulation."
825 [#^Node creature #^Spatial ear] 839 [#^Node creature #^Spatial ear]
826 (let [hearing-data (atom [])] 840 (let [hearing-data (atom [])
827 [(fn [world] 841 register-listener!
828 (create-listener! world creature ear 842 (runonce
829 (fn [data] 843 (fn [#^Application world]
830 (reset! hearing-data (vec data))))) 844 (create-listener!
831 [(fn [] 845 world creature ear
832 (let [data @hearing-data 846 (fn [data]
833 topology 847 (reset! hearing-data (vec data))))))]
834 (vec (map #(vector % 0) (range 0 (count data)))) 848 (fn [#^Application world]
835 scaled-data 849 (register-listener! world)
836 (vec 850 (let [data @hearing-data
837 (map 851 topology
838 #(rem (int (* 255 (/ (+ 1 %) 2))) 256) 852 (vec (map #(vector % 0) (range 0 (count data))))
839 data))] 853 scaled-data
840 [topology scaled-data])) 854 (vec
841 ]])) 855 (map
842 856 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)
857 data))]
858 [topology scaled-data]))))
859
843 (defn hearing! 860 (defn hearing!
861 "Endow the creature in a particular world with the sense of
862 hearing. Will return a sequence of functions, one for each ear,
863 which when called will return the auditory data from that ear."
844 [#^Node creature] 864 [#^Node creature]
845 (reduce 865 (for [ear (ears creature)]
846 (fn [[init-a senses-a] 866 (hearing-fn creature ear)))
847 [init-b senses-b]]
848 [(conj init-a init-b)
849 (into senses-a senses-b)])
850 [[][]]
851 (for [ear (ears creature)]
852 (hearing-fn creature ear))))
853 867
854 868
855 #+end_src 869 #+end_src
856 870
857 * Example 871 * Example