| 1 |
{%MainUnit castlesoundengine.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
ENoMoreSources = CastleSoundBase.ENoMoreSources; |
| 21 |
ENoMoreOpenALSources = ENoMoreSources deprecated 'use ENoMoreSources'; |
| 22 |
ESoundBufferNotLoaded = class(Exception); |
| 23 |
EInvalidSoundBufferFree = class(Exception); |
| 24 |
ESoundFileError = CastleSoundBase.ESoundFileError; |
| 25 |
EInvalidSoundRepositoryXml = class(Exception); |
| 26 |
|
| 27 |
var |
| 28 |
{ Common sounds. |
| 29 |
|
| 30 |
The sounds types listed below are automatically |
| 31 |
initialized when you set TRepoSoundEngine.RepositoryURL. |
| 32 |
All engine units can use them if you define them in your sounds XML file. |
| 33 |
If they are not defined in your XML file (or if you don't even have |
| 34 |
an XML file, that is you leave TRepoSoundEngine.RepositoryURL empty) |
| 35 |
then they remain nil (and nothing will happen if anything will try |
| 36 |
to play them by TRepoSoundEngine.Sound or TRepoSoundEngine.Sound3D). |
| 37 |
|
| 38 |
Simply define them in your sounds XML file (see |
| 39 |
TRepoSoundEngine.RepositoryURL) under a suitable name with underscores, |
| 40 |
like 'player_dies' for stPlayerDies. } |
| 41 |
|
| 42 |
{ Player sounds. |
| 43 |
@groupBegin } |
| 44 |
stPlayerInteractFailed, |
| 45 |
stPlayerPickItem, |
| 46 |
stPlayerDropItem, |
| 47 |
stPlayerSwimming, |
| 48 |
stPlayerDrowning, |
| 49 |
stPlayerFootstepsDefault, |
| 50 |
stPlayerToxicPain, |
| 51 |
stPlayerSuddenPain, |
| 52 |
stPlayerDies, |
| 53 |
stPlayerSwimmingChange, |
| 54 |
{ @groupEnd } |
| 55 |
|
| 56 |
{ Sounds used by TCastleOnScreenMenu. |
| 57 |
@groupBegin } |
| 58 |
stMenuCurrentItemChanged, |
| 59 |
stMenuClick |
| 60 |
{ @groupEnd } |
| 61 |
:TCastleSound deprecated 'these predefined sounds should not be used'; |
| 62 |
|
| 63 |
const |
| 64 |
dmInverse = CastleSoundBase.dmInverse; |
| 65 |
dmLinear = CastleSoundBase.dmLinear; |
| 66 |
|
| 67 |
{ Supported sound file formats. |
| 68 |
Use these filters with LCL file dialog (easily set by FileFiltersToDialog) |
| 69 |
or TCastleWindow.FileDialog. } |
| 70 |
LoadSound_FileFilters = |
| 71 |
'All Files|*|' + |
| 72 |
'*All Sound Files|*.wav;*.ogg|' + |
| 73 |
'WAV (*.wav)|*.wav|' + |
| 74 |
'OggVorbis (*.ogg)|*.ogg'; |
| 75 |
|
| 76 |
{ The sound engine. Singleton instance of TRepoSoundEngine, the most capable |
| 77 |
engine class. Created on first call to this function. } |
| 78 |
function SoundEngine: TRepoSoundEngine; |
| 79 |
|
| 80 |
{ The sound engine, but may be @nil very early in initialization / very late in finalization. |
| 81 |
@exclude } |
| 82 |
function InternalSoundEngine: TRepoSoundEngine; |
| 83 |
|
| 84 |
{$endif read_interface} |
| 85 |
|
| 86 |
{$ifdef read_implementation} |
| 87 |
|
| 88 |
var |
| 89 |
FSoundEngine: TRepoSoundEngine; |
| 90 |
|
| 91 |
{ Internal constants, only to support deprected "importance" things in XML files. } |
| 92 |
const |
| 93 |
MaxSoundImportance = MaxInt; |
| 94 |
LevelEventSoundImportance = 100000; |
| 95 |
PlayerSoundImportance = 10000 ; |
| 96 |
DefaultCreatureSoundImportance = 1000 ; |
| 97 |
MinorNonSpatialSoundImportance = 100 ; |
| 98 |
DefaultSoundImportance = 10 ; |
| 99 |
|
| 100 |
{ globals -------------------------------------------------------------------- } |
| 101 |
|
| 102 |
function SoundEngine: TRepoSoundEngine; |
| 103 |
begin |
| 104 |
if FSoundEngine = nil then |
| 105 |
begin |
| 106 |
TRepoSoundEngine.Create; |
| 107 |
// TRepoSoundEngine.Create already assigns FSoundEngine |
| 108 |
Assert(FSoundEngine <> nil); |
| 109 |
end; |
| 110 |
|
| 111 |
Result := FSoundEngine; |
| 112 |
end; |
| 113 |
|
| 114 |
function InternalSoundEngine: TRepoSoundEngine; |
| 115 |
begin |
| 116 |
Result := FSoundEngine; |
| 117 |
end; |
| 118 |
|
| 119 |
function ImportanceToPriority(const Importance: Cardinal): Single; |
| 120 |
begin |
| 121 |
{ Deprecated importance is translated to priority using the equation |
| 122 |
|
| 123 |
priority = sqrt(importance) / sqrt(100 * 1000) ~= sqrt(importance) / 316 |
| 124 |
|
| 125 |
Note: 100 * 1000 = LevelEventSoundImportance. So we practically assume |
| 126 |
it is the maximum. |
| 127 |
|
| 128 |
Thus |
| 129 |
- importance = 100 * 1000 is equivalent to priority = 1.0 (max). |
| 130 |
- importance = 10 * 1000 is equivalent to priority = 0.31. |
| 131 |
- importance = 10 (DefaultSoundImportance) is equivalent to priority = 0.01. |
| 132 |
|
| 133 |
This maps the expected importance values into sensible range. |
| 134 |
And it means that DefaultSoundImportance will not get rounded to zero |
| 135 |
even with backends like FMOD or Wwise that put limits on priority granularity |
| 136 |
(FMOD has only 257 levels, Wwise has only 100 levels). } |
| 137 |
|
| 138 |
{$warnings off} // using deprecated LevelEventSoundImportance |
| 139 |
Result := Sqrt(Importance) / Sqrt(LevelEventSoundImportance); |
| 140 |
{$warnings on} |
| 141 |
end; |
| 142 |
|
| 143 |
function PriorityToImportance(const Priority: Single): Cardinal; |
| 144 |
begin |
| 145 |
// just invert ImportanceToPriority |
| 146 |
{$warnings off} // using deprecated LevelEventSoundImportance |
| 147 |
Result := Round(Sqr(Priority * Sqrt(LevelEventSoundImportance))); |
| 148 |
{$warnings on} |
| 149 |
end; |
| 150 |
|
| 151 |
{$endif read_implementation} |