/* * Copyright (C) 1996-1997 Intel Corporation * All Rights Reserved * * A royalty-free copyright license to copy, modify and compile this source * code and to distribute it in your own products in source and binary forms * is hereby granted, so long as this copyright notice and this copyright * license appear in all source code copies. No other rights or licenses * are granted. The Intel name or other trademarks may not be used to * endorse or promote products using this header file without Intel's prior * written permission. * * THIS SOURCE CODE IS PROVIDED "AS IS" WITH NO WARRANTIES WHATSOEVER, * INCLUDING ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, OR FITNESS * FOR ANY PARTICULAR PURPOSE. Intel disclaims all liability, including * liability for infringement of any proprietary rights, relating to use * of this source code. */ import java.applet.*; import java.awt.*; import spatialAudio.*; public class ControllingAudio extends java.applet.Applet { /* * the spatialAudio environment */ Environment env = null; /* * the direct listener through which we hear the sounds */ DirectListener listener = null; /* * the name of the image that represents the listener */ String listenerIcon; /* * the sound source which produces the sound we hear */ CachedSoundSource soundSource = null; /* * the name of the sound file and image that represent the sound source */ String soundName; String soundIcon; /* * labels for describing the buttons */ Label label1; Label label2; Label label3; Label label4; /* * buttons for controlling how the listener moves */ Button listener_up; Button listener_down; Button listener_left; Button listener_right; Button listener_reset; /* * buttons for controlling how the sound source moves */ Button soundSource_up; Button soundSource_down; Button soundSource_left; Button soundSource_right; Button soundSource_reset; /* * buttons for controlling the range of the sound source */ Button bigger; Button smaller; /* * buttons for controlling how the sound plays */ Button playSound; Button pauseSound; Button resumeSound; Button stopSound; /* * canvas that we draw on */ DrawingSpace top; public void init() { System.out.println("Initializing applet ControllingAudio...."); setBackground(Color.white); /* * Get the sound and image names. * You can make the applet use your own sound and image files by providing * the names in the HTML file. The names must be expressed as paths relative * to the location of the HTML file. */ soundName = getParameter("SOUND"); if (soundName == null) soundName = "id82.wav"; listenerIcon = getParameter("LISTENER_ICON"); if (listenerIcon == null) listenerIcon = "ear.gif"; soundIcon = getParameter("SOUND_ICON"); if (soundIcon == null) soundIcon = "purple.gif"; /* * Create the images */ top = new DrawingSpace(getImage(getDocumentBase(), listenerIcon), getImage(getDocumentBase(), soundIcon)); /* * Create the buttons */ listener_up = new Button("^"); listener_down = new Button("v"); listener_left = new Button("<-"); listener_right = new Button("->"); listener_reset = new Button("Reset"); soundSource_up = new Button("^"); soundSource_down = new Button("v"); soundSource_left = new Button("<-"); soundSource_right = new Button("->"); soundSource_reset = new Button("Reset"); playSound = new Button("Play"); pauseSound = new Button("Pause"); resumeSound = new Button("Resume"); stopSound = new Button("Stop"); bigger = new Button("Bigger"); smaller = new Button("Smaller"); /* * Set the overall layout type */ GridBagLayout gb = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gb); c.weightx = 1; // take entire x space inside grid; don't pad outside c.fill = GridBagConstraints.BOTH; // expand in x and y directions c.gridwidth = GridBagConstraints.REMAINDER; // each in separate row Panel controls = new Panel(); controls.setLayout(new GridLayout(1, 4)); /* * Set up the labels */ Panel labels = new Panel(); labels.setBackground(Color.green); labels.setLayout(new GridLayout(1, 4)); label1 = new Label("Listener", Label.CENTER); label2 = new Label("Sound Model", Label.CENTER); label3 = new Label("Sound Source", Label.CENTER); label4 = new Label("Audio Controls", Label.CENTER); labels.add(label1); labels.add(label2); labels.add(label3); labels.add(label4); /* * Set up the control panels */ Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); /* * Set up the buttons on the control panels */ panel1.setLayout(new BorderLayout()); panel1.add("North", listener_up); panel1.add("South", listener_down); panel1.add("West", listener_left); panel1.add("East", listener_right); panel1.add("Center", listener_reset); panel3.setLayout(new BorderLayout()); panel3.add("North", soundSource_up); panel3.add("South", soundSource_down); panel3.add("West", soundSource_left); panel3.add("East", soundSource_right); panel3.add("Center", soundSource_reset); panel2.setLayout(new GridLayout(2,1)); panel2.add(bigger); panel2.add(smaller); panel4.setLayout(new GridLayout(2, 2)); panel4.add(playSound); panel4.add(pauseSound); panel4.add(resumeSound); panel4.add(stopSound); controls.add(panel1); controls.add(panel2); controls.add(panel3); controls.add(panel4); /* * Put all the control components together */ controls.add(panel1); controls.add(panel2); controls.add(panel3); controls.add(panel4); /* * Add the components to our main applet space */ c.weighty = .6; gb.setConstraints(top, c); add(top); c.weighty = .1; gb.setConstraints(labels, c); add(labels); c.weighty = .3; gb.setConstraints(controls, c); add(controls); /* * Initialize the audio */ /* * First, create the environment */ try { env = SpatialAudio.createEnvironment(); System.out.println("Created the environment"); } catch (SpatialAudioException e) { System.out.println("Failed to create the environment"); env = null; return; } /* * Next, create the listener. then set the listener position and orientation. * This listener will be at the origin facing in the positive Z direction. */ try { listener = env.createDirectListener(); System.out.println("Created the listener..."); if (!listener.setOrientation(0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f)) System.out.println("Failed to set the orientation of the listener"); } catch (SpatialAudioException e) { System.out.println("Failed to create the listener"); listener = null; return; } /* * Set the listener position and orientation. * This listener will be at the origin facing in the positive Z direction. */ /* * Here we create the sound source from a String that represents an URL. * You must provide the fully qualified URL; file URL's are okay */ /* * First, we construct the complete URL for an audio file which is * located in the same directory as the HTML page which loaded this * applet */ String docBase = this.getDocumentBase().toString(); int lastSlash = docBase.lastIndexOf('/'); if (lastSlash != -1) docBase = docBase.substring(0, lastSlash+1); /* * Now, we create the SoundSource (audio emitter) */ try { soundSource = env.createCachedSoundSource(docBase + soundName); System.out.println("Created the sound source"); soundSource.setModel(top.soundSourceModel); } catch (Exception e) { System.out.println("Failed to create the sound source"); soundSource = null; } System.out.println("Done initializing the audio!"); } // init() public void start() { System.out.println("Starting applet ControllingAudio...."); top.setup(); if (listener != null) listener.setPosition(top.listenerPos); if (soundSource != null) soundSource.setPosition(top.soundSourcePos); /* * We need to connect the direct listener to the audio device * each time the applet starts, and disconnect it when the applet * stops. This frees up the audio device for other applets and * applications when the applet isn't running. */ if (listener != null) listener.connect(); /* * Tell the sound to play, looping continuously. */ if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_PLAY); top.repaint(); } // start() public void stop() { System.out.println("Stopping applet ControllingAudio...."); /* * Disconnect the listener from the audio device. * This will free the audio device for use by other applets * and applications when the applet isn't running, and make * the sounds stop playing. */ if (listener != null) listener.disconnect(); } // stop() public void destroy() { System.out.println("Destroying applet ControllingAudio...."); /* * Release all resources */ if (soundSource != null) { soundSource.release(); soundSource = null; } if (listener != null) { listener.release(); listener = null; } if (env != null) { env.release(); env = null; } } // destroy() public boolean action(Event evt, Object arg) { /* * change the position of the listener */ if (evt.target == listener_up) { if (listener != null) { try { top.listenerPos = listener.getPosition(); } catch (SpatialAudioException e) { } top.listenerPos.y -= 10.0f; listener.setPosition(top.listenerPos); } top.repaint(); return true; } if (evt.target == listener_down) { if (listener != null) { try { top.listenerPos = listener.getPosition(); } catch (SpatialAudioException e) { } top.listenerPos.y += 10.0f; listener.setPosition(top.listenerPos); } top.repaint(); return true; } if (evt.target == listener_left) { if (listener != null) { try { top.listenerPos = listener.getPosition(); } catch (SpatialAudioException e) { } top.listenerPos.x -= 10.0f; listener.setPosition(top.listenerPos); } top.repaint(); return true; } if (evt.target == listener_right) { if (listener != null) { try { top.listenerPos = listener.getPosition(); } catch (SpatialAudioException e) { } top.listenerPos.x += 10.0f; listener.setPosition(top.listenerPos); } top.repaint(); return true; } if (evt.target == listener_reset) { if (listener != null) { top.listenerPos.x = top.size().width/2.0f - 10; top.listenerPos.y = top.size().height/2.0f; top.listenerPos.z = 0.0f; listener.setPosition(top.listenerPos); } top.repaint(); return true; } /* * change the position of the sound source */ if (evt.target == soundSource_up) { if (soundSource != null) { try { top.soundSourcePos = soundSource.getPosition(); } catch (SpatialAudioException e) { } top.soundSourcePos.y -= 10.0f; soundSource.setPosition(top.soundSourcePos); } top.repaint(); return true; } if (evt.target == soundSource_down) { if (soundSource != null) { try { top.soundSourcePos = soundSource.getPosition(); } catch (SpatialAudioException e) { } top.soundSourcePos.y += 10.0f; soundSource.setPosition(top.soundSourcePos); } top.repaint(); return true; } if (evt.target == soundSource_left) { if (soundSource != null) { try { top.soundSourcePos = soundSource.getPosition(); } catch (SpatialAudioException e) { } top.soundSourcePos.x -= 10.0f; soundSource.setPosition(top.soundSourcePos); } top.repaint(); return true; } if (evt.target == soundSource_right) { if (soundSource != null) { try { top.soundSourcePos = soundSource.getPosition(); } catch (SpatialAudioException e) { } top.soundSourcePos.x += 10.0f; soundSource.setPosition(top.soundSourcePos); } top.repaint(); return true; } if (evt.target == soundSource_reset) { if (soundSource != null) { top.soundSourcePos.x = top.size().width/2.0f + 10; top.soundSourcePos.y = top.size().height/2.0f; top.soundSourcePos.z = 0.0f; soundSource.setPosition(top.soundSourcePos); } top.repaint(); return true; } /* * change the size of the sound source */ if (evt.target == bigger) { top.soundSourceModel.maxFront += 10.0f; top.soundSourceModel.maxBack += 10.0f; top.soundSourceModel.minFront += 2.0f; top.soundSourceModel.minBack += 2.0f; if (soundSource !=null) soundSource.setModel(top.soundSourceModel); top.repaint(); return true; } if (evt.target == smaller) { top.soundSourceModel.maxFront -= 10.0f; top.soundSourceModel.maxBack -= 10.0f; top.soundSourceModel.minFront -= 2.0f; top.soundSourceModel.minBack -= 2.0f; if (soundSource != null) soundSource.setModel(top.soundSourceModel); top.repaint(); return true; } /* * change the play state of the sound source */ if (evt.target == playSound) { if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_PLAY); return true; } if (evt.target == pauseSound) { if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_PAUSE); return true; } if (evt.target == resumeSound) { if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_RESUME); return true; } if (evt.target == stopSound) { if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_STOP); return true; } top.repaint(); return super.action(evt, arg); } // action() } // ControllingAudio class class DrawingSpace extends Canvas { Image soundSourceImage; Image listenerImage; public Vector3D listenerPos; public Vector3D soundSourcePos; public SoundSourceModel soundSourceModel; public DrawingSpace(Image listenerImage, Image soundSourceImage) { this.listenerImage = listenerImage; this.soundSourceImage = soundSourceImage; listenerPos = new Vector3D(); soundSourcePos = new Vector3D(); soundSourceModel = new SoundSourceModel(10.0f, 10.0f, 70.0f, 70.0f, 1.0f); } public void setup() { listenerPos.x = size().width/2.0f - 10; listenerPos.y = size().height/2.0f; soundSourcePos.x = size().width/2.0f + 10; soundSourcePos.y = size().height/2.0f; } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, size().width, size().height); g.setColor(Color.black); g.drawImage(soundSourceImage, (int)soundSourcePos.x - soundSourceImage.getWidth(this)/2, (int)soundSourcePos.y - soundSourceImage.getHeight(this)/2, this); g.drawOval((int)(soundSourcePos.x - soundSourceModel.maxFront), (int)(soundSourcePos.y - soundSourceModel.maxFront), (int)(soundSourceModel.maxFront*2), (int)(soundSourceModel.maxFront*2)); g.drawOval((int)(soundSourcePos.x - soundSourceModel.minFront), (int)(soundSourcePos.y - soundSourceModel.minFront), (int)(soundSourceModel.minFront*2), (int)(soundSourceModel.minFront*2)); g.drawImage(listenerImage, (int)listenerPos.x - listenerImage.getWidth(this)/2, (int)listenerPos.y - listenerImage.getHeight(this)/2, this); } }