reading wavs
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good

This commit is contained in:
austin 2024-08-06 19:48:12 -04:00
parent 920f3912de
commit 2ebeb71f86
6 changed files with 86 additions and 13 deletions

Binary file not shown.

View File

@ -180,6 +180,9 @@
"nameThirdPerson" : "Land", "nameThirdPerson" : "Land",
"nameFirstPerson" : "Land", "nameFirstPerson" : "Land",
"priorityCategory" : "MOVEMENT_MODIFIER" "priorityCategory" : "MOVEMENT_MODIFIER"
},
"audioData" : {
"audioPath" : "Audio/movement/Equip A.wav"
} }
} }
} }

View File

@ -9,7 +9,16 @@ import java.nio.ShortBuffer;
import java.nio.channels.SeekableByteChannel; import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL11;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
import org.lwjgl.stb.STBVorbis; import org.lwjgl.stb.STBVorbis;
@ -26,9 +35,6 @@ public class AudioBuffer {
//the id of the buffer //the id of the buffer
private int bufferId; private int bufferId;
//buffer containing vorbis metadata
private ByteBuffer vorbis = null;
//The main audio data //The main audio data
private ShortBuffer pcm = null; private ShortBuffer pcm = null;
@ -43,15 +49,23 @@ public class AudioBuffer {
String fileNameSanitized = FileUtils.sanitizeFilePath(fileNameRaw); String fileNameSanitized = FileUtils.sanitizeFilePath(fileNameRaw);
bufferId = alGenBuffers(); bufferId = alGenBuffers();
//create buffer to store vorbis data //read vorbis
try (STBVorbisInfo info = STBVorbisInfo.malloc()) { if(fileNameSanitized.contains(".ogg")){
//read the vorbis data as well as main audio data //create buffer to store vorbis data
ShortBuffer pcm = readVorbis(fileNameSanitized, 32 * 1024, info); try(STBVorbisInfo info = STBVorbisInfo.malloc()){
// Copy to buffer readVorbis(fileNameSanitized, bufferId, 32 * 1024, info);
alBufferData(bufferId, info.channels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate()); } catch (Exception e){
} catch (Exception e){ LoggerInterface.loggerAudio.ERROR("Failed to load audio", e);
LoggerInterface.loggerAudio.ERROR("Failed to load audio", e); }
} }
//read wav
if(fileNameSanitized.contains(".wav")){
readWav(fileNameSanitized, 32 * 1024, bufferId);
}
LoggerInterface.loggerAudio.DEBUG("Created audio buffer(" + fileNameRaw + ") with length " + length); LoggerInterface.loggerAudio.DEBUG("Created audio buffer(" + fileNameRaw + ") with length " + length);
} }
@ -63,7 +77,9 @@ public class AudioBuffer {
* @return The main audio data buffer * @return The main audio data buffer
* @throws Exception Throws an exception if the decoder fails * @throws Exception Throws an exception if the decoder fails
*/ */
private ShortBuffer readVorbis(String filepath, int bufferSize, STBVorbisInfo info) throws Exception { private void readVorbis(String filepath, int bufferId, int bufferSize, STBVorbisInfo info) throws Exception {
//buffer containing vorbis metadata
ByteBuffer vorbis = null;
try (MemoryStack stack = MemoryStack.stackPush()) { try (MemoryStack stack = MemoryStack.stackPush()) {
//read the vorbis data from disk //read the vorbis data from disk
@ -89,7 +105,43 @@ public class AudioBuffer {
//close decoder and return //close decoder and return
STBVorbis.stb_vorbis_close(decoder); STBVorbis.stb_vorbis_close(decoder);
return pcm; // Copy to buffer
alBufferData(bufferId, info.channels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate());
}
}
/**
* Reads a wav file
* @param filepath The filepath to the wav
* @param bufferSize The buffer size
* @param bufferId The id of the buffer
*/
private void readWav(String filepath, int bufferSize, int bufferId){
try {
//get raw file objects
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(FileUtils.getAssetFile(filepath));
AudioFormat format = fileFormat.getFormat();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(FileUtils.getAssetFile(filepath));
//get data about specific file
int channels = format.getChannels();
float sampleRate = format.getSampleRate();
this.length = fileFormat.getFrameLength() * format.getFrameRate();
//read data
byte[] dataRaw = inputStream.readAllBytes();
ByteBuffer buffer = MemoryUtil.memAlloc(dataRaw.length);
buffer.put(dataRaw);
buffer.flip();
//buffer to openal
AL11.alBufferData(bufferId, channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, buffer, (int)sampleRate);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }

View File

@ -69,6 +69,7 @@ public class AudioEngine {
LoggerInterface.loggerEngine.ERROR("Error initializing audio device", ex); LoggerInterface.loggerEngine.ERROR("Error initializing audio device", ex);
} }
if(initialized){ if(initialized){
//recursively load all audio files
listener = new AudioListener(); listener = new AudioListener();
} }
} }

View File

@ -470,6 +470,7 @@ public class Globals {
"/Audio/ambienceWind1SeamlessMono.ogg", "/Audio/ambienceWind1SeamlessMono.ogg",
"/Audio/weapons/swordUnsheath1.ogg", "/Audio/weapons/swordUnsheath1.ogg",
"/Audio/weapons/swoosh-03.ogg", "/Audio/weapons/swoosh-03.ogg",
"/Audio/movement/Equip A.wav",
}; };
LoggerInterface.loggerStartup.INFO("Loading default audio resources"); LoggerInterface.loggerStartup.INFO("Loading default audio resources");
for(String path : audioToInit){ for(String path : audioToInit){

View File

@ -1,5 +1,6 @@
package electrosphere.entity.state.movement; package electrosphere.entity.state.movement;
import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType;
import electrosphere.engine.Globals; import electrosphere.engine.Globals;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityDataStrings;
@ -10,6 +11,7 @@ import electrosphere.entity.btree.StateTransitionUtil.StateTransitionUtilItem;
import electrosphere.entity.state.AnimationPriorities; import electrosphere.entity.state.AnimationPriorities;
import electrosphere.entity.state.client.firstPerson.FirstPersonTree; import electrosphere.entity.state.client.firstPerson.FirstPersonTree;
import electrosphere.entity.state.movement.jump.ClientJumpTree; import electrosphere.entity.state.movement.jump.ClientJumpTree;
import electrosphere.game.data.common.TreeDataAudio;
import electrosphere.game.data.creature.type.movement.FallMovementSystem; import electrosphere.game.data.creature.type.movement.FallMovementSystem;
import electrosphere.renderer.actor.Actor; import electrosphere.renderer.actor.Actor;
@ -99,6 +101,20 @@ public class FallTree implements BehaviorTree {
} }
FirstPersonTree.conditionallyPlayAnimation(parent, fallMovementSystem.getLandState().getAnimation()); FirstPersonTree.conditionallyPlayAnimation(parent, fallMovementSystem.getLandState().getAnimation());
} }
//play animation, audio, etc, for state
TreeDataAudio audioData = fallMovementSystem.getLandState().getAudioData();
if(parent == Globals.playerEntity && !Globals.controlHandler.cameraIsThirdPerson()){
//first person
//play first person audio
if(Globals.audioEngine.initialized() && audioData != null && audioData.getAudioPath() != null){
Globals.virtualAudioSourceManager.createVirtualAudioSource(audioData.getAudioPath(), VirtualAudioSourceType.CREATURE, false);
}
} else {
//play third person audio
if(Globals.audioEngine.initialized() && audioData != null && audioData.getAudioPath() != null){
Globals.virtualAudioSourceManager.createVirtualAudioSource(audioData.getAudioPath(), VirtualAudioSourceType.CREATURE, false, EntityUtils.getPosition(parent));
}
}
} }
} }