diff src/com/aurellem/capture/examples/AdvancedAudio.java @ 14:e299cd89074d

creating Advanced Audio documentation.
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 21:55:51 -0700
parents
children be5ac56826be
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/com/aurellem/capture/examples/AdvancedAudio.java	Thu Oct 27 21:55:51 2011 -0700
     1.3 @@ -0,0 +1,163 @@
     1.4 +package com.aurellem.capture.examples;
     1.5 +
     1.6 +import java.io.File;
     1.7 +
     1.8 +import com.aurellem.capture.IsoTimer;
     1.9 +import com.aurellem.capture.audio.MultiListener;
    1.10 +import com.aurellem.capture.audio.WaveFileWriter;
    1.11 +import com.jme3.app.SimpleApplication;
    1.12 +import com.jme3.audio.AudioNode;
    1.13 +import com.jme3.audio.Listener;
    1.14 +import com.jme3.input.controls.ActionListener;
    1.15 +import com.jme3.input.controls.MouseButtonTrigger;
    1.16 +import com.jme3.material.Material;
    1.17 +import com.jme3.math.ColorRGBA;
    1.18 +import com.jme3.math.Quaternion;
    1.19 +import com.jme3.math.Vector3f;
    1.20 +import com.jme3.scene.Geometry;
    1.21 +import com.jme3.scene.shape.Box;
    1.22 +import com.jme3.system.AppSettings;
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + * 
    1.27 + * Demonstrates advanced use of the audio capture and recording features.
    1.28 + * Multiple perspectives of the same scene are simultaneously rendered to 
    1.29 + * different sound files.  
    1.30 + * 
    1.31 + * A key limitation of the way multiple listeners are implemented is that 
    1.32 + * only 3D positioning effects are realized for listeners other than the
    1.33 + * main LWJGL listener.  This means that audio effects such as environment
    1.34 + * settings will *not* be heard on any auxiliary listeners, though sound 
    1.35 + * attenuation will work correctly.  
    1.36 + * 
    1.37 + * Multiple listeners as realized here might be used to make AI entities 
    1.38 + * that can each hear the world from their own perspective.  
    1.39 + * 
    1.40 + * @author Robert McIntyre
    1.41 + *
    1.42 + */
    1.43 +
    1.44 +public class AdvancedAudio extends SimpleApplication {
    1.45 +	    
    1.46 +  public static void main(String[] args) {
    1.47 +	  
    1.48 +	AdvancedAudio app = new AdvancedAudio();
    1.49 +	AppSettings settings = new AppSettings(true);
    1.50 +	
    1.51 +	settings.setAudioRenderer("Send");
    1.52 +	app.setSettings(settings);
    1.53 +	app.setShowSettings(false);
    1.54 +	app.start();
    1.55 +	app.setPauseOnLostFocus(false);
    1.56 +  }
    1.57 +
    1.58 +  private AudioNode audio_gun;
    1.59 +  private AudioNode audio_nature;
    1.60 +  private Geometry player;
    1.61 +  private Listener auxListener = new Listener(); 
    1.62 +  public File data1 = new File("/home/r/tmp/data1.wav");
    1.63 +  public File data2 = new File("/home/r/tmp/data2.wav");
    1.64 +  public File data3 = new File("/home/r/tmp/data3.wav");
    1.65 +   
    1.66 +  private File makeTarget(int n){
    1.67 +	  	return new File("/home/r/tmp/assload-" + n + ".wav");
    1.68 +  }
    1.69 +  
    1.70 +  
    1.71 +  @Override
    1.72 +  public void simpleInitApp() {
    1.73 +	this.setTimer(new IsoTimer(60));
    1.74 +	
    1.75 +	
    1.76 +	if (this.audioRenderer instanceof MultiListener){
    1.77 +		MultiListener rf = (MultiListener)this.audioRenderer;
    1.78 +		
    1.79 +		for (int n = 0; n < 0; n++){
    1.80 +			Listener zzz = new Listener();
    1.81 +			rf.addListener(zzz);
    1.82 +			rf.registerSoundProcessor(
    1.83 +						zzz, new WaveFileWriter(makeTarget(n)));
    1.84 +		}
    1.85 +		Listener listener3 = new Listener();
    1.86 +		rf.addListener(auxListener);
    1.87 +		rf.addListener(listener3);
    1.88 +		rf.registerSoundProcessor(new WaveFileWriter(data1));
    1.89 +		rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
    1.90 +		rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
    1.91 +	}
    1.92 +    flyCam.setMoveSpeed(40);
    1.93 +    
    1.94 +    /** just a blue box floating in space */
    1.95 +    Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
    1.96 +    player = new Geometry("Player", box1);
    1.97 +    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    1.98 +    mat1.setColor("Color", ColorRGBA.Blue);
    1.99 +    player.setMaterial(mat1);
   1.100 +    rootNode.attachChild(player);
   1.101 +    
   1.102 +    /** custom init methods, see below */
   1.103 +    initKeys();
   1.104 +    initAudio();
   1.105 +	
   1.106 +	this.audioRenderer.playSource(audio_gun);
   1.107 +
   1.108 +	
   1.109 +
   1.110 +  }
   1.111 +
   1.112 +  /** We create two audio nodes. */ 
   1.113 +  private void initAudio() {
   1.114 +	//audioRenderer.setEnvironment(Environment.Cavern);
   1.115 +    /* gun shot sound is to be triggered by a mouse click. */
   1.116 +	audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
   1.117 +	//audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
   1.118 +    audio_gun.setLooping(false);
   1.119 +    audio_gun.setVolume(2);
   1.120 +    audio_gun.setPositional(true);
   1.121 +
   1.122 +
   1.123 +    /* nature sound - keeps playing in a loop. */
   1.124 +    audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
   1.125 +    audio_nature.setLooping(true);
   1.126 +    audio_nature.setPositional(true);
   1.127 +    audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
   1.128 +    audio_nature.setVolume(3);
   1.129 +    audio_nature.updateGeometricState();
   1.130 +  }
   1.131 +
   1.132 +  /** Declaring the "Shoot" action, and
   1.133 +   *  mapping it to a trigger (mouse click). */
   1.134 +  private void initKeys() {
   1.135 +    inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
   1.136 +    inputManager.addListener(actionListener, "Shoot");
   1.137 +  }
   1.138 +
   1.139 +  /** Defining the "Shoot" action: Play a gun sound. */
   1.140 +  private ActionListener actionListener = new ActionListener() {
   1.141 +    @Override
   1.142 +    public void onAction(String name, boolean keyPressed, float tpf) {
   1.143 +      if (name.equals("Shoot") && !keyPressed) {
   1.144 +        audioRenderer.playSource(audio_gun); // play once!
   1.145 +      }
   1.146 +    }
   1.147 +  };
   1.148 +
   1.149 +  /** Move the listener with the camera - for 3D audio. */
   1.150 +  @Override
   1.151 +  public void simpleUpdate(float tpf) {
   1.152 +	Vector3f loc = cam.getLocation();
   1.153 +	Quaternion rot = cam.getRotation();
   1.154 +    listener.setLocation(loc);
   1.155 +    listener.setRotation(rot);
   1.156 +    auxListener.setLocation(loc);
   1.157 +    auxListener.setRotation(rot);
   1.158 +    if (audio_gun.getStatus() == AudioNode.Status.Stopped){
   1.159 +    	System.out.println("I'm Stopped!");
   1.160 +    	this.requestClose(false);
   1.161 +    }
   1.162 +    	
   1.163 +    	
   1.164 +  }
   1.165 +
   1.166 +}