Mercurial > audio-send
comparison org/ear.org @ 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 |
parents | 32c69ba451d9 |
children |
comparison
equal
deleted
inserted
replaced
30:32c69ba451d9 | 31:0e794e48a0cc |
---|---|
755 "Simulate the sense of hearing in jMonkeyEngine3. Enables multiple | 755 "Simulate the sense of hearing in jMonkeyEngine3. Enables multiple |
756 listeners at different positions in the same world. Passes vectors | 756 listeners at different positions in the same world. Passes vectors |
757 of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary | 757 of floats in the range [-1.0 -- 1.0] in PCM format to any arbitrary |
758 function." | 758 function." |
759 {:author "Robert McIntyre"} | 759 {:author "Robert McIntyre"} |
760 (:use (cortex world util)) | 760 (:use (cortex world util sense)) |
761 (:import java.nio.ByteBuffer) | 761 (:import java.nio.ByteBuffer) |
762 (:import org.tritonus.share.sampled.FloatSampleTools) | 762 (:import org.tritonus.share.sampled.FloatSampleTools) |
763 (:import com.aurellem.capture.audio.SoundProcessor) | 763 (:import com.aurellem.capture.audio.SoundProcessor) |
764 (:import javax.sound.sampled.AudioFormat)) | 764 (:import javax.sound.sampled.AudioFormat)) |
765 | 765 |
766 (cortex.import/mega-import-jme3) | |
767 | |
766 (defn sound-processor | 768 (defn sound-processor |
767 "Deals with converting ByteBuffers into Vectors of floats so that | 769 "Deals with converting ByteBuffers into Vectors of floats so that |
768 the continuation functions can be defined in terms of immutable | 770 the continuation functions can be defined in terms of immutable |
769 stuff." | 771 stuff." |
770 [continuation] | 772 [continuation] |
779 (FloatSampleTools/byte2floatInterleaved | 781 (FloatSampleTools/byte2floatInterleaved |
780 bytes 0 floats 0 num-floats audioFormat) | 782 bytes 0 floats 0 num-floats audioFormat) |
781 (continuation | 783 (continuation |
782 (vec floats)))))) | 784 (vec floats)))))) |
783 | 785 |
784 (defn add-ear | 786 |
785 "Add an ear to the world. The continuation function will be called | 787 |
786 on the FFT or the sounds which the ear hears in the given | 788 |
787 timeframe. Sound is 3D." | 789 ;; Ears work the same way as vision. |
788 [world listener continuation] | 790 |
789 (let [renderer (.getAudioRenderer world)] | 791 ;; (hearing creature) will return [init-functions |
790 (.addListener renderer listener) | 792 ;; sensor-functions]. The init functions each take the world and |
791 (.registerSoundProcessor renderer listener | 793 ;; register a SoundProcessor that does foureier transforms on the |
792 (sound-processor continuation)) | 794 ;; incommong sound data, making it available to each sensor function. |
793 listener)) | 795 |
796 (defn creature-ears | |
797 "Return the children of the creature's \"ears\" node." | |
798 ;;dylan | |
799 ;;"The ear nodes which are children of the \"ears\" node in the | |
800 ;;creature." | |
801 [#^Node creature] | |
802 (if-let [ear-node (.getChild creature "ears")] | |
803 (seq (.getChildren ear-node)) | |
804 (do (println-repl "could not find ears node") []))) | |
805 | |
806 | |
807 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli, | |
808 ;;anchor-qualia, augment-organ, with-organ | |
809 | |
810 | |
811 (defn update-listener-velocity | |
812 "Update the listener's velocity every update loop." | |
813 [#^Spatial obj #^Listener lis] | |
814 (let [old-position (atom (.getLocation lis))] | |
815 (.addControl | |
816 obj | |
817 (proxy [AbstractControl] [] | |
818 (controlUpdate [tpf] | |
819 (let [new-position (.getLocation lis)] | |
820 (.setVelocity | |
821 lis | |
822 (.mult (.subtract new-position @old-position) | |
823 (float (/ tpf)))) | |
824 (reset! old-position new-position))) | |
825 (controlRender [_ _]))))) | |
826 | |
827 (import com.aurellem.capture.audio.AudioSendRenderer) | |
828 | |
829 (defn attach-ear | |
830 [#^Application world #^Node creature #^Spatial ear continuation] | |
831 (let [target (closest-node creature ear) | |
832 lis (Listener.) | |
833 audio-renderer (.getAudioRenderer world) | |
834 sp (sound-processor continuation)] | |
835 (.setLocation lis (.getWorldTranslation ear)) | |
836 (.setRotation lis (.getWorldRotation ear)) | |
837 (bind-sense target lis) | |
838 (update-listener-velocity target lis) | |
839 (.addListener audio-renderer lis) | |
840 (.registerSoundProcessor audio-renderer lis sp))) | |
841 | |
842 (defn enable-hearing | |
843 [#^Node creature #^Spatial ear] | |
844 (let [hearing-data (atom [])] | |
845 [(fn [world] | |
846 (attach-ear world creature ear | |
847 (fn [data] | |
848 (reset! hearing-data (vec data))))) | |
849 [(fn [] | |
850 (let [data @hearing-data | |
851 topology | |
852 (vec (map #(vector % 0) (range 0 (count data)))) | |
853 scaled-data | |
854 (vec | |
855 (map | |
856 #(rem (int (* 255 (/ (+ 1 %) 2))) 256) | |
857 data))] | |
858 [topology scaled-data])) | |
859 ]])) | |
860 | |
861 (defn hearing | |
862 [#^Node creature] | |
863 (reduce | |
864 (fn [[init-a senses-a] | |
865 [init-b senses-b]] | |
866 [(conj init-a init-b) | |
867 (into senses-a senses-b)]) | |
868 [[][]] | |
869 (for [ear (creature-ears creature)] | |
870 (enable-hearing creature ear)))) | |
871 | |
872 | |
794 #+end_src | 873 #+end_src |
795 | 874 |
796 * Example | 875 * Example |
797 | 876 |
798 #+name: test-hearing | 877 #+name: test-hearing |