closes #29
This commit is contained in:
parent
7bb796933c
commit
4663573cbc
BIN
assets/Audio/MenuStartup.ogg
Normal file
BIN
assets/Audio/MenuStartup.ogg
Normal file
Binary file not shown.
@ -1,5 +1,7 @@
|
||||
package electrosphere.audio;
|
||||
|
||||
import electrosphere.logger.LoggerInterface;
|
||||
import electrosphere.util.FileUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
@ -28,12 +30,15 @@ public class AudioBuffer {
|
||||
|
||||
private ShortBuffer pcm = null;
|
||||
|
||||
public AudioBuffer(String fileName) throws Exception {
|
||||
public AudioBuffer(String fileName) {
|
||||
bufferId = alGenBuffers();
|
||||
try (STBVorbisInfo info = STBVorbisInfo.malloc()) {
|
||||
ShortBuffer pcm = readVorbis(fileName, 32 * 1024, info);
|
||||
// Copy to buffer
|
||||
alBufferData(bufferId, info.channels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate());
|
||||
} catch (Exception e){
|
||||
LoggerInterface.loggerEngine.ERROR("Failed to load audio", e);
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,8 +78,9 @@ public class AudioBuffer {
|
||||
}
|
||||
} else {
|
||||
try (
|
||||
InputStream source = AudioBuffer.class.getResourceAsStream(resource);
|
||||
ReadableByteChannel rbc = Channels.newChannel(source)) {
|
||||
InputStream source = FileUtils.getAssetFileAsStream(resource);
|
||||
ReadableByteChannel rbc = Channels.newChannel(source)
|
||||
) {
|
||||
buffer = createByteBuffer(bufferSize);
|
||||
|
||||
while (true) {
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
package electrosphere.audio;
|
||||
|
||||
import electrosphere.logger.LoggerInterface;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.openal.*;
|
||||
import static org.lwjgl.openal.ALC10.*;
|
||||
@ -21,16 +24,26 @@ public class AudioEngine {
|
||||
private final List<AudioBuffer> soundBufferList;
|
||||
|
||||
private final Map<String, AudioSource> soundSourceMap;
|
||||
|
||||
private final Matrix4f cameraMatrix;
|
||||
|
||||
|
||||
private float engineGain = 1.0f;
|
||||
|
||||
|
||||
public AudioEngine() {
|
||||
soundBufferList = new ArrayList<>();
|
||||
soundSourceMap = new HashMap<>();
|
||||
cameraMatrix = new Matrix4f();
|
||||
soundBufferList = new ArrayList();
|
||||
soundSourceMap = new HashMap();
|
||||
}
|
||||
|
||||
public void init() throws Exception {
|
||||
public void init() {
|
||||
try {
|
||||
initDevice();
|
||||
} catch (Exception ex) {
|
||||
LoggerInterface.loggerEngine.ERROR("Error initializing audio device", ex);
|
||||
}
|
||||
listener = new AudioListener();
|
||||
}
|
||||
|
||||
void initDevice() throws Exception{
|
||||
this.device = alcOpenDevice((ByteBuffer) null);
|
||||
if (device == NULL) {
|
||||
throw new IllegalStateException("Failed to open the default OpenAL device.");
|
||||
@ -43,4 +56,15 @@ public class AudioEngine {
|
||||
alcMakeContextCurrent(context);
|
||||
AL.createCapabilities(deviceCaps);
|
||||
}
|
||||
|
||||
|
||||
public void setGain(float gain){
|
||||
engineGain = gain;
|
||||
}
|
||||
|
||||
public float getGain(){
|
||||
return engineGain;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -9,12 +9,15 @@ import static org.lwjgl.openal.AL10.*;
|
||||
*/
|
||||
public class AudioListener {
|
||||
|
||||
Vector3f position;
|
||||
|
||||
public AudioListener() {
|
||||
this(new Vector3f(0, 0, 0));
|
||||
}
|
||||
|
||||
public AudioListener(Vector3f position) {
|
||||
alListener3f(AL_POSITION, position.x, position.y, position.z);
|
||||
this.position = position;
|
||||
alListener3f(AL_POSITION, this.position.x, this.position.y, this.position.z);
|
||||
alListener3f(AL_VELOCITY, 0, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
29
src/main/java/electrosphere/audio/AudioUtils.java
Normal file
29
src/main/java/electrosphere/audio/AudioUtils.java
Normal file
@ -0,0 +1,29 @@
|
||||
package electrosphere.audio;
|
||||
|
||||
import electrosphere.logger.LoggerInterface;
|
||||
import electrosphere.main.Globals;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author amaterasu
|
||||
*/
|
||||
public class AudioUtils {
|
||||
|
||||
|
||||
public static AudioSource playAudioAtLocation(String audioFile, Vector3f position){
|
||||
AudioSource rVal = null;
|
||||
AudioBuffer buffer = Globals.assetManager.fetchAudio(audioFile);
|
||||
if(buffer != null){
|
||||
rVal = new AudioSource(false,false);
|
||||
rVal.setBuffer(buffer.getBufferId());
|
||||
rVal.setGain(Globals.audioEngine.getGain());
|
||||
rVal.setPosition(position);
|
||||
rVal.play();
|
||||
} else {
|
||||
LoggerInterface.loggerEngine.WARNING("Failed to start audio in playAudioAtLocation");
|
||||
}
|
||||
return rVal;
|
||||
}
|
||||
|
||||
}
|
||||
@ -41,7 +41,7 @@ import electrosphere.net.server.Server;
|
||||
import electrosphere.renderer.ActorUtils;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.RenderUtils;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package electrosphere.renderer.assetmanager;
|
||||
package electrosphere.engine.assetmanager;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -1,5 +1,6 @@
|
||||
package electrosphere.renderer.assetmanager;
|
||||
package electrosphere.engine.assetmanager;
|
||||
|
||||
import electrosphere.audio.AudioBuffer;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.texture.Texture;
|
||||
import electrosphere.util.ModelLoader;
|
||||
@ -22,6 +23,18 @@ public class AssetManager {
|
||||
ConcurrentHashMap<String,Texture> texturesLoadedIntoMemory = new ConcurrentHashMap();
|
||||
CopyOnWriteArrayList<String> texturesInQueue = new CopyOnWriteArrayList();
|
||||
|
||||
ConcurrentHashMap<String,AudioBuffer> audioLoadedIntoMemory = new ConcurrentHashMap();
|
||||
CopyOnWriteArrayList<String> audioInQueue = new CopyOnWriteArrayList();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//General asset manager stuff
|
||||
//
|
||||
|
||||
public void loadAssetsInQueue(){
|
||||
for(String currentPath : modelsInQueue){
|
||||
modelsInQueue.remove(currentPath);
|
||||
@ -31,8 +44,25 @@ public class AssetManager {
|
||||
texturesInQueue.remove(currentPath);
|
||||
texturesLoadedIntoMemory.put(currentPath, new Texture(currentPath));
|
||||
}
|
||||
for(String currentPath : audioInQueue){
|
||||
audioInQueue.remove(currentPath);
|
||||
audioLoadedIntoMemory.put(currentPath, new AudioBuffer(currentPath));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//Models
|
||||
//
|
||||
|
||||
public void addModelPathToQueue(String path){
|
||||
if(!modelsInQueue.contains(path) && !modelsLoadedIntoMemory.containsKey(path)){
|
||||
modelsInQueue.add(path);
|
||||
@ -63,6 +93,23 @@ public class AssetManager {
|
||||
modelsLoadedIntoMemory.put(s,m);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// Textures
|
||||
//
|
||||
|
||||
|
||||
public void addTexturePathtoQueue(String path){
|
||||
if(!texturesInQueue.contains(path) && !texturesLoadedIntoMemory.containsKey(path)){
|
||||
texturesInQueue.add(path);
|
||||
@ -84,4 +131,41 @@ public class AssetManager {
|
||||
texturesLoadedIntoMemory.put(rVal,t);
|
||||
return rVal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//AUDIO
|
||||
//
|
||||
|
||||
public void addAudioPathToQueue(String path){
|
||||
if(!audioInQueue.contains(path) && !audioLoadedIntoMemory.containsKey(path)){
|
||||
audioInQueue.add(path);
|
||||
}
|
||||
}
|
||||
|
||||
public AudioBuffer fetchAudio(String path){
|
||||
AudioBuffer rVal = null;
|
||||
if(audioLoadedIntoMemory.containsKey(path)){
|
||||
rVal = audioLoadedIntoMemory.get(path);
|
||||
}
|
||||
return rVal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -7,6 +7,7 @@ import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.texture.Texture;
|
||||
import electrosphere.renderer.texture.TextureMap;
|
||||
import com.google.gson.Gson;
|
||||
import electrosphere.audio.AudioEngine;
|
||||
import electrosphere.controls.ControlHandler;
|
||||
import electrosphere.entity.Entity;
|
||||
import electrosphere.entity.EntityManager;
|
||||
@ -43,8 +44,8 @@ import electrosphere.renderer.ModelUtils;
|
||||
import electrosphere.renderer.RenderUtils;
|
||||
import electrosphere.renderer.RenderingEngine;
|
||||
import electrosphere.renderer.ShaderProgram;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.assetmanager.AssetManager;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetManager;
|
||||
import electrosphere.renderer.ui.WidgetManager;
|
||||
import electrosphere.renderer.ui.WidgetUtils;
|
||||
import electrosphere.renderer.ui.font.FontUtils;
|
||||
@ -82,6 +83,11 @@ public class Globals {
|
||||
//
|
||||
public static RenderingEngine renderingEngine;
|
||||
|
||||
//
|
||||
//Audio Engine
|
||||
//
|
||||
public static AudioEngine audioEngine;
|
||||
|
||||
|
||||
//
|
||||
//Client connection to server
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package electrosphere.main;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import electrosphere.audio.AudioEngine;
|
||||
import electrosphere.audio.AudioSource;
|
||||
import electrosphere.audio.AudioUtils;
|
||||
import electrosphere.controls.ControlHandler;
|
||||
import electrosphere.entity.CameraEntityUtils;
|
||||
import electrosphere.entity.types.creature.CreatureUtils;
|
||||
@ -138,6 +141,25 @@ public class Main {
|
||||
// }
|
||||
// }
|
||||
|
||||
// if(1==1){
|
||||
// Globals.audioEngine = new AudioEngine();
|
||||
// Globals.audioEngine.init();
|
||||
// Globals.assetManager.addAudioPathToQueue("/Audio/MenuStartup.ogg");
|
||||
// Globals.assetManager.loadAssetsInQueue();
|
||||
// Globals.audioEngine.setGain(0.1f);
|
||||
// AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuStartup.ogg", new Vector3f(3,0,0));
|
||||
// if(startupSound != null){
|
||||
// while(startupSound.isPlaying()){
|
||||
// try {
|
||||
// TimeUnit.MILLISECONDS.sleep(10);
|
||||
// } catch (InterruptedException ex) {
|
||||
// ex.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// System.exit(0);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
//debug: create terrain/world viewer
|
||||
@ -147,6 +169,11 @@ public class Main {
|
||||
Globals.renderingEngine = new RenderingEngine();
|
||||
Globals.renderingEngine.createOpenglContext();
|
||||
|
||||
//create the audio context
|
||||
Globals.audioEngine = new AudioEngine();
|
||||
Globals.audioEngine.init();
|
||||
Globals.audioEngine.setGain(0.1f);
|
||||
|
||||
//init default resources
|
||||
Globals.initDefaultGraphicalResources();
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package electrosphere.renderer;
|
||||
|
||||
import electrosphere.main.Globals;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.texture.Texture;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
@ -2,7 +2,7 @@ package electrosphere.renderer.ui.font;
|
||||
|
||||
import electrosphere.main.Globals;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.ui.Widget;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import electrosphere.renderer.Material;
|
||||
import electrosphere.renderer.framebuffer.Framebuffer;
|
||||
import electrosphere.renderer.ui.Widget;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.framebuffer.FramebufferUtils;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@ -2,7 +2,7 @@ package electrosphere.renderer.ui.transparenttextbox;
|
||||
|
||||
import electrosphere.main.Globals;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.ui.Widget;
|
||||
import electrosphere.renderer.ui.font.FontUtils;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
@ -3,7 +3,7 @@ package electrosphere.renderer.ui.widgets;
|
||||
import electrosphere.main.Globals;
|
||||
import electrosphere.renderer.Material;
|
||||
import electrosphere.renderer.Model;
|
||||
import electrosphere.renderer.assetmanager.AssetDataStrings;
|
||||
import electrosphere.engine.assetmanager.AssetDataStrings;
|
||||
import electrosphere.renderer.framebuffer.Framebuffer;
|
||||
import electrosphere.renderer.framebuffer.FramebufferUtils;
|
||||
import electrosphere.renderer.texture.Texture;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user