The Sound of Shooting
Java 3D has three kinds of sound node classes. All three are subclasses of the Sound class.
-
BackgroundSound A
BackgroundSoundnode allows a sound to permeate the entire scene, located at no particular place.-
PointSound A
PointSoundnode has a location, so its volume varies as the user moves (or as the sound node moves). I usePointSoundnodes for the laser-beam and explosion sounds inShooter3D.-
ConeSound A ConeSoundnode is aPointSoundthat can be aimed in a particular direction.
Before sound nodes can be added to a scene, an audio device must be created and linked to the Viewer object. This is simple if the SimpleUniverse utility class is being used (as in this example):
AudioDevice audioDev = su.getViewer().createAudioDevice();
Tip
This line of code appears in the WrapShooter3D
constructor.
SimpleUniverse was introduced in Chapter 14; it builds the view branch part of the scene graph, which specifies how the user's viewpoint is positioned in the world and includes the Viewer object.
WrapShooter3D uses initSound() to load a WAV sound file and create a PointSound object:
private PointSound initSound(String filename) { MediaContainer soundMC = null; try { soundMC = new MediaContainer("file:sounds/" + filename); soundMC.setCacheEnable(true); // load sound into container } catch (Exception ex) { System.out.println(ex); } // create a point sound PointSound ps = new PointSound(); ps.setSchedulingBounds( bounds ); ps.setSoundData( soundMC ); ps.setInitialGain(1.0f); ...