//package joalplaywavwitheffects; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.ByteBuffer; import com.jogamp.openal.*; import com.jogamp.openal.util.*; public class JOALPlayWavWithEffects { static AL al; static ALExt alExt; // Buffers hold sound data. static int[] buffer = new int[1]; // Sources are points emitting sound. static int[] source = new int[1]; // Position of the source sound. static float[] sourcePos = { 0.0f, 0.0f, 0.0f }; // Velocity of the source sound. static float[] sourceVel = { 0.0f, 0.0f, 0.0f }; // Position of the listener. static float[] listenerPos = { 0.0f, 0.0f, 0.0f }; // Velocity of the listener. static float[] listenerVel = { 0.0f, 0.0f, 0.0f }; // Orientation of the listener. (first 3 elems are "at", second 3 are "up") static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }; //New Effects Objects //holds id's of effect slots static int[] uiEffectsSlot = new int[4]; //holds id's of effects static int[] uiEffect = new int[2]; static int loadALData() { // variables to load into int[] format = new int[1]; int[] size = new int[1]; ByteBuffer[] data = new ByteBuffer[1]; int[] freq = new int[1]; int[] loop = new int[1]; // Load wav data into a buffer. al.alGenBuffers(1, buffer, 0); if (al.alGetError() != AL.AL_NO_ERROR) return AL.AL_FALSE; ALut.alutLoadWAVFile( "Sound.wav", format, data, size, freq, loop); al.alBufferData(buffer[0], format[0], data[0], size[0], freq[0]); // ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); // Bind buffer with a source. al.alGenSources(1, source, 0); if (al.alGetError() != AL.AL_NO_ERROR) return AL.AL_FALSE; al.alSourcei(source[0], AL.AL_BUFFER, buffer[0]); al.alSourcef(source[0], AL.AL_PITCH, .8f); al.alSourcef(source[0], AL.AL_GAIN, 0.1f); al.alSourcefv(source[0], AL.AL_POSITION, sourcePos, 1); al.alSourcefv(source[0], AL.AL_VELOCITY, sourceVel, 1); al.alSourcei(source[0], AL.AL_LOOPING, loop[0]); // Do another error check and return. if (al.alGetError() == AL.AL_NO_ERROR) return AL.AL_TRUE; return AL.AL_FALSE; } static int loadALEffects() { al.alGetError(); alExt.alGenAuxiliaryEffectSlots(1, uiEffectsSlot, 0); if (al.alGetError() != AL.AL_NO_ERROR) return AL.AL_FALSE; System.out.println("generated 1 effects slot"); alExt.alGenEffects(1, uiEffect, 0); if(al.alGetError() != AL.AL_NO_ERROR) return AL.AL_FALSE; System.out.println("generated 1 effect"); if (alExt.alIsEffect(uiEffect[0])) { alExt.alEffecti(uiEffect[0], ALExt.AL_EFFECT_TYPE, ALExt.AL_EFFECT_REVERB); if (al.alGetError() != AL.AL_NO_ERROR) System.out.println("ReverbEffectnotSupported"); else alExt.alEffectf(uiEffect[0], ALExt.AL_REVERB_DECAY_TIME, 5.0f); } alExt.alAuxiliaryEffectSloti(uiEffectsSlot[0], ALExt.AL_EFFECTSLOT_EFFECT, uiEffect[0]); if(al.alGetError() == AL.AL_NO_ERROR) System.out.println("Successfully loaded effect into effect slot"); return AL.AL_TRUE; } static int attachSourcesToEffects() { al.alSource3i(source[0], ALExt.AL_AUXILIARY_SEND_FILTER, uiEffectsSlot[0], 0, 0); if (al.alGetError() != AL.AL_NO_ERROR) { System.out.println("failed to configure source send 0"); return AL.AL_FALSE; } return AL.AL_TRUE; } static void setListenerValues() { al.alListenerfv(AL.AL_POSITION, listenerPos, 1); al.alListenerfv(AL.AL_VELOCITY, listenerVel, 1); al.alListenerfv(AL.AL_ORIENTATION, listenerOri, 1); } static void killAllData() { al.alDeleteBuffers(1, buffer, 0); al.alDeleteSources(1, source, 0); ALut.alutExit(); } public static void main(String[] args) { // Initialize OpenAL and clear the error bit. try { al = ALFactory.getAL(); alExt = ALFactory.getALExt(); ALut.alutInit(); al.alGetError(); // } catch (OpenALException e) { } catch (Exception e) { e.printStackTrace(); return; } // Load the wav data. if (loadALData() == AL.AL_FALSE) System.exit(1); if(loadALEffects() == AL.AL_FALSE) System.exit(1); if(attachSourcesToEffects() == AL.AL_FALSE) System.exit(1); setListenerValues(); char[] c = new char[1]; while (c[0] != 'q') { try { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println( "Press a key and hit ENTER: \n" + "'p' to play, 's' to stop, " + "'h' to pause and 'q' to quit"); buf.read(c); switch (c[0]) { case 'p' : // Pressing 'p' will begin playing the sample. al.alSourcePlay(source[0]); break; case 's' : // Pressing 's' will stop the sample from playing. al.alSourceStop(source[0]); break; case 'h' : // Pressing 'n' will pause (hold) the sample. al.alSourcePause(source[0]); break; case 'q' : killAllData(); break; } } catch (IOException e) { System.exit(1); } } } }