7#include <libnyquist/Decoders.h>
11#include <smk/Audio.hpp>
12#include <smk/SoundBuffer.hpp>
17SoundBuffer::SoundBuffer() =
default;
20SoundBuffer::SoundBuffer(
const std::string& filename) :
SoundBuffer() {
22 static bool once =
true;
25 <<
"Error: smk::Audio has not been initialized. Please create a "
26 "smk::Audio instance in the main() function before creating a "
33 nqr::AudioData fileData;
34 nqr::NyquistIO loader;
35 loader.Load(&fileData, filename);
37 auto sample_rate =
static_cast<ALsizei
>(fileData.sampleRate);
39 std::vector<ALshort> data;
40 for (
auto& it : fileData.samples) {
41 it = std::min(it, +1.f);
42 it = std::max(it, -1.f);
43 data.push_back(ALshort(it *
float((1 << 15) - 1)));
47 switch (fileData.channelCount) {
49 format = AL_FORMAT_MONO16;
52 format = AL_FORMAT_STEREO16;
55 std::cerr <<
"SoundBuffer: Unsupported format file " + filename
60 alGenBuffers(1, &buffer_);
61 alBufferData(buffer_, format, data.data(),
62 ALsizei(data.size() *
sizeof(ALshort)), sample_rate);
64 if (alGetError() != AL_NO_ERROR) {
65 std::cerr <<
"SoundBuffer: OpenAL error" << std::endl;
70SoundBuffer::~SoundBuffer() {
72 alDeleteBuffers(1, &buffer_);
76SoundBuffer::SoundBuffer(SoundBuffer&& o)
noexcept {
77 this->operator=(std::move(o));
80SoundBuffer& SoundBuffer::operator=(SoundBuffer&& o)
noexcept {
81 std::swap(buffer_, o.buffer_);
85unsigned int SoundBuffer::buffer()
const {
static bool Initialized()
A sound file loaded in memory.