/* * 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 AdvancedControls1 extends java.applet.Applet implements Runnable { /* * 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 thread that moves the sound back and forth */ private Thread spatializerThread = null; boolean movingRight = true; /* * the name of the sound file and image that represent the sound source */ String soundName; String soundIcon; /* * the current sound source position */ Vector3D soundSourcePos; /* * canvas that we draw on */ Canvas top; /* * image that represents the sound source */ Image soundSourceImage; /* * controls for the reverberation settings */ Button room_reverb; Button chamber_reverb; Button stage_reverb; Button hall_reverb; Button plate_reverb; /* * controls for the speed of sound settings */ Button speed_up; Button speed_down; Label speed_label; /* * controls for the pitch settings */ Button pitch_up; Button pitch_down; Label pitch_label; /* * reset button */ Button reset; public void init() { System.out.println("Initializing applet AdvancedControls1...."); 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 = "scifi10.wav"; soundIcon = getParameter("SOUND_ICON"); if (soundIcon == null) soundIcon = "purple.gif"; /* * Create the image */ soundSourceImage = getImage(getDocumentBase(), soundIcon); /* * Create the controls and set up the ui */ room_reverb = new Button("Room"); chamber_reverb = new Button("Chamber"); stage_reverb = new Button("Stage"); hall_reverb = new Button("Hall"); plate_reverb = new Button("Plate"); speed_up = new Button("+"); speed_down = new Button("-"); speed_label = new Label("Speed of Sound: = 16000", Label.CENTER); pitch_up = new Button("+"); pitch_down = new Button("-"); pitch_label = new Label("Pitch: 1", Label.CENTER); reset = new Button("Reset"); /* * Set the overall layout type * The first element shows the sound moving; * the others control the sound */ setLayout(new GridLayout(5,1)); /* * Here we show the sound moving */ top = new Canvas(); /* * Here we show the reverb buttons */ Panel reverbPanel = new Panel(); reverbPanel.setLayout(new GridLayout(1,5)); reverbPanel.add(room_reverb); reverbPanel.add(chamber_reverb); reverbPanel.add(stage_reverb); reverbPanel.add(hall_reverb); reverbPanel.add(plate_reverb); /* * Here we show the speed of sound controls */ Panel speedPanel = new Panel(); speedPanel.setLayout(new GridLayout(1,3)); speedPanel.add(speed_up); speedPanel.add(speed_label, Label.CENTER); speedPanel.add(speed_down); /* * Here we show the pitch controls */ Panel pitchPanel = new Panel(); pitchPanel.setLayout(new GridLayout(1,3)); pitchPanel.add(pitch_up); pitchPanel.add(pitch_label, Label.CENTER); pitchPanel.add(pitch_down); /* * Here we show the reset button */ Panel resetPanel = new Panel(); resetPanel.setLayout(new GridLayout(1,1)); resetPanel.add(reset); /* * Add the components to our main applet space */ this.add(top); this.add(reverbPanel); this.add(speedPanel); this.add(pitchPanel); this.add(resetPanel); /* * 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"); showStatus("FAILED TO INITIALIZE AUDIO"); 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"); showStatus("FAILED TO CREATE THE LISTENER; CHECK AUDIO DEVICE"); 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 SoundSource (audio emitter) */ try { soundSource = env.createCachedSoundSource(docBase + soundName); soundSource.setModel(new SoundSourceModel(40.0f, 40.0f, 250.0f, 250.0f, 0.7f)); System.out.println("Created the sound source"); } catch (Exception e) { System.out.println("Failed to create the sound source"); showStatus("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 AdvancedControls1...."); /* * Set the position of the listener (in the middle of * the sound's path) and the Y component of the sound * source */ soundSourcePos = new Vector3D(); soundSourcePos.y = top.size().height/2.0f; if (listener != null) listener.setPosition(top.size().width/2.0f, top.size().height/2.0f, -60.0f); if (soundSource != null) soundSource.setPosition(soundSourcePos); top.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); /* * 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 AdvancedControls1...."); /* * 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 AdvancedControls1...."); /* * 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 void run() { /* * This moves the sound back and forth */ while (true) { if (movingRight) { soundSourcePos.x += 3.0f; if (soundSourcePos.x > top.size().width - soundSourceImage.getWidth(this)) movingRight = false; } else { soundSourcePos.x -= 3.0f; if (soundSourcePos.x < 0.0f) movingRight = true; } if (soundSource != null) soundSource.setPosition(soundSourcePos); repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { } } // while } // run() public boolean action(Event evt, Object arg) { /* * change the reverb settings */ if (evt.target == room_reverb) { if (env != null) env.setReverb(ReverbModel.ROOM_REVERB); resetLabels(); room_reverb.setLabel("ROOM"); repaint(); return true; } if (evt.target == chamber_reverb) { if (env != null) env.setReverb(ReverbModel.CHAMBER_REVERB); resetLabels(); chamber_reverb.setLabel("CHAMBER"); repaint(); return true; } if (evt.target == stage_reverb) { if (env != null) env.setReverb(ReverbModel.STAGE_REVERB); resetLabels(); stage_reverb.setLabel("STAGE"); repaint(); return true; } if (evt.target == hall_reverb) { if (env != null) env.setReverb(ReverbModel.HALL_REVERB); resetLabels(); hall_reverb.setLabel("HALL"); repaint(); return true; } if (evt.target == plate_reverb) { if (env != null) env.setReverb(ReverbModel.PLATE_REVERB); resetLabels(); plate_reverb.setLabel("PLATE"); repaint(); return true; } /* * change the speed of sound settings */ if (evt.target == speed_up && env != null) { EnvironmentModel model; try { model = env.getModel(); } catch (SpatialAudioException e) { return true; } if (model.speedOfSound == 0.0f) model.speedOfSound = 100.0f; model.speedOfSound *= 2.0f; env.setModel(model); speed_label.setText("Speed of Sound: " + model.speedOfSound); return true; } if (evt.target == speed_down && env != null) { EnvironmentModel model; try { model = env.getModel(); } catch (SpatialAudioException e) { return true; } model.speedOfSound = model.speedOfSound / 2.0f; if (model.speedOfSound < 0.0f) model.speedOfSound = 0.0f; env.setModel(model); speed_label.setText("Speed of Sound: " + model.speedOfSound); return true; } /* * change the pitch settings */ if (evt.target == pitch_up && soundSource != null) { float pitch = soundSource.getPitch(); pitch *= 2.0f; soundSource.setPitch(pitch); pitch_label.setText("Pitch: " + pitch); return true; } if (evt.target == pitch_down && soundSource != null) { float pitch = soundSource.getPitch(); pitch = pitch/2.0f; soundSource.setPitch(pitch); pitch_label.setText("Pitch: " + pitch); return true; } /* * reset everything */ if (evt.target == reset) { /* set reverb to "ROOM" */ if (env != null) { env.setReverb(ReverbModel.ROOM_REVERB); resetLabels(); room_reverb.setLabel("ROOM"); repaint(); /* set speed of sound to 0.0f, disabling doppler effects */ EnvironmentModel model; try { model = env.getModel(); } catch (SpatialAudioException e) { return true; } model.speedOfSound = 16000.0f; env.setModel(model); speed_label.setText("Speed of Sound: " + model.speedOfSound); } /* set pitch adjustment to 1.0f */ float pitch = 1.0f; if (soundSource != null) soundSource.setPitch(pitch); pitch_label.setText("Pitch: " + pitch); return true; } return super.action(evt, arg); } // action() void resetLabels() { room_reverb.setLabel("Room"); chamber_reverb.setLabel("Chamber"); stage_reverb.setLabel("Stage"); hall_reverb.setLabel("Hall"); plate_reverb.setLabel("Plate"); } public void paint(Graphics g) { /* * This draws the images representing the listener and the sound source */ Graphics h = top.getGraphics(); h.setColor(Color.white); h.fillRect(0, 0, top.size().width, top.size().height); h.drawImage(soundSourceImage, (int)soundSourcePos.x, (int)soundSourcePos.y, top); } // paint() } // Validate class