Mercurial > jmeCapture
changeset 12:d10f4d4ff15a
going to improve documentation
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 27 Oct 2011 21:07:50 -0700 |
parents | 8a6b1684f536 |
children | 6dc62c7866c2 |
files | build.xml src/com/aurellem/capture/Capture.java src/com/aurellem/capture/hello/AdvancedAudio.java src/com/aurellem/capture/hello/BasicAVRecord.java src/com/aurellem/capture/hello/HelloAudio.java src/com/aurellem/capture/hello/HelloVideo.java |
diffstat | 6 files changed, 291 insertions(+), 170 deletions(-) [+] |
line wrap: on
line diff
1.1 --- a/build.xml Thu Oct 27 02:27:02 2011 -0700 1.2 +++ b/build.xml Thu Oct 27 21:07:50 2011 -0700 1.3 @@ -11,8 +11,6 @@ 1.4 <pathelement path="${lib}/lwjgl.jar"/> 1.5 <pathelement path="${lib}/xuggle/xuggle-xuggler.jar"/> 1.6 <pathelement path="${lib}/audio-send.jar"/> 1.7 - 1.8 - 1.9 </path> 1.10 1.11 <target name="prepare">
2.1 --- a/src/com/aurellem/capture/Capture.java Thu Oct 27 02:27:02 2011 -0700 2.2 +++ b/src/com/aurellem/capture/Capture.java Thu Oct 27 21:07:50 2011 -0700 2.3 @@ -2,34 +2,73 @@ 2.4 2.5 import java.io.File; 2.6 import java.io.IOException; 2.7 +import java.util.concurrent.Callable; 2.8 2.9 +import com.aurellem.capture.audio.MultiListener; 2.10 +import com.aurellem.capture.audio.WaveFileWriter; 2.11 import com.aurellem.capture.video.AVIVideoRecorder; 2.12 import com.aurellem.capture.video.AbstractVideoRecorder; 2.13 import com.aurellem.capture.video.XuggleVideoRecorder; 2.14 import com.jme3.app.Application; 2.15 -import com.jme3.math.ColorRGBA; 2.16 +import com.jme3.audio.AudioRenderer; 2.17 +import com.jme3.renderer.ViewPort; 2.18 +import com.jme3.scene.Spatial; 2.19 +import com.jme3.system.AppSettings; 2.20 2.21 public class Capture { 2.22 2.23 - public static void SimpleCaptureVideo(Application app, File file) throws IOException{ 2.24 - app.getViewPort().setClearFlags(true, true, true); 2.25 - // this prevents pixels from staying in the render buffer between frames 2.26 - // and messing the video up. It's not a problem since Black is the default, and this 2.27 - // can be overridden by user code. 2.28 - app.getViewPort().setBackgroundColor(ColorRGBA.Black); 2.29 - 2.30 + public static void captureVideo(final Application app, final File file) throws IOException{ 2.31 + 2.32 + final AbstractVideoRecorder videoRecorder; 2.33 // The XuggleVideoRecorder is better than the AVIVideoRecorder in every way 2.34 // except for ease of installation. The excellent work by Werner Randelshofer 2.35 // is used as a fallback option. Please visit http://www.xuggle.com/ to learn 2.36 // how to set up the XuggleVideoRecorder. 2.37 + 2.38 + if (file.getCanonicalPath().endsWith(".avi")){ 2.39 + videoRecorder = new AVIVideoRecorder(file);} 2.40 + else { videoRecorder = new XuggleVideoRecorder(file);} 2.41 + 2.42 + Callable<Object> thunk = new Callable<Object>(){ 2.43 + public Object call(){ 2.44 + 2.45 + ViewPort viewPort = 2.46 + app.getRenderManager() 2.47 + .createPostView("aurellem record", app.getCamera()); 2.48 + 2.49 + viewPort.setClearFlags(false, false, false); 2.50 + 2.51 + // get GUI node stuff 2.52 + for (Spatial s : app.getGuiViewPort().getScenes()){ 2.53 + viewPort.attachScene(s); 2.54 + } 2.55 + 2.56 + app.getStateManager().attach(videoRecorder); 2.57 + viewPort.addProcessor(videoRecorder); 2.58 + return null; 2.59 + } 2.60 + }; 2.61 + app.enqueue(thunk); 2.62 + } 2.63 + 2.64 + 2.65 + public static void captureAudio(final Application app, final File file) throws IOException{ 2.66 + AppSettings settings = new AppSettings(true); 2.67 + settings.setAudioRenderer("Send"); 2.68 + app.setSettings(settings); 2.69 + 2.70 + Callable<Object> thunk = new Callable<Object>(){ 2.71 + public Object call(){ 2.72 + AudioRenderer ar = app.getAudioRenderer(); 2.73 + if (ar instanceof MultiListener){ 2.74 + MultiListener ml = (MultiListener)ar; 2.75 + ml.registerSoundProcessor(new WaveFileWriter(file)); 2.76 + } 2.77 + return null; 2.78 + } 2.79 + }; 2.80 2.81 - AbstractVideoRecorder videoRecorder; 2.82 - 2.83 - if (file.getCanonicalPath().endsWith(".avi")){ 2.84 - videoRecorder = new AVIVideoRecorder(file);} 2.85 - else { videoRecorder = new XuggleVideoRecorder(file);} 2.86 - 2.87 - app.getStateManager().attach(videoRecorder); 2.88 - app.getViewPort().addFinalProcessor(videoRecorder); 2.89 - } 2.90 + app.enqueue(thunk); 2.91 + } 2.92 + 2.93 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/com/aurellem/capture/hello/AdvancedAudio.java Thu Oct 27 21:07:50 2011 -0700 3.3 @@ -0,0 +1,158 @@ 3.4 +package com.aurellem.capture.hello; 3.5 + 3.6 +import java.io.File; 3.7 + 3.8 +import com.aurellem.capture.IsoTimer; 3.9 +import com.aurellem.capture.audio.MultiListener; 3.10 +import com.aurellem.capture.audio.WaveFileWriter; 3.11 +import com.jme3.app.SimpleApplication; 3.12 +import com.jme3.audio.AudioNode; 3.13 +import com.jme3.audio.Listener; 3.14 +import com.jme3.input.controls.ActionListener; 3.15 +import com.jme3.input.controls.MouseButtonTrigger; 3.16 +import com.jme3.material.Material; 3.17 +import com.jme3.math.ColorRGBA; 3.18 +import com.jme3.math.Quaternion; 3.19 +import com.jme3.math.Vector3f; 3.20 +import com.jme3.scene.Geometry; 3.21 +import com.jme3.scene.shape.Box; 3.22 +import com.jme3.system.AppSettings; 3.23 + 3.24 + 3.25 +/** 3.26 + * 3.27 + * 3.28 + * 3.29 + * @author Robert McIntyre 3.30 + * 3.31 + */ 3.32 + 3.33 +public class AdvancedAudio extends SimpleApplication { 3.34 + 3.35 + 3.36 + 3.37 + public static void main(String[] args) { 3.38 + 3.39 + // Logger.getLogger("com.jme3").setLevel(Level.OFF); 3.40 + 3.41 + AdvancedAudio app = new AdvancedAudio(); 3.42 + AppSettings settings = new AppSettings(true); 3.43 + 3.44 + settings.setAudioRenderer("Send"); 3.45 + app.setSettings(settings); 3.46 + app.setShowSettings(false); 3.47 + app.start(); 3.48 + app.setPauseOnLostFocus(false); 3.49 + } 3.50 + 3.51 + 3.52 + 3.53 + private AudioNode audio_gun; 3.54 + private AudioNode audio_nature; 3.55 + private Geometry player; 3.56 + private Listener auxListener = new Listener(); 3.57 + public File data1 = new File("/home/r/tmp/data1.wav"); 3.58 + public File data2 = new File("/home/r/tmp/data2.wav"); 3.59 + public File data3 = new File("/home/r/tmp/data3.wav"); 3.60 + 3.61 + private File makeTarget(int n){ 3.62 + return new File("/home/r/tmp/assload-" + n + ".wav"); 3.63 + } 3.64 + 3.65 + 3.66 + @Override 3.67 + public void simpleInitApp() { 3.68 + this.setTimer(new IsoTimer(60)); 3.69 + 3.70 + 3.71 + if (this.audioRenderer instanceof MultiListener){ 3.72 + MultiListener rf = (MultiListener)this.audioRenderer; 3.73 + 3.74 + for (int n = 0; n < 0; n++){ 3.75 + Listener zzz = new Listener(); 3.76 + rf.addListener(zzz); 3.77 + rf.registerSoundProcessor( 3.78 + zzz, new WaveFileWriter(makeTarget(n))); 3.79 + } 3.80 + Listener listener3 = new Listener(); 3.81 + rf.addListener(auxListener); 3.82 + rf.addListener(listener3); 3.83 + rf.registerSoundProcessor(new WaveFileWriter(data1)); 3.84 + rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2)); 3.85 + rf.registerSoundProcessor(listener3, new WaveFileWriter(data3)); 3.86 + } 3.87 + flyCam.setMoveSpeed(40); 3.88 + 3.89 + /** just a blue box floating in space */ 3.90 + Box box1 = new Box(Vector3f.ZERO, 1, 1, 1); 3.91 + player = new Geometry("Player", box1); 3.92 + Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 3.93 + mat1.setColor("Color", ColorRGBA.Blue); 3.94 + player.setMaterial(mat1); 3.95 + rootNode.attachChild(player); 3.96 + 3.97 + /** custom init methods, see below */ 3.98 + initKeys(); 3.99 + initAudio(); 3.100 + 3.101 + this.audioRenderer.playSource(audio_gun); 3.102 + 3.103 + 3.104 + 3.105 + } 3.106 + 3.107 + /** We create two audio nodes. */ 3.108 + private void initAudio() { 3.109 + //audioRenderer.setEnvironment(Environment.Cavern); 3.110 + /* gun shot sound is to be triggered by a mouse click. */ 3.111 + audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false); 3.112 + //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false); 3.113 + audio_gun.setLooping(false); 3.114 + audio_gun.setVolume(2); 3.115 + audio_gun.setPositional(true); 3.116 + 3.117 + 3.118 + /* nature sound - keeps playing in a loop. */ 3.119 + audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false); 3.120 + audio_nature.setLooping(true); 3.121 + audio_nature.setPositional(true); 3.122 + audio_nature.setLocalTranslation(Vector3f.ZERO.clone()); 3.123 + audio_nature.setVolume(3); 3.124 + audio_nature.updateGeometricState(); 3.125 + } 3.126 + 3.127 + /** Declaring the "Shoot" action, and 3.128 + * mapping it to a trigger (mouse click). */ 3.129 + private void initKeys() { 3.130 + inputManager.addMapping("Shoot", new MouseButtonTrigger(0)); 3.131 + inputManager.addListener(actionListener, "Shoot"); 3.132 + } 3.133 + 3.134 + /** Defining the "Shoot" action: Play a gun sound. */ 3.135 + private ActionListener actionListener = new ActionListener() { 3.136 + @Override 3.137 + public void onAction(String name, boolean keyPressed, float tpf) { 3.138 + if (name.equals("Shoot") && !keyPressed) { 3.139 + audioRenderer.playSource(audio_gun); // play once! 3.140 + } 3.141 + } 3.142 + }; 3.143 + 3.144 + /** Move the listener with the camera - for 3D audio. */ 3.145 + @Override 3.146 + public void simpleUpdate(float tpf) { 3.147 + Vector3f loc = cam.getLocation(); 3.148 + Quaternion rot = cam.getRotation(); 3.149 + listener.setLocation(loc); 3.150 + listener.setRotation(rot); 3.151 + auxListener.setLocation(loc); 3.152 + auxListener.setRotation(rot); 3.153 + if (audio_gun.getStatus() == AudioNode.Status.Stopped){ 3.154 + System.out.println("I'm Stopped!"); 3.155 + this.requestClose(false); 3.156 + } 3.157 + 3.158 + 3.159 + } 3.160 + 3.161 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/com/aurellem/capture/hello/BasicAVRecord.java Thu Oct 27 21:07:50 2011 -0700 4.3 @@ -0,0 +1,76 @@ 4.4 +package com.aurellem.capture.hello; 4.5 + 4.6 +import java.io.File; 4.7 +import java.io.IOException; 4.8 + 4.9 +import jme3test.helloworld.HelloAudio; 4.10 +import jme3test.helloworld.HelloJME3; 4.11 +import jme3test.niftygui.TestNiftyExamples; 4.12 +import jme3test.water.TestPostWater; 4.13 + 4.14 +import com.aurellem.capture.Capture; 4.15 +import com.aurellem.capture.IsoTimer; 4.16 +import com.jme3.app.SimpleApplication; 4.17 + 4.18 + 4.19 +/** 4.20 + * 4.21 + * Demonstrates how to use basic Audio/Video capture with a jMonkeyEngine 4.22 + * application. 4.23 + * 4.24 + * @author Robert McIntyre 4.25 + * 4.26 + */ 4.27 + 4.28 +public class BasicAVRecord { 4.29 + 4.30 + public static void basicVideo() throws IOException{ 4.31 + File video = File.createTempFile("HelloJME3", ".avi"); 4.32 + System.out.println("Saving video to: " + video.getCanonicalPath()); 4.33 + SimpleApplication app = new HelloJME3(); 4.34 + app.setTimer(new IsoTimer(60)); 4.35 + 4.36 + Capture.captureVideo(app, video); 4.37 + app.start(); 4.38 + } 4.39 + 4.40 + public static void basicVideoGUI() throws IOException { 4.41 + File video = File.createTempFile("GUI", ".avi"); 4.42 + System.out.println("Saving video to: " + video.getCanonicalPath()); 4.43 + SimpleApplication app = new TestNiftyExamples(); 4.44 + app.setTimer(new IsoTimer(60)); 4.45 + 4.46 + Capture.captureVideo(app, video); 4.47 + app.start(); 4.48 + } 4.49 + 4.50 + public static void basicAudio() throws IOException{ 4.51 + File audio = File.createTempFile("BasicAudio", ".wav"); 4.52 + System.out.println("Saving audio to: " + audio.getCanonicalPath()); 4.53 + SimpleApplication app = new HelloAudio(); 4.54 + app.setTimer(new IsoTimer(60)); 4.55 + 4.56 + // you will not hear the audio while it is being captured. 4.57 + Capture.captureAudio(app, audio); 4.58 + 4.59 + app.start(); 4.60 + } 4.61 + 4.62 + public static void basicAudioVideo() throws IOException{ 4.63 + File video = new File("/home/r/tmp/basicVideo.avi"); 4.64 + File audio = new File("/home/r/tmp/basicAudio.wav"); 4.65 + 4.66 + SimpleApplication app = new TestPostWater(); 4.67 + app.setTimer(new IsoTimer(60)); 4.68 + 4.69 + Capture.captureVideo(app, video); 4.70 + Capture.captureAudio(app, audio); 4.71 + 4.72 + app.start(); 4.73 + } 4.74 + 4.75 + 4.76 + public static void main(String[] ignore) throws IOException{ 4.77 + basicAudio(); 4.78 + } 4.79 +}
5.1 --- a/src/com/aurellem/capture/hello/HelloAudio.java Thu Oct 27 02:27:02 2011 -0700 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,150 +0,0 @@ 5.4 -package com.aurellem.capture.hello; 5.5 - 5.6 -import java.io.File; 5.7 - 5.8 -import com.aurellem.capture.IsoTimer; 5.9 -import com.aurellem.capture.audio.MultiListener; 5.10 -import com.aurellem.capture.audio.WaveFileWriter; 5.11 -import com.jme3.app.SimpleApplication; 5.12 -import com.jme3.audio.AudioNode; 5.13 -import com.jme3.audio.Listener; 5.14 -import com.jme3.input.controls.ActionListener; 5.15 -import com.jme3.input.controls.MouseButtonTrigger; 5.16 -import com.jme3.material.Material; 5.17 -import com.jme3.math.ColorRGBA; 5.18 -import com.jme3.math.Quaternion; 5.19 -import com.jme3.math.Vector3f; 5.20 -import com.jme3.scene.Geometry; 5.21 -import com.jme3.scene.shape.Box; 5.22 -import com.jme3.system.AppSettings; 5.23 - 5.24 -/** Sample 11 - playing 3D audio. */ 5.25 -public class HelloAudio extends SimpleApplication { 5.26 - 5.27 - 5.28 - 5.29 - public static void main(String[] args) { 5.30 - 5.31 - // Logger.getLogger("com.jme3").setLevel(Level.OFF); 5.32 - 5.33 - HelloAudio app = new HelloAudio(); 5.34 - AppSettings settings = new AppSettings(true); 5.35 - 5.36 - //settings.setAudioRenderer("Send"); 5.37 - app.setSettings(settings); 5.38 - app.setShowSettings(false); 5.39 - app.start(); 5.40 - app.setPauseOnLostFocus(false); 5.41 - } 5.42 - 5.43 - 5.44 - 5.45 - private AudioNode audio_gun; 5.46 - private AudioNode audio_nature; 5.47 - private Geometry player; 5.48 - private Listener auxListener = new Listener(); 5.49 - public File data1 = new File("/home/r/tmp/data1.wav"); 5.50 - public File data2 = new File("/home/r/tmp/data2.wav"); 5.51 - public File data3 = new File("/home/r/tmp/data3.wav"); 5.52 - 5.53 - private File makeTarget(int n){ 5.54 - return new File("/home/r/tmp/assload-" + n + ".wav"); 5.55 - } 5.56 - 5.57 - 5.58 - @Override 5.59 - public void simpleInitApp() { 5.60 - this.setTimer(new IsoTimer(60)); 5.61 - 5.62 - 5.63 - if (this.audioRenderer instanceof MultiListener){ 5.64 - MultiListener rf = (MultiListener)this.audioRenderer; 5.65 - 5.66 - for (int n = 0; n < 0; n++){ 5.67 - Listener zzz = new Listener(); 5.68 - rf.addListener(zzz); 5.69 - rf.registerSoundProcessor( 5.70 - zzz, new WaveFileWriter(makeTarget(n))); 5.71 - } 5.72 - Listener listener3 = new Listener(); 5.73 - rf.addListener(auxListener); 5.74 - rf.addListener(listener3); 5.75 - rf.registerSoundProcessor(new WaveFileWriter(data1)); 5.76 - rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2)); 5.77 - rf.registerSoundProcessor(listener3, new WaveFileWriter(data3)); 5.78 - } 5.79 - flyCam.setMoveSpeed(40); 5.80 - 5.81 - /** just a blue box floating in space */ 5.82 - Box box1 = new Box(Vector3f.ZERO, 1, 1, 1); 5.83 - player = new Geometry("Player", box1); 5.84 - Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 5.85 - mat1.setColor("Color", ColorRGBA.Blue); 5.86 - player.setMaterial(mat1); 5.87 - rootNode.attachChild(player); 5.88 - 5.89 - /** custom init methods, see below */ 5.90 - initKeys(); 5.91 - initAudio(); 5.92 - 5.93 - this.audioRenderer.playSource(audio_gun); 5.94 - 5.95 - 5.96 - 5.97 - } 5.98 - 5.99 - /** We create two audio nodes. */ 5.100 - private void initAudio() { 5.101 - //audioRenderer.setEnvironment(Environment.Cavern); 5.102 - /* gun shot sound is to be triggered by a mouse click. */ 5.103 - audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false); 5.104 - //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false); 5.105 - audio_gun.setLooping(false); 5.106 - audio_gun.setVolume(2); 5.107 - audio_gun.setPositional(true); 5.108 - 5.109 - 5.110 - /* nature sound - keeps playing in a loop. */ 5.111 - audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false); 5.112 - audio_nature.setLooping(true); 5.113 - audio_nature.setPositional(true); 5.114 - audio_nature.setLocalTranslation(Vector3f.ZERO.clone()); 5.115 - audio_nature.setVolume(3); 5.116 - audio_nature.updateGeometricState(); 5.117 - } 5.118 - 5.119 - /** Declaring the "Shoot" action, and 5.120 - * mapping it to a trigger (mouse click). */ 5.121 - private void initKeys() { 5.122 - inputManager.addMapping("Shoot", new MouseButtonTrigger(0)); 5.123 - inputManager.addListener(actionListener, "Shoot"); 5.124 - } 5.125 - 5.126 - /** Defining the "Shoot" action: Play a gun sound. */ 5.127 - private ActionListener actionListener = new ActionListener() { 5.128 - @Override 5.129 - public void onAction(String name, boolean keyPressed, float tpf) { 5.130 - if (name.equals("Shoot") && !keyPressed) { 5.131 - audioRenderer.playSource(audio_gun); // play once! 5.132 - } 5.133 - } 5.134 - }; 5.135 - 5.136 - /** Move the listener with the camera - for 3D audio. */ 5.137 - @Override 5.138 - public void simpleUpdate(float tpf) { 5.139 - Vector3f loc = cam.getLocation(); 5.140 - Quaternion rot = cam.getRotation(); 5.141 - listener.setLocation(loc); 5.142 - listener.setRotation(rot); 5.143 - auxListener.setLocation(loc); 5.144 - auxListener.setRotation(rot); 5.145 - if (audio_gun.getStatus() == AudioNode.Status.Stopped){ 5.146 - System.out.println("I'm Stopped!"); 5.147 - this.requestClose(false); 5.148 - } 5.149 - 5.150 - 5.151 - } 5.152 - 5.153 -}
6.1 --- a/src/com/aurellem/capture/hello/HelloVideo.java Thu Oct 27 02:27:02 2011 -0700 6.2 +++ b/src/com/aurellem/capture/hello/HelloVideo.java Thu Oct 27 21:07:50 2011 -0700 6.3 @@ -48,7 +48,7 @@ 6.4 }catch (IOException e) { 6.5 e.printStackTrace();} 6.6 */ 6.7 - try {Capture.SimpleCaptureVideo(this, movingVideo);} 6.8 + try {Capture.captureVideo(this, movingVideo);} 6.9 catch (IOException e) {e.printStackTrace();} 6.10 6.11 }