rlm@0: #+title: The EARS! rlm@0: #+author: Robert McIntyre rlm@0: #+email: rlm@mit.edu rlm@0: #+MATHJAX: align:"left" mathml:t path:"../aurellem/src/MathJax/MathJax.js" rlm@0: #+STYLE: rlm@0: #+BABEL: :exports both :noweb yes :cache no :mkdirp yes rlm@0: #+INCLUDE: ../aurellem/src/templates/level-0.org rlm@0: #+description: Simulating multiple listeners and the sense of hearing in uMonkeyEngine3 rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: * Ears! rlm@0: rlm@0: I want to be able to place ears in a similiar manner to how I place rlm@0: the eyes. I want to be able to place ears in a unique spatial rlm@0: position, and recieve as output at every tick the FFT of whatever rlm@0: signals are happening at that point. rlm@0: rlm@0: #+srcname: ear-header rlm@0: #+begin_src clojure rlm@0: (ns body.ear) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.awt.Color) rlm@0: (import java.awt.Dimension) rlm@0: (import java.awt.Graphics) rlm@0: (import java.awt.Graphics2D) rlm@0: (import java.awt.event.WindowAdapter) rlm@0: (import java.awt.event.WindowEvent) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import javax.swing.JFrame) rlm@0: (import javax.swing.JPanel) rlm@0: (import javax.swing.SwingUtilities) rlm@0: (import javax.swing.ImageIcon) rlm@0: (import javax.swing.JOptionPane) rlm@0: (import java.awt.image.ImageObserver) rlm@0: #+end_src rlm@0: rlm@0: JMonkeyEngine3's audio system works as follows: rlm@0: first, an appropiate audio renderer is created during initialization rlm@0: and depending on the context. On my computer, this is the rlm@0: LwjglAudioRenderer. rlm@0: rlm@0: The LwjglAudioRenderer sets a few internal state variables depending rlm@0: on what capabilities the audio system has. rlm@0: rlm@0: may very well need to make my own AudioRenderer rlm@0: rlm@0: #+srcname: ear-body-1 rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'body.ear) rlm@0: (import 'com.jme3.capture.SoundProcessor) rlm@0: rlm@0: rlm@0: (defn sound-processor rlm@0: "deals with converting ByteBuffers into Arrays of bytes so that the rlm@0: continuation functions can be defined in terms of immutable stuff." rlm@0: [continuation] rlm@0: (proxy [SoundProcessor] [] rlm@0: (cleanup []) rlm@0: (process rlm@0: [#^ByteBuffer audioSamples numSamples] rlm@0: (no-exceptions rlm@0: (let [byte-array (byte-array numSamples)] rlm@0: (.get audioSamples byte-array 0 numSamples) rlm@0: (continuation rlm@0: (vec byte-array))))))) rlm@0: rlm@0: rlm@0: (defn add-ear rlm@0: "add an ear to the world. The continuation function will be called rlm@0: on the FFT or the sounds which the ear hears in the given rlm@0: timeframe. Sound is 3D." rlm@0: [world listener continuation] rlm@0: (let [renderer (.getAudioRenderer world)] rlm@0: (.addListener renderer listener) rlm@0: (.registerSoundProcessor renderer listener rlm@0: (sound-processor continuation)) rlm@0: listener)) rlm@0: rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: #+srcname: test-hearing rlm@0: #+begin_src clojure :results silent rlm@0: (ns test.hearing) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (use 'body.ear) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: rlm@0: (defn setup-fn [world] rlm@0: (let [listener (Listener.)] rlm@0: (add-ear world listener #(println (nth % 0))))) rlm@0: rlm@0: (defn play-sound [node world value] rlm@0: (if (not value) rlm@0: (do rlm@0: (.playSource (.getAudioRenderer world) node)))) rlm@0: rlm@0: (defn test-world [] rlm@0: (let [node1 (AudioNode. (asset-manager) "Sounds/pure.wav" false false)] rlm@0: (world rlm@0: (Node.) rlm@0: {"key-space" (partial play-sound node1)} rlm@0: setup-fn rlm@0: no-op rlm@0: ))) rlm@0: rlm@0: rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: * COMMENT Code Generation rlm@0: rlm@0: #+begin_src clojure :tangle /home/r/cortex/src/body/ear.clj rlm@0: <> rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: rlm@0: #+begin_src clojure :tangle /home/r/cortex/src/test/hearing.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: