diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/xe_engine.hpp | 2 | ||||
-rw-r--r-- | engine/xe_sound.cpp | 83 | ||||
-rw-r--r-- | engine/xe_sound.hpp | 37 | ||||
-rw-r--r-- | engine/xe_sound_device.cpp | 64 | ||||
-rw-r--r-- | engine/xe_sound_device.hpp | 25 |
5 files changed, 211 insertions, 0 deletions
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 <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; 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 <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); +}; + +}
\ 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 <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; + +}; + +}
\ 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 <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); +} + +}
\ 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 <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; + +}; + +}
\ No newline at end of file |