diff --git a/.gitmodules b/.gitmodules index ec4e33c..c48bf84 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,6 +7,3 @@ [submodule "stb"] path = lib/stb url = https://github.com/nothings/stb.git -[submodule "openal-soft"] - path = lib/openal - url = https://github.com/kcat/openal-soft.git diff --git a/Makefile b/Makefile index 77f7dcf..e31683e 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ -CC = clang++ +CC = g++ INCFLAGS = -Isrc INCFLAGS += -Iengine INCFLAGS += -Ilib/glfw/include INCFLAGS += -Ilib/glm INCFLAGS += -Ilib/stb -INCFLAGS += -Ilib/openal/include +INCFLAGS += -I/usr/include/AL CCFLAGS = -std=c++17 -O2 -g CCFLAGS += $(INCFLAGS) @@ -14,7 +14,8 @@ LDFLAGS = -lm LDFLAGS += $(INCFLAGS) LDFLAGS += lib/glfw/src/libglfw3.a LDFLAGS += lib/glm/glm/libglm_static.a -LDFLAGS += lib/openal/build/libcommon.a +LDFLAGS += -lopenal +LDFLAGS += -lalut LDFLAGS += -lvulkan SRC = $(shell find src -name "*.cpp") @@ -34,7 +35,6 @@ all: dirs libs shader build libs: cd lib/glfw && cmake . && make cd lib/glm && cmake . && make - cd lib/openal/build && cmake .. && make dirs: mkdir -p ./$(BIN) diff --git a/engine/xe_engine.hpp b/engine/xe_engine.hpp index 5731b50..285edf3 100644 --- a/engine/xe_engine.hpp +++ b/engine/xe_engine.hpp @@ -6,6 +6,7 @@ #include "xe_camera.hpp" #include "xe_descriptors.hpp" #include "xe_image.hpp" +#include "xe_sound_device.hpp" #include #include @@ -44,6 +45,7 @@ class XeEngine { XeDevice xeDevice; XeRenderer xeRenderer; XeCamera xeCamera; + XeSoundDevice xeSoundDevice; std::chrono::_V2::system_clock::time_point currentTime; float frameTime; diff --git a/engine/xe_sound.cpp b/engine/xe_sound.cpp new file mode 100644 index 0000000..fbb4628 --- /dev/null +++ b/engine/xe_sound.cpp @@ -0,0 +1,83 @@ +#include "xe_sound.hpp" + +#include +#include +#include + +namespace xe { + +XeSound::XeSound(const std::string& filename) { + + ALvoid *data; + ALsizei size, freq; + ALenum format; + ALboolean loop; + char *bufferData; + + // alutLoadWAVFile(filename.c_str(), &format, &data, &size, &freq, &loop); + + // AudioFile file; + // file.load(filename); + + // std::vector data; + // file.writePCMToBuffer(data); + // auto getALSoundFormat = [](AudioFile& audioFile) { + // int bitDepth = audioFile.getBitDepth(); + // if (bitDepth == 16) + // return audioFile.isStereo() ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; + // else if (bitDepth == 8) + // return audioFile.isStereo() ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8; + // else + // return -1; + // }; + + // alGenBuffers(1, &buffer); + // alBufferData(buffer, getALSoundFormat(file), data.data(), data.size(), file.getSampleRate()); + + alGenSources(1, &source); + alSourcef(source, AL_GAIN, 1.f); + alSourcef(source, AL_PITCH, 1.f); + alSource3f(source, AL_POSITION, 0, 0, 0); + alSource3f(source, AL_VELOCITY, 0, 0, 0); + alSourcei(source, AL_LOOPING, AL_FALSE); + alSourcei(source, AL_BUFFER, buffer); + +} + +XeSound::~XeSound() { + alDeleteSources(1, &source); + alDeleteBuffers(1, &buffer); +} + +void XeSound::play() { + stop(); + alSourcePlay(source); +}; + +void XeSound::stop() { + alSourceStop(source); +}; + +void XeSound::pause() { + alSourcePause(source); +}; + +void XeSound::resume() { + alSourcePlay(source); +}; + +bool XeSound::isPlaying() { + ALint playing; + alGetSourcei(source, AL_SOURCE_STATE, &playing); + return playing == AL_PLAYING; +}; + +void XeSound::setPosition(glm::vec3 position) { + alSource3f(source, AL_POSITION, position.x, position.y, position.z); +}; + +void XeSound::setLooping(bool looping) { + alSourcei(source, AL_LOOPING, looping ? 1 : 0); +}; + +} \ No newline at end of file diff --git a/engine/xe_sound.hpp b/engine/xe_sound.hpp new file mode 100644 index 0000000..3d0caa2 --- /dev/null +++ b/engine/xe_sound.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include + +namespace xe { + +class XeSound { + + public: + + XeSound(const std::string& filename); + ~XeSound(); + + void play(); + void stop(); + void pause(); + void resume(); + + bool isPlaying(); + + void setPosition(glm::vec3 position); + void setLooping(bool looping); + + private: + ALuint source; + ALuint buffer; + ALenum format; + +}; + +} \ No newline at end of file diff --git a/engine/xe_sound_device.cpp b/engine/xe_sound_device.cpp new file mode 100644 index 0000000..0b85b6a --- /dev/null +++ b/engine/xe_sound_device.cpp @@ -0,0 +1,64 @@ +#include "xe_sound_device.hpp" + +#include +#include +#include + +namespace xe { + +XeSoundDevice::XeSoundDevice() { + + ALboolean enumeration; + enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"); + if (enumeration == AL_FALSE) { + fprintf(stderr, "enumeration extension not available\n"); + listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); + } + + listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); + + const ALCchar* name = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); + + device = alcOpenDevice(name); + if (!device) { + std::runtime_error("failed to get sound device"); + } + + std::cout << "Audio Device: " << alcGetString(device, ALC_DEVICE_SPECIFIER) << "\n"; + + context = alcCreateContext(device, NULL); + if(!alcMakeContextCurrent(context)) { + std::runtime_error("failed to make sound context current"); + } + + alListener3f(AL_POSITION, 0.f, 0.f, 0.f); + alListener3f(AL_VELOCITY, 0.f, 0.f, 0.f); + ALfloat frontAndUpVectors[] = { + /* front*/1.f, 0.f, 0.f, + /* up */ 0.f, 1.f, 0.f + }; + alListenerfv(AL_ORIENTATION, frontAndUpVectors); + +} + +void XeSoundDevice::listAudioDevices(const ALCchar* devices) { + const ALCchar *device = devices, *next = devices + 1; + size_t len = 0; + + fprintf(stdout, "Devices list: "); + while (device && *device != '\0' && next && *next != '\0') { + fprintf(stdout, "%s, ", device); + len = strlen(device); + device += (len + 1); + next += (len + 2); + } + std::cout << "\n"; +} + +XeSoundDevice::~XeSoundDevice() { + alcMakeContextCurrent(nullptr); + alcDestroyContext(context); + alcCloseDevice(device); +} + +} \ No newline at end of file diff --git a/engine/xe_sound_device.hpp b/engine/xe_sound_device.hpp new file mode 100644 index 0000000..a6a034e --- /dev/null +++ b/engine/xe_sound_device.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +namespace xe { + +class XeSoundDevice { + + public: + + XeSoundDevice(); + ~XeSoundDevice(); + + private: + + void listAudioDevices(const ALCchar* devices); + + ALCdevice* device; + ALCcontext* context; + +}; + +} \ No newline at end of file diff --git a/lib/openal b/lib/openal deleted file mode 160000 index c52df6d..0000000 --- a/lib/openal +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c52df6d78ab7131a543326cd2257f267036754e1 diff --git a/res/sound/when_the_world_ends.wav b/res/sound/when_the_world_ends.wav new file mode 100644 index 0000000..0db984c Binary files /dev/null and b/res/sound/when_the_world_ends.wav differ diff --git a/src/first_app.cpp b/src/first_app.cpp index 40a5681..30c639f 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -7,6 +7,7 @@ #include "xe_render_system.hpp" #include "keyboard_movement_controller.hpp" #include "simple_renderer.hpp" +#include "xe_sound.hpp" #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE @@ -26,12 +27,13 @@ FirstApp::~FirstApp() {} void FirstApp::run() { - const std::string s = "res/image/texture.png"; - std::shared_ptr image = xeEngine.loadImage("res/image/texture.png"); std::shared_ptr image2 = xeEngine.loadImage("res/image/scaly.png"); SimpleRenderer renderer{xeEngine, image.get()}; + + xe::XeSound sound{"res/sound/when_the_world_ends.wav"}; + sound.play(); auto viewerObject = xe::XeGameObject::createGameObject(); viewerObject.transform.translation = {-7.f, 3.f, -7.f};