Les sons en Java3D
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.net.*;
import java.io.*;
public class SimpleSound extends Applet {
static boolean applet = true ;
private String base ;
private MediaContainer chercheMediaContainer(String s) {
MediaContainer sd = null;
if ( applet ) {
try {
URL u1 = new URL(base+s);
sd = new MediaContainer(u1); }
catch(MalformedURLException e) {
System.out.println("Malformed URL"); } }
else {
try {
URL u1 = new URL("file:./"+s);
sd = new MediaContainer(u1); }
catch(MalformedURLException e) {
System.out.println("File not found"); } }
return(sd);
}
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
objRoot.addChild(objTrans);
MouseRotate behavior = new MouseRotate(objTrans);
objTrans.addChild(behavior);
behavior.setSchedulingBounds(bounds);
objTrans.addChild(new ColorCube(0.4));
{ MediaContainer sd = chercheMediaContainer("Drum.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,0.0f,0.0f,1.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
{ MediaContainer sd = chercheMediaContainer("Laser.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,0.0f,0.0f,-1.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
{ MediaContainer sd = chercheMediaContainer("Applause.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
{ MediaContainer sd = chercheMediaContainer("Clap.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,0.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
{ MediaContainer sd = chercheMediaContainer("Exclamation.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
{ MediaContainer sd = chercheMediaContainer("Chimes.wav");
if ( sd != null ) {
ConeSound cs = new ConeSound(sd,3.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f);
cs.setLoop(Sound.INFINITE_LOOPS);
cs.setEnable(true);
cs.setSchedulingBounds(bounds);
objTrans.addChild(cs); } }
objRoot.compile();
return objRoot;
}
public void lanceJava3D() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
AudioDevice audioDev = u.getViewer().createAudioDevice();
BranchGroup scene = createSceneGraph();
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void init() {
if ( applet ) {
URL url = this.getCodeBase();
base = url.toString();
lanceJava3D(); }
}
public SimpleSound() {
if ( !applet ) {
lanceJava3D(); }
}
public static void main(String[] args) {
applet = false ;
new MainFrame(new SimpleSound(),350,250);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr