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