Update Audio.java
This commit is contained in:
parent
255e27f81c
commit
942cc6fb87
1 changed files with 279 additions and 298 deletions
|
@ -1,9 +1,8 @@
|
|||
package mrdev023.audio;
|
||||
|
||||
import static org.lwjgl.openal.AL.createCapabilities;
|
||||
import static org.lwjgl.openal.AL10.*;
|
||||
import static org.lwjgl.openal.ALC10.*;
|
||||
import static org.lwjgl.openal.ALC11.*;
|
||||
import static org.lwjgl.openal.ALUtil.*;
|
||||
import static org.lwjgl.stb.STBVorbis.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
|
@ -23,9 +22,9 @@ public class Audio {
|
|||
|
||||
//Variables global
|
||||
//------------------------------------------------------
|
||||
public static ALDevice device;
|
||||
public static long device;
|
||||
public static ALCCapabilities caps;
|
||||
public static ALContext context;
|
||||
public static long context;
|
||||
public static final int INITIAL_STATE = 4113,PAUSED_STATE = 4115,STOPPED_STATE = 4116,PLAYING_STATE = 4114;
|
||||
//------------------------------------------------------
|
||||
|
||||
|
@ -39,31 +38,17 @@ public class Audio {
|
|||
//Fonction global
|
||||
//------------------------------------------------------
|
||||
public static void create(){
|
||||
device = ALDevice.create(null);
|
||||
if ( device == null )
|
||||
throw new IllegalStateException("Failed to open the default device.");
|
||||
caps = device.getCapabilities();
|
||||
System.out.println("---------------------------- Create Audio Device -------------------------------------");
|
||||
System.out.println("OpenALC10: " + caps.OpenALC10);
|
||||
System.out.println("OpenALC11: " + caps.OpenALC11);
|
||||
System.out.println("caps.ALC_EXT_EFX = " + caps.ALC_EXT_EFX);
|
||||
device = alcOpenDevice((ByteBuffer)null);
|
||||
ALCCapabilities deviceCaps = ALC.createCapabilities(device);
|
||||
|
||||
String defaultDeviceSpecifier = alcGetString(0L, ALC_DEFAULT_DEVICE_SPECIFIER);
|
||||
System.out.println("Default device: " + defaultDeviceSpecifier);
|
||||
|
||||
context = ALContext.create(device);
|
||||
|
||||
System.out.println("ALC_FREQUENCY: " + alcGetInteger(device.address(), ALC_FREQUENCY) + "Hz");
|
||||
System.out.println("ALC_REFRESH: " + alcGetInteger(device.address(), ALC_REFRESH) + "Hz");
|
||||
System.out.println("ALC_SYNC: " + (alcGetInteger(device.address(), ALC_SYNC) == ALC_TRUE));
|
||||
System.out.println("ALC_MONO_SOURCES: " + alcGetInteger(device.address(), ALC_MONO_SOURCES));
|
||||
System.out.println("ALC_STEREO_SOURCES: " + alcGetInteger(device.address(), ALC_STEREO_SOURCES));
|
||||
System.out.println("---------------------------------------------------------------------------------------");
|
||||
context = alcCreateContext(device, (IntBuffer)null);
|
||||
alcMakeContextCurrent(context);
|
||||
createCapabilities(deviceCaps);
|
||||
}
|
||||
|
||||
public static void destroy(){
|
||||
context.destroy();
|
||||
device.destroy();
|
||||
alcCloseDevice(device);
|
||||
alcDestroyContext(context);
|
||||
}
|
||||
//------------------------------------------------------
|
||||
|
||||
|
@ -86,7 +71,6 @@ public class Audio {
|
|||
throw new Exception("Format not supported !");
|
||||
}
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
checkALError();
|
||||
int size = alGetBufferi(buffer,AL_SIZE);
|
||||
int bits = alGetBufferi(buffer, AL_BITS);
|
||||
int channels = alGetBufferi(buffer, AL_CHANNELS);
|
||||
|
@ -193,20 +177,17 @@ public class Audio {
|
|||
stb_vorbis_seek_start(decoder);
|
||||
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
|
||||
|
||||
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
|
||||
ShortBuffer pcm = BufferUtils.createShortBuffer(lengthSamples * channels);
|
||||
|
||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
|
||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm);
|
||||
stb_vorbis_close(decoder);
|
||||
|
||||
buffer = alGenBuffers();
|
||||
checkALError();
|
||||
|
||||
source = alGenSources();
|
||||
checkALError();
|
||||
|
||||
if(channels == 1)alBufferData(buffer, AL_FORMAT_MONO16, pcm, info.sample_rate());
|
||||
else alBufferData(buffer, AL_FORMAT_STEREO16, pcm, info.sample_rate());
|
||||
checkALError();
|
||||
}
|
||||
|
||||
public void playSound(){
|
||||
|
|
Reference in a new issue