diff src/com/aurellem/capture/hello/HelloAudio.java @ 0:9c4438349e88

added Hello sample programs. Will slowly move all the capture code to here
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 10:42:36 -0700
parents
children a92de00f0414
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/HelloAudio.java	Tue Oct 25 10:42:36 2011 -0700
     1.3 @@ -0,0 +1,144 @@
     1.4 +package com.aurellem.capture.hello;
     1.5 +
     1.6 +import java.io.File;
     1.7 +import java.util.logging.Level;
     1.8 +import java.util.logging.Logger;
     1.9 +
    1.10 +import com.jme3.app.SimpleApplication;
    1.11 +import com.jme3.audio.AudioNode;
    1.12 +import com.jme3.audio.Listener;
    1.13 +import com.jme3.capture.MultiListener;
    1.14 +import com.jme3.capture.WaveFileWriter;
    1.15 +import com.jme3.input.controls.ActionListener;
    1.16 +import com.jme3.input.controls.MouseButtonTrigger;
    1.17 +import com.jme3.material.Material;
    1.18 +import com.jme3.math.ColorRGBA;
    1.19 +import com.jme3.math.Quaternion;
    1.20 +import com.jme3.math.Vector3f;
    1.21 +import com.jme3.scene.Geometry;
    1.22 +import com.jme3.scene.shape.Box;
    1.23 +import com.jme3.system.AppSettings;
    1.24 +import com.jme3.system.IsoTimer;
    1.25 +
    1.26 +/** Sample 11 - playing 3D audio. */
    1.27 +public class HelloAudio extends SimpleApplication {
    1.28 +	  
    1.29 +  private AudioNode audio_gun;
    1.30 +  private AudioNode audio_nature;
    1.31 +  private Geometry player;
    1.32 +  private Listener auxListener = new Listener(); 
    1.33 +  public File data1 = new File("/home/r/tmp/data1.wav");
    1.34 +  public File data2 = new File("/home/r/tmp/data2.wav");
    1.35 +  public File data3 = new File("/home/r/tmp/data3.wav");
    1.36 +  
    1.37 + 
    1.38 +  
    1.39 +  
    1.40 +  private File makeTarget(int n){
    1.41 +	  	return new File("/home/r/tmp/assload-" + n + ".wav");
    1.42 +  }
    1.43 +  
    1.44 +  
    1.45 +  public static void main(String[] args) {
    1.46 +
    1.47 +	 // Logger.getLogger("com.jme3").setLevel(Level.OFF);
    1.48 +	  
    1.49 +	HelloAudio app = new HelloAudio();
    1.50 +	AppSettings settings = new AppSettings(true);
    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 +  @Override
    1.59 +  public void simpleInitApp() {
    1.60 +	this.setTimer(new IsoTimer(60));
    1.61 +	
    1.62 +	
    1.63 +	if (this.audioRenderer instanceof MultiListener){
    1.64 +		MultiListener rf = (MultiListener)this.audioRenderer;
    1.65 +		
    1.66 +		for (int n = 0; n < 0; n++){
    1.67 +			Listener zzz = new Listener();
    1.68 +			rf.addListener(zzz);
    1.69 +			rf.registerSoundProcessor(
    1.70 +						zzz, new WaveFileWriter(makeTarget(n)));
    1.71 +		}
    1.72 +		Listener listener3 = new Listener();
    1.73 +		rf.addListener(auxListener);
    1.74 +		rf.addListener(listener3);
    1.75 +		rf.registerSoundProcessor(new WaveFileWriter(data1));
    1.76 +		rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
    1.77 +		rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
    1.78 +	}
    1.79 +    flyCam.setMoveSpeed(40);
    1.80 +    
    1.81 +    /** just a blue box floating in space */
    1.82 +    Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
    1.83 +    player = new Geometry("Player", box1);
    1.84 +    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    1.85 +    mat1.setColor("Color", ColorRGBA.Blue);
    1.86 +    player.setMaterial(mat1);
    1.87 +    rootNode.attachChild(player);
    1.88 +    
    1.89 +    /** custom init methods, see below */
    1.90 +    initKeys();
    1.91 +    initAudio();
    1.92 +	
    1.93 +	this.audioRenderer.playSource(audio_gun);
    1.94 +
    1.95 +	
    1.96 +
    1.97 +  }
    1.98 +
    1.99 +  /** We create two audio nodes. */ 
   1.100 +  private void initAudio() {
   1.101 +	//audioRenderer.setEnvironment(Environment.Cavern);
   1.102 +    /* gun shot sound is to be triggered by a mouse click. */
   1.103 +	//audio_gun = new AudioNode(audioRenderer, assetManager, "Sound/Effects/Gun.wav", false);
   1.104 +	audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
   1.105 +    audio_gun.setLooping(false);
   1.106 +    audio_gun.setVolume(2);
   1.107 +    audio_gun.setPositional(true);
   1.108 +
   1.109 +
   1.110 +    /* nature sound - keeps playing in a loop. */
   1.111 +    audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
   1.112 +    audio_nature.setLooping(true);
   1.113 +    audio_nature.setPositional(true);
   1.114 +    audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
   1.115 +    audio_nature.setVolume(3);
   1.116 +    audio_nature.updateGeometricState();
   1.117 +  }
   1.118 +
   1.119 +  /** Declaring the "Shoot" action, and
   1.120 +   *  mapping it to a trigger (mouse click). */
   1.121 +  private void initKeys() {
   1.122 +    inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
   1.123 +    inputManager.addListener(actionListener, "Shoot");
   1.124 +  }
   1.125 +
   1.126 +  /** Defining the "Shoot" action: Play a gun sound. */
   1.127 +  private ActionListener actionListener = new ActionListener() {
   1.128 +    @Override
   1.129 +    public void onAction(String name, boolean keyPressed, float tpf) {
   1.130 +      if (name.equals("Shoot") && !keyPressed) {
   1.131 +        audioRenderer.playSource(audio_gun); // play once!
   1.132 +      }
   1.133 +    }
   1.134 +  };
   1.135 +
   1.136 +  /** Move the listener with the camera - for 3D audio. */
   1.137 +  @Override
   1.138 +  public void simpleUpdate(float tpf) {
   1.139 +	Vector3f loc = cam.getLocation();
   1.140 +	Quaternion rot = cam.getRotation();
   1.141 +    listener.setLocation(loc);
   1.142 +    listener.setRotation(rot);
   1.143 +    auxListener.setLocation(loc);
   1.144 +    auxListener.setRotation(rot);
   1.145 +  }
   1.146 +
   1.147 +}