001: /*
002: * $RCSfile: SimpleSounds.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.3 $
041: * $Date: 2007/02/09 17:21:52 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.sound;
046:
047: import java.applet.Applet;
048: import java.net.URL;
049: import java.awt.*;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.ColorCube;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.swing.JOptionPane;
055: import javax.vecmath.*;
056: import org.jdesktop.j3d.examples.Resources;
057:
058: /*
059: * This Java3D program:
060: * Creates an instance of the JavaSoundMixer AudioDevice, initializing it
061: * and attaching it to the PhysicalEnvironment by using
062: * SimpleUniverse.
063: * Creates one cached Background and two cached Point sound sources.
064: * Creates and executes a custom behavior (SimpleSoundsBehavior) that
065: * starts the sound playing and transforms the PointSound source
066: * by modifying the TransformGroup that contains the Sound nodes.
067: *
068: * Usage:
069: * java SimpleSounds [URLpath [name1 [name2 [name2]]]]
070: *
071: * The first optional command line parameter is the URL path to directory
072: * containing "file:" or "http:" and then directory path string.
073: * If you are using the suppled default sound files in the same directory
074: * as this test program then only the URLpath need be supplied on the
075: * command line.
076: * If this parameter is not included then the current path to the directory
077: * this program is running in is used for an application
078: * and the codebase is used for an applet.
079: * The second thru fourth optional command line parameters are sound file names
080: * If not given, the default file names are:
081: * techno_machine.au
082: * hello_universe.au
083: * roar.au
084: * that correspond to the 3 'voice' quality, 8-bit, u-law, 8-kHz samples
085: * included in the same directory as this test program.
086: *
087: * Java Sound engine has been advertised to support the following 8- and 16-
088: * bit, linear and u-law, mono and stereo sound sample file formats: AU,
089: * AIFF, WAV, and PCM. Non-cached MIDI and RMF files are also supported.
090: * Neither compressed formats (DVI, GSM, MOD) nor A-law formated files are
091: * supported at this time, but they will be converted.
092: */
093:
094: public class SimpleSounds extends Applet {
095:
096: private static URL[] url = new URL[3];
097: private SimpleUniverse u = null;
098:
099: public BranchGroup createSceneGraph() {
100: // Create the root of the subgraph
101: BranchGroup objRoot = new BranchGroup();
102:
103: // Create the transform group node and initialize it to the identity.
104: // Enable the TRANSFORM_WRITE capability so that our behavior code
105: // can modify it at runtime. Add it to the root of the subgraph.
106: TransformGroup objTrans = new TransformGroup();
107: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
108: objRoot.addChild(objTrans);
109:
110: // Create a simple shape leaf node and add it into the scene graph.
111: objTrans.addChild(new ColorCube(0.4));
112:
113: // Create a new Behavior object that will perform the desired
114: // operation on the specified transform object and add it into the
115: // scene graph.
116: Transform3D yAxis = new Transform3D();
117: Alpha rotation = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
118: 20000, 0, 0, 0, 0, 0);
119: RotationInterpolator rotator = new RotationInterpolator(
120: rotation, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
121: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
122: 0.0, 0.0), 100.0);
123: rotator.setSchedulingBounds(bounds);
124: objTrans.addChild(rotator);
125: /*
126: * Create a sound node and add it to the scene graph
127: */
128: BackgroundSound sound1 = new BackgroundSound();
129: PointSound sound2 = new PointSound();
130: PointSound sound3 = new PointSound();
131: sound1.setCapability(PointSound.ALLOW_ENABLE_WRITE);
132: sound1.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
133: sound1.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
134: sound1.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
135: sound1.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
136: sound1.setCapability(PointSound.ALLOW_RELEASE_WRITE);
137: sound1.setCapability(PointSound.ALLOW_DURATION_READ);
138: sound1.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
139: sound1.setCapability(PointSound.ALLOW_LOOP_WRITE);
140: sound2.setCapability(PointSound.ALLOW_ENABLE_WRITE);
141: sound2.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
142: sound2.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
143: sound2.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
144: sound2.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
145: sound2.setCapability(PointSound.ALLOW_RELEASE_WRITE);
146: sound2.setCapability(PointSound.ALLOW_DURATION_READ);
147: sound2.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
148: sound2.setCapability(PointSound.ALLOW_POSITION_WRITE);
149: sound2.setCapability(PointSound.ALLOW_LOOP_WRITE);
150: sound3.setCapability(PointSound.ALLOW_ENABLE_WRITE);
151: sound3.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
152: sound3.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
153: sound3.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
154: sound3.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
155: sound3.setCapability(PointSound.ALLOW_RELEASE_WRITE);
156: sound3.setCapability(PointSound.ALLOW_DURATION_READ);
157: sound3.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
158: sound3.setCapability(PointSound.ALLOW_POSITION_WRITE);
159:
160: BoundingSphere soundBounds = new BoundingSphere(new Point3d(
161: 0.0, 0.0, 0.0), 100.0);
162: sound1.setSchedulingBounds(soundBounds);
163: sound2.setSchedulingBounds(soundBounds);
164: sound3.setSchedulingBounds(soundBounds);
165: objTrans.addChild(sound1);
166: objTrans.addChild(sound2);
167: objTrans.addChild(sound3);
168:
169: /*
170: * Create a new Behavior object that will play the sound
171: */
172: SimpleSoundsBehavior player = new SimpleSoundsBehavior(sound1,
173: sound2, sound3, url[0], url[1], url[2], soundBounds);
174: player.setSchedulingBounds(soundBounds);
175: objTrans.addChild(player);
176:
177: // Let Java 3D perform optimizations on this scene graph.
178: objRoot.compile();
179:
180: return objRoot;
181: }
182:
183: public SimpleSounds() {
184: }
185:
186: public void init() {
187:
188: setLayout(new BorderLayout());
189: GraphicsConfiguration config = SimpleUniverse
190: .getPreferredConfiguration();
191:
192: Canvas3D c = new Canvas3D(config);
193: add("Center", c);
194:
195: url[0] = Resources
196: .getResource("resources/audio/techno_machine.au");
197: if (url == null) {
198: System.err
199: .println("resources/audio/techno_machine.au not found");
200: System.exit(1);
201: }
202:
203: url[1] = Resources
204: .getResource("resources/audio/hello_universe.au");
205: if (url == null) {
206: System.err
207: .println("resources/audio/hello_universe.au not found");
208: System.exit(1);
209: }
210:
211: url[2] = Resources.getResource("resources/audio/roar.au");
212: if (url == null) {
213: System.err.println("resources/audio/roar.au not found");
214: System.exit(1);
215: }
216:
217: /*
218: * Create a simple scene and attach it to the virtual universe
219: */
220: u = new SimpleUniverse(c);
221: AudioDevice audioDev = u.getViewer().createAudioDevice();
222: BranchGroup scene = createSceneGraph();
223:
224: // This will move the ViewPlatform back a bit so the
225: // objects in the scene can be viewed.
226: u.getViewingPlatform().setNominalViewingTransform();
227:
228: u.addBranchGraph(scene);
229:
230: JOptionPane.showMessageDialog(this ,
231: ("This program is still a work in progress.\n"
232: + "Please check back in Java 3D 1.5.\n"),
233: "Incomplete Work", JOptionPane.INFORMATION_MESSAGE);
234: }
235:
236: public void destroy() {
237: u.cleanup();
238: }
239:
240: /*
241: * The following allows SimpleSounds to be run as an application
242: * as well as an applet
243: */
244: public static void main(String[] args) {
245: new MainFrame(new SimpleSounds(), args, 256, 256);
246: }
247: }
|