annotate src/body/ear.clj @ 0:f9476ff7637e

initial forking of open-al to create multiple listeners
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 13:02:31 -0700
parents
children
rev   line source
rlm@0 1
rlm@0 2 (ns body.ear)
rlm@0 3 (use 'cortex.world)
rlm@0 4 (use 'cortex.import)
rlm@0 5 (use 'clojure.contrib.def)
rlm@0 6 (cortex.import/mega-import-jme3)
rlm@0 7 (rlm.rlm-commands/help)
rlm@0 8 (import java.nio.ByteBuffer)
rlm@0 9 (import java.awt.image.BufferedImage)
rlm@0 10 (import java.awt.Color)
rlm@0 11 (import java.awt.Dimension)
rlm@0 12 (import java.awt.Graphics)
rlm@0 13 (import java.awt.Graphics2D)
rlm@0 14 (import java.awt.event.WindowAdapter)
rlm@0 15 (import java.awt.event.WindowEvent)
rlm@0 16 (import java.awt.image.BufferedImage)
rlm@0 17 (import java.nio.ByteBuffer)
rlm@0 18 (import javax.swing.JFrame)
rlm@0 19 (import javax.swing.JPanel)
rlm@0 20 (import javax.swing.SwingUtilities)
rlm@0 21 (import javax.swing.ImageIcon)
rlm@0 22 (import javax.swing.JOptionPane)
rlm@0 23 (import java.awt.image.ImageObserver)
rlm@0 24 (in-ns 'body.ear)
rlm@0 25 (import 'com.jme3.capture.SoundProcessor)
rlm@0 26
rlm@0 27
rlm@0 28 (defn sound-processor
rlm@0 29 "deals with converting ByteBuffers into Vectors of Bytes so that the
rlm@0 30 continuation functions can be defined in terms of immutable stuff."
rlm@0 31 [continuation]
rlm@0 32 (proxy [SoundProcessor] []
rlm@0 33 (cleanup [])
rlm@0 34 (process
rlm@0 35 [#^ByteBuffer audioSamples numSamples]
rlm@0 36
rlm@0 37 (let [byte-array (make-array Byte numSamples)]
rlm@0 38 (.get audioSamples byte-array 0 numSamples)
rlm@0 39
rlm@0 40 (continuation
rlm@0 41 (vec byte-array))))))
rlm@0 42
rlm@0 43
rlm@0 44 (defn add-ear
rlm@0 45 "add an ear to the world. The continuation function will be called
rlm@0 46 on the FFT or the sounds which the ear hears in the given
rlm@0 47 timeframe. Sound is 3D."
rlm@0 48 [world ear continuation]
rlm@0 49 (let [listener (Listener.)
rlm@0 50 renderer (.getAudioRenderer world)]
rlm@0 51 (.addListener renderer listener)
rlm@0 52 (.registerSoundProcessor renderer listener (sound-processor continuation))
rlm@0 53 listener))