| Description | Hierarchy | Fields | Methods | Properties |
type TGameSoundEngine = class(TObject)
Easy to use sound manager, using OpenAL, ALUtils and ALSourceAllocator underneath.
At ALContextInit, right before initializing OpenAL stuff, this reads sounds information from SoundsXmlFileName file. When OpenAL is initialized, it loads all sound files. Sound filenames are specified inside SoundsXmlFileName file (they may be relative filenames, relative to the location of SoundsXmlFileName file).
So this assumes that you want to load all sound files at once (along with initializing OpenAL context) and free them at once when releasing AL context. And it requires that you place your sounds data and XML file in appropriate locations.
So the basic principle of this unit is to load all files at once, and require file like sounds/index.xml. That's the price for having easy and comfortable unit. All these assumptions are perfectly OK for most games, for more general sound programs... not necessarily. If you need more flexibility, you should write your own sound manager (or heavily extend this), using ALUtils and ALSourceAllocator units directly.
![]() |
constructor Create; |
![]() |
destructor Destroy; override; |
![]() |
function SoundFromName(const SoundName: string): TSoundType; |
![]() |
procedure ALContextInit(WasParam_NoSound: boolean); virtual; |
![]() |
procedure ALRefreshUsedSources; |
![]() |
procedure ALContextClose; |
![]() |
procedure AppendALInformation(S: TStrings); |
![]() |
function ALInformation: string; |
![]() |
function Sound(SoundType: TSoundType; const Looping: boolean = false): TALAllocatedSource; |
![]() |
function Sound3d(SoundType: TSoundType; const Position: TVector3Single; const Looping: boolean = false): TALAllocatedSource; overload; |
![]() |
procedure AddSoundImportanceName(const Name: string; Importance: Integer); |
![]() |
procedure ReadSoundInfos; |
![]() |
procedure LoadFromConfig(ConfigFile: TKamXMLConfig); |
![]() |
procedure SaveToConfig(ConfigFile: TKamXMLConfig); |
![]() |
procedure ALChangeDevice(const NewALCDevice: string); |
![]() |
property SoundsXmlFileName: string
read FSoundsXmlFileName write FSoundsXmlFileName; |
![]() |
property SoundNames: TStringList read FSoundNames; |
![]() |
property SoundVolume: Single read GetSoundVolume write SetSoundVolume
default DefaultSoundVolume; |
![]() |
property SoundImportanceNames: TStringList read FSoundImportanceNames; |
![]() |
property SoundInitializationReport: string read FSoundInitializationReport; |
![]() |
property MusicPlayer: TMusicPlayer read FMusicPlayer; |
![]() |
property ALMinAllocatedSources: Cardinal
read GetALMinAllocatedSources write SetALMinAllocatedSources
default DefaultALMinAllocatedSources; |
![]() |
property ALMaxAllocatedSources: Cardinal
read GetALMaxAllocatedSources write SetALMaxAllocatedSources
default DefaultALMaxAllocatedSources; |
![]() |
constructor Create; |
![]() |
destructor Destroy; override; |
![]() |
function SoundFromName(const SoundName: string): TSoundType; |
|
Return sound with given name. Available names are given in SoundNames, and inside ../data/sounds/index.xml. Always for SoundName = '' it will return stNone. Exceptions raised
| |
![]() |
procedure ALContextInit(WasParam_NoSound: boolean); virtual; |
|
Call this always to initialize OpenAL and OpenAL context, and load sound files. This sets SoundInitializationReport and ALActive. You can set ALCDevice before calling this. | |
![]() |
procedure ALRefreshUsedSources; |
|
This will call RefreshUsed on internal ALSourceAllocator, see TALSourceAllocator.RefreshUsed for info. It's silently ignored when not ALActive. | |
![]() |
procedure ALContextClose; |
|
Call this always to release OpenAL things. This is ignored if not ALActive. | |
![]() |
procedure AppendALInformation(S: TStrings); |
|
If ALActive, then will append some info about current OpenAL used. | |
![]() |
function ALInformation: string; |
![]() |
function Sound(SoundType: TSoundType; const Looping: boolean = false): TALAllocatedSource; |
|
Play given sound. This should be used to play sounds that are not spatial actually, i.e. have no place in 3D space. Returns used TALAllocatedSource (or nil if none was available). You don't have to do anything with this returned TALAllocatedSource. | |
![]() |
function Sound3d(SoundType: TSoundType; const Position: TVector3Single; const Looping: boolean = false): TALAllocatedSource; overload; |
|
Play given sound at appropriate position in 3D space. Returns used TALAllocatedSource (or nil if none was available). You don't have to do anything with this returned TALAllocatedSource. | |
![]() |
procedure AddSoundImportanceName(const Name: string; Importance: Integer); |
![]() |
procedure ReadSoundInfos; |
![]() |
procedure LoadFromConfig(ConfigFile: TKamXMLConfig); |
|
These methods load/save into config file some sound properties. Namely: sound/music volume, min/max allocated sounds, and current ALCDevice. ALCDevice is technically declared in another unit, ALUtils, but still this is probably the best place to save/load it. Everything is loaded/saved under the path sound/ inside ConfigFile. | |
![]() |
procedure SaveToConfig(ConfigFile: TKamXMLConfig); |
![]() |
procedure ALChangeDevice(const NewALCDevice: string); |
|
Change ALCDevice while OpenAL is already initialized. This cleanly closes the old device (ALContextClose), changes ALCDevice value, initializes context again (ALContextInit). | |
![]() |
property SoundsXmlFileName: string
read FSoundsXmlFileName write FSoundsXmlFileName; |
|
The XML file that contains description of your sounds. See It's crucial that you create such file, and eventually adjust this property before calling ReadSoundInfos (or ALContextInit, that always callsReadSoundInfos). By default (in our constryctor) this is initialized to | |
![]() |
property SoundNames: TStringList read FSoundNames; |
|
This is a list of sound names used by your game. Each sound has a unique name, used to identify sound in sounds/index.xml file and for SoundFromName function. These names are stored here. At the beginning, this list always contains exactly one item: empty string. This is a special "sound type" that has index 0 (should be always expressed as TSoundType value stNone) and name ''. stNone is a special sound as it actually means "no sound" in many cases. You can (and should !) fill this array with all sound names your game is using before calling ALContextInit (or ReadSoundInfos, but ReadSoundInfos is usually called for the first time by ALContextInit). TODO: in the future this may be automatically filled when ReadSoundInfos is called. For now, ReadSoundInfos just read sounds information for all sounds mentioned here — in the future, ReadSoundInfos may also just fill this list. | |
![]() |
property SoundVolume: Single read GetSoundVolume write SetSoundVolume
default DefaultSoundVolume; |
|
Sound volume, affects all OpenAL sounds (effects and music). This must always be within 0..1 range. 0.0 means that there are no effects (this case should be optimized). | |
![]() |
property SoundImportanceNames: TStringList read FSoundImportanceNames; |
|
Sound importance names and values. Each item is a name (as a string) and a value (that is stored in Objects property of the item as a pointer; add new importances by AddSoundImportanceName for comfort). These can be used within sounds.xml file. Before using ALContextInit, you can fill this list with values. Initially, it contains only the 'max' value associated with MaxSoundImportance. | |
![]() |
property SoundInitializationReport: string read FSoundInitializationReport; |
![]() |
property MusicPlayer: TMusicPlayer read FMusicPlayer; |
![]() |
property ALMinAllocatedSources: Cardinal
read GetALMinAllocatedSources write SetALMinAllocatedSources
default DefaultALMinAllocatedSources; |
|
Min/max number of allocated OpenAL sources. These properties are used when creating TALSourceAllocator. When TALSourceAllocator is already created, these properties correspond to allocator properties (setting them sets also allocator properties). In summary, you can treat these properties just like analogous TALSourceAllocator properties, but you can freely operate on them even when OpenAL is not initialized. Which is useful if user disabled sound or you want to load/save these values from some config files at time when OpenAL couldn't be initialized yet — in such cases AL allocator doesn't exist, but you can operate on these properties without worry. When changing Min/MaxAllocatedSources, remember to always keep MinAllocatedSources <= MaxAllocatedSources. | |
![]() |
property ALMaxAllocatedSources: Cardinal
read GetALMaxAllocatedSources write SetALMaxAllocatedSources
default DefaultALMaxAllocatedSources; |