VisClient/org/hfbk/vis/visnode/Sound.java

Go to the documentation of this file.
00001 package org.hfbk.vis.visnode;
00002 
00003 import java.net.MalformedURLException;
00004 
00005 import org.dronus.al.ALUtil;
00006 import org.dronus.gl.Buffers;
00007 import org.hfbk.vis.Prefs;
00008 import org.lwjgl.openal.AL;
00009 import org.lwjgl.openal.AL10;
00010 import org.lwjgl.util.vector.Vector3f;
00011 
00018 public class Sound extends VisNode {
00019 
00020         String wavfile;
00021         int source, buffer;
00022         
00023         
00028         public Sound(String wavfile){
00029                 super(null, new Vector3f());
00030                 this.wavfile=wavfile;
00031                 radius=1000;
00032         }
00033         
00034         void renderSelf() {
00035                 //get world position 
00036                 Vector3f worldPosition=traverse(getRoot(), position);
00037                 if (Prefs.current.sound && AL.isCreated()) 
00038                         AL10.alSource(source, AL10.AL_POSITION, Buffers.buffer(worldPosition));
00039         }
00040         
00044         public void trigger(){
00045                 trigger(false);
00046         }
00047 
00051         public void trigger(boolean looping){
00052                 if (!AL.isCreated()) return; 
00053                 if (source==0){
00054                         source=ALUtil.createSource();
00055                         try {
00056                                 buffer=ALUtil.createBuffer(ALUtil.loadWav(wavfile));
00057                         } catch (MalformedURLException e) {
00058                                 e.printStackTrace();
00059                         }
00060                         AL10.alSourcei(source, AL10.AL_BUFFER, buffer);
00061                 }
00062                 AL10.alSourcei(source, AL10.AL_LOOPING, (looping?AL10.AL_TRUE:AL10.AL_FALSE));
00063                 AL10.alSourcePlay(source);
00064         }
00065         
00066         protected void finalize() throws Throwable {
00067                 if(source>0) AL10.alDeleteSources(Buffers.i(source));
00068                 if(buffer>0) AL10.alDeleteBuffers(Buffers.i(buffer));
00069                 if (Prefs.current.verbose) System.out.println("AL Source Destroyed!");
00070                 super.finalize();
00071         }
00072 }

Generated on Tue Apr 7 17:57:21 2009 for visclient by  doxygen 1.5.1