001: /*
002: * $RCSfile: Sound.java,v $
003: *
004: * @(#)Sound.java 1.28 98/11/05 20:35:21
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:07:02 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Rick Goldberg
038: * @Author: Doug Gehringer
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: import javax.media.j3d.BoundingSphere;
044: import javax.media.j3d.BranchGroup;
045: import javax.vecmath.Point3d;
046: import javax.vecmath.Point3f;
047:
048: /** Description of the Class */
049: public class Sound extends NonSharedNode {
050: // exposedFields
051:
052: SFVec3f direction;
053: SFFloat intensity;
054: SFVec3f location;
055: SFFloat maxBack;
056: SFFloat maxFront;
057: SFFloat minBack;
058: SFFloat minFront;
059: SFFloat priority;
060: SFNode source;
061:
062: // field
063: SFBool spatialize;
064:
065: BranchGroup impl;
066: javax.media.j3d.PointSound soundImpl;
067: //javax.media.j3d.BackgroundSound soundImpl;
068: boolean wait = false;
069: Point3f pos3f = new Point3f();
070: Point3d pos3d = new Point3d();
071: BoundingSphere bounds = new BoundingSphere();
072: float[] distance = new float[2];
073: float[] attenuation = new float[2];
074: javax.media.j3d.MediaContainer media;
075: AudioClip clip;
076:
077: /**
078: *Constructor for the Sound object
079: *
080: *@param loader Description of the Parameter
081: */
082: public Sound(Loader loader) {
083: super (loader);
084: direction = new SFVec3f(0.0f, 0.0f, 1.0f);
085: intensity = new SFFloat(1.0f);
086: location = new SFVec3f(0.0f, 0.0f, 0.0f);
087: maxBack = new SFFloat(10.0f);
088: maxFront = new SFFloat(10.0f);
089: minBack = new SFFloat(1.0f);
090: minFront = new SFFloat(1.0f);
091: priority = new SFFloat(0.0f);
092: source = new SFNode();
093: spatialize = new SFBool(true);
094:
095: initFields();
096: }
097:
098: /**
099: *Constructor for the Sound object
100: *
101: *@param loader Description of the Parameter
102: *@param direction Description of the Parameter
103: *@param intensity Description of the Parameter
104: *@param location Description of the Parameter
105: *@param maxBack Description of the Parameter
106: *@param maxFront Description of the Parameter
107: *@param minBack Description of the Parameter
108: *@param minFront Description of the Parameter
109: *@param priority Description of the Parameter
110: *@param source Description of the Parameter
111: *@param spatialize Description of the Parameter
112: */
113: public Sound(Loader loader, SFVec3f direction, SFFloat intensity,
114: SFVec3f location, SFFloat maxBack, SFFloat maxFront,
115: SFFloat minBack, SFFloat minFront, SFFloat priority,
116: SFNode source, SFBool spatialize) {
117:
118: super (loader);
119: this .direction = direction;
120: this .intensity = intensity;
121: this .location = location;
122: this .maxBack = maxBack;
123: this .maxFront = maxFront;
124: this .minBack = minBack;
125: this .minFront = minFront;
126: this .priority = priority;
127: this .source = source;
128:
129: initFields();
130: }
131:
132: /** Description of the Method */
133: public void initImpl() {
134: pos3f.x = location.value[0];
135: pos3f.y = location.value[1];
136: pos3f.z = location.value[2];
137: // todo: double check the double check
138: pos3d.x = (double) pos3f.x;
139: pos3d.y = (double) pos3f.y;
140: pos3d.z = (double) pos3f.z;
141: soundImpl = new javax.media.j3d.PointSound();
142: soundImpl.setPosition(pos3f);
143: //soundImpl = new javax.media.j3d.BackgroundSound();
144: bounds.setCenter(pos3d);
145: bounds.setRadius((double) maxFront.value);
146: soundImpl.setReleaseEnable(false);
147: soundImpl.setContinuousEnable(false);
148: soundImpl.setSchedulingBounds(bounds);
149: soundImpl.setInitialGain(intensity.value);
150: //soundImpl.setLoop(javax.media.j3d.Sound.INFINITE_LOOPS);
151: soundImpl.setLoop(0);
152: distance[0] = minFront.value;
153: distance[1] = maxFront.value * 100.0f;
154: attenuation[0] = 1.0f;
155: attenuation[1] = 0.0f;
156: soundImpl.setDistanceGain(distance, attenuation);
157: soundImpl
158: .setCapability(javax.media.j3d.Sound.ALLOW_ENABLE_WRITE);
159: soundImpl
160: .setCapability(javax.media.j3d.Sound.ALLOW_INITIAL_GAIN_WRITE);
161: soundImpl
162: .setCapability(javax.media.j3d.Sound.ALLOW_SOUND_DATA_WRITE);
163: soundImpl
164: .setCapability(javax.media.j3d.Sound.ALLOW_DURATION_READ);
165: soundImpl
166: .setCapability(javax.media.j3d.Sound.ALLOW_IS_PLAYING_READ);
167: soundImpl
168: .setCapability(javax.media.j3d.Sound.ALLOW_SCHEDULING_BOUNDS_WRITE);
169: soundImpl
170: .setCapability(javax.media.j3d.Sound.ALLOW_CONT_PLAY_WRITE);
171: soundImpl.setCapability(javax.media.j3d.Sound.ALLOW_LOOP_WRITE);
172: soundImpl
173: .setCapability(javax.media.j3d.Sound.ALLOW_RELEASE_WRITE);
174: soundImpl
175: .setCapability(javax.media.j3d.Sound.ALLOW_IS_READY_READ);
176:
177: //setClip();
178:
179: impl = new RGroup();
180: impl.addChild(soundImpl);
181: implNode = impl;
182: implReady = true;
183: }
184:
185: // pull this out to make available at updateParent() time for the clip.
186: /** Sets the clip attribute of the Sound object */
187: void setClip() {
188: if ((source.node != null) && (source.node instanceof AudioClip)) {
189: if (browser.debug) {
190: System.out.println(this + " setClip() available");
191: }
192: clip = (AudioClip) (source.node);
193: if (media != clip.impl) {
194: media = clip.impl;
195: clip.setSound(this );
196: }
197:
198: if (browser.debug) {
199: System.out
200: .println(browser.environment.getAudioDevice());
201: }
202: soundImpl.setSoundData(media);
203: waitSoundReadiness();
204: long duration = soundImpl.getDuration();
205: if (browser.debug) {
206: System.out.println(this + " duration = " + duration);
207: }
208: clip.setDuration(duration / 1000.0);
209: } else {
210: if (browser.debug) {
211: System.out.println(this + "setClip() null media");
212: }
213: soundImpl.setSoundData(null);
214: }
215: }
216:
217: /**
218: * Sets the enable attribute of the Sound object
219: *
220: *@param enable The new enable value
221: */
222: void setEnable(boolean enable) {
223: soundImpl.setEnable(enable);
224: }
225:
226: /**
227: * Sets the loop attribute of the Sound object
228: *
229: *@param l The new loop value
230: */
231: void setLoop(int l) {
232: soundImpl.setLoop(l);
233: }
234:
235: /**
236: * Description of the Method
237: *
238: *@param eventInName Description of the Parameter
239: *@param time Description of the Parameter
240: */
241: public void notifyMethod(String eventInName, double time) {
242:
243: }
244:
245: /** Description of the Method */
246: void doneParse() {// done with _ALL_ parsing
247: ;
248: }
249:
250: /** Description of the Method */
251: void initFields() {
252: direction.init(this , FieldSpec, Field.EXPOSED_FIELD,
253: "direction");
254: intensity.init(this , FieldSpec, Field.EXPOSED_FIELD,
255: "intensity");
256: location.init(this , FieldSpec, Field.EXPOSED_FIELD, "location");
257: maxBack.init(this , FieldSpec, Field.EXPOSED_FIELD, "maxBack");
258: minBack.init(this , FieldSpec, Field.EXPOSED_FIELD, "minBack");
259: minFront.init(this , FieldSpec, Field.EXPOSED_FIELD, "minFront");
260: maxFront.init(this , FieldSpec, Field.EXPOSED_FIELD, "maxFront");
261: priority.init(this , FieldSpec, Field.EXPOSED_FIELD, "priority");
262: source.init(this , FieldSpec, Field.EXPOSED_FIELD, "source");
263: spatialize.init(this , FieldSpec, Field.FIELD, "spatialize");
264: }
265:
266: /**
267: * Description of the Method
268: *
269: *@return Description of the Return Value
270: */
271: public Object clone() {
272: return new Sound(loader, (SFVec3f) direction.clone(),
273: (SFFloat) intensity.clone(),
274: (SFVec3f) location.clone(), (SFFloat) maxBack.clone(),
275: (SFFloat) maxFront.clone(), (SFFloat) minBack.clone(),
276: (SFFloat) minFront.clone(), (SFFloat) priority.clone(),
277: (SFNode) source.clone(), (SFBool) spatialize.clone());
278: }
279:
280: /**
281: * Gets the type attribute of the Sound object
282: *
283: *@return The type value
284: */
285: public String getType() {
286: return "Sound";
287: }
288:
289: /** Description of the Method */
290: void waitSoundReadiness() {
291: ContentNegotiator cn = new ContentNegotiator(this );
292: // getContent blocks until sound is ready
293: // never happens. Sound is loaded by soundscheduler, sometime
294: // when it is live which could not happen here. If someday,
295: // Sound has a byte array in method, then keep this.
296: Sound returned = (Sound) cn.getContent();
297: }
298:
299: /**
300: * Description of the Method
301: *
302: *@param p Description of the Parameter
303: */
304: void updateParent(javax.media.j3d.Node p) {
305: if (impl.getParent() != null) {
306: ((javax.media.j3d.Group) p).addChild(impl);
307: }
308: setClip();
309: }
310:
311: }
|