/* * 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 SoundModel extends java.applet.Applet { /* * the spatialAudio environment */ Environment env = null; /* * the direct listener through which we hear the sounds */ DirectListener listener = null; /* * 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; /* * variables for storing the current sound source position, * and sound source model */ Vector3D soundSourcePos; SoundSourceModel soundSourceModel; /* * buttons for choosing a sound model */ Button pointSound; Button directionalSound; Button ambientSound; /* * image that represents the sound source */ Image soundSourceImage; public void init() { System.out.println("Initializing applet SoundModel...."); 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 = "Guitar.wav"; soundIcon = getParameter("SOUND_ICON"); if (soundIcon == null) soundIcon = "purple.gif"; /* * Create the image */ soundSourceImage = getImage(getDocumentBase(), soundIcon); /* * Create the buttons */ pointSound = new Button("Point"); directionalSound = new Button("Directional"); ambientSound = new Button("Ambient"); /* * Add the components to our main applet space */ this.add(pointSound); this.add(directionalSound); this.add(ambientSound); /* * Initialize the audio */ soundSourcePos = new Vector3D(); /* * 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 and set its orientation. */ try { listener = env.createDirectListener(); System.out.println("Created the listener..."); listener.setOrientation(0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); } catch (SpatialAudioException e) { System.out.println("Failed to create the listener"); listener = null; return; } /* * 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); soundSourceModel = new SoundSourceModel(20.0f, 20.0f, 100.0f, 100.0f, 1.0f); soundSource.setModel(soundSourceModel); soundSource.setOrientation(1.0f, 0.0f, 0.0f); System.out.println("Created the sound source"); } catch (Exception e) { System.out.println("Failed to create the sound source"); soundSource = null; } System.out.println("Done initializing the audio!"); repaint(); } // init() public void start() { System.out.println("Starting applet SoundModel...."); soundSourcePos.x = this.size().width/2.0f; soundSourcePos.y = this.size().height/2.0f; if (soundSource != null) soundSource.setPosition(soundSourcePos); if (listener != null) listener.setPosition(soundSourcePos.x+25, soundSourcePos.y, soundSourcePos.z); this.invalidate(); 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 sound to play, looping continuously. */ if (soundSource != null) soundSource.setPlayState(MediaState.AUDIO_PLAY); repaint(); } // start() public void stop() { System.out.println("Stopping applet SoundModel...."); /* * 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 SoundModel...."); /* * 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 handleEvent(Event evt) { if (evt.target == pointSound) { /* * Make sure we're centered */ soundSourcePos.x = this.size().width/2.0f; soundSourcePos.y = this.size().height/2.0f; if (soundSource != null) soundSource.setPosition(soundSourcePos); /* * Adjust the model */ soundSourceModel.minFront = 20.0f; soundSourceModel.minBack = 20.0f; soundSourceModel.maxFront = 100.0f; soundSourceModel.maxBack = 100.0f; if (soundSource != null) soundSource.setModel(soundSourceModel); /* * Update the graphics */ repaint(); return true; } if (evt.target == directionalSound) { /* * Move to the side */ soundSourcePos.x = this.size().width/2.0f - 70; soundSourcePos.y = this.size().height/2.0f; if (soundSource != null) soundSource.setPosition(soundSourcePos); /* * Adjust the model */ soundSourceModel.minFront = 50.0f; soundSourceModel.minBack = 12.0f; soundSourceModel.maxFront = 190.0f; soundSourceModel.maxBack = 40.0f; if (soundSource != null) soundSource.setModel(soundSourceModel); /* * Update the graphics */ repaint(); return true; } if (evt.target == ambientSound) { /* * Make sure we're centered */ soundSourcePos.x = this.size().width/2.0f; soundSourcePos.y = this.size().height/2.0f; if (soundSource != null) soundSource.setPosition(soundSourcePos); /* * Adjust the model */ soundSourceModel.minFront = 68.0f; soundSourceModel.minBack = 68.0f; soundSourceModel.maxFront = 70.0f; soundSourceModel.maxBack = 70.0f; if (soundSource != null) soundSource.setModel(soundSourceModel); /* * Update the graphics */ repaint(); return true; } return false; } public boolean mouseMove(Event evt, int x, int y) { /* * Keep the listener synched with the mouse position * We just set z to zero all the time because we are working in 2D. */ if (listener != null) listener.setPosition((float)x, (float)y, 0.0f); return false; } public boolean mouseEnter(Event evt, int x, int y) { /* * This is a contrived way of forcing the applet to * paint itself. */ invalidate(); pointSound.invalidate(); directionalSound.invalidate(); ambientSound.invalidate(); repaint(); 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.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.maxBack), (int)(soundSourcePos.y - soundSourceModel.maxBack), (int)(soundSourceModel.maxBack + soundSourceModel.maxFront), (int)(soundSourceModel.maxBack*2)); g.setColor(Color.red); g.drawOval((int)(soundSourcePos.x - soundSourceModel.minBack), (int)(soundSourcePos.y - soundSourceModel.minBack), (int)(soundSourceModel.minBack + soundSourceModel.minFront), (int)(soundSourceModel.minBack*2)); } // paint() } // Validate class