VisClient/org/hfbk/vis/visnode/VisSound.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.graph.Node;
00007 import org.lwjgl.openal.AL;
00008 import org.lwjgl.openal.AL10;
00009 import org.lwjgl.openal.AL11;
00010 import org.lwjgl.opengl.GL11;
00011 import org.lwjgl.util.WaveData;
00012 import org.lwjgl.util.vector.Vector3f;
00013 
00014 
00021 public class VisSound extends Sound {
00022         
00023         final int SAMPLE_STEP=150;
00024         short[] samples;
00025         
00026         int ringpos=0;
00027         final int RING_LENGTH=100;
00028         // a ring buffer for past sample storage
00029         final short[] ring=new short[RING_LENGTH];
00030         
00031         public VisSound(Node n, Vector3f pos) {
00032                 super(n.text);
00033                 this.position=pos;
00034                 // 
00035         }
00036 
00037         void renderSelf() {
00038                 super.renderSelf();
00039                 
00040                 GL11.glDisable(GL11.GL_TEXTURE_2D);
00041                 GL11.glColor3f(1,1,1);
00042                 
00043                 
00044                 if (samples!=null){
00045                         int samplepos=(int)(AL10.alGetSourcef(source,AL11.AL_SAMPLE_OFFSET)/SAMPLE_STEP);
00046                         
00047                         if (samplepos<samples.length)                           
00048                                 ring[ringpos]=samples[samplepos];
00049                         else
00050                                 ring[ringpos]=0;
00051                         
00052                         for (int i=1; i<100; i++){
00053                                 float y;
00054                                 
00055                                 y=ring[(ringpos-i+RING_LENGTH)%RING_LENGTH]*.0001f;
00056                                  
00057                                 float r=i/5f;
00058                                 
00059                                 if (Math.abs(y)>.01){
00060                                         GL11.glColor4f(1,1,1,  1-i/130f);
00061                                         GL11.glBegin(GL11.GL_LINE_LOOP);
00062                                                 for (float a=0; a<2*Math.PI; a+=.1f)
00063                                                         GL11.glVertex3f((float)Math.sin(a)*r, y, (float)Math.cos(a)*r);
00064                                         GL11.glEnd();
00065                                 }
00066                         }
00067                 }
00068                 ringpos++; ringpos%=RING_LENGTH;
00069         }
00070 
00074         public void trigger(boolean looping){
00075                 if (!AL.isCreated()) return; 
00076                 if (source==0){
00077                         source=ALUtil.createSource();
00078                         try {
00079                                 WaveData wav=ALUtil.loadWav(wavfile);
00080                                 buffer=ALUtil.createBuffer(wav);
00081                                 samples=ALUtil.sample(wav,SAMPLE_STEP);
00082                         } catch (MalformedURLException e) {
00083                                 e.printStackTrace();
00084                         }
00085                         AL10.alSourcei(source, AL10.AL_BUFFER, buffer);
00086                 }
00087                 
00088                 super.trigger(looping);
00089         }
00090 }

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