1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include "AudioStreamPlayer.h"
#include "../ServiceLocator.h"
#define NCINE_INCLUDE_OPENAL
#include "../CommonHeaders.h"
namespace nCine
{
AudioStreamPlayer::AudioStreamPlayer()
: IAudioPlayer(ObjectType::AudioStreamPlayer), audioStream_()
{
}
AudioStreamPlayer::AudioStreamPlayer(StringView filename)
: IAudioPlayer(ObjectType::AudioStreamPlayer), audioStream_(filename)
{
}
AudioStreamPlayer::~AudioStreamPlayer()
{
stop();
}
bool AudioStreamPlayer::loadFromFile(const char* filename)
{
if (state_ != PlayerState::Stopped) {
audioStream_.stop(sourceId_);
}
return audioStream_.loadFromFile(filename);
}
void AudioStreamPlayer::play()
{
IAudioDevice& device = theServiceLocator().GetAudioDevice();
switch (state_) {
case PlayerState::Initial:
case PlayerState::Stopped: {
const unsigned int source = device.registerPlayer(this);
if (source == IAudioDevice::UnavailableSource) {
if (device.isValid()) {
LOGW("No more available audio sources for playing");
}
break;
}
sourceId_ = source;
// Streams looping is not handled at enqueued buffer level
alSourcei(sourceId_, AL_LOOPING, AL_FALSE);
alSourcef(sourceId_, AL_GAIN, gain_);
alSourcef(sourceId_, AL_PITCH, pitch_);
updateFilters();
bool isSourceRelative = GetFlags(PlayerFlags::SourceRelative);
bool isAs2D = GetFlags(PlayerFlags::As2D);
alSourcei(sourceId_, AL_SOURCE_RELATIVE, isSourceRelative || isAs2D ? AL_TRUE : AL_FALSE);
alSourcef(sourceId_, AL_REFERENCE_DISTANCE, IAudioDevice::ReferenceDistance);
alSourcef(sourceId_, AL_MAX_DISTANCE, IAudioDevice::MaxDistance);
setPositionInternal(getAdjustedPosition(device, position_, isSourceRelative, isAs2D));
alSourcePlay(sourceId_);
state_ = PlayerState::Playing;
break;
}
case PlayerState::Paused: {
updateFilters();
alSourcePlay(sourceId_);
state_ = PlayerState::Playing;
break;
}
}
}
void AudioStreamPlayer::pause()
{
switch (state_) {
case PlayerState::Playing: {
alSourcePause(sourceId_);
state_ = PlayerState::Paused;
break;
}
}
}
void AudioStreamPlayer::stop()
{
switch (state_) {
case PlayerState::Playing:
case PlayerState::Paused: {
// Stop the source then unqueue every buffer
audioStream_.stop(sourceId_);
// Detach the buffer from source
alSourcei(sourceId_, AL_BUFFER, 0);
#if defined(OPENAL_FILTERS_SUPPORTED)
if (filterHandle_ != 0) {
alSourcei(sourceId_, AL_DIRECT_FILTER, 0);
}
#endif
state_ = PlayerState::Stopped;
break;
}
}
IAudioDevice& device = theServiceLocator().GetAudioDevice();
device.unregisterPlayer(this);
}
void AudioStreamPlayer::setLooping(bool value)
{
IAudioPlayer::setLooping(value);
audioStream_.setLooping(value);
}
void AudioStreamPlayer::updateState()
{
if (state_ == PlayerState::Playing) {
bool shouldStillPlay = audioStream_.enqueue(sourceId_, GetFlags(PlayerFlags::Looping));
if (!shouldStillPlay) {
// Detach the buffer from source
alSourcei(sourceId_, AL_BUFFER, 0);
#if defined(OPENAL_FILTERS_SUPPORTED)
if (filterHandle_ != 0) {
alSourcei(sourceId_, AL_DIRECT_FILTER, 0);
}
#endif
state_ = PlayerState::Stopped;
IAudioDevice& device = theServiceLocator().GetAudioDevice();
device.unregisterPlayer(this);
}
}
}
}
|