/* * 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.net.*; import java.awt.*; import java.lang.Math; import spatialAudio.*; import java.io.IOException; import java.net.MalformedURLException; public class Validate extends java.applet.Applet implements Runnable { /* * the thread that moves the sounds around */ private Thread spatializerThread = null; /* * the spatialAudio environment */ Environment env = null; /* * the direct listener through which we hear the sounds */ DirectListener listener = null; /* * the first sound */ CachedSoundSource sound1 = null; String soundName1; /* * the second sound */ CachedSoundSource sound2 = null; String soundName2; /* * the third sound */ CachedSoundSource sound3 = null; String soundName3; /* * buttons for controlling on which plane the sounds move */ Button XY_button; Button XZ_button; Button YZ_button; /* * buttons that show the names of the three sounds and allow * the user to select the active sound */ Button button1; Button button2; Button button3; /* * keeps track of which sound and axis is active */ int activeAxis = 0; int activeSound = 1; /* * variables for controlling the circular movement of the sounds */ double angle = 0.0; double increment = 0.1; double radius = 100.0; Vector3D vec; public void init() { System.out.println("Initializing applet Validate...."); setBackground(Color.white); /* * Get the sound names. * You can make the applet use your own sound files by providing * the names in the HTML file. The sound files need to be in the * same directory as the HTML file. */ soundName1 = getParameter("SOUND1"); if (soundName1 == null) soundName1 = "band1.wav"; soundName2 = getParameter("SOUND2"); if (soundName2 == null) soundName2 = "band.wav"; soundName3 = getParameter("SOUND3"); if (soundName3 == null) soundName3 = "banjo1.wav"; /* * Create the buttons for controlling the axes */ XY_button = new Button("XY Plane"); XZ_button = new Button("XZ Plane"); YZ_button = new Button("YZ Plane"); this.add(XY_button); this.add(XZ_button); this.add(YZ_button); /* * Create the buttons for controlling which sound is active */ String buttonLabel1 = soundName1; if (buttonLabel1.endsWith(".wav")) buttonLabel1 = buttonLabel1.substring(0, buttonLabel1.length() - 4); String buttonLabel2= soundName2; if (buttonLabel2.endsWith(".wav")) buttonLabel2 = buttonLabel2.substring(0, buttonLabel2.length() - 4); String buttonLabel3 = soundName3; if (buttonLabel3.endsWith(".wav")) buttonLabel3 = buttonLabel3.substring(0, buttonLabel3.length() - 4); button1 = new Button(buttonLabel1); button2 = new Button(buttonLabel2); button3 = new Button(buttonLabel3); this.add(button1); this.add(button2); this.add(button3); /* * Initialize the position vector */ vec = new Vector3D(0.0f, 0.0f, 0.0f); /* * 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; } /* * Then set the characteristics of the environment. * This creates an environment which uses a right-handed coordinate system, * no Doppler effects, and the best quality localization. */ EnvironmentModel envModel = new EnvironmentModel(RenderOptions.USE_RIGHT_HAND, RenderOptions.DEFAULT, RenderOptions.MODERATE_BUDGET, null); if (!env.setModel(envModel)) System.out.println("Failed to set the environment model"); /* * Next, create the listener. * Passing the applet as the info parameter allows the package to * extract a window handle and enable DirectSound. */ try { listener = env.createDirectListener(WaveFormat.PCM_22K_16BIT_STEREO, (Object)this); System.out.println("Created the listener..."); } catch (SpatialAudioException e) { System.out.println("Failed to create the listener"); listener = null; return; } /* * Set the listener position. * This listener will be at the origin facing in the positive Z direction. */ 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"); if (!listener.setPosition(0.0f, 0.0f, 0.0f)) System.out.println("Failed to set the position of the listener"); System.out.println("Done with spatialAudio initialization"); /* * Here we create, in parallel, the three sound sources 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(); String urlStr = new String(); int lastSlash = docBase.lastIndexOf('/'); if (lastSlash != -1) docBase = docBase.substring(0, lastSlash+1); /* * Now, we create the SoundSources (audio emitters) */ try { sound1 = env.createCachedSoundSource(docBase + soundName1); System.out.println("Created the first sound"); } catch (Exception e) { System.out.println("Failed to create the first sound"); sound1 = null; } try { sound2 = env.createCachedSoundSource(docBase + soundName2); System.out.println("Created the second sound"); } catch (Exception e) { System.out.println("Failed to create the second sound"); sound2 = null; } try { sound3 = env.createCachedSoundSource(docBase + soundName3); System.out.println("Created the third sound"); } catch (Exception e) { System.out.println("Failed to create the third sound"); sound3 = null; } /* * Now, set the model, position, and orientation of the sounds. * Thd sounds will be directionless (spherical), so direction doesn't matter. * We place them at the origin. */ Vector3D posVec = new Vector3D(0.0f, 0.0f, 0.0f); Vector3D dirVec = new Vector3D(1.0f, 0.0f, 0.0f); SoundSourceModel soundModel = new SoundSourceModel(20.0f, 20.0f, 180.0f, 180.0f, 1.0f); if (sound1 != null) { sound1.setModel(soundModel); sound1.setPosition(posVec); sound1.setOrientation(dirVec); } if (sound2 != null) { sound2.setModel(soundModel); sound2.setPosition(posVec); sound2.setOrientation(dirVec); } if (sound3 != null) { sound3.setModel(soundModel); sound3.setPosition(posVec); sound3.setOrientation(dirVec); } System.out.println("Done initializing the sound sources!"); } // init() public void start() { System.out.println("Starting applet Validate...."); /* * 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 first sound to play, looping continuously. */ playSound(); /* * Start the thread that moves the sound. */ if (spatializerThread == null) { spatializerThread = new Thread(this); spatializerThread.start(); } } // start() public void stop() { System.out.println("Stopping applet Validate...."); /* * Stop the spatializer thread */ if ((spatializerThread != null) && spatializerThread.isAlive()) spatializerThread.stop(); spatializerThread = null; /* * 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. */ listener.disconnect(); } // stop() public void destroy() { System.out.println("Destroying applet Validate...."); /* * Release all resources */ if (sound1 != null) { sound1.release(); sound1 = null; } if (sound2 != null) { sound2.release(); sound2 = null; } if (sound3 != null) { sound3.release(); sound3 = null; } if (listener != null) { listener.release(); listener = null; } if (env != null) { env.release(); env = null; } } // destroy() public void run() { /* * This moves the active sound around in a circle. */ while (true) { angle += increment; if (angle >= 2*Math.PI) { angle = 0.0f; } float a = (float)((Math.sin(angle))*radius); float b = (float)((Math.cos(angle))*radius); if (activeAxis == 0) { // XY plane vec.z = 0.0f; vec.x = b; vec.y = a; } if (activeAxis == 1) { // XZ plane vec.y = 0.0f; vec.x = b; vec.z = a; } if (activeAxis == 2) { // YZ plane vec.x = 0.0f; vec.y = b; vec.z = a; } if (sound1 != null) sound1.setPosition(vec); if (sound2 != null) sound2.setPosition(vec); if (sound3 != null) sound3.setPosition(vec); try { Thread.sleep(100); } catch (InterruptedException e) { } } // while (true) } // run() public boolean action(Event evt, Object arg) { if (evt.target == XY_button) { activeAxis = 0; repaint(); return true; } if (evt.target == XZ_button) { activeAxis = 1; repaint(); return true; } if (evt.target == YZ_button) { activeAxis = 2; repaint(); return true; } if (evt.target == button1) { activeSound = 1; playSound(); return true; } if (evt.target == button2) { activeSound = 2; playSound(); return true; } if (evt.target == button3) { activeSound = 3; playSound(); return true; } return super.action(evt, arg); } // action() public void playSound() { /* * This causes the active sound to start looping and * the other sounds to stop playing. */ switch(activeSound) { case 1: if (sound1 != null) { sound1.setPlayState(MediaState.AUDIO_PLAY); System.out.println(" The first sound should be playing...!"); } else System.out.println("Failed to play the first sound"); if (sound2 != null) sound2.setPlayState(MediaState.AUDIO_STOP); if (sound3 != null) sound3.setPlayState(MediaState.AUDIO_STOP); break; case 2: if (sound2 != null) { sound2.setPlayState(MediaState.AUDIO_PLAY); System.out.println(" The second sound should be playing...!"); } else System.out.println("Failed to play the second sound"); if (sound1 != null) sound1.setPlayState(MediaState.AUDIO_STOP); if (sound3 != null) sound3.setPlayState(MediaState.AUDIO_STOP); break; case 3: if (sound3 != null) { sound3.setPlayState(MediaState.AUDIO_PLAY); System.out.println(" The third sound should be playing...!"); } else System.out.println("Failed to play the third sound"); if (sound1 != null) sound1.setPlayState(MediaState.AUDIO_STOP); if (sound2 != null) sound2.setPlayState(MediaState.AUDIO_STOP); break; } // switch } public void paint(Graphics g) { /* * This draws the active axis in red and the others in blue. */ if (activeAxis == 0) g.setColor(Color.red); else g.setColor(Color.blue); g.drawOval(10, 60, 190, 190); // 0, the XY plane if (activeAxis == 1) g.setColor(Color.red); else g.setColor(Color.blue); g.drawOval(10, 140, 190, 20); // 1, the XZ plane if (activeAxis == 2) g.setColor(Color.red); else g.setColor(Color.blue); g.drawOval(90, 60, 20, 190); // 2, the YZ plane } // paint() } // Validate class