18 #include "auxeffectslot.h"
19 #include "sourcegroup.h"
24 class ALBufferStream
{
25 SharedPtr
<Decoder
> mDecoder
;
37 Vector
<ALuint
> mBufferIds
;
41 std::pair
<uint64_t,uint64_t> mLoopPts
;
43 std::atomic
<bool> mDone
;
46 ALBufferStream(SharedPtr
<Decoder
> decoder
, ALuint updatelen
, ALuint numupdates
)
47 : mDecoder(decoder
), mUpdateLen(updatelen
), mNumUpdates(numupdates
),
48 mFormat(AL_NONE
), mFrequency(0), mFrameSize(0), mSilence(0),
49 mCurrentIdx(0), mSamplePos(0), mLoopPts
{0,0}, mHasLooped(false),
54 if(!mBufferIds
.empty())
56 alDeleteBuffers(mBufferIds
.size(), mBufferIds
.data());
61 uint64_t getLength() const { return mDecoder
->getLength(); }
62 uint64_t getPosition() const { return mSamplePos
; }
64 ALuint
getNumUpdates() const { return mNumUpdates
; }
65 ALuint
getUpdateLength() const { return mUpdateLen
; }
67 ALuint
getFrequency() const { return mFrequency
; }
69 bool seek(uint64_t pos
)
71 if(!mDecoder
->seek(pos
))
75 mDone
.store(false, std::memory_order_release
);
81 ALuint srate
= mDecoder
->getFrequency();
82 ChannelConfig chans
= mDecoder
->getChannelConfig();
83 SampleType type
= mDecoder
->getSampleType();
85 mLoopPts
= mDecoder
->getLoopPoints();
86 if(mLoopPts
.first
>= mLoopPts
.second
)
89 mLoopPts
.second
= std::numeric_limits
<uint64_t>::max();
93 mFrameSize
= FramesToBytes(1, chans
, type
);
94 mFormat
= GetFormat(chans
, type
);
95 if(mFormat
== AL_NONE
)
97 std::stringstream sstr
;
98 sstr
<< "Format not supported ("<<GetSampleTypeName(type
)<<", "<<GetChannelConfigName(chans
)<<")";
99 throw std::runtime_error(sstr
.str());
102 mData
.resize(mUpdateLen
* mFrameSize
);
103 if(type
== SampleType::UInt8
) mSilence
= 0x80;
104 else if(type
== SampleType::Mulaw
) mSilence
= 0x7f;
105 else mSilence
= 0x00;
107 mBufferIds
.assign(mNumUpdates
, 0);
108 alGenBuffers(mBufferIds
.size(), mBufferIds
.data());
111 int64_t getLoopStart() const { return mLoopPts
.first
; }
112 int64_t getLoopEnd() const { return mLoopPts
.second
; }
114 bool hasLooped() const { return mHasLooped
; }
115 bool hasMoreData() const { return !mDone
.load(std::memory_order_acquire
); }
116 bool streamMoreData(ALuint srcid
, bool loop
)
118 if(mDone
.load(std::memory_order_acquire
))
122 ALuint len
= mUpdateLen
;
123 if(loop
&& mSamplePos
<= mLoopPts
.second
)
124 len
= std::min
<uint64_t>(len
, mLoopPts
.second
- mSamplePos
);
128 frames
= mDecoder
->read(mData
.data(), len
);
129 mSamplePos
+= frames
;
130 if(frames
< mUpdateLen
&& loop
&& mSamplePos
> 0)
132 if(mSamplePos
< mLoopPts
.second
)
134 mLoopPts
.second
= mSamplePos
;
135 mLoopPts
.first
= std::min(mLoopPts
.first
, mLoopPts
.second
-1);
139 if(!mDecoder
->seek(mLoopPts
.first
))
141 mSamplePos
= mLoopPts
.first
;
144 len
= std::min
<uint64_t>(mUpdateLen
-frames
, mLoopPts
.second
-mLoopPts
.first
);
145 ALuint got
= mDecoder
->read(&mData
[frames
*mFrameSize
], len
);
149 } while(frames
< mUpdateLen
);
151 if(frames
< mUpdateLen
)
153 mDone
.store(true, std::memory_order_release
);
154 if(frames
== 0) return false;
155 mSamplePos
+= mUpdateLen
- frames
;
156 std::fill(mData
.begin() + frames
*mFrameSize
, mData
.end(), mSilence
);
159 alBufferData(mBufferIds
[mCurrentIdx
], mFormat
, mData
.data(), mData
.size(), mFrequency
);
160 alSourceQueueBuffers(srcid
, 1, &mBufferIds
[mCurrentIdx
]);
161 mCurrentIdx
= (mCurrentIdx
+1) % mBufferIds
.size();
167 SourceImpl::SourceImpl(ContextImpl
*context
)
168 : mContext(context
), mId(0), mBuffer(0), mGroup(nullptr), mIsAsync(false),
169 mDirectFilter(AL_FILTER_NULL
)
174 SourceImpl::~SourceImpl()
179 void SourceImpl::resetProperties()
182 mGroup
->removeSource(Source(this));
185 mPaused
.store(false, std::memory_order_release
);
192 mMaxDist
= std::numeric_limits
<float>::max();
193 mPosition
= Vector3(0.0f
);
194 mVelocity
= Vector3(0.0f
);
195 mDirection
= Vector3(0.0f
);
196 mOrientation
[0] = Vector3(0.0f
, 0.0f
, -1.0f
);
197 mOrientation
[1] = Vector3(0.0f
, 1.0f
, 0.0f
);
198 mConeInnerAngle
= 360.0f
;
199 mConeOuterAngle
= 360.0f
;
200 mConeOuterGain
= 0.0f
;
201 mConeOuterGainHF
= 1.0f
;
202 mRolloffFactor
= 1.0f
;
203 mRoomRolloffFactor
= 0.0f
;
204 mDopplerFactor
= 1.0f
;
205 mAirAbsorptionFactor
= 0.0f
;
207 mStereoAngles
[0] = F_PI
/ 6.0f
;
208 mStereoAngles
[1] = -F_PI
/ 6.0f
;
209 mSpatialize
= Spatialize::Auto
;
210 mResampler
= mContext
->hasExtension(SOFT_source_resampler
) ?
211 alGetInteger(AL_DEFAULT_RESAMPLER_SOFT
) : 0;
214 mDryGainHFAuto
= true;
216 mWetGainHFAuto
= true;
218 mContext
->alDeleteFilters(1, &mDirectFilter
);
220 for(auto &i
: mEffectSlots
)
223 i
.second
.mSlot
->removeSourceSend(Source(this), i
.first
);
225 mContext
->alDeleteFilters(1, &i
.second
.mFilter
);
227 mEffectSlots
.clear();
232 void SourceImpl::applyProperties(bool looping
, ALuint offset
) const
234 alSourcei(mId
, AL_LOOPING
, looping
? AL_TRUE
: AL_FALSE
);
235 alSourcei(mId
, AL_SAMPLE_OFFSET
, offset
);
238 alSourcef(mId
, AL_PITCH
, mPitch
* mGroup
->getAppliedPitch());
239 alSourcef(mId
, AL_GAIN
, mGain
* mGroup
->getAppliedGain());
243 alSourcef(mId
, AL_PITCH
, mPitch
);
244 alSourcef(mId
, AL_GAIN
, mGain
);
246 alSourcef(mId
, AL_MIN_GAIN
, mMinGain
);
247 alSourcef(mId
, AL_MAX_GAIN
, mMaxGain
);
248 alSourcef(mId
, AL_REFERENCE_DISTANCE
, mRefDist
);
249 alSourcef(mId
, AL_MAX_DISTANCE
, mMaxDist
);
250 alSourcefv(mId
, AL_POSITION
, mPosition
.getPtr());
251 alSourcefv(mId
, AL_VELOCITY
, mVelocity
.getPtr());
252 alSourcefv(mId
, AL_DIRECTION
, mDirection
.getPtr());
253 if(mContext
->hasExtension(EXT_BFORMAT
))
254 alSourcefv(mId
, AL_ORIENTATION
, &mOrientation
[0][0]);
255 alSourcef(mId
, AL_CONE_INNER_ANGLE
, mConeInnerAngle
);
256 alSourcef(mId
, AL_CONE_OUTER_ANGLE
, mConeOuterAngle
);
257 alSourcef(mId
, AL_CONE_OUTER_GAIN
, mConeOuterGain
);
258 alSourcef(mId
, AL_ROLLOFF_FACTOR
, mRolloffFactor
);
259 alSourcef(mId
, AL_DOPPLER_FACTOR
, mDopplerFactor
);
260 if(mContext
->hasExtension(EXT_SOURCE_RADIUS
))
261 alSourcef(mId
, AL_SOURCE_RADIUS
, mRadius
);
262 if(mContext
->hasExtension(EXT_STEREO_ANGLES
))
263 alSourcefv(mId
, AL_STEREO_ANGLES
, mStereoAngles
);
264 if(mContext
->hasExtension(SOFT_source_spatialize
))
265 alSourcei(mId
, AL_SOURCE_SPATIALIZE_SOFT
, (ALint
)mSpatialize
);
266 if(mContext
->hasExtension(SOFT_source_resampler
))
267 alSourcei(mId
, AL_SOURCE_RESAMPLER_SOFT
, mResampler
);
268 alSourcei(mId
, AL_SOURCE_RELATIVE
, mRelative
? AL_TRUE
: AL_FALSE
);
269 if(mContext
->hasExtension(EXT_EFX
))
271 alSourcef(mId
, AL_CONE_OUTER_GAINHF
, mConeOuterGainHF
);
272 alSourcef(mId
, AL_ROOM_ROLLOFF_FACTOR
, mRoomRolloffFactor
);
273 alSourcef(mId
, AL_AIR_ABSORPTION_FACTOR
, mAirAbsorptionFactor
);
274 alSourcei(mId
, AL_DIRECT_FILTER_GAINHF_AUTO
, mDryGainHFAuto
? AL_TRUE
: AL_FALSE
);
275 alSourcei(mId
, AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
, mWetGainAuto
? AL_TRUE
: AL_FALSE
);
276 alSourcei(mId
, AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
, mWetGainHFAuto
? AL_TRUE
: AL_FALSE
);
277 alSourcei(mId
, AL_DIRECT_FILTER
, mDirectFilter
);
278 for(const auto &i
: mEffectSlots
)
280 ALuint slotid
= (i
.second
.mSlot
? i
.second
.mSlot
->getId() : 0);
281 alSource3i(mId
, AL_AUXILIARY_SEND_FILTER
, slotid
, i
.first
, i
.second
.mFilter
);
287 void SourceImpl::setGroup(SourceGroupImpl
*group
)
290 mGroup
->removeSource(Source(this));
295 void SourceImpl::unsetGroup()
301 void SourceImpl::groupUpdate()
307 alSourcef(mId
, AL_PITCH
, mPitch
* mGroup
->getAppliedPitch());
308 alSourcef(mId
, AL_GAIN
, mGain
* mGroup
->getAppliedGain());
312 alSourcef(mId
, AL_PITCH
, mPitch
);
313 alSourcef(mId
, AL_GAIN
, mGain
);
318 void SourceImpl::groupPropUpdate(ALfloat gain
, ALfloat pitch
)
322 alSourcef(mId
, AL_PITCH
, mPitch
* pitch
);
323 alSourcef(mId
, AL_GAIN
, mGain
* gain
);
328 void SourceImpl::play(Buffer buffer
)
330 BufferImpl
*albuf
= buffer
.getHandle();
331 if(!albuf
) throw std::runtime_error("Buffer is not valid");
332 CheckContext(mContext
);
333 CheckContext(albuf
->getContext());
335 if(!albuf
->isReady())
336 throw std::runtime_error("Buffer is not ready");
339 mContext
->removeStream(this);
340 mIsAsync
.store(false, std::memory_order_release
);
344 mId
= mContext
->getSourceId(mPriority
);
345 applyProperties(mLooping
, (ALuint
)std::min
<uint64_t>(mOffset
, std::numeric_limits
<ALint
>::max()));
349 mContext
->removePlayingSource(this);
351 alSourcei(mId
, AL_BUFFER
, 0);
352 alSourcei(mId
, AL_LOOPING
, mLooping
? AL_TRUE
: AL_FALSE
);
353 alSourcei(mId
, AL_SAMPLE_OFFSET
, (ALuint
)std::min
<uint64_t>(mOffset
, std::numeric_limits
<ALint
>::max()));
359 mBuffer
->removeSource(Source(this));
361 mBuffer
->addSource(Source(this));
363 alSourcei(mId
, AL_BUFFER
, mBuffer
->getId());
365 mPaused
.store(false, std::memory_order_release
);
366 mContext
->addPlayingSource(this, mId
);
369 void SourceImpl::play(SharedPtr
<Decoder
> decoder
, ALuint updatelen
, ALuint queuesize
)
372 throw std::runtime_error("Update length out of range");
374 throw std::runtime_error("Queue size out of range");
375 CheckContext(mContext
);
377 auto stream
= MakeUnique
<ALBufferStream
>(decoder
, updatelen
, queuesize
);
381 mContext
->removeStream(this);
382 mIsAsync
.store(false, std::memory_order_release
);
386 mId
= mContext
->getSourceId(mPriority
);
387 applyProperties(false, 0);
391 mContext
->removePlayingSource(this);
393 alSourcei(mId
, AL_BUFFER
, 0);
394 alSourcei(mId
, AL_LOOPING
, AL_FALSE
);
395 alSourcei(mId
, AL_SAMPLE_OFFSET
, 0);
400 mBuffer
->removeSource(Source(this));
403 mStream
= std::move(stream
);
405 mStream
->seek(mOffset
);
408 for(ALuint i
= 0;i
< mStream
->getNumUpdates();i
++)
410 if(!mStream
->streamMoreData(mId
, mLooping
))
414 mPaused
.store(false, std::memory_order_release
);
416 mContext
->addStream(this);
417 mIsAsync
.store(true, std::memory_order_release
);
418 mContext
->addPlayingSource(this);
422 void SourceImpl::makeStopped(bool dolock
)
427 mContext
->removeStream(this);
429 mContext
->removeStreamNoLock(this);
431 mIsAsync
.store(false, std::memory_order_release
);
436 alSourcei(mId
, AL_BUFFER
, 0);
437 if(mContext
->hasExtension(EXT_EFX
))
439 alSourcei(mId
, AL_DIRECT_FILTER
, AL_FILTER_NULL
);
440 for(auto &i
: mEffectSlots
)
441 alSource3i(mId
, AL_AUXILIARY_SEND_FILTER
, 0, i
.first
, AL_FILTER_NULL
);
443 mContext
->insertSourceId(mId
);
449 mBuffer
->removeSource(Source(this));
452 mPaused
.store(false, std::memory_order_release
);
455 void SourceImpl::stop()
457 CheckContext(mContext
);
458 mContext
->removePlayingSource(this);
463 void SourceImpl::checkPaused()
465 if(mPaused
.load(std::memory_order_acquire
) || mId
== 0)
469 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
470 // Streaming sources may be in a stopped state if underrun
471 mPaused
.store((state
== AL_PAUSED
) ||
472 (state
== AL_STOPPED
&& mStream
&& mStream
->hasMoreData()),
473 std::memory_order_release
);
476 void SourceImpl::pause()
478 CheckContext(mContext
);
479 if(mPaused
.load(std::memory_order_acquire
))
484 std::lock_guard
<std::mutex
> lock(mMutex
);
487 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
488 // Streaming sources may be in a stopped state if underrun
489 mPaused
.store((state
== AL_PAUSED
) ||
490 (state
== AL_STOPPED
&& mStream
&& mStream
->hasMoreData()),
491 std::memory_order_release
);
495 void SourceImpl::resume()
497 CheckContext(mContext
);
498 if(!mPaused
.load(std::memory_order_acquire
))
503 mPaused
.store(false, std::memory_order_release
);
507 bool SourceImpl::isPlaying() const
509 CheckContext(mContext
);
510 if(mId
== 0) return false;
513 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
515 throw std::runtime_error("Source state error");
517 return state
== AL_PLAYING
|| (!mPaused
.load(std::memory_order_acquire
) &&
518 mStream
&& mStream
->hasMoreData());
521 bool SourceImpl::isPaused() const
523 CheckContext(mContext
);
524 if(mId
== 0) return false;
527 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
529 throw std::runtime_error("Source state error");
531 return state
== AL_PAUSED
|| mPaused
.load(std::memory_order_acquire
);
535 bool SourceImpl::playUpdate(ALuint id
)
538 alGetSourcei(id
, AL_SOURCE_STATE
, &state
);
539 if(EXPECT((state
== AL_PLAYING
|| state
== AL_PAUSED
), true))
543 mContext
->send(&MessageHandler::sourceStopped
, Source(this));
547 bool SourceImpl::playUpdate()
549 if(EXPECT(mIsAsync
.load(std::memory_order_acquire
), true))
553 mContext
->send(&MessageHandler::sourceStopped
, Source(this));
558 ALint
SourceImpl::refillBufferStream()
561 alGetSourcei(mId
, AL_BUFFERS_PROCESSED
, &processed
);
565 alSourceUnqueueBuffers(mId
, 1, &buf
);
570 alGetSourcei(mId
, AL_BUFFERS_QUEUED
, &queued
);
571 for(;(ALuint
)queued
< mStream
->getNumUpdates();queued
++)
573 if(!mStream
->streamMoreData(mId
, mLooping
))
580 bool SourceImpl::updateAsync()
582 std::lock_guard
<std::mutex
> lock(mMutex
);
584 ALint queued
= refillBufferStream();
587 mIsAsync
.store(false, std::memory_order_release
);
590 if(!mPaused
.load(std::memory_order_acquire
))
593 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
594 if(state
!= AL_PLAYING
)
601 void SourceImpl::setPriority(ALuint priority
)
603 mPriority
= priority
;
607 void SourceImpl::setOffset(uint64_t offset
)
609 CheckContext(mContext
);
618 if(offset
>= std::numeric_limits
<ALint
>::max())
619 throw std::runtime_error("Offset out of range");
621 alSourcei(mId
, AL_SAMPLE_OFFSET
, (ALint
)offset
);
622 if(alGetError() != AL_NO_ERROR
)
623 throw std::runtime_error("Offset out of range");
627 std::lock_guard
<std::mutex
> lock(mMutex
);
628 if(!mStream
->seek(offset
))
629 throw std::runtime_error("Failed to seek to offset");
631 alSourcei(mId
, AL_BUFFER
, 0);
632 ALint queued
= refillBufferStream();
633 if(queued
> 0 && !mPaused
)
638 std::pair
<uint64_t,std::chrono::nanoseconds
> SourceImpl::getSampleOffsetLatency() const
640 std::pair
<uint64_t,std::chrono::nanoseconds
> ret
{0, std::chrono::nanoseconds::zero()};
641 CheckContext(mContext
);
642 if(mId
== 0) return ret
;
646 std::lock_guard
<std::mutex
> lock(mMutex
);
647 ALint queued
= 0, state
= -1, srcpos
= 0;
649 alGetSourcei(mId
, AL_BUFFERS_QUEUED
, &queued
);
650 if(mContext
->hasExtension(SOFT_source_latency
))
653 mContext
->alGetSourcei64vSOFT(mId
, AL_SAMPLE_OFFSET_LATENCY_SOFT
, val
);
655 ret
.second
= std::chrono::nanoseconds(val
[1]);
658 alGetSourcei(mId
, AL_SAMPLE_OFFSET
, &srcpos
);
659 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
661 int64_t streampos
= mStream
->getPosition();
662 if(state
!= AL_STOPPED
)
664 // The amount of samples in the queue waiting to play
665 ALuint inqueue
= queued
*mStream
->getUpdateLength() - srcpos
;
666 if(!mStream
->hasLooped())
668 // A non-looped stream should never have more samples queued
669 // than have been read...
670 streampos
= std::max
<int64_t>(streampos
, inqueue
) - inqueue
;
674 streampos
-= inqueue
;
675 int64_t looplen
= mStream
->getLoopEnd() - mStream
->getLoopStart();
676 while(streampos
< mStream
->getLoopStart())
677 streampos
+= looplen
;
681 ret
.first
= streampos
;
686 if(mContext
->hasExtension(SOFT_source_latency
))
689 mContext
->alGetSourcei64vSOFT(mId
, AL_SAMPLE_OFFSET_LATENCY_SOFT
, val
);
691 ret
.second
= std::chrono::nanoseconds(val
[1]);
694 alGetSourcei(mId
, AL_SAMPLE_OFFSET
, &srcpos
);
699 std::pair
<Seconds
,Seconds
> SourceImpl::getSecOffsetLatency() const
701 std::pair
<Seconds
,Seconds
> ret
{Seconds::zero(), Seconds::zero()};
702 CheckContext(mContext
);
703 if(mId
== 0) return ret
;
707 std::lock_guard
<std::mutex
> lock(mMutex
);
708 ALint queued
= 0, state
= -1;
711 alGetSourcei(mId
, AL_BUFFERS_QUEUED
, &queued
);
712 if(mContext
->hasExtension(SOFT_source_latency
))
715 mContext
->alGetSourcedvSOFT(mId
, AL_SEC_OFFSET_LATENCY_SOFT
, val
);
717 ret
.second
= Seconds(val
[1]);
722 alGetSourcef(mId
, AL_SEC_OFFSET
, &f
);
725 alGetSourcei(mId
, AL_SOURCE_STATE
, &state
);
728 int64_t streampos
= mStream
->getPosition();
729 if(state
!= AL_STOPPED
)
732 frac
= std::modf(srcpos
* mStream
->getFrequency(), &ipos
);
734 // The amount of samples in the queue waiting to play
735 ALuint inqueue
= queued
*mStream
->getUpdateLength() - (ALuint
)ipos
;
736 if(!mStream
->hasLooped())
738 // A non-looped stream should never have more samples queued
739 // than have been read...
740 streampos
= std::max
<int64_t>(streampos
, inqueue
) - inqueue
;
744 streampos
-= inqueue
;
745 int64_t looplen
= mStream
->getLoopEnd() - mStream
->getLoopStart();
746 while(streampos
< mStream
->getLoopStart())
747 streampos
+= looplen
;
751 ret
.first
= Seconds((streampos
+frac
) / mStream
->getFrequency());
755 ALdouble srcpos
= 0.0;
756 if(mContext
->hasExtension(SOFT_source_latency
))
759 mContext
->alGetSourcedvSOFT(mId
, AL_SEC_OFFSET_LATENCY_SOFT
, val
);
761 ret
.second
= Seconds(val
[1]);
766 alGetSourcef(mId
, AL_SEC_OFFSET
, &f
);
769 ret
.first
= Seconds(srcpos
);
774 void SourceImpl::setLooping(bool looping
)
776 CheckContext(mContext
);
779 alSourcei(mId
, AL_LOOPING
, looping
? AL_TRUE
: AL_FALSE
);
784 void SourceImpl::setPitch(ALfloat pitch
)
787 throw std::runtime_error("Pitch out of range");
788 CheckContext(mContext
);
790 alSourcef(mId
, AL_PITCH
, pitch
* (mGroup
? mGroup
->getAppliedPitch() : 1.0f
));
795 void SourceImpl::setGain(ALfloat gain
)
798 throw std::runtime_error("Gain out of range");
799 CheckContext(mContext
);
801 alSourcef(mId
, AL_GAIN
, gain
* (mGroup
? mGroup
->getAppliedGain() : 1.0f
));
805 void SourceImpl::setGainRange(ALfloat mingain
, ALfloat maxgain
)
807 if(!(mingain
>= 0.0f
&& maxgain
<= 1.0f
&& maxgain
>= mingain
))
808 throw std::runtime_error("Gain range out of range");
809 CheckContext(mContext
);
812 alSourcef(mId
, AL_MIN_GAIN
, mingain
);
813 alSourcef(mId
, AL_MAX_GAIN
, maxgain
);
820 void SourceImpl::setDistanceRange(ALfloat refdist
, ALfloat maxdist
)
822 if(!(refdist
>= 0.0f
&& maxdist
<= std::numeric_limits
<float>::max() && refdist
<= maxdist
))
823 throw std::runtime_error("Distance range out of range");
824 CheckContext(mContext
);
827 alSourcef(mId
, AL_REFERENCE_DISTANCE
, refdist
);
828 alSourcef(mId
, AL_MAX_DISTANCE
, maxdist
);
835 void SourceImpl::set3DParameters(const Vector3
&position
, const Vector3
&velocity
, const Vector3
&direction
)
837 CheckContext(mContext
);
840 Batcher batcher
= mContext
->getBatcher();
841 alSourcefv(mId
, AL_POSITION
, position
.getPtr());
842 alSourcefv(mId
, AL_VELOCITY
, velocity
.getPtr());
843 alSourcefv(mId
, AL_DIRECTION
, direction
.getPtr());
845 mPosition
= position
;
846 mVelocity
= velocity
;
847 mDirection
= direction
;
850 void SourceImpl::set3DParameters(const Vector3
&position
, const Vector3
&velocity
, std::pair
<Vector3
,Vector3
> orientation
)
852 static_assert(sizeof(orientation
) == sizeof(ALfloat
[6]), "Invalid Vector3 pair size");
853 CheckContext(mContext
);
856 Batcher batcher
= mContext
->getBatcher();
857 alSourcefv(mId
, AL_POSITION
, position
.getPtr());
858 alSourcefv(mId
, AL_VELOCITY
, velocity
.getPtr());
859 if(mContext
->hasExtension(EXT_BFORMAT
))
860 alSourcefv(mId
, AL_ORIENTATION
, orientation
.first
.getPtr());
861 alSourcefv(mId
, AL_DIRECTION
, orientation
.first
.getPtr());
863 mPosition
= position
;
864 mVelocity
= velocity
;
865 mDirection
= mOrientation
[0] = orientation
.first
;
866 mOrientation
[1] = orientation
.second
;
870 void SourceImpl::setPosition(ALfloat x
, ALfloat y
, ALfloat z
)
872 CheckContext(mContext
);
874 alSource3f(mId
, AL_POSITION
, x
, y
, z
);
880 void SourceImpl::setPosition(const ALfloat
*pos
)
882 CheckContext(mContext
);
884 alSourcefv(mId
, AL_POSITION
, pos
);
885 mPosition
[0] = pos
[0];
886 mPosition
[1] = pos
[1];
887 mPosition
[2] = pos
[2];
890 void SourceImpl::setVelocity(ALfloat x
, ALfloat y
, ALfloat z
)
892 CheckContext(mContext
);
894 alSource3f(mId
, AL_VELOCITY
, x
, y
, z
);
900 void SourceImpl::setVelocity(const ALfloat
*vel
)
902 CheckContext(mContext
);
904 alSourcefv(mId
, AL_VELOCITY
, vel
);
905 mVelocity
[0] = vel
[0];
906 mVelocity
[1] = vel
[1];
907 mVelocity
[2] = vel
[2];
910 void SourceImpl::setDirection(ALfloat x
, ALfloat y
, ALfloat z
)
912 CheckContext(mContext
);
914 alSource3f(mId
, AL_DIRECTION
, x
, y
, z
);
920 void SourceImpl::setDirection(const ALfloat
*dir
)
922 CheckContext(mContext
);
924 alSourcefv(mId
, AL_DIRECTION
, dir
);
925 mDirection
[0] = dir
[0];
926 mDirection
[1] = dir
[1];
927 mDirection
[2] = dir
[2];
930 void SourceImpl::setOrientation(ALfloat x1
, ALfloat y1
, ALfloat z1
, ALfloat x2
, ALfloat y2
, ALfloat z2
)
932 CheckContext(mContext
);
935 ALfloat ori
[6] = { x1
, y1
, z1
, x2
, y2
, z2
};
936 if(mContext
->hasExtension(EXT_BFORMAT
))
937 alSourcefv(mId
, AL_ORIENTATION
, ori
);
938 alSourcefv(mId
, AL_DIRECTION
, ori
);
940 mDirection
[0] = mOrientation
[0][0] = x1
;
941 mDirection
[1] = mOrientation
[0][1] = y1
;
942 mDirection
[2] = mOrientation
[0][2] = z1
;
943 mOrientation
[1][0] = x2
;
944 mOrientation
[1][1] = y2
;
945 mOrientation
[1][2] = z2
;
948 void SourceImpl::setOrientation(const ALfloat
*at
, const ALfloat
*up
)
950 CheckContext(mContext
);
953 ALfloat ori
[6] = { at
[0], at
[1], at
[2], up
[0], up
[1], up
[2] };
954 if(mContext
->hasExtension(EXT_BFORMAT
))
955 alSourcefv(mId
, AL_ORIENTATION
, ori
);
956 alSourcefv(mId
, AL_DIRECTION
, ori
);
958 mDirection
[0] = mOrientation
[0][0] = at
[0];
959 mDirection
[1] = mOrientation
[0][1] = at
[1];
960 mDirection
[2] = mOrientation
[0][2] = at
[2];
961 mOrientation
[1][0] = up
[0];
962 mOrientation
[1][1] = up
[1];
963 mOrientation
[1][2] = up
[2];
966 void SourceImpl::setOrientation(const ALfloat
*ori
)
968 CheckContext(mContext
);
971 if(mContext
->hasExtension(EXT_BFORMAT
))
972 alSourcefv(mId
, AL_ORIENTATION
, ori
);
973 alSourcefv(mId
, AL_DIRECTION
, ori
);
975 mDirection
[0] = mOrientation
[0][0] = ori
[0];
976 mDirection
[1] = mOrientation
[0][1] = ori
[1];
977 mDirection
[2] = mOrientation
[0][2] = ori
[2];
978 mOrientation
[1][0] = ori
[3];
979 mOrientation
[1][1] = ori
[4];
980 mOrientation
[1][2] = ori
[5];
984 void SourceImpl::setConeAngles(ALfloat inner
, ALfloat outer
)
986 if(!(inner
>= 0.0f
&& outer
<= 360.0f
&& outer
>= inner
))
987 throw std::runtime_error("Cone angles out of range");
988 CheckContext(mContext
);
991 alSourcef(mId
, AL_CONE_INNER_ANGLE
, inner
);
992 alSourcef(mId
, AL_CONE_OUTER_ANGLE
, outer
);
994 mConeInnerAngle
= inner
;
995 mConeOuterAngle
= outer
;
998 void SourceImpl::setOuterConeGains(ALfloat gain
, ALfloat gainhf
)
1000 if(!(gain
>= 0.0f
&& gain
<= 1.0f
&& gainhf
>= 0.0f
&& gainhf
<= 1.0f
))
1001 throw std::runtime_error("Outer cone gain out of range");
1002 CheckContext(mContext
);
1005 alSourcef(mId
, AL_CONE_OUTER_GAIN
, gain
);
1006 if(mContext
->hasExtension(EXT_EFX
))
1007 alSourcef(mId
, AL_CONE_OUTER_GAINHF
, gainhf
);
1009 mConeOuterGain
= gain
;
1010 mConeOuterGainHF
= gainhf
;
1014 void SourceImpl::setRolloffFactors(ALfloat factor
, ALfloat roomfactor
)
1016 if(!(factor
>= 0.0f
&& roomfactor
>= 0.0f
))
1017 throw std::runtime_error("Rolloff factor out of range");
1018 CheckContext(mContext
);
1021 alSourcef(mId
, AL_ROLLOFF_FACTOR
, factor
);
1022 if(mContext
->hasExtension(EXT_EFX
))
1023 alSourcef(mId
, AL_ROOM_ROLLOFF_FACTOR
, roomfactor
);
1025 mRolloffFactor
= factor
;
1026 mRoomRolloffFactor
= roomfactor
;
1029 void SourceImpl::setDopplerFactor(ALfloat factor
)
1031 if(!(factor
>= 0.0f
&& factor
<= 1.0f
))
1032 throw std::runtime_error("Doppler factor out of range");
1033 CheckContext(mContext
);
1035 alSourcef(mId
, AL_DOPPLER_FACTOR
, factor
);
1036 mDopplerFactor
= factor
;
1039 void SourceImpl::setAirAbsorptionFactor(ALfloat factor
)
1041 if(!(factor
>= 0.0f
&& factor
<= 10.0f
))
1042 throw std::runtime_error("Absorption factor out of range");
1043 CheckContext(mContext
);
1044 if(mId
!= 0 && mContext
->hasExtension(EXT_EFX
))
1045 alSourcef(mId
, AL_AIR_ABSORPTION_FACTOR
, factor
);
1046 mAirAbsorptionFactor
= factor
;
1049 void SourceImpl::setRadius(ALfloat radius
)
1051 if(!(mRadius
>= 0.0f
))
1052 throw std::runtime_error("Radius out of range");
1053 CheckContext(mContext
);
1054 if(mId
!= 0 && mContext
->hasExtension(EXT_SOURCE_RADIUS
))
1055 alSourcef(mId
, AL_SOURCE_RADIUS
, radius
);
1059 void SourceImpl::setStereoAngles(ALfloat leftAngle
, ALfloat rightAngle
)
1061 CheckContext(mContext
);
1062 if(mId
!= 0 && mContext
->hasExtension(EXT_STEREO_ANGLES
))
1064 ALfloat angles
[2] = { leftAngle
, rightAngle
};
1065 alSourcefv(mId
, AL_STEREO_ANGLES
, angles
);
1067 mStereoAngles
[0] = leftAngle
;
1068 mStereoAngles
[1] = rightAngle
;
1071 void SourceImpl::set3DSpatialize(Spatialize spatialize
)
1073 CheckContext(mContext
);
1074 if(mId
!= 0 && mContext
->hasExtension(SOFT_source_spatialize
))
1075 alSourcei(mId
, AL_SOURCE_SPATIALIZE_SOFT
, (ALint
)spatialize
);
1076 mSpatialize
= spatialize
;
1079 void SourceImpl::setResamplerIndex(ALsizei index
)
1082 throw std::runtime_error("Resampler index out of range");
1083 index
= std::min
<ALsizei
>(index
, mContext
->getAvailableResamplers().size());
1084 if(mId
!= 0 && mContext
->hasExtension(SOFT_source_resampler
))
1085 alSourcei(mId
, AL_SOURCE_RESAMPLER_SOFT
, index
);
1089 void SourceImpl::setRelative(bool relative
)
1091 CheckContext(mContext
);
1093 alSourcei(mId
, AL_SOURCE_RELATIVE
, relative
? AL_TRUE
: AL_FALSE
);
1094 mRelative
= relative
;
1097 void SourceImpl::setGainAuto(bool directhf
, bool send
, bool sendhf
)
1099 CheckContext(mContext
);
1100 if(mId
!= 0 && mContext
->hasExtension(EXT_EFX
))
1102 alSourcei(mId
, AL_DIRECT_FILTER_GAINHF_AUTO
, directhf
? AL_TRUE
: AL_FALSE
);
1103 alSourcei(mId
, AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
, send
? AL_TRUE
: AL_FALSE
);
1104 alSourcei(mId
, AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
, sendhf
? AL_TRUE
: AL_FALSE
);
1106 mDryGainHFAuto
= directhf
;
1107 mWetGainAuto
= send
;
1108 mWetGainHFAuto
= sendhf
;
1112 void SourceImpl::setFilterParams(ALuint
&filterid
, const FilterParams
¶ms
)
1114 if(!mContext
->hasExtension(EXT_EFX
))
1117 if(!(params
.mGain
< 1.0f
|| params
.mGainHF
< 1.0f
|| params
.mGainLF
< 1.0f
))
1120 mContext
->alFilteri(filterid
, AL_FILTER_TYPE
, AL_FILTER_NULL
);
1127 mContext
->alGenFilters(1, &filterid
);
1128 if(alGetError() != AL_NO_ERROR
)
1129 throw std::runtime_error("Failed to create Filter");
1131 bool filterset
= false;
1132 if(params
.mGainHF
< 1.0f
&& params
.mGainLF
< 1.0f
)
1134 mContext
->alFilteri(filterid
, AL_FILTER_TYPE
, AL_FILTER_BANDPASS
);
1135 if(alGetError() == AL_NO_ERROR
)
1137 mContext
->alFilterf(filterid
, AL_BANDPASS_GAIN
, std::min(params
.mGain
, 1.0f
));
1138 mContext
->alFilterf(filterid
, AL_BANDPASS_GAINHF
, std::min(params
.mGainHF
, 1.0f
));
1139 mContext
->alFilterf(filterid
, AL_BANDPASS_GAINLF
, std::min(params
.mGainLF
, 1.0f
));
1143 if(!filterset
&& !(params
.mGainHF
< 1.0f
) && params
.mGainLF
< 1.0f
)
1145 mContext
->alFilteri(filterid
, AL_FILTER_TYPE
, AL_FILTER_HIGHPASS
);
1146 if(alGetError() == AL_NO_ERROR
)
1148 mContext
->alFilterf(filterid
, AL_HIGHPASS_GAIN
, std::min(params
.mGain
, 1.0f
));
1149 mContext
->alFilterf(filterid
, AL_HIGHPASS_GAINLF
, std::min(params
.mGainLF
, 1.0f
));
1155 mContext
->alFilteri(filterid
, AL_FILTER_TYPE
, AL_FILTER_LOWPASS
);
1156 if(alGetError() == AL_NO_ERROR
)
1158 mContext
->alFilterf(filterid
, AL_LOWPASS_GAIN
, std::min(params
.mGain
, 1.0f
));
1159 mContext
->alFilterf(filterid
, AL_LOWPASS_GAINHF
, std::min(params
.mGainHF
, 1.0f
));
1166 void SourceImpl::setDirectFilter(const FilterParams
&filter
)
1168 if(!(filter
.mGain
>= 0.0f
&& filter
.mGainHF
>= 0.0f
&& filter
.mGainLF
>= 0.0f
))
1169 throw std::runtime_error("Gain value out of range");
1170 CheckContext(mContext
);
1172 setFilterParams(mDirectFilter
, filter
);
1174 alSourcei(mId
, AL_DIRECT_FILTER
, mDirectFilter
);
1177 void SourceImpl::setSendFilter(ALuint send
, const FilterParams
&filter
)
1179 if(!(filter
.mGain
>= 0.0f
&& filter
.mGainHF
>= 0.0f
&& filter
.mGainLF
>= 0.0f
))
1180 throw std::runtime_error("Gain value out of range");
1181 CheckContext(mContext
);
1183 SendPropMap::iterator siter
= mEffectSlots
.find(send
);
1184 if(siter
== mEffectSlots
.end())
1186 ALuint filterid
= 0;
1188 setFilterParams(filterid
, filter
);
1189 if(!filterid
) return;
1191 siter
= mEffectSlots
.insert(std::make_pair(send
, SendProps(filterid
))).first
;
1194 setFilterParams(siter
->second
.mFilter
, filter
);
1198 ALuint slotid
= (siter
->second
.mSlot
? siter
->second
.mSlot
->getId() : 0);
1199 alSource3i(mId
, AL_AUXILIARY_SEND_FILTER
, slotid
, send
, siter
->second
.mFilter
);
1203 void SourceImpl::setAuxiliarySend(AuxiliaryEffectSlot auxslot
, ALuint send
)
1205 AuxiliaryEffectSlotImpl
*slot
= auxslot
.getHandle();
1206 if(slot
) CheckContext(slot
->getContext());
1207 CheckContext(mContext
);
1209 SendPropMap::iterator siter
= mEffectSlots
.find(send
);
1210 if(siter
== mEffectSlots
.end())
1213 slot
->addSourceSend(Source(this), send
);
1214 siter
= mEffectSlots
.insert(std::make_pair(send
, SendProps(slot
))).first
;
1216 else if(siter
->second
.mSlot
!= slot
)
1218 if(slot
) slot
->addSourceSend(Source(this), send
);
1219 if(siter
->second
.mSlot
)
1220 siter
->second
.mSlot
->removeSourceSend(Source(this), send
);
1221 siter
->second
.mSlot
= slot
;
1226 ALuint slotid
= (siter
->second
.mSlot
? siter
->second
.mSlot
->getId() : 0);
1227 alSource3i(mId
, AL_AUXILIARY_SEND_FILTER
, slotid
, send
, siter
->second
.mFilter
);
1231 void SourceImpl::setAuxiliarySendFilter(AuxiliaryEffectSlot auxslot
, ALuint send
, const FilterParams
&filter
)
1233 if(!(filter
.mGain
>= 0.0f
&& filter
.mGainHF
>= 0.0f
&& filter
.mGainLF
>= 0.0f
))
1234 throw std::runtime_error("Gain value out of range");
1235 AuxiliaryEffectSlotImpl
*slot
= auxslot
.getHandle();
1236 if(slot
) CheckContext(slot
->getContext());
1237 CheckContext(mContext
);
1239 SendPropMap::iterator siter
= mEffectSlots
.find(send
);
1240 if(siter
== mEffectSlots
.end())
1242 ALuint filterid
= 0;
1244 setFilterParams(filterid
, filter
);
1245 if(!filterid
&& !slot
)
1248 if(slot
) slot
->addSourceSend(Source(this), send
);
1249 siter
= mEffectSlots
.insert(std::make_pair(send
, SendProps(slot
, filterid
))).first
;
1253 if(siter
->second
.mSlot
!= slot
)
1255 if(slot
) slot
->addSourceSend(Source(this), send
);
1256 if(siter
->second
.mSlot
)
1257 siter
->second
.mSlot
->removeSourceSend(Source(this), send
);
1258 siter
->second
.mSlot
= slot
;
1260 setFilterParams(siter
->second
.mFilter
, filter
);
1265 ALuint slotid
= (siter
->second
.mSlot
? siter
->second
.mSlot
->getId() : 0);
1266 alSource3i(mId
, AL_AUXILIARY_SEND_FILTER
, slotid
, send
, siter
->second
.mFilter
);
1271 void SourceImpl::release()
1276 mContext
->alDeleteFilters(1, &mDirectFilter
);
1277 mDirectFilter
= AL_FILTER_NULL
;
1279 for(auto &i
: mEffectSlots
)
1282 i
.second
.mSlot
->removeSourceSend(Source(this), i
.first
);
1283 if(i
.second
.mFilter
)
1284 mContext
->alDeleteFilters(1, &i
.second
.mFilter
);
1286 mEffectSlots
.clear();
1292 // Need to use these to avoid extraneous commas in macro parameter lists
1293 using UInt64NSecPair
= std::pair
<uint64_t,std::chrono::nanoseconds
>;
1294 using SecondsPair
= std::pair
<Seconds
,Seconds
>;
1295 using ALfloatPair
= std::pair
<ALfloat
,ALfloat
>;
1296 using Vector3Pair
= std::pair
<Vector3
,Vector3
>;
1297 using BoolTriple
= std::tuple
<bool,bool,bool>;
1299 DECL_THUNK1(void, Source
, play
,, Buffer
)
1300 DECL_THUNK3(void, Source
, play
,, SharedPtr
<Decoder
>, ALuint
, ALuint
)
1301 DECL_THUNK0(void, Source
, stop
,)
1302 DECL_THUNK0(void, Source
, pause
,)
1303 DECL_THUNK0(void, Source
, resume
,)
1304 DECL_THUNK0(bool, Source
, isPlaying
, const)
1305 DECL_THUNK0(bool, Source
, isPaused
, const)
1306 DECL_THUNK1(void, Source
, setPriority
,, ALuint
)
1307 DECL_THUNK0(ALuint
, Source
, getPriority
, const)
1308 DECL_THUNK1(void, Source
, setOffset
,, uint64_t)
1309 DECL_THUNK0(UInt64NSecPair
, Source
, getSampleOffsetLatency
, const)
1310 DECL_THUNK0(SecondsPair
, Source
, getSecOffsetLatency
, const)
1311 DECL_THUNK1(void, Source
, setLooping
,, bool)
1312 DECL_THUNK0(bool, Source
, getLooping
, const)
1313 DECL_THUNK1(void, Source
, setPitch
,, ALfloat
)
1314 DECL_THUNK0(ALfloat
, Source
, getPitch
, const)
1315 DECL_THUNK1(void, Source
, setGain
,, ALfloat
)
1316 DECL_THUNK0(ALfloat
, Source
, getGain
, const)
1317 DECL_THUNK2(void, Source
, setGainRange
,, ALfloat
, ALfloat
)
1318 DECL_THUNK0(ALfloatPair
, Source
, getGainRange
, const)
1319 DECL_THUNK2(void, Source
, setDistanceRange
,, ALfloat
, ALfloat
)
1320 DECL_THUNK0(ALfloatPair
, Source
, getDistanceRange
, const)
1321 DECL_THUNK3(void, Source
, set3DParameters
,, const Vector3
&, const Vector3
&, const Vector3
&)
1322 DECL_THUNK3(void, Source
, set3DParameters
,, const Vector3
&, const Vector3
&, Vector3Pair
)
1323 DECL_THUNK3(void, Source
, setPosition
,, ALfloat
, ALfloat
, ALfloat
)
1324 DECL_THUNK1(void, Source
, setPosition
,, const ALfloat
*)
1325 DECL_THUNK0(Vector3
, Source
, getPosition
, const)
1326 DECL_THUNK3(void, Source
, setVelocity
,, ALfloat
, ALfloat
, ALfloat
)
1327 DECL_THUNK1(void, Source
, setVelocity
,, const ALfloat
*)
1328 DECL_THUNK0(Vector3
, Source
, getVelocity
, const)
1329 DECL_THUNK3(void, Source
, setDirection
,, ALfloat
, ALfloat
, ALfloat
)
1330 DECL_THUNK1(void, Source
, setDirection
,, const ALfloat
*)
1331 DECL_THUNK0(Vector3
, Source
, getDirection
, const)
1332 DECL_THUNK6(void, Source
, setOrientation
,, ALfloat
, ALfloat
, ALfloat
, ALfloat
, ALfloat
, ALfloat
)
1333 DECL_THUNK2(void, Source
, setOrientation
,, const ALfloat
*, const ALfloat
*)
1334 DECL_THUNK1(void, Source
, setOrientation
,, const ALfloat
*)
1335 DECL_THUNK0(Vector3Pair
, Source
, getOrientation
, const)
1336 DECL_THUNK2(void, Source
, setConeAngles
,, ALfloat
, ALfloat
)
1337 DECL_THUNK0(ALfloatPair
, Source
, getConeAngles
, const)
1338 DECL_THUNK2(void, Source
, setOuterConeGains
,, ALfloat
, ALfloat
)
1339 DECL_THUNK0(ALfloatPair
, Source
, getOuterConeGains
, const)
1340 DECL_THUNK2(void, Source
, setRolloffFactors
,, ALfloat
, ALfloat
)
1341 DECL_THUNK0(ALfloatPair
, Source
, getRolloffFactors
, const)
1342 DECL_THUNK1(void, Source
, setDopplerFactor
,, ALfloat
)
1343 DECL_THUNK0(ALfloat
, Source
, getDopplerFactor
, const)
1344 DECL_THUNK1(void, Source
, setRelative
,, bool)
1345 DECL_THUNK0(bool, Source
, getRelative
, const)
1346 DECL_THUNK1(void, Source
, setRadius
,, ALfloat
)
1347 DECL_THUNK0(ALfloat
, Source
, getRadius
, const)
1348 DECL_THUNK2(void, Source
, setStereoAngles
,, ALfloat
, ALfloat
)
1349 DECL_THUNK0(ALfloatPair
, Source
, getStereoAngles
, const)
1350 DECL_THUNK1(void, Source
, set3DSpatialize
,, Spatialize
)
1351 DECL_THUNK0(Spatialize
, Source
, get3DSpatialize
, const)
1352 DECL_THUNK1(void, Source
, setResamplerIndex
,, ALsizei
)
1353 DECL_THUNK0(ALsizei
, Source
, getResamplerIndex
, const)
1354 DECL_THUNK1(void, Source
, setAirAbsorptionFactor
,, ALfloat
)
1355 DECL_THUNK0(ALfloat
, Source
, getAirAbsorptionFactor
, const)
1356 DECL_THUNK3(void, Source
, setGainAuto
,, bool, bool, bool)
1357 DECL_THUNK0(BoolTriple
, Source
, getGainAuto
, const)
1358 DECL_THUNK1(void, Source
, setDirectFilter
,, const FilterParams
&)
1359 DECL_THUNK2(void, Source
, setSendFilter
,, ALuint
, const FilterParams
&)
1360 DECL_THUNK2(void, Source
, setAuxiliarySend
,, AuxiliaryEffectSlot
, ALuint
)
1361 DECL_THUNK3(void, Source
, setAuxiliarySendFilter
,, AuxiliaryEffectSlot
, ALuint
, const FilterParams
&)
1362 void Source::release()