001: /*
002: * Main.java
003: *
004: * Created on 5 June 2006, 04:08
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.jdesktop.j3d.apps.sound;
011:
012: import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
013: import com.sun.j3d.utils.geometry.ColorCube;
014: import com.sun.j3d.utils.universe.SimpleUniverse;
015: import com.sun.j3d.utils.universe.Viewer;
016: import com.sun.j3d.utils.universe.ViewingPlatform;
017: import javax.media.j3d.Alpha;
018: import javax.media.j3d.AmbientLight;
019: import javax.media.j3d.Appearance;
020: import javax.media.j3d.BackgroundSound;
021: import javax.media.j3d.BoundingSphere;
022: import javax.media.j3d.Bounds;
023: import javax.media.j3d.BranchGroup;
024: import javax.media.j3d.Canvas3D;
025: import javax.media.j3d.ColoringAttributes;
026: import javax.media.j3d.DirectionalLight;
027: import javax.media.j3d.LineArray;
028: import javax.media.j3d.LineAttributes;
029:
030: import javax.media.j3d.MediaContainer;
031: import javax.media.j3d.PhysicalBody;
032: import javax.media.j3d.PhysicalEnvironment;
033: import javax.media.j3d.RotationInterpolator;
034: import javax.media.j3d.Shape3D;
035: import javax.media.j3d.Transform3D;
036: import javax.media.j3d.TransformGroup;
037: import javax.media.j3d.View;
038: import javax.swing.JCheckBoxMenuItem;
039: import javax.swing.JFrame;
040: import javax.vecmath.Point3d;
041: import javax.vecmath.Vector3f;
042: import org.jdesktop.j3d.audioengines.joal.JOALMixer;
043:
044: /** This is a test for a BackgroundSound, to test the functionality of
045: * JOALAudioEngine3DL2 for Java3D.
046: *
047: * @author David Grace (dave@dutchie.net)
048: * @version 0.1
049: */
050: public class JOALBackgroundSoundTest {
051:
052: private boolean webstart = false;
053:
054: private SimpleUniverse universe;
055: private Canvas3D canvas3D;
056:
057: private TransformGroup viewingPlatformTransformGroup;
058:
059: //Options for which type of helper geometry to show
060: private boolean showBounds = true;
061: private boolean showDistanceAttenuation = true;
062: private boolean showAngularAttenuation = true;
063:
064: //The activation radius for the ViewPlatform
065: private float activationRadius = 1;
066:
067: //The name of the test file
068: private String testFilename = "magic_bells.wav";
069:
070: private SoundBranchGroup sbg;
071:
072: private JCheckBoxMenuItem isDistanceAttenuationVisible;
073: private JCheckBoxMenuItem isAngularAttenuationVisible;
074:
075: /** Creates a new instance of Main */
076: public JOALBackgroundSoundTest(boolean webstart) {
077: this .webstart = webstart;
078: System.out.println("JOALPointSoundTest");
079: setupUniverse();
080: setupGUI();
081: setupNavigation();
082: setupLighting();
083: addGrid();
084:
085: addSoundAndGeometry(0, 0, 0);
086: }
087:
088: private void addSoundAndGeometry(float x, float y, float z) {
089: setupBackgroundSoundFromURL();
090: setupGeometry(x, y, z);
091: }
092:
093: private void setupGUI() {
094: JFrame frame = new JFrame("BackgroundSound Test for JOALMixer");
095:
096: frame.add("Center", canvas3D);
097: frame.setSize(800, 600);
098: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
099: frame.setVisible(true);
100: canvas3D.setFocusable(true);
101: canvas3D.requestFocus();
102:
103: }
104:
105: private void setupUniverse() {
106: PhysicalEnvironment physicalEnvironment = null;
107: if (webstart) {
108: physicalEnvironment = new PhysicalEnvironment();
109: JOALMixer audioDevice3D = new JOALMixer(physicalEnvironment);
110: audioDevice3D.initialize();
111: physicalEnvironment.setAudioDevice(audioDevice3D);
112:
113: } else {
114: //PhysicalBody physicalBody = new PhysicalBody();
115: }
116:
117: canvas3D = new Canvas3D(SimpleUniverse
118: .getPreferredConfiguration());
119: Canvas3D[] canvases = new Canvas3D[] { canvas3D };
120: Viewer viewer = new Viewer(canvas3D);
121: if (webstart) {
122: viewer.getView()
123: .setPhysicalEnvironment(physicalEnvironment);
124:
125: } else
126: viewer.createAudioDevice();
127: viewer.getView().setBackClipDistance(1000.0f);
128: viewer.getView().setUserHeadToVworldEnable(true);
129: viewer.getView().setMinimumFrameCycleTime(30);
130: viewer.getView().setTransparencySortingPolicy(
131: View.TRANSPARENCY_SORT_GEOMETRY);
132: ViewingPlatform viewingPlatform = new ViewingPlatform();
133: viewingPlatformTransformGroup = viewingPlatform
134: .getViewPlatformTransform();
135: viewingPlatformTransformGroup
136: .setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
137: viewingPlatformTransformGroup
138: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
139:
140: universe = new SimpleUniverse(viewingPlatform, viewer);
141: viewer.getView().getViewPlatform().setActivationRadius(
142: activationRadius);
143: }
144:
145: private void setupNavigation() {
146: BranchGroup bg = new BranchGroup();
147: KeyNavigatorBehavior knb = new KeyNavigatorBehavior(canvas3D,
148: viewingPlatformTransformGroup);
149: Bounds b = new BoundingSphere(new Point3d(),
150: Double.POSITIVE_INFINITY);
151: knb.setSchedulingBounds(b);
152: bg.addChild(knb);
153: universe.addBranchGraph(bg);
154: }
155:
156: private void setupLighting() {
157: BranchGroup bg = new BranchGroup();
158: Bounds b = new BoundingSphere(new Point3d(),
159: Double.POSITIVE_INFINITY);
160: AmbientLight al = new AmbientLight();
161: al.setInfluencingBounds(b);
162: Bounds b2 = new BoundingSphere(new Point3d(),
163: Double.POSITIVE_INFINITY);
164: DirectionalLight dl = new DirectionalLight();
165: dl.setDirection(-1, -1, -1);
166: dl.setInfluencingBounds(b2);
167: bg.addChild(al);
168: bg.addChild(dl);
169: universe.addBranchGraph(bg);
170: }
171:
172: private void setupGeometry(float x, float y, float z) {
173: BranchGroup bg = new BranchGroup();
174: TransformGroup rootTransformGroup = new TransformGroup();
175: Transform3D t3D = new Transform3D();
176: t3D.setTranslation(new Vector3f(x, y, z));
177: rootTransformGroup.setTransform(t3D);
178:
179: TransformGroup t = new TransformGroup();
180: t.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
181: t.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
182:
183: Bounds b = new BoundingSphere(new Point3d(),
184: Double.POSITIVE_INFINITY);
185: ColorCube cc = new ColorCube(0.1);
186: Alpha alpha = new Alpha(-1, 2000);
187: RotationInterpolator ri = new RotationInterpolator(alpha, t);
188: ri.setSchedulingBounds(b);
189: t.addChild(cc);
190: t.addChild(ri);
191: rootTransformGroup.addChild(t);
192: bg.addChild(rootTransformGroup);
193: universe.addBranchGraph(bg);
194: }
195:
196: private void setupBackgroundSoundFromURL() {
197: Bounds b = new BoundingSphere(new Point3d(), 20);
198: MediaContainer mc = JOALTestResources.getTestMediaContainer(
199: testFilename, true);
200: BackgroundSound cc = new BackgroundSound(mc, 1);
201: cc.setEnable(true);
202: cc.setPause(false);
203: cc.setLoop(-1);
204: cc.setContinuousEnable(true);
205: cc.setSchedulingBounds(b);
206:
207: sbg = new SoundBranchGroup(cc);
208: universe.addBranchGraph(sbg);
209: }
210:
211: private BranchGroup getDefaultGrid(int noOfLines, double size,
212: double height) {
213: BranchGroup bg = new BranchGroup();
214: bg.setCapability(BranchGroup.ALLOW_DETACH);
215:
216: Shape3D shape = new Shape3D();
217: double lineLength = noOfLines * size / 2;
218: LineArray la = new LineArray(noOfLines * 4,
219: LineArray.COORDINATES);
220: int count = 0;
221: for (int i = 0; i < noOfLines; i++) {
222: la.setCoordinate(count, new Point3d(-lineLength, height, i
223: * size - lineLength));
224: count++;
225: la.setCoordinate(count, new Point3d(lineLength, height, i
226: * size - lineLength));
227: count++;
228: }
229: for (int i = 0; i < noOfLines; i++) {
230: la.setCoordinate(count, new Point3d(i * size - lineLength,
231: height, -lineLength));
232: count++;
233: la.setCoordinate(count, new Point3d(i * size - lineLength,
234: height, lineLength));
235: count++;
236: }
237: shape.setGeometry(la);
238: Appearance a = new Appearance();
239: ColoringAttributes ca = new ColoringAttributes();
240: ca.setColor(0.3f, 0.3f, 0.3f);
241: a.setColoringAttributes(ca);
242: LineAttributes sla = new LineAttributes();
243: sla.setLineWidth(1.0f);
244: a.setLineAttributes(sla);
245: shape.setAppearance(a);
246:
247: bg.addChild(shape);
248:
249: return bg;
250: }
251:
252: private void addGrid() {
253: universe.addBranchGraph(getDefaultGrid(40, 1, -1));
254: }
255:
256: /**
257: * @param args the command line arguments
258: */
259: public static void main(String[] args) {
260: boolean webstart = false;
261: if (args.length > 0) {
262: for (int i = 0; i < args.length; i++) {
263: if (args[i].equals("webstart")) {
264: webstart = true;
265: System.out.println("Running through webstart");
266: }
267: }
268: }
269: new JOALBackgroundSoundTest(webstart);
270: }
271: }
|