VisClient/org/dronus/al/ALUtil.java

Go to the documentation of this file.
00001 package org.dronus.al;
00002 
00003 import java.io.File;
00004 import java.net.MalformedURLException;
00005 import java.nio.ByteBuffer;
00006 import java.nio.FloatBuffer;
00007 import java.nio.IntBuffer;
00008 
00009 import org.dronus.gl.Buffers;
00010 import org.hfbk.vis.Prefs;
00011 import org.lwjgl.BufferUtils;
00012 import org.lwjgl.LWJGLException;
00013 import org.lwjgl.openal.AL;
00014 import org.lwjgl.openal.AL10;
00015 import org.lwjgl.util.WaveData;
00016 import org.lwjgl.util.vector.Vector3f;
00017 
00023 public class ALUtil {
00024 
00025         public static boolean closed=false;
00026 
00027         public static void enable(boolean enabled){
00028                 init();
00029                 if (AL.isCreated())
00030                         AL10.alListenerf(AL10.AL_GAIN, enabled ? 1 : 0);
00031         }
00032         
00033         public static void init() {
00034                 if (closed || AL.isCreated()) return;
00035                 try {
00036                         AL.create(null, 15, 22050, true);
00037                         if (AL10.alGetError() != AL10.AL_NO_ERROR)
00038                                 throw new RuntimeException("ALUtil: Cannot open Al Context.");
00039                         
00040                         AL10.alListenerf(AL10.AL_GAIN,0); //mute until enable(true).
00041                         AL10.alDopplerFactor(0);
00042                         //AL10.alDopplerVelocity(10);
00043                         
00044                         Runtime.getRuntime().addShutdownHook(new Thread(){
00045                                 public void run() {
00046                                         if (AL.isCreated()){
00047                                                 closed=true;
00048                                                 AL.destroy();
00049                                                 if(Prefs.current.verbose) System.out.println("AL destroyed!");
00050                                         }
00051                                 }
00052                         });
00053                         
00054                 } catch (LWJGLException le) {
00055                         closed=true;
00056                         System.out.println("No audio support.");
00057                 }
00058         }
00059 
00060         public static void setSourceOrientation(int source, Vector3f dir, Vector3f up) {
00061                 FloatBuffer b = BufferUtils.createFloatBuffer(6);
00062                 dir.store(b);
00063                 up.store(b);
00064                 b.flip();
00065                 AL10.alSource(source, AL10.AL_ORIENTATION, b);
00066         }
00067 
00068         public static int createSource() {
00069                 init();
00070                 
00071                 IntBuffer b = Buffers.getIntBuffer();
00072                 AL10.alGenSources(b);
00073                 int source = b.get(0);
00074 
00075                 AL10.alSourcef(source, AL10.AL_PITCH, 1f);
00076                 AL10.alSourcef(source, AL10.AL_GAIN, 1f);
00077                 
00078                 AL10.alSourcef(source, AL10.AL_REFERENCE_DISTANCE, 15f);
00079 
00080                 return source;
00081         }
00082 
00083         public static void setListenerOrientation(Vector3f dir, Vector3f up) {
00084                 FloatBuffer b = BufferUtils.createFloatBuffer(6);
00085                 dir.store(b);
00086                 up.store(b);
00087                 b.flip();
00088                 AL10.alListener(AL10.AL_ORIENTATION, b);
00089         }
00090 
00098         public static int createBuffer(WaveData wav) throws MalformedURLException {
00099                 init();
00100                 
00101                 IntBuffer b = Buffers.getIntBuffer();
00102                 AL10.alGenBuffers(b);
00103                 int buffer = b.get(0);
00104 
00105                 AL10.alBufferData(buffer, wav.format, wav.data, wav.samplerate);
00106                 return buffer;
00107         }
00115         public static WaveData loadWav(String wavFileName) throws MalformedURLException {
00116                 File f=new File(wavFileName);
00117                 return WaveData.create(f.toURI().toURL());
00118         }
00119         
00120         public static short[] sample(WaveData wav, int step){
00121                 
00122                 ByteBuffer b=wav.data;
00123                 b.rewind();
00124                 int len=b.remaining()/2/step;
00125                 
00126                 short[] sample=new short[len];
00127                 
00128                 for (int i=0; i<len; i++){
00129                         sample[i]=b.getShort(i*2*step);
00130                 }                       
00131                 
00132                 return sample;          
00133         }
00134 }

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