Refactoring and add travis ant compilation
This commit is contained in:
parent
c2e793be12
commit
0b9fb6e127
68 changed files with 383 additions and 400 deletions
17
.gitignore
vendored
Normal file → Executable file
17
.gitignore
vendored
Normal file → Executable file
|
@ -1,12 +1,5 @@
|
|||
*.class
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
save/*
|
||||
.settings/*
|
||||
.classpath
|
||||
.project
|
||||
bin/*
|
||||
|
|
3
.travis.yml
Executable file
3
.travis.yml
Executable file
|
@ -0,0 +1,3 @@
|
|||
language: java
|
||||
jdk:
|
||||
- oraclejdk8
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL3"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
1
First Game Engine Project/.gitignore
vendored
1
First Game Engine Project/.gitignore
vendored
|
@ -1 +0,0 @@
|
|||
/bin/
|
|
@ -1,17 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>First 2D Game UDP</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -1,11 +0,0 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
|
@ -1,341 +0,0 @@
|
|||
package mrdev023.audio;
|
||||
|
||||
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.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.openal.*;
|
||||
|
||||
|
||||
import org.lwjgl.stb.STBVorbisInfo;
|
||||
|
||||
public class Audio {
|
||||
|
||||
//Variables global
|
||||
//------------------------------------------------------
|
||||
public static ALDevice device;
|
||||
public static ALCCapabilities caps;
|
||||
public static ALContext context;
|
||||
public static final int INITIAL_STATE = 4113,PAUSED_STATE = 4115,STOPPED_STATE = 4116,PLAYING_STATE = 4114;
|
||||
//------------------------------------------------------
|
||||
|
||||
//Variable de l'objet audio ou du son a lire
|
||||
//------------------------------------------------------
|
||||
private int buffer,source;
|
||||
private String fileName;
|
||||
private String format;
|
||||
//------------------------------------------------------
|
||||
|
||||
//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);
|
||||
|
||||
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("---------------------------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void destroy(){
|
||||
context.destroy();
|
||||
device.destroy();
|
||||
}
|
||||
//------------------------------------------------------
|
||||
|
||||
//Fonction de l'objet audio ou du son a lire
|
||||
//------------------------------------------------------
|
||||
|
||||
public Audio(String fileName) throws Exception{
|
||||
this.fileName = fileName;
|
||||
setSound();
|
||||
}
|
||||
|
||||
private void setSound() throws Exception{
|
||||
if(fileName.endsWith(".ogg")){
|
||||
loadOGGFormat();
|
||||
format = "OGG";
|
||||
}else if(fileName.endsWith(".wav")){
|
||||
loadWavFormat();
|
||||
format = "WAV";
|
||||
}else{
|
||||
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);
|
||||
int freq = alGetBufferi(buffer, AL_FREQUENCY);
|
||||
System.out.println(fileName + " loaded !" + " | TIME : " + (size/channels/(bits/8)/freq) + "s | BITS : " + bits + " | CHANNELS : " + channels + " | FREQUENCE : " + freq + " FORMAT : " + format);
|
||||
}
|
||||
|
||||
public void loadWavFormat() throws Exception{
|
||||
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(fileName)));
|
||||
AudioFormat audioformat = ais.getFormat();
|
||||
|
||||
// get channels
|
||||
int channels = 0;
|
||||
if (audioformat.getChannels() == 1) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_MONO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_MONO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else if (audioformat.getChannels() == 2) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_STEREO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_STEREO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else {
|
||||
assert false : "Only mono or stereo is supported";
|
||||
}
|
||||
|
||||
int available = ais.available();
|
||||
if(available <= 0) {
|
||||
available = ais.getFormat().getChannels() * (int) ais.getFrameLength() * ais.getFormat().getSampleSizeInBits() / 8;
|
||||
}
|
||||
byte[] buf = new byte[ais.available()];
|
||||
int read = 0, total = 0;
|
||||
while ((read = ais.read(buf, total, buf.length - total)) != -1
|
||||
&& total < buf.length) {
|
||||
total += read;
|
||||
}
|
||||
byte[] audio_bytes = buf;
|
||||
boolean two_bytes_data = audioformat.getSampleSizeInBits() == 16;
|
||||
ByteOrder order = audioformat.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
|
||||
ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
|
||||
dest.order(ByteOrder.nativeOrder());
|
||||
ByteBuffer src = ByteBuffer.wrap(audio_bytes);
|
||||
src.order(order);
|
||||
if (two_bytes_data) {
|
||||
ShortBuffer dest_short = dest.asShortBuffer();
|
||||
ShortBuffer src_short = src.asShortBuffer();
|
||||
while (src_short.hasRemaining())
|
||||
dest_short.put(src_short.get());
|
||||
} else {
|
||||
while (src.hasRemaining())
|
||||
dest.put(src.get());
|
||||
}
|
||||
dest.rewind();
|
||||
|
||||
this.buffer = alGenBuffers();
|
||||
this.source = alGenSources();
|
||||
alBufferData(this.buffer, channels, dest, (int)audioformat.getSampleRate());
|
||||
dest.clear();
|
||||
}
|
||||
|
||||
public void loadOGGFormat(){
|
||||
STBVorbisInfo info = STBVorbisInfo.malloc();
|
||||
ByteBuffer buff = BufferUtils.createByteBuffer(0);
|
||||
//lecture du fichier
|
||||
//----------------------------------------------------------------------------------------------------------------
|
||||
try {
|
||||
File file = new File(fileName);
|
||||
if ( file.isFile() ) {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
FileChannel fc = fis.getChannel();
|
||||
buff = BufferUtils.createByteBuffer((int)fc.size() + 1);
|
||||
|
||||
while ( fc.read(buff) != -1 ) ;
|
||||
|
||||
fis.close();
|
||||
fc.close();
|
||||
} else {
|
||||
System.err.println("File not found !");
|
||||
return;
|
||||
}
|
||||
|
||||
buff.flip();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
IntBuffer error = BufferUtils.createIntBuffer(1);
|
||||
long decoder = stb_vorbis_open_memory(buff, error, null);
|
||||
if ( decoder == NULL )
|
||||
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
|
||||
|
||||
stb_vorbis_get_info(decoder, info);
|
||||
|
||||
int channels = info.channels();
|
||||
|
||||
stb_vorbis_seek_start(decoder);
|
||||
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
|
||||
|
||||
ByteBuffer pcm = BufferUtils.createByteBuffer(lengthSamples * 2 * channels);
|
||||
|
||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm, lengthSamples);
|
||||
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(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourcePlay(source);
|
||||
}
|
||||
|
||||
public int getPosition(){
|
||||
return alGetSourcei(source, AL_POSITION);
|
||||
}
|
||||
|
||||
public int getDurationInSeconds(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
int size = alGetBufferi(buffer,AL_SIZE);
|
||||
int bits = alGetBufferi(buffer, AL_BITS);
|
||||
int channels = alGetBufferi(buffer, AL_CHANNELS);
|
||||
int freq = alGetBufferi(buffer, AL_FREQUENCY);
|
||||
return size/channels/(bits/8)/freq;
|
||||
}
|
||||
|
||||
public int getStateSound(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcei(source, AL_SOURCE_STATE);
|
||||
}
|
||||
|
||||
public boolean isStopped(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == STOPPED_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isPaused(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == PAUSED_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isPlaying(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == PLAYING_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isInitial(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == INITIAL_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public void stopSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourceStop(source);
|
||||
}
|
||||
|
||||
public void pauseSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourcePause(source);
|
||||
}
|
||||
|
||||
public void rewindSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourceRewind(source);
|
||||
}
|
||||
|
||||
public void setGain(float gain){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(gain > 1.0f)gain = 1.0f;
|
||||
if(gain < 0.0f)gain = 0.0f;
|
||||
alSourcef(source, AL_GAIN, gain);
|
||||
}
|
||||
|
||||
public void setPitch(float pitch){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(pitch < 0.0f)pitch = 0.0f;
|
||||
alSourcef(source, AL_PITCH, pitch);
|
||||
}
|
||||
|
||||
|
||||
public float getGain(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcef(source, AL_GAIN);
|
||||
}
|
||||
|
||||
public float getPitch(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcef(source, AL_PITCH);
|
||||
}
|
||||
|
||||
public void setLooping(boolean looping){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(looping){
|
||||
alSourcef(source, AL_LOOPING, AL_TRUE);
|
||||
}else{
|
||||
alSourcef(source, AL_LOOPING, AL_FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroySound(){
|
||||
alDeleteSources(source);
|
||||
alDeleteBuffers(buffer);
|
||||
source = 0;
|
||||
buffer = 0;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) throws Exception {
|
||||
this.fileName = fileName;
|
||||
destroySound();
|
||||
setSound();
|
||||
}
|
||||
|
||||
public int getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void setBuffer(int buffer) {
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
public int getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(int source) {
|
||||
this.source = source;
|
||||
}
|
||||
//------------------------------------------------------
|
||||
|
||||
}
|
8
README.md
Executable file
8
README.md
Executable file
|
@ -0,0 +1,8 @@
|
|||
# My First Voxel
|
||||
|
||||
Compile status: ![Status](https://travis-ci.org/mrdev023/MyFirstVoxel.svg?branch=master)
|
||||
|
||||
Ce projet est un Voxel que j'ai creer et optimiser en JAVA et avec LWJGL 2 en OpenGL Modern.
|
||||
Le projet n'est pas aboutie par manque d'idee.
|
||||
|
||||
![Image](https://pbs.twimg.com/media/CPWaGv4WsAAXPhU.png:large)
|
38
build.xml
Executable file
38
build.xml
Executable file
|
@ -0,0 +1,38 @@
|
|||
<project name="lwjgl-examples">
|
||||
|
||||
<property file="build.properties" />
|
||||
<property name="src.dir" value="src" />
|
||||
<property name="build.dir" value="bin"/>
|
||||
<property name="lwjgl_jars.dir" value="./libs/"/>
|
||||
<property name="lwjgl_natives.dir" value="./libs/"/>
|
||||
<property name="build.sysclasspath" value="last"/>
|
||||
|
||||
<path id="my_cp">
|
||||
<fileset dir="${lwjgl_jars.dir}" includes="*.jar"/>
|
||||
</path>
|
||||
|
||||
<target name="compile">
|
||||
<mkdir dir="${build.dir}"/>
|
||||
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="my_cp"/>
|
||||
</target>
|
||||
|
||||
<target name="clean">
|
||||
<delete dir="${build.dir}"/>
|
||||
</target>
|
||||
|
||||
<target name="test" depends="compile">
|
||||
<!--
|
||||
fork="true" seems to be absolutely
|
||||
necessary for the setting of java.library.path
|
||||
to be effective.-->
|
||||
|
||||
<java fork="true" classname="mrdev023/Main">
|
||||
<classpath>
|
||||
<path refid="my_cp"/>
|
||||
<path location="${build.dir}"/>
|
||||
</classpath>
|
||||
<sysproperty key="java.library.path"
|
||||
value="${lwjgl_natives.dir}"/>
|
||||
</java>
|
||||
</target>
|
||||
</project>
|
BIN
libs/OpenAL.dll
Executable file
BIN
libs/OpenAL.dll
Executable file
Binary file not shown.
BIN
libs/OpenAL32.dll
Executable file
BIN
libs/OpenAL32.dll
Executable file
Binary file not shown.
BIN
libs/glfw.dll
Executable file
BIN
libs/glfw.dll
Executable file
Binary file not shown.
BIN
libs/glfw32.dll
Executable file
BIN
libs/glfw32.dll
Executable file
Binary file not shown.
BIN
libs/jemalloc.dll
Executable file
BIN
libs/jemalloc.dll
Executable file
Binary file not shown.
BIN
libs/jemalloc32.dll
Executable file
BIN
libs/jemalloc32.dll
Executable file
Binary file not shown.
BIN
libs/libglfw.dylib
Executable file
BIN
libs/libglfw.dylib
Executable file
Binary file not shown.
BIN
libs/libglfw.so
Executable file
BIN
libs/libglfw.so
Executable file
Binary file not shown.
BIN
libs/libjemalloc.dylib
Executable file
BIN
libs/libjemalloc.dylib
Executable file
Binary file not shown.
BIN
libs/libjemalloc.so
Executable file
BIN
libs/libjemalloc.so
Executable file
Binary file not shown.
BIN
libs/liblwjgl.dylib
Executable file
BIN
libs/liblwjgl.dylib
Executable file
Binary file not shown.
BIN
libs/liblwjgl.so
Executable file
BIN
libs/liblwjgl.so
Executable file
Binary file not shown.
BIN
libs/libopenal.dylib
Executable file
BIN
libs/libopenal.dylib
Executable file
Binary file not shown.
BIN
libs/libopenal.so
Executable file
BIN
libs/libopenal.so
Executable file
Binary file not shown.
BIN
libs/lwjgl.dll
Executable file
BIN
libs/lwjgl.dll
Executable file
Binary file not shown.
BIN
libs/lwjgl.jar
Executable file
BIN
libs/lwjgl.jar
Executable file
Binary file not shown.
BIN
libs/lwjgl32.dll
Executable file
BIN
libs/lwjgl32.dll
Executable file
Binary file not shown.
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
322
src/mrdev023/audio/Audio.java
Executable file
322
src/mrdev023/audio/Audio.java
Executable file
|
@ -0,0 +1,322 @@
|
|||
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.stb.STBVorbis.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.openal.*;
|
||||
|
||||
|
||||
import org.lwjgl.stb.STBVorbisInfo;
|
||||
|
||||
public class Audio {
|
||||
|
||||
//Variables global
|
||||
//------------------------------------------------------
|
||||
public static long device;
|
||||
public static ALCCapabilities caps;
|
||||
public static long context;
|
||||
public static final int INITIAL_STATE = 4113,PAUSED_STATE = 4115,STOPPED_STATE = 4116,PLAYING_STATE = 4114;
|
||||
//------------------------------------------------------
|
||||
|
||||
//Variable de l'objet audio ou du son a lire
|
||||
//------------------------------------------------------
|
||||
private int buffer,source;
|
||||
private String fileName;
|
||||
private String format;
|
||||
//------------------------------------------------------
|
||||
|
||||
//Fonction global
|
||||
//------------------------------------------------------
|
||||
public static void create(){
|
||||
device = alcOpenDevice((ByteBuffer)null);
|
||||
ALCCapabilities deviceCaps = ALC.createCapabilities(device);
|
||||
|
||||
context = alcCreateContext(device, (IntBuffer)null);
|
||||
alcMakeContextCurrent(context);
|
||||
createCapabilities(deviceCaps);
|
||||
}
|
||||
|
||||
public static void destroy(){
|
||||
alcCloseDevice(device);
|
||||
alcDestroyContext(context);
|
||||
}
|
||||
//------------------------------------------------------
|
||||
|
||||
//Fonction de l'objet audio ou du son a lire
|
||||
//------------------------------------------------------
|
||||
|
||||
public Audio(String fileName) throws Exception{
|
||||
this.fileName = fileName;
|
||||
setSound();
|
||||
}
|
||||
|
||||
private void setSound() throws Exception{
|
||||
if(fileName.endsWith(".ogg")){
|
||||
loadOGGFormat();
|
||||
format = "OGG";
|
||||
}else if(fileName.endsWith(".wav")){
|
||||
loadWavFormat();
|
||||
format = "WAV";
|
||||
}else{
|
||||
throw new Exception("Format not supported !");
|
||||
}
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
int size = alGetBufferi(buffer,AL_SIZE);
|
||||
int bits = alGetBufferi(buffer, AL_BITS);
|
||||
int channels = alGetBufferi(buffer, AL_CHANNELS);
|
||||
int freq = alGetBufferi(buffer, AL_FREQUENCY);
|
||||
System.out.println(fileName + " loaded !" + " | TIME : " + (size/channels/(bits/8)/freq) + "s | BITS : " + bits + " | CHANNELS : " + channels + " | FREQUENCE : " + freq + " FORMAT : " + format);
|
||||
}
|
||||
|
||||
public void loadWavFormat() throws Exception{
|
||||
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(fileName)));
|
||||
AudioFormat audioformat = ais.getFormat();
|
||||
|
||||
// get channels
|
||||
int channels = 0;
|
||||
if (audioformat.getChannels() == 1) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_MONO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_MONO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else if (audioformat.getChannels() == 2) {
|
||||
if (audioformat.getSampleSizeInBits() == 8) {
|
||||
channels = AL10.AL_FORMAT_STEREO8;
|
||||
} else if (audioformat.getSampleSizeInBits() == 16) {
|
||||
channels = AL10.AL_FORMAT_STEREO16;
|
||||
} else {
|
||||
assert false : "Illegal sample size";
|
||||
}
|
||||
} else {
|
||||
assert false : "Only mono or stereo is supported";
|
||||
}
|
||||
|
||||
int available = ais.available();
|
||||
if(available <= 0) {
|
||||
available = ais.getFormat().getChannels() * (int) ais.getFrameLength() * ais.getFormat().getSampleSizeInBits() / 8;
|
||||
}
|
||||
byte[] buf = new byte[ais.available()];
|
||||
int read = 0, total = 0;
|
||||
while ((read = ais.read(buf, total, buf.length - total)) != -1
|
||||
&& total < buf.length) {
|
||||
total += read;
|
||||
}
|
||||
byte[] audio_bytes = buf;
|
||||
boolean two_bytes_data = audioformat.getSampleSizeInBits() == 16;
|
||||
ByteOrder order = audioformat.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
|
||||
ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
|
||||
dest.order(ByteOrder.nativeOrder());
|
||||
ByteBuffer src = ByteBuffer.wrap(audio_bytes);
|
||||
src.order(order);
|
||||
if (two_bytes_data) {
|
||||
ShortBuffer dest_short = dest.asShortBuffer();
|
||||
ShortBuffer src_short = src.asShortBuffer();
|
||||
while (src_short.hasRemaining())
|
||||
dest_short.put(src_short.get());
|
||||
} else {
|
||||
while (src.hasRemaining())
|
||||
dest.put(src.get());
|
||||
}
|
||||
dest.rewind();
|
||||
|
||||
this.buffer = alGenBuffers();
|
||||
this.source = alGenSources();
|
||||
alBufferData(this.buffer, channels, dest, (int)audioformat.getSampleRate());
|
||||
dest.clear();
|
||||
}
|
||||
|
||||
public void loadOGGFormat(){
|
||||
STBVorbisInfo info = STBVorbisInfo.malloc();
|
||||
ByteBuffer buff = BufferUtils.createByteBuffer(0);
|
||||
//lecture du fichier
|
||||
//----------------------------------------------------------------------------------------------------------------
|
||||
try {
|
||||
File file = new File(fileName);
|
||||
if ( file.isFile() ) {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
FileChannel fc = fis.getChannel();
|
||||
buff = BufferUtils.createByteBuffer((int)fc.size() + 1);
|
||||
|
||||
while ( fc.read(buff) != -1 ) ;
|
||||
|
||||
fis.close();
|
||||
fc.close();
|
||||
} else {
|
||||
System.err.println("File not found !");
|
||||
return;
|
||||
}
|
||||
|
||||
buff.flip();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
IntBuffer error = BufferUtils.createIntBuffer(1);
|
||||
long decoder = stb_vorbis_open_memory(buff, error, null);
|
||||
if ( decoder == NULL )
|
||||
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
|
||||
|
||||
stb_vorbis_get_info(decoder, info);
|
||||
|
||||
int channels = info.channels();
|
||||
|
||||
stb_vorbis_seek_start(decoder);
|
||||
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
|
||||
|
||||
ShortBuffer pcm = BufferUtils.createShortBuffer(lengthSamples * channels);
|
||||
|
||||
stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm);
|
||||
stb_vorbis_close(decoder);
|
||||
|
||||
buffer = alGenBuffers();
|
||||
|
||||
source = alGenSources();
|
||||
|
||||
if(channels == 1)alBufferData(buffer, AL_FORMAT_MONO16, pcm, info.sample_rate());
|
||||
else alBufferData(buffer, AL_FORMAT_STEREO16, pcm, info.sample_rate());
|
||||
}
|
||||
|
||||
public void playSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourcePlay(source);
|
||||
}
|
||||
|
||||
public int getPosition(){
|
||||
return alGetSourcei(source, AL_POSITION);
|
||||
}
|
||||
|
||||
public int getDurationInSeconds(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
int size = alGetBufferi(buffer,AL_SIZE);
|
||||
int bits = alGetBufferi(buffer, AL_BITS);
|
||||
int channels = alGetBufferi(buffer, AL_CHANNELS);
|
||||
int freq = alGetBufferi(buffer, AL_FREQUENCY);
|
||||
return size/channels/(bits/8)/freq;
|
||||
}
|
||||
|
||||
public int getStateSound(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcei(source, AL_SOURCE_STATE);
|
||||
}
|
||||
|
||||
public boolean isStopped(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == STOPPED_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isPaused(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == PAUSED_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isPlaying(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == PLAYING_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isInitial(){
|
||||
if(source == 0 || buffer == 0) return false;
|
||||
if(alGetSourcei(source, AL_SOURCE_STATE) == INITIAL_STATE)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public void stopSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourceStop(source);
|
||||
}
|
||||
|
||||
public void pauseSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourcePause(source);
|
||||
}
|
||||
|
||||
public void rewindSound(){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
alSourceRewind(source);
|
||||
}
|
||||
|
||||
public void setGain(float gain){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(gain > 1.0f)gain = 1.0f;
|
||||
if(gain < 0.0f)gain = 0.0f;
|
||||
alSourcef(source, AL_GAIN, gain);
|
||||
}
|
||||
|
||||
public void setPitch(float pitch){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(pitch < 0.0f)pitch = 0.0f;
|
||||
alSourcef(source, AL_PITCH, pitch);
|
||||
}
|
||||
|
||||
|
||||
public float getGain(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcef(source, AL_GAIN);
|
||||
}
|
||||
|
||||
public float getPitch(){
|
||||
if(source == 0 || buffer == 0) return 0;
|
||||
return alGetSourcef(source, AL_PITCH);
|
||||
}
|
||||
|
||||
public void setLooping(boolean looping){
|
||||
if(source == 0 || buffer == 0) return;
|
||||
if(looping){
|
||||
alSourcef(source, AL_LOOPING, AL_TRUE);
|
||||
}else{
|
||||
alSourcef(source, AL_LOOPING, AL_FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroySound(){
|
||||
alDeleteSources(source);
|
||||
alDeleteBuffers(buffer);
|
||||
source = 0;
|
||||
buffer = 0;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) throws Exception {
|
||||
this.fileName = fileName;
|
||||
destroySound();
|
||||
setSound();
|
||||
}
|
||||
|
||||
public int getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void setBuffer(int buffer) {
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
public int getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(int source) {
|
||||
this.source = source;
|
||||
}
|
||||
//------------------------------------------------------
|
||||
|
||||
}
|
|
@ -8,7 +8,6 @@ import mrdev023.opengl.exception.*;
|
|||
import static org.lwjgl.glfw.GLFW.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
import org.lwjgl.glfw.GLFWWindowSizeCallback.SAM;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
|
@ -42,7 +41,7 @@ public class Frame {
|
|||
glfwSetErrorCallback(errorCallback);
|
||||
|
||||
//init
|
||||
if(glfwInit() != GL11.GL_TRUE)throw new FrameException("GLFW not init");
|
||||
if(!glfwInit())throw new FrameException("GLFW not init");
|
||||
|
||||
//config de la fenetre
|
||||
glfwDefaultWindowHints();
|
||||
|
@ -100,7 +99,7 @@ public class Frame {
|
|||
glfwSetErrorCallback(errorCallback);
|
||||
|
||||
//init
|
||||
if(glfwInit() != GL11.GL_TRUE)throw new FrameException("GLFW not init");
|
||||
if(!glfwInit())throw new FrameException("GLFW not init");
|
||||
|
||||
//config de la fenetre
|
||||
glfwDefaultWindowHints();
|
||||
|
@ -216,14 +215,11 @@ public class Frame {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return true si la croix rouge a été cliquer
|
||||
* @return true si la croix rouge a ete cliquer
|
||||
*/
|
||||
public boolean isClosedRequested(){
|
||||
if(glfwWindowShouldClose(windowID) == GL11.GL_FALSE){
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
return glfwWindowShouldClose(windowID);
|
||||
|
||||
}
|
||||
|
||||
public void setTitle(String title){
|
|
@ -93,8 +93,8 @@ public class Input{
|
|||
}
|
||||
|
||||
public static void destroy(){
|
||||
mousePos.release();
|
||||
scroll.release();
|
||||
mousePos.free();
|
||||
scroll.free();
|
||||
}
|
||||
|
||||
public static void scroll(long window, double xoffset, double yoffset) {
|
Reference in a new issue