/* * 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 AdvancedControls2 extends java.applet.Applet implements Runnable { /* * the spatialAudio environment */ Environment env; /* * the direct listener through which we hear the sounds */ DirectListener listener; /* * the sound sources */ CachedSoundSource soundSource1; CachedSoundSource soundSource2; CachedSoundSource soundSource3; CachedSoundSource soundSource4; CachedSoundSource soundSource5; /* * the thread that moves the sound back and forth */ private Thread spatializerThread; boolean movingRight = true; /* * the name of the sound files and image that represent the sound source */ String soundName1; String soundName2; String soundName3; String soundName4; String soundName5; String soundIcon; /* * the current sound source position */ Vector3D soundSourcePos; /* * image that represents the sound source */ Image soundSourceImage; public void init() { System.out.println("Initializing applet AdvancedControls2...."); 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. */ soundName1 = getParameter("SOUND1"); if (soundName1 == null) soundName1 = "sounds/bigben.wav"; soundName2 = getParameter("SOUND2"); if (soundName2 == null) soundName2 = "sounds/vamphowl.wav"; soundName3 = getParameter("SOUND3"); if (soundName3 == null) soundName3 = "sounds/kenned3.wav"; soundName4 = getParameter("SOUND4"); if (soundName4 == null) soundName4 = "sounds/nixon.wav"; soundName5 = getParameter("SOUND5"); if (soundName5 == null) soundName5 = "sounds/drum1.wav"; soundIcon = getParameter("SOUND_ICON"); if (soundIcon == null) soundIcon = "purple.gif"; /* * Create the image */ soundSourceImage = getImage(getDocumentBase(), soundIcon); /* * Initialize the audio */ /* * First, create the environment and set a value for speed of sound */ try { env = SpatialAudio.createEnvironment(); EnvironmentModel model = env.getModel(); model.speedOfSound = 16000.0f; model.cpuBudget = RenderOptions.MODERATE_BUDGET; env.setModel(model); System.out.println("Created the environment"); } catch (SpatialAudioException e) { System.out.println("Failed to create the environment"); env = null; return; } /* * Next, create the listener. */ try { listener = env.createDirectListener(); System.out.println("Created 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. */ 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"); /* * 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 four SoundSources with various creation flags. * The first two are in the same synchronization group. We want to * get notification when each one finishes playing. */ /* * This one opts for the minimum on everything and belongs to * synchronization group 1 */ try { soundSource1 = env.createCachedSoundSource( RenderOptions.NO_DOPPLER + RenderOptions.NO_REVERB + RenderOptions.NO_SPATIALIZE + RenderOptions.NO_ATTENUATE, docBase + soundName1, 1, "event1", this, null); System.out.println("Created the first sound source"); } catch (Exception e) { System.out.println("Failed to create the first sound source"); soundSource1 = null; } /* * No Doppler on this sound, which is also in synchronization group 1 */ try { soundSource2 = env.createCachedSoundSource( RenderOptions.NO_DOPPLER, docBase + soundName2, 1, "event2", this, null); System.out.println("Created the second sound source"); } catch (Exception e) { System.out.println("Failed to create the second sound source"); soundSource2 = null; } /* * This one is entirely standard, but requests no reverb */ try { soundSource3 = env.createCachedSoundSource( RenderOptions.NO_REVERB, docBase + soundName3, 0, "event3", this, null); System.out.println("Created the third sound source"); } catch (Exception e) { System.out.println("Failed to create the third sound source"); soundSource3 = null; } /* * This one does not have reverb applied to it */ try { soundSource4 = env.createCachedSoundSource( RenderOptions.NO_REVERB, docBase + soundName4, 0, "event4", this, null); System.out.println("Created the fourth sound source"); } catch (Exception e) { System.out.println("Failed to create the fourth sound source"); soundSource4 = null; } /* * This one does not have reverb applied to it */ try { soundSource5 = env.createCachedSoundSource( RenderOptions.NO_SPATIALIZE, docBase + soundName5, 0, "event5", this, null); System.out.println("Created the fifth sound source"); } catch (Exception e) { System.out.println("Failed to create the fifth sound source"); soundSource4 = null; } /* * They all get the same model, with some intensity adjustments */ SoundSourceModel model = new SoundSourceModel(40.0f, 40.0f, 250.0f, 250.0f, 0.05f); if (soundSource1 != null) soundSource1.setModel(model); model.intensity = 0.3f; if (soundSource2 != null) soundSource2.setModel(model); model.intensity = 1.0f; if (soundSource3 != null) soundSource3.setModel(model); if (soundSource4 != null) soundSource4.setModel(model); if (soundSource5 != null) soundSource5.setModel(model); System.out.println("Done initializing the audio!"); repaint(); } // init() public void start() { System.out.println("Starting applet AdvancedControls2...."); soundSourcePos = new Vector3D(); soundSourcePos.y = this.size().height/2.0f; if (listener != null) listener.setPosition(this.size().width/2.0f, this.size().height/2.0f, 0.0f); repaint(); /* * 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 twice and starting 150 milliseconds in * from the beginning. Tell the fifth sound to play once from the beginning. */ if (soundSource1 != null) soundSource1.setPlayState(MediaState.AUDIO_PLAY, 2, 0.15f); if (soundSource1 != null) soundSource5.setPlayState(MediaState.AUDIO_PLAY, 3, 0.0f); /* * 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 AdvancedControls2...."); /* * 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. */ if (listener != null) listener.disconnect(); } // stop() public void destroy() { System.out.println("Destroying applet AdvancedControls2...."); /* * Release all resources */ if (soundSource1 != null) { soundSource1.release(); soundSource1 = null; } if (soundSource2 != null) { soundSource2.release(); soundSource2 = null; } if (soundSource3 != null) { soundSource3.release(); soundSource3 = null; } if (soundSource4 != null) { soundSource4.release(); soundSource4 = null; } if (soundSource5 != null) { soundSource5.release(); soundSource5 = null; } if (listener != null) { listener.release(); listener = null; } if (env != null) { env.release(); env = null; } } // destroy() public void run() { /* * This moves the sound back and forth */ while (true) { if (movingRight) { soundSourcePos.x += 3.0f; if (soundSourcePos.x > this.size().width - soundSourceImage.getWidth(this)) movingRight = false; } else { soundSourcePos.x -= 3.0f; if (soundSourcePos.x < 0.0f) movingRight = true; } if (soundSource1 != null) soundSource1.setPosition(soundSourcePos); if (soundSource2 != null) soundSource2.setPosition(soundSourcePos); if (soundSource3 != null) soundSource3.setPosition(soundSourcePos); if (soundSource4 != null) soundSource4.setPosition(soundSourcePos); if (soundSource5 != null) soundSource5.setPosition(soundSourcePos); repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { } } // while } // run() public boolean handleEvent(Event evt) { switch(evt.id) { case SoundSource.PLAYBACK_EVENT: System.out.println("Got an event!"); /* * The first two sounds belong to the same synchronization group. * Here they take turns controlling their synchronized playback. */ if (evt.arg == "event1") { if (soundSource2 != null) soundSource2.setPlayState(MediaState.AUDIO_PLAY, 3, 0.0f); return true; } if (evt.arg == "event2") { if (soundSource1 != null) soundSource1.setPlayState(MediaState.AUDIO_PLAY, 1, 0.1f); return true; } /* * The remaining three sounds trade off: when the playback completion * event from sound 3 is received, sound 4 is started. When 4 is done, * 5 is started. When 5 is done, 3 is started again. */ if (evt.arg == "event3") { System.out.println("EVENT 3"); if (soundSource4 != null) soundSource4.setPlayState(MediaState.AUDIO_PLAY, 1, 0.0f); return true; } if (evt.arg == "event4") { System.out.println("EVENT 4"); if (soundSource5 != null) soundSource5.setPlayState(MediaState.AUDIO_PLAY, 1, 0.2f); return true; } if (evt.arg == "event5") { System.out.println("EVENT 5"); if (soundSource3 != null) soundSource3.setPlayState(MediaState.AUDIO_PLAY, 1, 0.0f); return true; } } // switch return false; } public void paint(Graphics g) { /* * This draws the images representing the listener and the sound source */ g.setColor(Color.white); g.fillRect(0, 0, this.size().width, this.size().height); g.drawImage(soundSourceImage, (int)soundSourcePos.x, (int)soundSourcePos.y, this); } // paint() } // Validate class