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