Mercurial > jmeCapture
diff src/com/aurellem/capture/examples/AdvancedAudio.java @ 33:c4bfbf5d090e
starting to get something that can percieve sound
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 30 Oct 2011 13:57:16 -0700 |
parents | be37291c62b8 |
children | 13d354e1184b |
line wrap: on
line diff
1.1 --- a/src/com/aurellem/capture/examples/AdvancedAudio.java Sun Oct 30 10:17:09 2011 -0700 1.2 +++ b/src/com/aurellem/capture/examples/AdvancedAudio.java Sun Oct 30 13:57:16 2011 -0700 1.3 @@ -1,13 +1,20 @@ 1.4 package com.aurellem.capture.examples; 1.5 1.6 import java.io.File; 1.7 +import java.io.FileNotFoundException; 1.8 +import java.io.IOException; 1.9 import java.nio.ByteBuffer; 1.10 1.11 import javax.sound.sampled.AudioFormat; 1.12 1.13 +import org.tritonus.share.sampled.FloatSampleTools; 1.14 + 1.15 +import com.aurellem.capture.Capture; 1.16 import com.aurellem.capture.IsoTimer; 1.17 +import com.aurellem.capture.audio.CompositeSoundProcessor; 1.18 import com.aurellem.capture.audio.MultiListener; 1.19 import com.aurellem.capture.audio.SoundProcessor; 1.20 +import com.aurellem.capture.audio.WaveFileWriter; 1.21 import com.jme3.app.SimpleApplication; 1.22 import com.jme3.audio.AudioNode; 1.23 import com.jme3.audio.Listener; 1.24 @@ -59,6 +66,8 @@ 1.25 app.setShowSettings(false); 1.26 app.setPauseOnLostFocus(false); 1.27 org.lwjgl.input.Mouse.setGrabbed(false); 1.28 + try {Capture.captureVideo(app, new File("/home/r/tmp/out.avi"));} 1.29 + catch (IOException e) {e.printStackTrace();} 1.30 app.start(); 1.31 } 1.32 1.33 @@ -162,10 +171,11 @@ 1.34 1.35 1.36 motionControl = new MotionTrack(bell,track); 1.37 + motionControl.setTimer(new IsoTimer(60)); 1.38 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation); 1.39 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y)); 1.40 - motionControl.setInitialDuration(10f); 1.41 - motionControl.setSpeed(0.1f); 1.42 + motionControl.setInitialDuration(20f); 1.43 + motionControl.setSpeed(1f); 1.44 1.45 1.46 track.enableDebugShape(assetManager, rootNode); 1.47 @@ -195,12 +205,12 @@ 1.48 1.49 rootNode.attachChild(music); 1.50 audioRenderer.playSource(music); 1.51 + music.setPositional(true); 1.52 + //music.setVolume(1f); 1.53 1.54 - music.setVolume(1f); 1.55 - music.setPositional(true); 1.56 - music.setMaxDistance(200.0f); 1.57 - music.setRefDistance(0.1f); 1.58 - music.setRolloffFactor(5f); 1.59 + //music.setMaxDistance(200.0f); 1.60 + //music.setRefDistance(0.1f); 1.61 + //music.setRolloffFactor(5f); 1.62 audioRenderer.pauseSource(music); 1.63 1.64 } 1.65 @@ -222,9 +232,10 @@ 1.66 Spatial entity; 1.67 1.68 float scale = 2; 1.69 - 1.70 - public Dancer(Spatial entity){ 1.71 + String debug; 1.72 + public Dancer(Spatial entity, String debug){ 1.73 this.entity = entity; 1.74 + this.debug = debug; 1.75 } 1.76 1.77 /** 1.78 @@ -238,10 +249,24 @@ 1.79 * hears it's surroundings and reacts to them. 1.80 */ 1.81 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) { 1.82 - //System.out.println("I'm DANCING <3"); 1.83 - entity.scale(this.scale); 1.84 - if (this.scale == 2f){this.scale = 0.5f;} 1.85 - else {this.scale = 2;} 1.86 + audioSamples.clear(); 1.87 + byte[] data = new byte[numSamples]; 1.88 + float[] out = new float[numSamples]; 1.89 + audioSamples.get(data); 1.90 + FloatSampleTools.byte2floatInterleaved(data, 0, out, 0, 1.91 + numSamples/format.getFrameSize(), format); 1.92 + 1.93 + float max = Float.NEGATIVE_INFINITY; 1.94 + for (float f : out){if (f > max) max = f;} 1.95 + 1.96 + System.out.println(debug); 1.97 + System.out.println(max); 1.98 + 1.99 + 1.100 + 1.101 + //entity.scale(this.scale); 1.102 + //if (this.scale == 2f){this.scale = 0.5f;} 1.103 + //else {this.scale = 2;} 1.104 } 1.105 1.106 1.107 @@ -260,10 +285,21 @@ 1.108 1.109 1.110 rf.addListener(auxListener); 1.111 + WaveFileWriter writer = null; 1.112 + WaveFileWriter writer2 = null; 1.113 + auxListener.setLocation(ear1.getLocalTranslation()); 1.114 + listener.setLocation(ear1.getLocalTranslation()); 1.115 + try {writer = new WaveFileWriter(new File("/home/r/tmp/out.wav"));} 1.116 + catch (FileNotFoundException e) {e.printStackTrace();} 1.117 1.118 - //rf.registerSoundProcessor(new WaveFileWriter(data1)); 1.119 - rf.registerSoundProcessor(auxListener, new Dancer(ear1)); 1.120 + try {writer2 = new WaveFileWriter(new File("/home/r/tmp/outmain.wav"));} 1.121 + catch (FileNotFoundException e) {e.printStackTrace();} 1.122 + 1.123 + rf.registerSoundProcessor(auxListener, 1.124 + new CompositeSoundProcessor(new Dancer(ear1, "aux"), writer)); 1.125 1.126 + rf.registerSoundProcessor( 1.127 + new CompositeSoundProcessor(new Dancer(ear1, "main"), writer2)); 1.128 } 1.129 1.130 motionControl.play(); 1.131 @@ -285,7 +321,7 @@ 1.132 if (name.equals("Shoot") && !keyPressed) { 1.133 System.out.println("I'm playing! <3"); 1.134 System.out.println(bell.getLocalTranslation().subtract(cam.getLocation()).length()); 1.135 - 1.136 + bell.getMaterial().setColor("Color", ColorRGBA.randomColor()); 1.137 audioRenderer.playSource(music); 1.138 System.out.println(music.getRefDistance()); 1.139 1.140 @@ -297,6 +333,8 @@ 1.141 1.142 1.143 private Vector3f prevBellPos = Vector3f.ZERO; 1.144 + 1.145 + 1.146 public void simpleUpdate(float tpf) { 1.147 //Vector3f loc = cam.getLocation(); 1.148 //Quaternion rot = cam.getRotation(); 1.149 @@ -304,14 +342,14 @@ 1.150 //listener.setRotation(rot); 1.151 1.152 1.153 - listener.setLocation(cam.getLocation()); 1.154 - listener.setRotation(cam.getRotation()); 1.155 + //listener.setLocation(cam.getLocation()); 1.156 + //listener.setRotation(cam.getRotation()); 1.157 //auxListener.setLocation(loc); 1.158 //auxListener.setRotation(rot); 1.159 - //if (music.getStatus() == AudioNode.Status.Stopped){ 1.160 - 1.161 - //music.playInstance(); 1.162 - //} 1.163 + if (music.getStatus() != AudioNode.Status.Playing){ 1.164 + audioRenderer.playSource(music); 1.165 + bell.getMaterial().setColor("Color", ColorRGBA.randomColor()); 1.166 + } 1.167 //audioRenderer.updateSourceParam(music, AudioParam.Direction); 1.168 1.169 Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);