start implementing openal soft

This commit is contained in:
tylermurphy534 2022-09-22 11:14:00 -04:00
parent b1e71a70a4
commit ac2868abc0
10 changed files with 219 additions and 10 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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)

View file

@ -6,6 +6,7 @@
#include "xe_camera.hpp"
#include "xe_descriptors.hpp"
#include "xe_image.hpp"
#include "xe_sound_device.hpp"
#include <chrono>
#include <string>
@ -44,6 +45,7 @@ class XeEngine {
XeDevice xeDevice;
XeRenderer xeRenderer;
XeCamera xeCamera;
XeSoundDevice xeSoundDevice;
std::chrono::_V2::system_clock::time_point currentTime;
float frameTime;

83
engine/xe_sound.cpp Normal file
View file

@ -0,0 +1,83 @@
#include "xe_sound.hpp"
#include <vector>
#include <stdexcept>
#include <iostream>
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<float> file;
// file.load(filename);
// std::vector<uint8_t> data;
// file.writePCMToBuffer(data);
// auto getALSoundFormat = [](AudioFile<float>& 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);
};
}

37
engine/xe_sound.hpp Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <AL/al.h>
#include <AL/alut.h>
#include <string>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
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;
};
}

View file

@ -0,0 +1,64 @@
#include "xe_sound_device.hpp"
#include <stdexcept>
#include <iostream>
#include <cstring>
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);
}
}

View file

@ -0,0 +1,25 @@
#pragma once
#include <AL/alc.h>
#include <AL/alext.h>
#include <AL/al.h>
namespace xe {
class XeSoundDevice {
public:
XeSoundDevice();
~XeSoundDevice();
private:
void listAudioDevices(const ALCchar* devices);
ALCdevice* device;
ALCcontext* context;
};
}

@ -1 +0,0 @@
Subproject commit c52df6d78ab7131a543326cd2257f267036754e1

Binary file not shown.

View file

@ -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,13 +27,14 @@ FirstApp::~FirstApp() {}
void FirstApp::run() {
const std::string s = "res/image/texture.png";
std::shared_ptr<xe::XeImage> image = xeEngine.loadImage("res/image/texture.png");
std::shared_ptr<xe::XeImage> 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};
viewerObject.transform.rotation.y = glm::radians(45.f);