63 lines
1.4 KiB
C++
63 lines
1.4 KiB
C++
#include "AudioStream.hh"
|
|
#include "exceptions.hh"
|
|
|
|
namespace render {
|
|
AudioStream::AudioStream()
|
|
{
|
|
alGenSources(1, &alSource_.get());
|
|
|
|
alSourcef(alSource_, AL_MIN_GAIN, 1.0f);
|
|
}
|
|
|
|
AudioStream::~AudioStream()
|
|
{
|
|
#ifndef NDEBUG
|
|
printf("%zu buffers at destruction\n", alBufs_.size());
|
|
#endif
|
|
}
|
|
|
|
void AudioStream::play()
|
|
{
|
|
alSourcePlay(alSource_);
|
|
}
|
|
|
|
void AudioStream::pause()
|
|
{
|
|
alSourceStop(alSource_);
|
|
}
|
|
|
|
bool AudioStream::isPlaying() const
|
|
{
|
|
ALenum state;
|
|
alGetSourcei(alSource_, AL_SOURCE_STATE, &state);
|
|
return (state == AL_PLAYING);
|
|
}
|
|
void AudioStream::queueSamples(unsigned channels, unsigned freq, int16_t const* data, size_t len)
|
|
{
|
|
// Reuse a processed buffer if possible, or create a new one
|
|
ALint doneBufs;
|
|
alGetSourcei(alSource_, AL_BUFFERS_PROCESSED, &doneBufs);
|
|
ALuint buf;
|
|
if (doneBufs > 0)
|
|
alSourceUnqueueBuffers(alSource_, 1, &buf);
|
|
else {
|
|
alGenBuffers(1, &buf);
|
|
alBufs_.push_back(buf);
|
|
}
|
|
|
|
// Fill buffer with data
|
|
ALenum format;
|
|
if (channels == 1)
|
|
format = AL_FORMAT_MONO16;
|
|
else if (channels == 2)
|
|
format = AL_FORMAT_STEREO16;
|
|
else
|
|
throw Exception{"More than 2 channels not supported"};
|
|
|
|
alBufferData(buf, format, data, len, freq);
|
|
|
|
// Enqueue buffer
|
|
alSourceQueueBuffers(alSource_, 1, &buf);
|
|
}
|
|
}
|