Class TALSourceAllocator

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TALSourceAllocator = class(TObject)

Description

This class manages a list of allocated OpenAL sounds.

The idea is that you leave to this class creating and deleting of OpenAL sounds. When you need OpenAL sound to do something, just call AllocateSource method.

This class will manage OpenAL sources in an intelligent manner, which means when you need new sound, we may

  1. reuse already allocated sound that is not used to play anything

  2. allocate new sound (but we will keep allocated sounds count within MaxAllocatedSources sound limit, to not overload OpenAL implementation with work).

  3. we may simply interrupt already allocated sound, if new sound is more important.

This class may exist only when OpenAL context is active (and it's the same OpenAL context).

The very reason behind this class is to hide from you the fact that the number of OpenAL sources are limited. In particular, this means that when OpenAL will run out of sources, no OpenAL error (alGetError) will be left, and no exception will be raised. In the worst case TALSourceAllocator.AllocateSource will return nil, but in more probable cases some other sources (unused, or with less priority) will be reused.

Note that this means that the code in this unit must read in some situations alGetError. That's because reading alGetError is the only way to know when OpenAL implementation has run out of sources. So the code in this unit may in various places raise EALError if you made some error in your OpenAL code, and you didn't check alGetError yourself often enough.

Hierarchy

Overview

Methods

Public constructor Create(const AMinAllocatedSources, AMaxAllocatedSources: Cardinal);
Public destructor Destroy; override;
Public function AllocateSource(const Importance: Integer): TALAllocatedSource;
Public procedure RefreshUsed;

Properties

Public property MinAllocatedSources: Cardinal read FMinAllocatedSources write SetMinAllocatedSources;
Public property MaxAllocatedSources: Cardinal read FMaxAllocatedSources write SetMaxAllocatedSources;
Public property AllocatedSources: TALAllocatedSourcesList read FAllocatedSources;

Description

Methods

Public constructor Create(const AMinAllocatedSources, AMaxAllocatedSources: Cardinal);
 
Public destructor Destroy; override;
 
Public function AllocateSource(const Importance: Integer): TALAllocatedSource;

Call this when you need new OpenAL sound. This indicates that you want to use new sound.

This is the most important function of this class — actually the sole purpose of TALSourceAllocator and TALAllocatedSource classes is to provide you this function.

If we can't allocate new OpenAL sound for this (because we already allocated MaxAllocatedSources, or OpenAL cannot allocate so many sources (so ENoMoreOpenALSources is catched and silenced by this function), and all existing sounds are used and their Importance is > given here Importance), returns nil.

Else returns non-nil TALAllocatedSource instance, with Used to to True (to indicate that you'll use it) and Importance set as required. You should initialize all properties of this sound (it's buffer, gain, looping — everything), and start playing it. Note for looping sounds: just like any other sound, looping sound may be stopped because the sounds are needed for other sounds. If you want to try to restart the looping sound, you will have to implement it yourself. Or you can just set Importance of looping sounds high enough, and don't use too many looping sounds, to never let them be eliminated by other sounds.

Public procedure RefreshUsed;

Check and eventually set some sources from used to unused state.

For every source that is marked as Used, this checks whether this source is actually in playing/paused state right now. If not, it calls DoUsingEnd (thus setting Used to False and triggering OnUsingEnd) for this source.

See TALAllocatedSource.OnUsingEnd for more description. Generally, you don't need to call this procedure — this unit is smart, and every operation does such refreshing (at least partially) by itself, when it's really needed (e.g. AllocateSource may do this if no unused source will be found). You may need to call this procedure from time to time if you frequently perform some expensive operation for all used sources.

Properties

Public property MinAllocatedSources: Cardinal read FMinAllocatedSources write SetMinAllocatedSources;

For the sake of speed, this class always has allocated at least MinAllocatedSources OpenAL sources. This must be >= 1.

Setting this to too large value (so large that OpenAL cannot create so many sources) will raise ENoMoreOpenALSources. Also creating the TALSourceAllocator instance with initial value for MinAllocatedSources too large will raise ENoMoreOpenALSources. So set this property to really *the minimal* number or required sources. If you don't know what to do, just set this to 1.)

Public property MaxAllocatedSources: Cardinal read FMaxAllocatedSources write SetMaxAllocatedSources;

This class always has allocated at most MaxAllocatedSources OpenAL sounds. That's why whenever you try to get more sounds, sounds with less Importance may stop playing (because their OpenAL sounds may be allocated to some other sound). This limit must exist, because allocating too many sounds is bad for OpenAL speed (not to mention that it may be impossible under some OpenAL implementations, like Windows one).

This must always be >= MinAllocatedSources.

Public property AllocatedSources: TALAllocatedSourcesList read FAllocatedSources;

This is read-only from outside. You can read AllocatedSources properties to know various things about current state of TALSourceAllocator. But generally, this should be avoided — the way AllocatedSources are managed is internal for this class.


Generated by PasDoc 0.10.0 on 2008-02-25 00:00:29