diff src/com/aurellem/capture/hello/AdvancedAudio.java @ 12:d10f4d4ff15a

going to improve documentation
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 21:07:50 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/com/aurellem/capture/hello/AdvancedAudio.java	Thu Oct 27 21:07:50 2011 -0700
     1.3 @@ -0,0 +1,158 @@
     1.4 +package com.aurellem.capture.hello;
     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 + * 
    1.28 + * 
    1.29 + * @author Robert McIntyre
    1.30 + *
    1.31 + */
    1.32 +
    1.33 +public class AdvancedAudio extends SimpleApplication {
    1.34 +	  
    1.35 +
    1.36 +  
    1.37 +  public static void main(String[] args) {
    1.38 +
    1.39 +	 // Logger.getLogger("com.jme3").setLevel(Level.OFF);
    1.40 +	  
    1.41 +	AdvancedAudio app = new AdvancedAudio();
    1.42 +	AppSettings settings = new AppSettings(true);
    1.43 +	
    1.44 +	settings.setAudioRenderer("Send");
    1.45 +	app.setSettings(settings);
    1.46 +	app.setShowSettings(false);
    1.47 +	app.start();
    1.48 +	app.setPauseOnLostFocus(false);
    1.49 +  }
    1.50 +
    1.51 +  
    1.52 +  
    1.53 +  private AudioNode audio_gun;
    1.54 +  private AudioNode audio_nature;
    1.55 +  private Geometry player;
    1.56 +  private Listener auxListener = new Listener(); 
    1.57 +  public File data1 = new File("/home/r/tmp/data1.wav");
    1.58 +  public File data2 = new File("/home/r/tmp/data2.wav");
    1.59 +  public File data3 = new File("/home/r/tmp/data3.wav");
    1.60 +   
    1.61 +  private File makeTarget(int n){
    1.62 +	  	return new File("/home/r/tmp/assload-" + n + ".wav");
    1.63 +  }
    1.64 +  
    1.65 +  
    1.66 +  @Override
    1.67 +  public void simpleInitApp() {
    1.68 +	this.setTimer(new IsoTimer(60));
    1.69 +	
    1.70 +	
    1.71 +	if (this.audioRenderer instanceof MultiListener){
    1.72 +		MultiListener rf = (MultiListener)this.audioRenderer;
    1.73 +		
    1.74 +		for (int n = 0; n < 0; n++){
    1.75 +			Listener zzz = new Listener();
    1.76 +			rf.addListener(zzz);
    1.77 +			rf.registerSoundProcessor(
    1.78 +						zzz, new WaveFileWriter(makeTarget(n)));
    1.79 +		}
    1.80 +		Listener listener3 = new Listener();
    1.81 +		rf.addListener(auxListener);
    1.82 +		rf.addListener(listener3);
    1.83 +		rf.registerSoundProcessor(new WaveFileWriter(data1));
    1.84 +		rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
    1.85 +		rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
    1.86 +	}
    1.87 +    flyCam.setMoveSpeed(40);
    1.88 +    
    1.89 +    /** just a blue box floating in space */
    1.90 +    Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
    1.91 +    player = new Geometry("Player", box1);
    1.92 +    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    1.93 +    mat1.setColor("Color", ColorRGBA.Blue);
    1.94 +    player.setMaterial(mat1);
    1.95 +    rootNode.attachChild(player);
    1.96 +    
    1.97 +    /** custom init methods, see below */
    1.98 +    initKeys();
    1.99 +    initAudio();
   1.100 +	
   1.101 +	this.audioRenderer.playSource(audio_gun);
   1.102 +
   1.103 +	
   1.104 +
   1.105 +  }
   1.106 +
   1.107 +  /** We create two audio nodes. */ 
   1.108 +  private void initAudio() {
   1.109 +	//audioRenderer.setEnvironment(Environment.Cavern);
   1.110 +    /* gun shot sound is to be triggered by a mouse click. */
   1.111 +	audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
   1.112 +	//audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
   1.113 +    audio_gun.setLooping(false);
   1.114 +    audio_gun.setVolume(2);
   1.115 +    audio_gun.setPositional(true);
   1.116 +
   1.117 +
   1.118 +    /* nature sound - keeps playing in a loop. */
   1.119 +    audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
   1.120 +    audio_nature.setLooping(true);
   1.121 +    audio_nature.setPositional(true);
   1.122 +    audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
   1.123 +    audio_nature.setVolume(3);
   1.124 +    audio_nature.updateGeometricState();
   1.125 +  }
   1.126 +
   1.127 +  /** Declaring the "Shoot" action, and
   1.128 +   *  mapping it to a trigger (mouse click). */
   1.129 +  private void initKeys() {
   1.130 +    inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
   1.131 +    inputManager.addListener(actionListener, "Shoot");
   1.132 +  }
   1.133 +
   1.134 +  /** Defining the "Shoot" action: Play a gun sound. */
   1.135 +  private ActionListener actionListener = new ActionListener() {
   1.136 +    @Override
   1.137 +    public void onAction(String name, boolean keyPressed, float tpf) {
   1.138 +      if (name.equals("Shoot") && !keyPressed) {
   1.139 +        audioRenderer.playSource(audio_gun); // play once!
   1.140 +      }
   1.141 +    }
   1.142 +  };
   1.143 +
   1.144 +  /** Move the listener with the camera - for 3D audio. */
   1.145 +  @Override
   1.146 +  public void simpleUpdate(float tpf) {
   1.147 +	Vector3f loc = cam.getLocation();
   1.148 +	Quaternion rot = cam.getRotation();
   1.149 +    listener.setLocation(loc);
   1.150 +    listener.setRotation(rot);
   1.151 +    auxListener.setLocation(loc);
   1.152 +    auxListener.setRotation(rot);
   1.153 +    if (audio_gun.getStatus() == AudioNode.Status.Stopped){
   1.154 +    	System.out.println("I'm Stopped!");
   1.155 +    	this.requestClose(false);
   1.156 +    }
   1.157 +    	
   1.158 +    	
   1.159 +  }
   1.160 +
   1.161 +}