#include "client/audio/AudioBuffer.h"

#include <AL/al.h>

namespace blitz {
namespace audio {

static const std::vector<unsigned int> AudioFormats = {
	AL_FORMAT_MONO8,
	AL_FORMAT_MONO16,
	AL_FORMAT_STEREO8,
	AL_FORMAT_STEREO16,
};

AudioBuffer::AudioBuffer(AudioData&& audioData) {
	alGenBuffers(1, &m_ID);
	alBufferData(m_ID, AudioFormats[audioData.m_Format], audioData.m_Buffer.data() + audioData.m_Buffer.GetReadOffset(),
		audioData.m_Buffer.GetSize() - audioData.m_Buffer.GetReadOffset(), static_cast<ALsizei>(audioData.m_Frequency));
}

AudioBuffer::~AudioBuffer() {
	alDeleteBuffers(1, &m_ID);
	m_ID = 0;
}

}  // namespace audio
}  // namespace blitz