2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
34 #include "alAuxEffectSlot.h"
37 enum Resampler DefaultResampler
= LinearResampler
;
38 const ALsizei ResamplerPadding
[ResamplerMax
] = {
43 const ALsizei ResamplerPrePadding
[ResamplerMax
] = {
50 static ALvoid
InitSourceParams(ALsource
*Source
);
51 static ALint64
GetSourceOffset(const ALsource
*Source
);
52 static ALdouble
GetSourceSecOffset(const ALsource
*Source
);
53 static ALvoid
GetSourceOffsets(const ALsource
*Source
, ALenum name
, ALdouble
*offsets
, ALdouble updateLen
);
54 static ALint
GetSampleOffset(ALsource
*Source
);
56 static ALenum
SetSourcefv(ALsource
*Source
, ALCcontext
*Context
, ALenum name
, const ALfloat
*values
);
57 static ALenum
SetSourceiv(ALsource
*Source
, ALCcontext
*Context
, ALenum name
, const ALint
*values
);
59 static ALenum
GetSourcedv(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALdouble
*values
);
60 static ALenum
GetSourceiv(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALint
*values
);
61 static ALenum
GetSourcei64v(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALint64
*values
);
64 #define RETERR(x) do { \
65 alSetError(Context, (x)); \
69 #define CHECKVAL(x) do { \
71 RETERR(AL_INVALID_VALUE); \
74 static ALenum
SetSourcefv(ALsource
*Source
, ALCcontext
*Context
, ALenum name
, const ALfloat
*values
)
79 CHECKVAL(*values
>= 0.0f
);
81 Source
->Pitch
= *values
;
82 Source
->NeedsUpdate
= AL_TRUE
;
85 case AL_CONE_INNER_ANGLE
:
86 CHECKVAL(*values
>= 0.0f
&& *values
<= 360.0f
);
88 Source
->InnerAngle
= *values
;
89 Source
->NeedsUpdate
= AL_TRUE
;
92 case AL_CONE_OUTER_ANGLE
:
93 CHECKVAL(*values
>= 0.0f
&& *values
<= 360.0f
);
95 Source
->OuterAngle
= *values
;
96 Source
->NeedsUpdate
= AL_TRUE
;
100 CHECKVAL(*values
>= 0.0f
);
102 Source
->Gain
= *values
;
103 Source
->NeedsUpdate
= AL_TRUE
;
106 case AL_MAX_DISTANCE
:
107 CHECKVAL(*values
>= 0.0f
);
109 Source
->MaxDistance
= *values
;
110 Source
->NeedsUpdate
= AL_TRUE
;
113 case AL_ROLLOFF_FACTOR
:
114 CHECKVAL(*values
>= 0.0f
);
116 Source
->RollOffFactor
= *values
;
117 Source
->NeedsUpdate
= AL_TRUE
;
120 case AL_REFERENCE_DISTANCE
:
121 CHECKVAL(*values
>= 0.0f
);
123 Source
->RefDistance
= *values
;
124 Source
->NeedsUpdate
= AL_TRUE
;
128 CHECKVAL(*values
>= 0.0f
&& *values
<= 1.0f
);
130 Source
->MinGain
= *values
;
131 Source
->NeedsUpdate
= AL_TRUE
;
135 CHECKVAL(*values
>= 0.0f
&& *values
<= 1.0f
);
137 Source
->MaxGain
= *values
;
138 Source
->NeedsUpdate
= AL_TRUE
;
141 case AL_CONE_OUTER_GAIN
:
142 CHECKVAL(*values
>= 0.0f
&& *values
<= 1.0f
);
144 Source
->OuterGain
= *values
;
145 Source
->NeedsUpdate
= AL_TRUE
;
148 case AL_CONE_OUTER_GAINHF
:
149 CHECKVAL(*values
>= 0.0f
&& *values
<= 1.0f
);
151 Source
->OuterGainHF
= *values
;
152 Source
->NeedsUpdate
= AL_TRUE
;
155 case AL_AIR_ABSORPTION_FACTOR
:
156 CHECKVAL(*values
>= 0.0f
&& *values
<= 10.0f
);
158 Source
->AirAbsorptionFactor
= *values
;
159 Source
->NeedsUpdate
= AL_TRUE
;
162 case AL_ROOM_ROLLOFF_FACTOR
:
163 CHECKVAL(*values
>= 0.0f
&& *values
<= 10.0f
);
165 Source
->RoomRolloffFactor
= *values
;
166 Source
->NeedsUpdate
= AL_TRUE
;
169 case AL_DOPPLER_FACTOR
:
170 CHECKVAL(*values
>= 0.0f
&& *values
<= 1.0f
);
172 Source
->DopplerFactor
= *values
;
173 Source
->NeedsUpdate
= AL_TRUE
;
177 case AL_SAMPLE_OFFSET
:
179 CHECKVAL(*values
>= 0.0f
);
181 LockContext(Context
);
182 Source
->OffsetType
= name
;
183 Source
->Offset
= *values
;
185 if((Source
->state
== AL_PLAYING
|| Source
->state
== AL_PAUSED
) &&
186 !Context
->DeferUpdates
)
188 if(ApplyOffset(Source
) == AL_FALSE
)
190 UnlockContext(Context
);
191 RETERR(AL_INVALID_VALUE
);
194 UnlockContext(Context
);
199 CHECKVAL(isfinite(values
[0]) && isfinite(values
[1]) && isfinite(values
[2]));
201 LockContext(Context
);
202 Source
->Position
[0] = values
[0];
203 Source
->Position
[1] = values
[1];
204 Source
->Position
[2] = values
[2];
205 UnlockContext(Context
);
206 Source
->NeedsUpdate
= AL_TRUE
;
210 CHECKVAL(isfinite(values
[0]) && isfinite(values
[1]) && isfinite(values
[2]));
212 LockContext(Context
);
213 Source
->Velocity
[0] = values
[0];
214 Source
->Velocity
[1] = values
[1];
215 Source
->Velocity
[2] = values
[2];
216 UnlockContext(Context
);
217 Source
->NeedsUpdate
= AL_TRUE
;
221 CHECKVAL(isfinite(values
[0]) && isfinite(values
[1]) && isfinite(values
[2]));
223 LockContext(Context
);
224 Source
->Orientation
[0] = values
[0];
225 Source
->Orientation
[1] = values
[1];
226 Source
->Orientation
[2] = values
[2];
227 UnlockContext(Context
);
228 Source
->NeedsUpdate
= AL_TRUE
;
233 RETERR(AL_INVALID_ENUM
);
239 static ALenum
SetSourceiv(ALsource
*Source
, ALCcontext
*Context
, ALenum name
, const ALint
*values
)
241 ALCdevice
*device
= Context
->Device
;
242 ALbuffer
*buffer
= NULL
;
243 ALfilter
*filter
= NULL
;
244 ALeffectslot
*slot
= NULL
;
245 ALbufferlistitem
*oldlist
;
251 case AL_SOURCE_RELATIVE
:
252 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
254 Source
->HeadRelative
= (ALboolean
)*values
;
255 Source
->NeedsUpdate
= AL_TRUE
;
259 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
261 Source
->Looping
= (ALboolean
)*values
;
265 CHECKVAL(*values
== 0 || (buffer
=LookupBuffer(device
, *values
)) != NULL
);
267 LockContext(Context
);
268 if(!(Source
->state
== AL_STOPPED
|| Source
->state
== AL_INITIAL
))
270 UnlockContext(Context
);
271 RETERR(AL_INVALID_OPERATION
);
274 Source
->BuffersInQueue
= 0;
275 Source
->BuffersPlayed
= 0;
279 ALbufferlistitem
*BufferListItem
;
281 /* Source is now Static */
282 Source
->SourceType
= AL_STATIC
;
284 /* Add the selected buffer to a one-item queue */
285 BufferListItem
= malloc(sizeof(ALbufferlistitem
));
286 BufferListItem
->buffer
= buffer
;
287 BufferListItem
->next
= NULL
;
288 BufferListItem
->prev
= NULL
;
289 IncrementRef(&buffer
->ref
);
291 oldlist
= ExchangePtr((XchgPtr
*)&Source
->queue
, BufferListItem
);
292 Source
->BuffersInQueue
= 1;
294 ReadLock(&buffer
->lock
);
295 Source
->NumChannels
= ChannelsFromFmt(buffer
->FmtChannels
);
296 Source
->SampleSize
= BytesFromFmt(buffer
->FmtType
);
297 ReadUnlock(&buffer
->lock
);
298 if(buffer
->FmtChannels
== FmtMono
)
299 Source
->Update
= CalcSourceParams
;
301 Source
->Update
= CalcNonAttnSourceParams
;
302 Source
->NeedsUpdate
= AL_TRUE
;
306 /* Source is now Undetermined */
307 Source
->SourceType
= AL_UNDETERMINED
;
308 oldlist
= ExchangePtr((XchgPtr
*)&Source
->queue
, NULL
);
311 /* Delete all elements in the previous queue */
312 while(oldlist
!= NULL
)
314 ALbufferlistitem
*temp
= oldlist
;
315 oldlist
= temp
->next
;
318 DecrementRef(&temp
->buffer
->ref
);
321 UnlockContext(Context
);
324 case AL_SOURCE_STATE
:
326 RETERR(AL_INVALID_OPERATION
);
329 case AL_SAMPLE_OFFSET
:
331 CHECKVAL(*values
>= 0);
333 LockContext(Context
);
334 Source
->OffsetType
= name
;
335 Source
->Offset
= *values
;
337 if((Source
->state
== AL_PLAYING
|| Source
->state
== AL_PAUSED
) &&
338 !Context
->DeferUpdates
)
340 if(ApplyOffset(Source
) == AL_FALSE
)
342 UnlockContext(Context
);
343 RETERR(AL_INVALID_VALUE
);
346 UnlockContext(Context
);
349 case AL_DIRECT_FILTER
:
350 CHECKVAL(*values
== 0 || (filter
=LookupFilter(device
, *values
)) != NULL
);
352 LockContext(Context
);
355 Source
->DirectGain
= 1.0f
;
356 Source
->DirectGainHF
= 1.0f
;
360 Source
->DirectGain
= filter
->Gain
;
361 Source
->DirectGainHF
= filter
->GainHF
;
363 UnlockContext(Context
);
364 Source
->NeedsUpdate
= AL_TRUE
;
367 case AL_DIRECT_FILTER_GAINHF_AUTO
:
368 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
370 Source
->DryGainHFAuto
= *values
;
371 Source
->NeedsUpdate
= AL_TRUE
;
374 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
375 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
377 Source
->WetGainAuto
= *values
;
378 Source
->NeedsUpdate
= AL_TRUE
;
381 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
382 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
384 Source
->WetGainHFAuto
= *values
;
385 Source
->NeedsUpdate
= AL_TRUE
;
388 case AL_DIRECT_CHANNELS_SOFT
:
389 CHECKVAL(*values
== AL_FALSE
|| *values
== AL_TRUE
);
391 Source
->DirectChannels
= *values
;
392 Source
->NeedsUpdate
= AL_TRUE
;
395 case AL_DISTANCE_MODEL
:
396 CHECKVAL(*values
== AL_NONE
||
397 *values
== AL_INVERSE_DISTANCE
||
398 *values
== AL_INVERSE_DISTANCE_CLAMPED
||
399 *values
== AL_LINEAR_DISTANCE
||
400 *values
== AL_LINEAR_DISTANCE_CLAMPED
||
401 *values
== AL_EXPONENT_DISTANCE
||
402 *values
== AL_EXPONENT_DISTANCE_CLAMPED
);
404 Source
->DistanceModel
= *values
;
405 if(Context
->SourceDistanceModel
)
406 Source
->NeedsUpdate
= AL_TRUE
;
410 case AL_AUXILIARY_SEND_FILTER
:
411 LockContext(Context
);
412 if(!((ALuint
)values
[1] < device
->NumAuxSends
&&
413 (values
[0] == 0 || (slot
=LookupEffectSlot(Context
, values
[0])) != NULL
) &&
414 (values
[2] == 0 || (filter
=LookupFilter(device
, values
[2])) != NULL
)))
416 UnlockContext(Context
);
417 RETERR(AL_INVALID_VALUE
);
420 /* Add refcount on the new slot, and release the previous slot */
421 if(slot
) IncrementRef(&slot
->ref
);
422 slot
= ExchangePtr((XchgPtr
*)&Source
->Send
[values
[1]].Slot
, slot
);
423 if(slot
) DecrementRef(&slot
->ref
);
428 Source
->Send
[values
[1]].Gain
= 1.0f
;
429 Source
->Send
[values
[1]].GainHF
= 1.0f
;
433 Source
->Send
[values
[1]].Gain
= filter
->Gain
;
434 Source
->Send
[values
[1]].GainHF
= filter
->GainHF
;
436 Source
->NeedsUpdate
= AL_TRUE
;
437 UnlockContext(Context
);
441 case AL_MAX_DISTANCE
:
442 case AL_ROLLOFF_FACTOR
:
443 case AL_CONE_INNER_ANGLE
:
444 case AL_CONE_OUTER_ANGLE
:
445 case AL_REFERENCE_DISTANCE
:
446 fvals
[0] = (ALfloat
)*values
;
447 if((err
=SetSourcefv(Source
, Context
, name
, fvals
)) != AL_NO_ERROR
)
454 fvals
[0] = (ALfloat
)values
[0];
455 fvals
[1] = (ALfloat
)values
[1];
456 fvals
[2] = (ALfloat
)values
[2];
457 if((err
=SetSourcefv(Source
, Context
, name
, fvals
)) != AL_NO_ERROR
)
462 return AL_INVALID_ENUM
;
471 static ALenum
GetSourcedv(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALdouble
*values
)
480 case AL_MAX_DISTANCE
:
481 *values
= Source
->MaxDistance
;
484 case AL_ROLLOFF_FACTOR
:
485 *values
= Source
->RollOffFactor
;
488 case AL_REFERENCE_DISTANCE
:
489 *values
= Source
->RefDistance
;
492 case AL_CONE_INNER_ANGLE
:
493 *values
= Source
->InnerAngle
;
496 case AL_CONE_OUTER_ANGLE
:
497 *values
= Source
->OuterAngle
;
501 case AL_SAMPLE_OFFSET
:
503 LockContext(Context
);
504 updateLen
= (ALdouble
)Context
->Device
->UpdateSize
/
505 Context
->Device
->Frequency
;
506 GetSourceOffsets(Source
, name
, offsets
, updateLen
);
507 UnlockContext(Context
);
508 *values
= offsets
[0];
511 case AL_DOPPLER_FACTOR
:
512 *values
= Source
->DopplerFactor
;
515 case AL_SAMPLE_RW_OFFSETS_SOFT
:
516 case AL_BYTE_RW_OFFSETS_SOFT
:
517 LockContext(Context
);
518 updateLen
= (ALdouble
)Context
->Device
->UpdateSize
/
519 Context
->Device
->Frequency
;
520 GetSourceOffsets(Source
, name
, values
, updateLen
);
521 UnlockContext(Context
);
524 case AL_SEC_OFFSET_LATENCY_SOFT
:
525 LockContext(Context
);
526 values
[0] = GetSourceSecOffset(Source
);
527 values
[1] = (ALdouble
)ALCdevice_GetLatency(Context
->Device
) /
529 UnlockContext(Context
);
533 LockContext(Context
);
534 values
[0] = Source
->Position
[0];
535 values
[1] = Source
->Position
[1];
536 values
[2] = Source
->Position
[2];
537 UnlockContext(Context
);
541 LockContext(Context
);
542 values
[0] = Source
->Velocity
[0];
543 values
[1] = Source
->Velocity
[1];
544 values
[2] = Source
->Velocity
[2];
545 UnlockContext(Context
);
549 LockContext(Context
);
550 values
[0] = Source
->Orientation
[0];
551 values
[1] = Source
->Orientation
[1];
552 values
[2] = Source
->Orientation
[2];
553 UnlockContext(Context
);
556 case AL_SOURCE_RELATIVE
:
559 case AL_SOURCE_STATE
:
560 case AL_BUFFERS_QUEUED
:
561 case AL_BUFFERS_PROCESSED
:
563 case AL_DIRECT_FILTER_GAINHF_AUTO
:
564 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
565 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
566 case AL_DIRECT_CHANNELS_SOFT
:
567 case AL_DISTANCE_MODEL
:
568 if((err
=GetSourceiv(Source
, Context
, name
, ivals
)) != AL_NO_ERROR
)
570 *values
= (ALdouble
)ivals
[0];
574 RETERR(AL_INVALID_ENUM
);
580 static ALenum
GetSourceiv(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALint
*values
)
582 ALbufferlistitem
*BufferList
;
588 case AL_SOURCE_RELATIVE
:
589 *values
= Source
->HeadRelative
;
593 *values
= Source
->Looping
;
597 LockContext(Context
);
598 BufferList
= Source
->queue
;
599 if(Source
->SourceType
!= AL_STATIC
)
601 ALuint i
= Source
->BuffersPlayed
;
604 BufferList
= BufferList
->next
;
608 *values
= ((BufferList
&& BufferList
->buffer
) ?
609 BufferList
->buffer
->id
: 0);
610 UnlockContext(Context
);
613 case AL_SOURCE_STATE
:
614 *values
= Source
->state
;
617 case AL_BUFFERS_QUEUED
:
618 *values
= Source
->BuffersInQueue
;
621 case AL_BUFFERS_PROCESSED
:
622 LockContext(Context
);
623 if(Source
->Looping
|| Source
->SourceType
!= AL_STREAMING
)
625 /* Buffers on a looping source are in a perpetual state of
626 * PENDING, so don't report any as PROCESSED */
630 *values
= Source
->BuffersPlayed
;
631 UnlockContext(Context
);
635 *values
= Source
->SourceType
;
638 case AL_DIRECT_FILTER_GAINHF_AUTO
:
639 *values
= Source
->DryGainHFAuto
;
642 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
643 *values
= Source
->WetGainAuto
;
646 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
647 *values
= Source
->WetGainHFAuto
;
650 case AL_DIRECT_CHANNELS_SOFT
:
651 *values
= Source
->DirectChannels
;
654 case AL_DISTANCE_MODEL
:
655 *values
= Source
->DistanceModel
;
658 case AL_MAX_DISTANCE
:
659 case AL_ROLLOFF_FACTOR
:
660 case AL_REFERENCE_DISTANCE
:
661 case AL_CONE_INNER_ANGLE
:
662 case AL_CONE_OUTER_ANGLE
:
664 case AL_SAMPLE_OFFSET
:
666 case AL_DOPPLER_FACTOR
:
667 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
669 *values
= (ALint
)dvals
[0];
672 case AL_SAMPLE_RW_OFFSETS_SOFT
:
673 case AL_BYTE_RW_OFFSETS_SOFT
:
674 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
676 values
[0] = (ALint
)dvals
[0];
677 values
[1] = (ALint
)dvals
[0];
683 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
685 values
[0] = (ALint
)dvals
[0];
686 values
[1] = (ALint
)dvals
[1];
687 values
[2] = (ALint
)dvals
[2];
691 RETERR(AL_INVALID_ENUM
);
697 static ALenum
GetSourcei64v(const ALsource
*Source
, ALCcontext
*Context
, ALenum name
, ALint64
*values
)
705 case AL_SAMPLE_OFFSET_LATENCY_SOFT
:
706 LockContext(Context
);
707 values
[0] = GetSourceOffset(Source
);
708 values
[1] = ALCdevice_GetLatency(Context
->Device
);
709 UnlockContext(Context
);
712 case AL_MAX_DISTANCE
:
713 case AL_ROLLOFF_FACTOR
:
714 case AL_REFERENCE_DISTANCE
:
715 case AL_CONE_INNER_ANGLE
:
716 case AL_CONE_OUTER_ANGLE
:
718 case AL_SAMPLE_OFFSET
:
720 case AL_DOPPLER_FACTOR
:
721 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
723 *values
= (ALint64
)dvals
[0];
726 case AL_SAMPLE_RW_OFFSETS_SOFT
:
727 case AL_BYTE_RW_OFFSETS_SOFT
:
728 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
730 values
[0] = (ALint64
)dvals
[0];
731 values
[1] = (ALint64
)dvals
[0];
737 if((err
=GetSourcedv(Source
, Context
, name
, dvals
)) != AL_NO_ERROR
)
739 values
[0] = (ALint64
)dvals
[0];
740 values
[1] = (ALint64
)dvals
[1];
741 values
[2] = (ALint64
)dvals
[2];
744 case AL_SOURCE_RELATIVE
:
747 case AL_SOURCE_STATE
:
748 case AL_BUFFERS_QUEUED
:
749 case AL_BUFFERS_PROCESSED
:
751 case AL_DIRECT_FILTER_GAINHF_AUTO
:
752 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
753 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
754 case AL_DIRECT_CHANNELS_SOFT
:
755 case AL_DISTANCE_MODEL
:
756 if((err
=GetSourceiv(Source
, Context
, name
, ivals
)) != AL_NO_ERROR
)
762 RETERR(AL_INVALID_ENUM
);
771 AL_API ALvoid AL_APIENTRY
alGenSources(ALsizei n
, ALuint
*sources
)
776 Context
= GetContextRef();
783 CHECK_VALUE(Context
, n
>= 0);
784 for(cur
= 0;cur
< n
;cur
++)
786 ALsource
*source
= al_calloc(16, sizeof(ALsource
));
788 al_throwerr(Context
, AL_OUT_OF_MEMORY
);
789 InitSourceParams(source
);
791 err
= NewThunkEntry(&source
->id
);
792 if(err
== AL_NO_ERROR
)
793 err
= InsertUIntMapEntry(&Context
->SourceMap
, source
->id
, source
);
794 if(err
!= AL_NO_ERROR
)
796 FreeThunkEntry(source
->id
);
797 memset(source
, 0, sizeof(ALsource
));
800 al_throwerr(Context
, err
);
803 sources
[cur
] = source
->id
;
809 alDeleteSources(cur
, sources
);
813 ALCcontext_DecRef(Context
);
817 AL_API ALvoid AL_APIENTRY
alDeleteSources(ALsizei n
, const ALuint
*sources
)
821 Context
= GetContextRef();
826 ALbufferlistitem
*BufferList
;
830 CHECK_VALUE(Context
, n
>= 0);
832 /* Check that all Sources are valid */
835 if(LookupSource(Context
, sources
[i
]) == NULL
)
836 al_throwerr(Context
, AL_INVALID_NAME
);
841 ALsource
**srclist
, **srclistend
;
843 if((Source
=RemoveSource(Context
, sources
[i
])) == NULL
)
845 FreeThunkEntry(Source
->id
);
847 LockContext(Context
);
848 srclist
= Context
->ActiveSources
;
849 srclistend
= srclist
+ Context
->ActiveSourceCount
;
850 while(srclist
!= srclistend
)
852 if(*srclist
== Source
)
854 Context
->ActiveSourceCount
--;
855 *srclist
= *(--srclistend
);
860 UnlockContext(Context
);
862 while(Source
->queue
!= NULL
)
864 BufferList
= Source
->queue
;
865 Source
->queue
= BufferList
->next
;
867 if(BufferList
->buffer
!= NULL
)
868 DecrementRef(&BufferList
->buffer
->ref
);
872 for(j
= 0;j
< MAX_SENDS
;++j
)
874 if(Source
->Send
[j
].Slot
)
875 DecrementRef(&Source
->Send
[j
].Slot
->ref
);
876 Source
->Send
[j
].Slot
= NULL
;
879 memset(Source
, 0, sizeof(*Source
));
885 ALCcontext_DecRef(Context
);
889 AL_API ALboolean AL_APIENTRY
alIsSource(ALuint source
)
894 Context
= GetContextRef();
895 if(!Context
) return AL_FALSE
;
897 result
= (LookupSource(Context
, source
) ? AL_TRUE
: AL_FALSE
);
899 ALCcontext_DecRef(Context
);
905 AL_API ALvoid AL_APIENTRY
alSourcef(ALuint source
, ALenum param
, ALfloat value
)
910 Context
= GetContextRef();
913 if((Source
=LookupSource(Context
, source
)) == NULL
)
914 alSetError(Context
, AL_INVALID_NAME
);
918 case AL_CONE_INNER_ANGLE
:
919 case AL_CONE_OUTER_ANGLE
:
921 case AL_MAX_DISTANCE
:
922 case AL_ROLLOFF_FACTOR
:
923 case AL_REFERENCE_DISTANCE
:
926 case AL_CONE_OUTER_GAIN
:
927 case AL_CONE_OUTER_GAINHF
:
928 case AL_AIR_ABSORPTION_FACTOR
:
929 case AL_ROOM_ROLLOFF_FACTOR
:
930 case AL_DOPPLER_FACTOR
:
932 case AL_SAMPLE_OFFSET
:
934 SetSourcefv(Source
, Context
, param
, &value
);
938 alSetError(Context
, AL_INVALID_ENUM
);
941 ALCcontext_DecRef(Context
);
945 AL_API ALvoid AL_APIENTRY
alSource3f(ALuint source
, ALenum param
, ALfloat value1
, ALfloat value2
, ALfloat value3
)
951 Context
= GetContextRef();
954 if((Source
=LookupSource(Context
, source
)) == NULL
)
955 alSetError(Context
, AL_INVALID_NAME
);
964 SetSourcefv(Source
, Context
, param
, fvals
);
968 alSetError(Context
, AL_INVALID_ENUM
);
971 ALCcontext_DecRef(Context
);
975 AL_API ALvoid AL_APIENTRY
alSourcefv(ALuint source
, ALenum param
, const ALfloat
*values
)
980 Context
= GetContextRef();
983 if((Source
=LookupSource(Context
, source
)) == NULL
)
984 alSetError(Context
, AL_INVALID_NAME
);
986 alSetError(Context
, AL_INVALID_VALUE
);
990 case AL_CONE_INNER_ANGLE
:
991 case AL_CONE_OUTER_ANGLE
:
993 case AL_MAX_DISTANCE
:
994 case AL_ROLLOFF_FACTOR
:
995 case AL_REFERENCE_DISTANCE
:
998 case AL_CONE_OUTER_GAIN
:
999 case AL_CONE_OUTER_GAINHF
:
1001 case AL_SAMPLE_OFFSET
:
1002 case AL_BYTE_OFFSET
:
1003 case AL_AIR_ABSORPTION_FACTOR
:
1004 case AL_ROOM_ROLLOFF_FACTOR
:
1009 SetSourcefv(Source
, Context
, param
, values
);
1013 alSetError(Context
, AL_INVALID_ENUM
);
1016 ALCcontext_DecRef(Context
);
1020 AL_API ALvoid AL_APIENTRY
alSourcei(ALuint source
, ALenum param
, ALint value
)
1022 ALCcontext
*Context
;
1025 Context
= GetContextRef();
1026 if(!Context
) return;
1028 if((Source
=LookupSource(Context
, source
)) == NULL
)
1029 alSetError(Context
, AL_INVALID_NAME
);
1032 case AL_MAX_DISTANCE
:
1033 case AL_ROLLOFF_FACTOR
:
1034 case AL_CONE_INNER_ANGLE
:
1035 case AL_CONE_OUTER_ANGLE
:
1036 case AL_REFERENCE_DISTANCE
:
1037 case AL_SOURCE_RELATIVE
:
1040 case AL_SOURCE_STATE
:
1042 case AL_SAMPLE_OFFSET
:
1043 case AL_BYTE_OFFSET
:
1044 case AL_DIRECT_FILTER
:
1045 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1046 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1047 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1048 case AL_DIRECT_CHANNELS_SOFT
:
1049 case AL_DISTANCE_MODEL
:
1050 SetSourceiv(Source
, Context
, param
, &value
);
1054 alSetError(Context
, AL_INVALID_ENUM
);
1057 ALCcontext_DecRef(Context
);
1061 AL_API
void AL_APIENTRY
alSource3i(ALuint source
, ALenum param
, ALint value1
, ALint value2
, ALint value3
)
1063 ALCcontext
*Context
;
1067 Context
= GetContextRef();
1068 if(!Context
) return;
1070 if((Source
=LookupSource(Context
, source
)) == NULL
)
1071 alSetError(Context
, AL_INVALID_NAME
);
1077 case AL_AUXILIARY_SEND_FILTER
:
1081 SetSourceiv(Source
, Context
, param
, ivals
);
1085 alSetError(Context
, AL_INVALID_ENUM
);
1088 ALCcontext_DecRef(Context
);
1092 AL_API
void AL_APIENTRY
alSourceiv(ALuint source
, ALenum param
, const ALint
*values
)
1094 ALCcontext
*Context
;
1097 Context
= GetContextRef();
1098 if(!Context
) return;
1100 if((Source
=LookupSource(Context
, source
)) == NULL
)
1101 alSetError(Context
, AL_INVALID_NAME
);
1103 alSetError(Context
, AL_INVALID_VALUE
);
1106 case AL_SOURCE_RELATIVE
:
1107 case AL_CONE_INNER_ANGLE
:
1108 case AL_CONE_OUTER_ANGLE
:
1111 case AL_SOURCE_STATE
:
1113 case AL_SAMPLE_OFFSET
:
1114 case AL_BYTE_OFFSET
:
1115 case AL_MAX_DISTANCE
:
1116 case AL_ROLLOFF_FACTOR
:
1117 case AL_REFERENCE_DISTANCE
:
1118 case AL_DIRECT_FILTER
:
1119 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1120 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1121 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1122 case AL_DISTANCE_MODEL
:
1123 case AL_DIRECT_CHANNELS_SOFT
:
1128 case AL_AUXILIARY_SEND_FILTER
:
1129 SetSourceiv(Source
, Context
, param
, values
);
1133 alSetError(Context
, AL_INVALID_ENUM
);
1136 ALCcontext_DecRef(Context
);
1140 AL_API ALvoid AL_APIENTRY
alGetSourcef(ALuint source
, ALenum param
, ALfloat
*value
)
1142 ALCcontext
*Context
;
1146 Context
= GetContextRef();
1147 if(!Context
) return;
1149 if((Source
=LookupSource(Context
, source
)) == NULL
)
1150 alSetError(Context
, AL_INVALID_NAME
);
1152 alSetError(Context
, AL_INVALID_VALUE
);
1159 case AL_MAX_DISTANCE
:
1160 case AL_ROLLOFF_FACTOR
:
1161 case AL_CONE_OUTER_GAIN
:
1162 case AL_CONE_OUTER_GAINHF
:
1164 case AL_SAMPLE_OFFSET
:
1165 case AL_BYTE_OFFSET
:
1166 case AL_CONE_INNER_ANGLE
:
1167 case AL_CONE_OUTER_ANGLE
:
1168 case AL_REFERENCE_DISTANCE
:
1169 case AL_AIR_ABSORPTION_FACTOR
:
1170 case AL_ROOM_ROLLOFF_FACTOR
:
1171 case AL_DOPPLER_FACTOR
:
1172 if(GetSourcedv(Source
, Context
, param
, &dval
) == AL_NO_ERROR
)
1173 *value
= (ALfloat
)dval
;
1177 alSetError(Context
, AL_INVALID_ENUM
);
1180 ALCcontext_DecRef(Context
);
1184 AL_API ALvoid AL_APIENTRY
alGetSource3f(ALuint source
, ALenum param
, ALfloat
*value1
, ALfloat
*value2
, ALfloat
*value3
)
1186 ALCcontext
*Context
;
1190 Context
= GetContextRef();
1191 if(!Context
) return;
1193 if((Source
=LookupSource(Context
, source
)) == NULL
)
1194 alSetError(Context
, AL_INVALID_NAME
);
1195 else if(!(value1
&& value2
&& value3
))
1196 alSetError(Context
, AL_INVALID_VALUE
);
1202 if(GetSourcedv(Source
, Context
, param
, dvals
) == AL_NO_ERROR
)
1204 *value1
= (ALfloat
)dvals
[0];
1205 *value2
= (ALfloat
)dvals
[1];
1206 *value3
= (ALfloat
)dvals
[2];
1211 alSetError(Context
, AL_INVALID_ENUM
);
1214 ALCcontext_DecRef(Context
);
1218 AL_API ALvoid AL_APIENTRY
alGetSourcefv(ALuint source
, ALenum param
, ALfloat
*values
)
1220 ALCcontext
*Context
;
1230 case AL_MAX_DISTANCE
:
1231 case AL_ROLLOFF_FACTOR
:
1232 case AL_DOPPLER_FACTOR
:
1233 case AL_CONE_OUTER_GAIN
:
1235 case AL_SAMPLE_OFFSET
:
1236 case AL_BYTE_OFFSET
:
1237 case AL_CONE_INNER_ANGLE
:
1238 case AL_CONE_OUTER_ANGLE
:
1239 case AL_REFERENCE_DISTANCE
:
1240 case AL_CONE_OUTER_GAINHF
:
1241 case AL_AIR_ABSORPTION_FACTOR
:
1242 case AL_ROOM_ROLLOFF_FACTOR
:
1243 alGetSourcef(source
, param
, values
);
1249 alGetSource3f(source
, param
, values
+0, values
+1, values
+2);
1253 Context
= GetContextRef();
1254 if(!Context
) return;
1256 if((Source
=LookupSource(Context
, source
)) == NULL
)
1257 alSetError(Context
, AL_INVALID_NAME
);
1259 alSetError(Context
, AL_INVALID_VALUE
);
1262 case AL_SAMPLE_RW_OFFSETS_SOFT
:
1263 case AL_BYTE_RW_OFFSETS_SOFT
:
1264 if(GetSourcedv(Source
, Context
, param
, dvals
) == AL_NO_ERROR
)
1266 values
[0] = (ALfloat
)dvals
[0];
1267 values
[1] = (ALfloat
)dvals
[1];
1272 alSetError(Context
, AL_INVALID_ENUM
);
1275 ALCcontext_DecRef(Context
);
1279 AL_API
void AL_APIENTRY
alGetSourcedSOFT(ALuint source
, ALenum param
, ALdouble
*value
)
1281 ALCcontext
*Context
;
1284 Context
= GetContextRef();
1285 if(!Context
) return;
1287 if((Source
=LookupSource(Context
, source
)) == NULL
)
1288 alSetError(Context
, AL_INVALID_NAME
);
1290 alSetError(Context
, AL_INVALID_VALUE
);
1297 case AL_MAX_DISTANCE
:
1298 case AL_ROLLOFF_FACTOR
:
1299 case AL_CONE_OUTER_GAIN
:
1300 case AL_CONE_OUTER_GAINHF
:
1302 case AL_SAMPLE_OFFSET
:
1303 case AL_BYTE_OFFSET
:
1304 case AL_CONE_INNER_ANGLE
:
1305 case AL_CONE_OUTER_ANGLE
:
1306 case AL_REFERENCE_DISTANCE
:
1307 case AL_AIR_ABSORPTION_FACTOR
:
1308 case AL_ROOM_ROLLOFF_FACTOR
:
1309 case AL_DOPPLER_FACTOR
:
1310 GetSourcedv(Source
, Context
, param
, value
);
1314 alSetError(Context
, AL_INVALID_ENUM
);
1317 ALCcontext_DecRef(Context
);
1320 AL_API
void AL_APIENTRY
alGetSource3dSOFT(ALuint source
, ALenum param
, ALdouble
*value1
, ALdouble
*value2
, ALdouble
*value3
)
1322 ALCcontext
*Context
;
1326 Context
= GetContextRef();
1327 if(!Context
) return;
1329 if((Source
=LookupSource(Context
, source
)) == NULL
)
1330 alSetError(Context
, AL_INVALID_NAME
);
1331 else if(!(value1
&& value2
&& value3
))
1332 alSetError(Context
, AL_INVALID_VALUE
);
1338 if(GetSourcedv(Source
, Context
, param
, dvals
) == AL_NO_ERROR
)
1347 alSetError(Context
, AL_INVALID_ENUM
);
1350 ALCcontext_DecRef(Context
);
1353 AL_API
void AL_APIENTRY
alGetSourcedvSOFT(ALuint source
, ALenum param
, ALdouble
*values
)
1355 ALCcontext
*Context
;
1358 Context
= GetContextRef();
1359 if(!Context
) return;
1361 if((Source
=LookupSource(Context
, source
)) == NULL
)
1362 alSetError(Context
, AL_INVALID_NAME
);
1364 alSetError(Context
, AL_INVALID_VALUE
);
1371 case AL_MAX_DISTANCE
:
1372 case AL_ROLLOFF_FACTOR
:
1373 case AL_DOPPLER_FACTOR
:
1374 case AL_CONE_OUTER_GAIN
:
1376 case AL_SAMPLE_OFFSET
:
1377 case AL_BYTE_OFFSET
:
1378 case AL_CONE_INNER_ANGLE
:
1379 case AL_CONE_OUTER_ANGLE
:
1380 case AL_REFERENCE_DISTANCE
:
1381 case AL_CONE_OUTER_GAINHF
:
1382 case AL_AIR_ABSORPTION_FACTOR
:
1383 case AL_ROOM_ROLLOFF_FACTOR
:
1385 case AL_SAMPLE_RW_OFFSETS_SOFT
:
1386 case AL_BYTE_RW_OFFSETS_SOFT
:
1387 case AL_SEC_OFFSET_LATENCY_SOFT
:
1392 GetSourcedv(Source
, Context
, param
, values
);
1396 alSetError(Context
, AL_INVALID_ENUM
);
1399 ALCcontext_DecRef(Context
);
1403 AL_API ALvoid AL_APIENTRY
alGetSourcei(ALuint source
, ALenum param
, ALint
*value
)
1405 ALCcontext
*Context
;
1408 Context
= GetContextRef();
1409 if(!Context
) return;
1411 if((Source
=LookupSource(Context
, source
)) == NULL
)
1412 alSetError(Context
, AL_INVALID_NAME
);
1414 alSetError(Context
, AL_INVALID_VALUE
);
1417 case AL_MAX_DISTANCE
:
1418 case AL_ROLLOFF_FACTOR
:
1419 case AL_REFERENCE_DISTANCE
:
1420 case AL_SOURCE_RELATIVE
:
1421 case AL_CONE_INNER_ANGLE
:
1422 case AL_CONE_OUTER_ANGLE
:
1425 case AL_SOURCE_STATE
:
1426 case AL_BUFFERS_QUEUED
:
1427 case AL_BUFFERS_PROCESSED
:
1428 case AL_SOURCE_TYPE
:
1430 case AL_SAMPLE_OFFSET
:
1431 case AL_BYTE_OFFSET
:
1432 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1433 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1434 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1435 case AL_DOPPLER_FACTOR
:
1436 case AL_DIRECT_CHANNELS_SOFT
:
1437 case AL_DISTANCE_MODEL
:
1438 GetSourceiv(Source
, Context
, param
, value
);
1442 alSetError(Context
, AL_INVALID_ENUM
);
1445 ALCcontext_DecRef(Context
);
1449 AL_API
void AL_APIENTRY
alGetSource3i(ALuint source
, ALenum param
, ALint
*value1
, ALint
*value2
, ALint
*value3
)
1451 ALCcontext
*Context
;
1455 Context
= GetContextRef();
1456 if(!Context
) return;
1458 if((Source
=LookupSource(Context
, source
)) == NULL
)
1459 alSetError(Context
, AL_INVALID_NAME
);
1460 else if(!(value1
&& value2
&& value3
))
1461 alSetError(Context
, AL_INVALID_VALUE
);
1467 if(GetSourceiv(Source
, Context
, param
, ivals
) == AL_NO_ERROR
)
1476 alSetError(Context
, AL_INVALID_ENUM
);
1479 ALCcontext_DecRef(Context
);
1483 AL_API
void AL_APIENTRY
alGetSourceiv(ALuint source
, ALenum param
, ALint
*values
)
1485 ALCcontext
*Context
;
1488 Context
= GetContextRef();
1489 if(!Context
) return;
1491 if((Source
=LookupSource(Context
, source
)) == NULL
)
1492 alSetError(Context
, AL_INVALID_NAME
);
1494 alSetError(Context
, AL_INVALID_VALUE
);
1497 case AL_SOURCE_RELATIVE
:
1498 case AL_CONE_INNER_ANGLE
:
1499 case AL_CONE_OUTER_ANGLE
:
1502 case AL_SOURCE_STATE
:
1503 case AL_BUFFERS_QUEUED
:
1504 case AL_BUFFERS_PROCESSED
:
1506 case AL_SAMPLE_OFFSET
:
1507 case AL_BYTE_OFFSET
:
1508 case AL_MAX_DISTANCE
:
1509 case AL_ROLLOFF_FACTOR
:
1510 case AL_DOPPLER_FACTOR
:
1511 case AL_REFERENCE_DISTANCE
:
1512 case AL_SOURCE_TYPE
:
1513 case AL_DIRECT_FILTER
:
1514 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1515 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1516 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1517 case AL_DISTANCE_MODEL
:
1518 case AL_DIRECT_CHANNELS_SOFT
:
1520 case AL_SAMPLE_RW_OFFSETS_SOFT
:
1521 case AL_BYTE_RW_OFFSETS_SOFT
:
1526 GetSourceiv(Source
, Context
, param
, values
);
1530 alSetError(Context
, AL_INVALID_ENUM
);
1533 ALCcontext_DecRef(Context
);
1537 AL_API
void AL_APIENTRY
alGetSourcei64SOFT(ALuint source
, ALenum param
, ALint64SOFT
*value
)
1539 ALCcontext
*Context
;
1542 Context
= GetContextRef();
1543 if(!Context
) return;
1545 if((Source
=LookupSource(Context
, source
)) == NULL
)
1546 alSetError(Context
, AL_INVALID_NAME
);
1548 alSetError(Context
, AL_INVALID_VALUE
);
1551 case AL_MAX_DISTANCE
:
1552 case AL_ROLLOFF_FACTOR
:
1553 case AL_REFERENCE_DISTANCE
:
1554 case AL_SOURCE_RELATIVE
:
1555 case AL_CONE_INNER_ANGLE
:
1556 case AL_CONE_OUTER_ANGLE
:
1559 case AL_SOURCE_STATE
:
1560 case AL_BUFFERS_QUEUED
:
1561 case AL_BUFFERS_PROCESSED
:
1562 case AL_SOURCE_TYPE
:
1564 case AL_SAMPLE_OFFSET
:
1565 case AL_BYTE_OFFSET
:
1566 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1567 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1568 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1569 case AL_DOPPLER_FACTOR
:
1570 case AL_DIRECT_CHANNELS_SOFT
:
1571 case AL_DISTANCE_MODEL
:
1572 GetSourcei64v(Source
, Context
, param
, value
);
1576 alSetError(Context
, AL_INVALID_ENUM
);
1579 ALCcontext_DecRef(Context
);
1582 AL_API
void AL_APIENTRY
alGetSource3i64SOFT(ALuint source
, ALenum param
, ALint64SOFT
*value1
, ALint64SOFT
*value2
, ALint64SOFT
*value3
)
1584 ALCcontext
*Context
;
1588 Context
= GetContextRef();
1589 if(!Context
) return;
1591 if((Source
=LookupSource(Context
, source
)) == NULL
)
1592 alSetError(Context
, AL_INVALID_NAME
);
1593 else if(!(value1
&& value2
&& value3
))
1594 alSetError(Context
, AL_INVALID_VALUE
);
1600 if(GetSourcei64v(Source
, Context
, param
, i64vals
) == AL_NO_ERROR
)
1602 *value1
= i64vals
[0];
1603 *value2
= i64vals
[1];
1604 *value3
= i64vals
[2];
1609 alSetError(Context
, AL_INVALID_ENUM
);
1612 ALCcontext_DecRef(Context
);
1615 AL_API
void AL_APIENTRY
alGetSourcei64vSOFT(ALuint source
, ALenum param
, ALint64SOFT
*values
)
1617 ALCcontext
*Context
;
1620 Context
= GetContextRef();
1621 if(!Context
) return;
1623 if((Source
=LookupSource(Context
, source
)) == NULL
)
1624 alSetError(Context
, AL_INVALID_NAME
);
1626 alSetError(Context
, AL_INVALID_VALUE
);
1629 case AL_MAX_DISTANCE
:
1630 case AL_ROLLOFF_FACTOR
:
1631 case AL_REFERENCE_DISTANCE
:
1632 case AL_SOURCE_RELATIVE
:
1633 case AL_CONE_INNER_ANGLE
:
1634 case AL_CONE_OUTER_ANGLE
:
1637 case AL_SOURCE_STATE
:
1638 case AL_BUFFERS_QUEUED
:
1639 case AL_BUFFERS_PROCESSED
:
1640 case AL_SOURCE_TYPE
:
1642 case AL_SAMPLE_OFFSET
:
1643 case AL_BYTE_OFFSET
:
1644 case AL_DIRECT_FILTER_GAINHF_AUTO
:
1645 case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO
:
1646 case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO
:
1647 case AL_DOPPLER_FACTOR
:
1648 case AL_DIRECT_CHANNELS_SOFT
:
1649 case AL_DISTANCE_MODEL
:
1651 case AL_SAMPLE_RW_OFFSETS_SOFT
:
1652 case AL_BYTE_RW_OFFSETS_SOFT
:
1653 case AL_SAMPLE_OFFSET_LATENCY_SOFT
:
1658 GetSourcei64v(Source
, Context
, param
, values
);
1662 alSetError(Context
, AL_INVALID_ENUM
);
1665 ALCcontext_DecRef(Context
);
1669 AL_API ALvoid AL_APIENTRY
alSourcePlay(ALuint source
)
1671 alSourcePlayv(1, &source
);
1673 AL_API ALvoid AL_APIENTRY
alSourcePlayv(ALsizei n
, const ALuint
*sources
)
1675 ALCcontext
*Context
;
1679 Context
= GetContextRef();
1680 if(!Context
) return;
1684 CHECK_VALUE(Context
, n
>= 0);
1685 for(i
= 0;i
< n
;i
++)
1687 if(!LookupSource(Context
, sources
[i
]))
1688 al_throwerr(Context
, AL_INVALID_NAME
);
1691 LockContext(Context
);
1692 while(Context
->MaxActiveSources
-Context
->ActiveSourceCount
< n
)
1697 newcount
= Context
->MaxActiveSources
<< 1;
1699 temp
= realloc(Context
->ActiveSources
,
1700 sizeof(*Context
->ActiveSources
) * newcount
);
1703 UnlockContext(Context
);
1704 al_throwerr(Context
, AL_OUT_OF_MEMORY
);
1707 Context
->ActiveSources
= temp
;
1708 Context
->MaxActiveSources
= newcount
;
1711 for(i
= 0;i
< n
;i
++)
1713 Source
= LookupSource(Context
, sources
[i
]);
1714 if(Context
->DeferUpdates
) Source
->new_state
= AL_PLAYING
;
1715 else SetSourceState(Source
, Context
, AL_PLAYING
);
1717 UnlockContext(Context
);
1721 ALCcontext_DecRef(Context
);
1724 AL_API ALvoid AL_APIENTRY
alSourcePause(ALuint source
)
1726 alSourcePausev(1, &source
);
1728 AL_API ALvoid AL_APIENTRY
alSourcePausev(ALsizei n
, const ALuint
*sources
)
1730 ALCcontext
*Context
;
1734 Context
= GetContextRef();
1735 if(!Context
) return;
1739 CHECK_VALUE(Context
, n
>= 0);
1740 for(i
= 0;i
< n
;i
++)
1742 if(!LookupSource(Context
, sources
[i
]))
1743 al_throwerr(Context
, AL_INVALID_NAME
);
1746 LockContext(Context
);
1747 for(i
= 0;i
< n
;i
++)
1749 Source
= LookupSource(Context
, sources
[i
]);
1750 if(Context
->DeferUpdates
) Source
->new_state
= AL_PAUSED
;
1751 else SetSourceState(Source
, Context
, AL_PAUSED
);
1753 UnlockContext(Context
);
1757 ALCcontext_DecRef(Context
);
1760 AL_API ALvoid AL_APIENTRY
alSourceStop(ALuint source
)
1762 alSourceStopv(1, &source
);
1764 AL_API ALvoid AL_APIENTRY
alSourceStopv(ALsizei n
, const ALuint
*sources
)
1766 ALCcontext
*Context
;
1770 Context
= GetContextRef();
1771 if(!Context
) return;
1775 CHECK_VALUE(Context
, n
>= 0);
1776 for(i
= 0;i
< n
;i
++)
1778 if(!LookupSource(Context
, sources
[i
]))
1779 al_throwerr(Context
, AL_INVALID_NAME
);
1782 LockContext(Context
);
1783 for(i
= 0;i
< n
;i
++)
1785 Source
= LookupSource(Context
, sources
[i
]);
1786 Source
->new_state
= AL_NONE
;
1787 SetSourceState(Source
, Context
, AL_STOPPED
);
1789 UnlockContext(Context
);
1793 ALCcontext_DecRef(Context
);
1796 AL_API ALvoid AL_APIENTRY
alSourceRewind(ALuint source
)
1798 alSourceRewindv(1, &source
);
1800 AL_API ALvoid AL_APIENTRY
alSourceRewindv(ALsizei n
, const ALuint
*sources
)
1802 ALCcontext
*Context
;
1806 Context
= GetContextRef();
1807 if(!Context
) return;
1811 CHECK_VALUE(Context
, n
>= 0);
1812 for(i
= 0;i
< n
;i
++)
1814 if(!LookupSource(Context
, sources
[i
]))
1815 al_throwerr(Context
, AL_INVALID_NAME
);
1818 LockContext(Context
);
1819 for(i
= 0;i
< n
;i
++)
1821 Source
= LookupSource(Context
, sources
[i
]);
1822 Source
->new_state
= AL_NONE
;
1823 SetSourceState(Source
, Context
, AL_INITIAL
);
1825 UnlockContext(Context
);
1829 ALCcontext_DecRef(Context
);
1833 AL_API ALvoid AL_APIENTRY
alSourceQueueBuffers(ALuint source
, ALsizei nb
, const ALuint
*buffers
)
1835 ALCcontext
*Context
;
1838 ALbufferlistitem
*BufferListStart
= NULL
;
1839 ALbufferlistitem
*BufferList
;
1840 ALbuffer
*BufferFmt
;
1845 Context
= GetContextRef();
1846 if(!Context
) return;
1850 ALCdevice
*device
= Context
->Device
;
1852 CHECK_VALUE(Context
, nb
>= 0);
1854 if((Source
=LookupSource(Context
, source
)) == NULL
)
1855 al_throwerr(Context
, AL_INVALID_NAME
);
1857 LockContext(Context
);
1858 if(Source
->SourceType
== AL_STATIC
)
1860 UnlockContext(Context
);
1861 /* Can't queue on a Static Source */
1862 al_throwerr(Context
, AL_INVALID_OPERATION
);
1867 /* Check for a valid Buffer, for its frequency and format */
1868 BufferList
= Source
->queue
;
1871 if(BufferList
->buffer
)
1873 BufferFmt
= BufferList
->buffer
;
1876 BufferList
= BufferList
->next
;
1879 for(i
= 0;i
< nb
;i
++)
1881 ALbuffer
*buffer
= NULL
;
1882 if(buffers
[i
] && (buffer
=LookupBuffer(device
, buffers
[i
])) == NULL
)
1884 UnlockContext(Context
);
1885 al_throwerr(Context
, AL_INVALID_NAME
);
1888 if(!BufferListStart
)
1890 BufferListStart
= malloc(sizeof(ALbufferlistitem
));
1891 BufferListStart
->buffer
= buffer
;
1892 BufferListStart
->next
= NULL
;
1893 BufferListStart
->prev
= NULL
;
1894 BufferList
= BufferListStart
;
1898 BufferList
->next
= malloc(sizeof(ALbufferlistitem
));
1899 BufferList
->next
->buffer
= buffer
;
1900 BufferList
->next
->next
= NULL
;
1901 BufferList
->next
->prev
= BufferList
;
1902 BufferList
= BufferList
->next
;
1904 if(!buffer
) continue;
1905 IncrementRef(&buffer
->ref
);
1907 ReadLock(&buffer
->lock
);
1908 if(BufferFmt
== NULL
)
1912 Source
->NumChannels
= ChannelsFromFmt(buffer
->FmtChannels
);
1913 Source
->SampleSize
= BytesFromFmt(buffer
->FmtType
);
1914 if(buffer
->FmtChannels
== FmtMono
)
1915 Source
->Update
= CalcSourceParams
;
1917 Source
->Update
= CalcNonAttnSourceParams
;
1919 Source
->NeedsUpdate
= AL_TRUE
;
1921 else if(BufferFmt
->Frequency
!= buffer
->Frequency
||
1922 BufferFmt
->OriginalChannels
!= buffer
->OriginalChannels
||
1923 BufferFmt
->OriginalType
!= buffer
->OriginalType
)
1925 ReadUnlock(&buffer
->lock
);
1926 UnlockContext(Context
);
1927 al_throwerr(Context
, AL_INVALID_OPERATION
);
1929 ReadUnlock(&buffer
->lock
);
1932 /* Source is now streaming */
1933 Source
->SourceType
= AL_STREAMING
;
1935 if(Source
->queue
== NULL
)
1936 Source
->queue
= BufferListStart
;
1939 /* Append to the end of the queue */
1940 BufferList
= Source
->queue
;
1941 while(BufferList
->next
!= NULL
)
1942 BufferList
= BufferList
->next
;
1944 BufferListStart
->prev
= BufferList
;
1945 BufferList
->next
= BufferListStart
;
1948 Source
->BuffersInQueue
+= nb
;
1950 UnlockContext(Context
);
1954 while(BufferListStart
)
1956 BufferList
= BufferListStart
;
1957 BufferListStart
= BufferList
->next
;
1959 if(BufferList
->buffer
)
1960 DecrementRef(&BufferList
->buffer
->ref
);
1966 ALCcontext_DecRef(Context
);
1969 AL_API ALvoid AL_APIENTRY
alSourceUnqueueBuffers(ALuint source
, ALsizei nb
, ALuint
*buffers
)
1971 ALCcontext
*Context
;
1974 ALbufferlistitem
*BufferList
;
1979 Context
= GetContextRef();
1980 if(!Context
) return;
1984 CHECK_VALUE(Context
, nb
>= 0);
1986 if((Source
=LookupSource(Context
, source
)) == NULL
)
1987 al_throwerr(Context
, AL_INVALID_NAME
);
1989 LockContext(Context
);
1990 if(Source
->Looping
|| Source
->SourceType
!= AL_STREAMING
||
1991 (ALuint
)nb
> Source
->BuffersPlayed
)
1993 UnlockContext(Context
);
1994 /* Trying to unqueue pending buffers, or a buffer that wasn't queued. */
1995 al_throwerr(Context
, AL_INVALID_VALUE
);
1998 for(i
= 0;i
< nb
;i
++)
2000 BufferList
= Source
->queue
;
2001 Source
->queue
= BufferList
->next
;
2002 Source
->BuffersInQueue
--;
2003 Source
->BuffersPlayed
--;
2005 if(BufferList
->buffer
)
2007 buffers
[i
] = BufferList
->buffer
->id
;
2008 DecrementRef(&BufferList
->buffer
->ref
);
2016 Source
->queue
->prev
= NULL
;
2017 UnlockContext(Context
);
2021 ALCcontext_DecRef(Context
);
2025 static ALvoid
InitSourceParams(ALsource
*Source
)
2029 Source
->InnerAngle
= 360.0f
;
2030 Source
->OuterAngle
= 360.0f
;
2031 Source
->Pitch
= 1.0f
;
2032 Source
->Position
[0] = 0.0f
;
2033 Source
->Position
[1] = 0.0f
;
2034 Source
->Position
[2] = 0.0f
;
2035 Source
->Orientation
[0] = 0.0f
;
2036 Source
->Orientation
[1] = 0.0f
;
2037 Source
->Orientation
[2] = 0.0f
;
2038 Source
->Velocity
[0] = 0.0f
;
2039 Source
->Velocity
[1] = 0.0f
;
2040 Source
->Velocity
[2] = 0.0f
;
2041 Source
->RefDistance
= 1.0f
;
2042 Source
->MaxDistance
= FLT_MAX
;
2043 Source
->RollOffFactor
= 1.0f
;
2044 Source
->Looping
= AL_FALSE
;
2045 Source
->Gain
= 1.0f
;
2046 Source
->MinGain
= 0.0f
;
2047 Source
->MaxGain
= 1.0f
;
2048 Source
->OuterGain
= 0.0f
;
2049 Source
->OuterGainHF
= 1.0f
;
2051 Source
->DryGainHFAuto
= AL_TRUE
;
2052 Source
->WetGainAuto
= AL_TRUE
;
2053 Source
->WetGainHFAuto
= AL_TRUE
;
2054 Source
->AirAbsorptionFactor
= 0.0f
;
2055 Source
->RoomRolloffFactor
= 0.0f
;
2056 Source
->DopplerFactor
= 1.0f
;
2057 Source
->DirectChannels
= AL_FALSE
;
2059 Source
->DistanceModel
= DefaultDistanceModel
;
2061 Source
->Resampler
= DefaultResampler
;
2063 Source
->state
= AL_INITIAL
;
2064 Source
->new_state
= AL_NONE
;
2065 Source
->SourceType
= AL_UNDETERMINED
;
2066 Source
->Offset
= -1.0;
2068 Source
->DirectGain
= 1.0f
;
2069 Source
->DirectGainHF
= 1.0f
;
2070 for(i
= 0;i
< MAX_SENDS
;i
++)
2072 Source
->Send
[i
].Gain
= 1.0f
;
2073 Source
->Send
[i
].GainHF
= 1.0f
;
2076 Source
->NeedsUpdate
= AL_TRUE
;
2078 Source
->Hrtf
.Moving
= AL_FALSE
;
2079 Source
->Hrtf
.Counter
= 0;
2085 * Sets the source's new play state given its current state.
2087 ALvoid
SetSourceState(ALsource
*Source
, ALCcontext
*Context
, ALenum state
)
2089 if(state
== AL_PLAYING
)
2091 ALbufferlistitem
*BufferList
;
2094 /* Check that there is a queue containing at least one valid, non zero
2096 BufferList
= Source
->queue
;
2099 if(BufferList
->buffer
!= NULL
&& BufferList
->buffer
->SampleLen
)
2101 BufferList
= BufferList
->next
;
2104 if(Source
->state
!= AL_PLAYING
)
2106 for(j
= 0;j
< MaxChannels
;j
++)
2108 for(k
= 0;k
< SRC_HISTORY_LENGTH
;k
++)
2109 Source
->Hrtf
.History
[j
][k
] = 0.0f
;
2110 for(k
= 0;k
< HRIR_LENGTH
;k
++)
2112 Source
->Hrtf
.Values
[j
][k
][0] = 0.0f
;
2113 Source
->Hrtf
.Values
[j
][k
][1] = 0.0f
;
2118 if(Source
->state
!= AL_PAUSED
)
2120 Source
->state
= AL_PLAYING
;
2121 Source
->position
= 0;
2122 Source
->position_fraction
= 0;
2123 Source
->BuffersPlayed
= 0;
2126 Source
->state
= AL_PLAYING
;
2128 // Check if an Offset has been set
2129 if(Source
->Offset
>= 0.0)
2130 ApplyOffset(Source
);
2132 /* If there's nothing to play, or device is disconnected, go right to
2134 if(!BufferList
|| !Context
->Device
->Connected
)
2136 SetSourceState(Source
, Context
, AL_STOPPED
);
2140 for(j
= 0;j
< Context
->ActiveSourceCount
;j
++)
2142 if(Context
->ActiveSources
[j
] == Source
)
2145 if(j
== Context
->ActiveSourceCount
)
2146 Context
->ActiveSources
[Context
->ActiveSourceCount
++] = Source
;
2148 else if(state
== AL_PAUSED
)
2150 if(Source
->state
== AL_PLAYING
)
2152 Source
->state
= AL_PAUSED
;
2153 Source
->Hrtf
.Moving
= AL_FALSE
;
2154 Source
->Hrtf
.Counter
= 0;
2157 else if(state
== AL_STOPPED
)
2159 if(Source
->state
!= AL_INITIAL
)
2161 Source
->state
= AL_STOPPED
;
2162 Source
->BuffersPlayed
= Source
->BuffersInQueue
;
2163 Source
->Hrtf
.Moving
= AL_FALSE
;
2164 Source
->Hrtf
.Counter
= 0;
2166 Source
->Offset
= -1.0;
2168 else if(state
== AL_INITIAL
)
2170 if(Source
->state
!= AL_INITIAL
)
2172 Source
->state
= AL_INITIAL
;
2173 Source
->position
= 0;
2174 Source
->position_fraction
= 0;
2175 Source
->BuffersPlayed
= 0;
2176 Source
->Hrtf
.Moving
= AL_FALSE
;
2177 Source
->Hrtf
.Counter
= 0;
2179 Source
->Offset
= -1.0;
2185 * Gets the current read offset for the given Source, in 32.32 fixed-point
2186 * samples. The offset is relative to the start of the queue (not the start of
2187 * the current buffer).
2189 static ALint64
GetSourceOffset(const ALsource
*Source
)
2191 const ALbufferlistitem
*BufferList
;
2195 if(Source
->state
!= AL_PLAYING
&& Source
->state
!= AL_PAUSED
)
2198 /* NOTE: This is the offset into the *current* buffer, so add the length of
2199 * any played buffers */
2200 readPos
= (ALuint64
)Source
->position
<< 32;
2201 readPos
|= (ALuint64
)Source
->position_fraction
<< (32-FRACTIONBITS
);
2202 BufferList
= Source
->queue
;
2203 for(i
= 0;i
< Source
->BuffersPlayed
&& BufferList
;i
++)
2205 if(BufferList
->buffer
)
2206 readPos
+= (ALuint64
)BufferList
->buffer
->SampleLen
<< 32;
2207 BufferList
= BufferList
->next
;
2210 return (ALint64
)minu64(readPos
, MAKEU64(0x7fffffff,0xffffffff));
2213 /* GetSourceSecOffset
2215 * Gets the current read offset for the given Source, in seconds. The offset is
2216 * relative to the start of the queue (not the start of the current buffer).
2218 static ALdouble
GetSourceSecOffset(const ALsource
*Source
)
2220 const ALbufferlistitem
*BufferList
;
2221 const ALbuffer
*Buffer
= NULL
;
2225 BufferList
= Source
->queue
;
2228 if(BufferList
->buffer
)
2230 Buffer
= BufferList
->buffer
;
2233 BufferList
= BufferList
->next
;
2236 if((Source
->state
!= AL_PLAYING
&& Source
->state
!= AL_PAUSED
) || !Buffer
)
2239 /* NOTE: This is the offset into the *current* buffer, so add the length of
2240 * any played buffers */
2241 readPos
= (ALuint64
)Source
->position
<< FRACTIONBITS
;
2242 readPos
|= (ALuint64
)Source
->position_fraction
;
2243 BufferList
= Source
->queue
;
2244 for(i
= 0;i
< Source
->BuffersPlayed
&& BufferList
;i
++)
2246 if(BufferList
->buffer
)
2247 readPos
+= (ALuint64
)BufferList
->buffer
->SampleLen
<< FRACTIONBITS
;
2248 BufferList
= BufferList
->next
;
2251 return (ALdouble
)readPos
/ (ALdouble
)FRACTIONONE
/ (ALdouble
)Buffer
->Frequency
;
2256 * Gets the current read and write offsets for the given Source, in the
2257 * appropriate format (Bytes, Samples or Seconds). The offsets are relative to
2258 * the start of the queue (not the start of the current buffer).
2260 static ALvoid
GetSourceOffsets(const ALsource
*Source
, ALenum name
, ALdouble
*offset
, ALdouble updateLen
)
2262 const ALbufferlistitem
*BufferList
;
2263 const ALbuffer
*Buffer
= NULL
;
2264 ALuint readPos
, writePos
;
2265 ALuint totalBufferLen
;
2268 // Find the first valid Buffer in the Queue
2269 BufferList
= Source
->queue
;
2272 if(BufferList
->buffer
)
2274 Buffer
= BufferList
->buffer
;
2277 BufferList
= BufferList
->next
;
2280 if((Source
->state
!= AL_PLAYING
&& Source
->state
!= AL_PAUSED
) || !Buffer
)
2287 if(updateLen
> 0.0 && updateLen
< 0.015)
2290 /* NOTE: This is the offset into the *current* buffer, so add the length of
2291 * any played buffers */
2292 readPos
= Source
->position
;
2294 BufferList
= Source
->queue
;
2295 for(i
= 0;BufferList
;i
++)
2297 if(BufferList
->buffer
)
2299 if(i
< Source
->BuffersPlayed
)
2300 readPos
+= BufferList
->buffer
->SampleLen
;
2301 totalBufferLen
+= BufferList
->buffer
->SampleLen
;
2303 BufferList
= BufferList
->next
;
2305 if(Source
->state
== AL_PLAYING
)
2306 writePos
= readPos
+ (ALuint
)(updateLen
*Buffer
->Frequency
);
2312 readPos
%= totalBufferLen
;
2313 writePos
%= totalBufferLen
;
2317 /* Wrap positions back to 0 */
2318 if(readPos
>= totalBufferLen
)
2320 if(writePos
>= totalBufferLen
)
2327 offset
[0] = (ALdouble
)readPos
/ Buffer
->Frequency
;
2328 offset
[1] = (ALdouble
)writePos
/ Buffer
->Frequency
;
2331 case AL_SAMPLE_OFFSET
:
2332 case AL_SAMPLE_RW_OFFSETS_SOFT
:
2333 offset
[0] = (ALdouble
)readPos
;
2334 offset
[1] = (ALdouble
)writePos
;
2337 case AL_BYTE_OFFSET
:
2338 case AL_BYTE_RW_OFFSETS_SOFT
:
2339 if(Buffer
->OriginalType
== UserFmtIMA4
)
2341 ALuint BlockSize
= 36 * ChannelsFromFmt(Buffer
->FmtChannels
);
2342 ALuint FrameBlockSize
= 65;
2344 /* Round down to nearest ADPCM block */
2345 offset
[0] = (ALdouble
)(readPos
/ FrameBlockSize
* BlockSize
);
2346 if(Source
->state
!= AL_PLAYING
)
2347 offset
[1] = offset
[0];
2350 /* Round up to nearest ADPCM block */
2351 offset
[1] = (ALdouble
)((writePos
+FrameBlockSize
-1) /
2352 FrameBlockSize
* BlockSize
);
2357 ALuint FrameSize
= FrameSizeFromUserFmt(Buffer
->OriginalChannels
, Buffer
->OriginalType
);
2358 offset
[0] = (ALdouble
)(readPos
* FrameSize
);
2359 offset
[1] = (ALdouble
)(writePos
* FrameSize
);
2368 * Apply the stored playback offset to the Source. This function will update
2369 * the number of buffers "played" given the stored offset.
2371 ALboolean
ApplyOffset(ALsource
*Source
)
2373 const ALbufferlistitem
*BufferList
;
2374 const ALbuffer
*Buffer
;
2375 ALint bufferLen
, totalBufferLen
;
2376 ALint buffersPlayed
;
2379 /* Get sample frame offset */
2380 offset
= GetSampleOffset(Source
);
2387 BufferList
= Source
->queue
;
2390 Buffer
= BufferList
->buffer
;
2391 bufferLen
= Buffer
? Buffer
->SampleLen
: 0;
2393 if(bufferLen
<= offset
-totalBufferLen
)
2395 /* Offset is past this buffer so increment to the next buffer */
2398 else if(totalBufferLen
<= offset
)
2400 /* Offset is in this buffer */
2401 Source
->BuffersPlayed
= buffersPlayed
;
2403 Source
->position
= offset
- totalBufferLen
;
2404 Source
->position_fraction
= 0;
2408 totalBufferLen
+= bufferLen
;
2410 BufferList
= BufferList
->next
;
2413 /* Offset is out of range of the queue */
2420 * Returns the sample offset into the Source's queue (from the Sample, Byte or
2421 * Second offset supplied by the application). This takes into account the fact
2422 * that the buffer format may have been modifed since.
2424 static ALint
GetSampleOffset(ALsource
*Source
)
2426 const ALbuffer
*Buffer
= NULL
;
2427 const ALbufferlistitem
*BufferList
;
2430 /* Find the first valid Buffer in the Queue */
2431 BufferList
= Source
->queue
;
2434 if(BufferList
->buffer
)
2436 Buffer
= BufferList
->buffer
;
2439 BufferList
= BufferList
->next
;
2444 Source
->Offset
= -1.0;
2448 switch(Source
->OffsetType
)
2450 case AL_BYTE_OFFSET
:
2451 /* Determine the ByteOffset (and ensure it is block aligned) */
2452 Offset
= (ALint
)Source
->Offset
;
2453 if(Buffer
->OriginalType
== UserFmtIMA4
)
2455 Offset
/= 36 * ChannelsFromUserFmt(Buffer
->OriginalChannels
);
2459 Offset
/= FrameSizeFromUserFmt(Buffer
->OriginalChannels
, Buffer
->OriginalType
);
2462 case AL_SAMPLE_OFFSET
:
2463 Offset
= (ALint
)Source
->Offset
;
2467 Offset
= (ALint
)(Source
->Offset
* Buffer
->Frequency
);
2470 Source
->Offset
= -1.0;
2478 * Destroys all sources in the source map.
2480 ALvoid
ReleaseALSources(ALCcontext
*Context
)
2484 for(pos
= 0;pos
< Context
->SourceMap
.size
;pos
++)
2486 ALsource
*temp
= Context
->SourceMap
.array
[pos
].value
;
2487 Context
->SourceMap
.array
[pos
].value
= NULL
;
2489 while(temp
->queue
!= NULL
)
2491 ALbufferlistitem
*BufferList
= temp
->queue
;
2492 temp
->queue
= BufferList
->next
;
2494 if(BufferList
->buffer
!= NULL
)
2495 DecrementRef(&BufferList
->buffer
->ref
);
2499 for(j
= 0;j
< MAX_SENDS
;++j
)
2501 if(temp
->Send
[j
].Slot
)
2502 DecrementRef(&temp
->Send
[j
].Slot
->ref
);
2503 temp
->Send
[j
].Slot
= NULL
;
2506 FreeThunkEntry(temp
->id
);
2507 memset(temp
, 0, sizeof(*temp
));