This commit is contained in:
tylermurphy534 2022-09-22 11:29:32 -04:00
parent ac2868abc0
commit 5d07a3c101
3 changed files with 51 additions and 60 deletions

View file

@ -8,33 +8,22 @@ namespace xe {
XeSound::XeSound(const std::string& filename) { XeSound::XeSound(const std::string& filename) {
ALvoid *data; // ALvoid *data;
ALsizei size, freq; // ALsizei size, freq;
ALenum format; // ALenum format;
ALboolean loop; // ALboolean loop;
char *bufferData; // char *bufferData;
// alutLoadWAVFile(filename.c_str(), &format, &data, &size, &freq, &loop); alutInit(0, NULL);
// AudioFile<float> file; std::cout << std::hex << alutGetError() << "\n";
// file.load(filename);
// std::vector<uint8_t> data; buffer = alutCreateBufferFromFile(filename.c_str());
// 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); std::cout << std::hex << alutGetError() << "\n";
// alBufferData(buffer, getALSoundFormat(file), data.data(), data.size(), file.getSampleRate());
alGenSources(1, &source); alGenSources(1, &source);
alSourcef(source, AL_GAIN, 1.f); alSourcef(source, AL_GAIN, 1.f);
alSourcef(source, AL_PITCH, 1.f); alSourcef(source, AL_PITCH, 1.f);
alSource3f(source, AL_POSITION, 0, 0, 0); alSource3f(source, AL_POSITION, 0, 0, 0);
@ -47,6 +36,7 @@ XeSound::XeSound(const std::string& filename) {
XeSound::~XeSound() { XeSound::~XeSound() {
alDeleteSources(1, &source); alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer); alDeleteBuffers(1, &buffer);
alutExit();
} }
void XeSound::play() { void XeSound::play() {

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <AL/al.h> #include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h> #include <AL/alut.h>
#include <string> #include <string>

View file

@ -8,57 +8,57 @@ namespace xe {
XeSoundDevice::XeSoundDevice() { XeSoundDevice::XeSoundDevice() {
ALboolean enumeration; // ALboolean enumeration;
enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"); // enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT");
if (enumeration == AL_FALSE) { // if (enumeration == AL_FALSE) {
fprintf(stderr, "enumeration extension not available\n"); // fprintf(stderr, "enumeration extension not available\n");
listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); // listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
} // }
listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); // listAudioDevices(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
const ALCchar* name = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); // const ALCchar* name = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
device = alcOpenDevice(name); // device = alcOpenDevice(name);
if (!device) { // if (!device) {
std::runtime_error("failed to get sound device"); // std::runtime_error("failed to get sound device");
} // }
std::cout << "Audio Device: " << alcGetString(device, ALC_DEVICE_SPECIFIER) << "\n"; // std::cout << "Audio Device: " << alcGetString(device, ALC_DEVICE_SPECIFIER) << "\n";
context = alcCreateContext(device, NULL); // context = alcCreateContext(device, NULL);
if(!alcMakeContextCurrent(context)) { // if(!alcMakeContextCurrent(context)) {
std::runtime_error("failed to make sound context current"); // std::runtime_error("failed to make sound context current");
} // }
alListener3f(AL_POSITION, 0.f, 0.f, 0.f); // alListener3f(AL_POSITION, 0.f, 0.f, 0.f);
alListener3f(AL_VELOCITY, 0.f, 0.f, 0.f); // alListener3f(AL_VELOCITY, 0.f, 0.f, 0.f);
ALfloat frontAndUpVectors[] = { // ALfloat frontAndUpVectors[] = {
/* front*/1.f, 0.f, 0.f, // /* front*/1.f, 0.f, 0.f,
/* up */ 0.f, 1.f, 0.f // /* up */ 0.f, 1.f, 0.f
}; // };
alListenerfv(AL_ORIENTATION, frontAndUpVectors); // alListenerfv(AL_ORIENTATION, frontAndUpVectors);
} // }
void XeSoundDevice::listAudioDevices(const ALCchar* devices) { // void XeSoundDevice::listAudioDevices(const ALCchar* devices) {
const ALCchar *device = devices, *next = devices + 1; // const ALCchar *device = devices, *next = devices + 1;
size_t len = 0; // size_t len = 0;
fprintf(stdout, "Devices list: "); // fprintf(stdout, "Devices list: ");
while (device && *device != '\0' && next && *next != '\0') { // while (device && *device != '\0' && next && *next != '\0') {
fprintf(stdout, "%s, ", device); // fprintf(stdout, "%s, ", device);
len = strlen(device); // len = strlen(device);
device += (len + 1); // device += (len + 1);
next += (len + 2); // next += (len + 2);
} // }
std::cout << "\n"; // std::cout << "\n";
} }
XeSoundDevice::~XeSoundDevice() { XeSoundDevice::~XeSoundDevice() {
alcMakeContextCurrent(nullptr); // alcMakeContextCurrent(nullptr);
alcDestroyContext(context); // alcDestroyContext(context);
alcCloseDevice(device); // alcCloseDevice(device);
} }
} }