| 1 |
{%MainUnit castlesoundengine.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2024 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 // allow type declaration to continue |
| 20 |
|
| 21 |
{ Looping sound management, to easily play music or other looping sounds. |
| 22 |
|
| 23 |
Instance of this class should be created only internally |
| 24 |
by the TRepoSoundEngine. User code should use this only by accessing |
| 25 |
@link(TRepoSoundEngine.LoopingChannel). } |
| 26 |
TLoopingChannel = class(TComponent) |
| 27 |
private |
| 28 |
{ Engine that owns this. } |
| 29 |
FEngine: TRepoSoundEngine; |
| 30 |
|
| 31 |
{ @nil if we don't play sound right now. |
| 32 |
This may happen for many reasons -- e.g. because sound context is not open, |
| 33 |
or Sound = nil, or had URL = '' in RepositoryURL (sounds XML file), |
| 34 |
or TInternalSoundSource instance was necessary for higher-priority sounds. } |
| 35 |
FAllocatedSource: TInternalSoundSource; |
| 36 |
|
| 37 |
FVolume, FPitch: Single; |
| 38 |
FSound: TCastleSound; |
| 39 |
procedure SetSound(const Value: TCastleSound); |
| 40 |
procedure AllocatedSourceRelease(Sender: TInternalSoundSource); |
| 41 |
|
| 42 |
{ Called by ContextOpen. You should check here if |
| 43 |
Sound <> nil and eventually initialize FAllocatedSource. } |
| 44 |
procedure AllocateSource; |
| 45 |
function GetVolume: Single; |
| 46 |
procedure SetVolume(const Value: Single); |
| 47 |
function GetPitch: Single; |
| 48 |
procedure SetPitch(const Value: Single); |
| 49 |
procedure SoundChange(const Sender: TCastleSound; const Change: TSoundChange); |
| 50 |
protected |
| 51 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 52 |
public |
| 53 |
const |
| 54 |
DefaultVolume = 1.0; |
| 55 |
{$ifdef FPC}DefaultMusicVolume = 1.0 deprecated 'use DefaultVolume';{$endif} |
| 56 |
|
| 57 |
constructor Create(AnEngine: TRepoSoundEngine); reintroduce; |
| 58 |
destructor Destroy; override; |
| 59 |
|
| 60 |
{ Currently played looping sound. Set to @nil to just stop playing. |
| 61 |
|
| 62 |
This only plays non-spatial sounds, as it doesn't give any control over |
| 63 |
the sound 3D position. |
| 64 |
Instead of this method, use TCastleSoundSource |
| 65 |
(attached to some TCastleTransform) for spatial sounds to control their 3D position. } |
| 66 |
property Sound: TCastleSound read FSound write SetSound; |
| 67 |
|
| 68 |
{ Volume. This must always be within 0..1 range. |
| 69 |
0.0 means that there is no music (this case should be optimized).} |
| 70 |
property Volume: Single read GetVolume write SetVolume {$ifdef FPC}default DefaultVolume{$endif}; |
| 71 |
|
| 72 |
property Pitch: Single read GetPitch write SetPitch; |
| 73 |
end; |
| 74 |
|
| 75 |
TMusicPlayer = TLoopingChannel deprecated 'use TLoopingChannel'; |
| 76 |
|
| 77 |
{$endif read_interface} |
| 78 |
|
| 79 |
{$ifdef read_implementation} |
| 80 |
|
| 81 |
{ TLoopingChannel --------------------------------------------------------------- } |
| 82 |
|
| 83 |
constructor TLoopingChannel.Create(AnEngine: TRepoSoundEngine); |
| 84 |
begin |
| 85 |
inherited Create(nil); // TRepoSoundEngine will free us manually |
| 86 |
FVolume := DefaultVolume; |
| 87 |
FPitch := 1.0; |
| 88 |
FEngine := AnEngine; |
| 89 |
end; |
| 90 |
|
| 91 |
destructor TLoopingChannel.Destroy; |
| 92 |
begin |
| 93 |
if FAllocatedSource <> nil then |
| 94 |
FAllocatedSource.Release; |
| 95 |
inherited; |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TLoopingChannel.AllocateSource; |
| 99 |
var |
| 100 |
Parameters: TPlaySoundParameters; |
| 101 |
begin |
| 102 |
if (Sound = nil) or (Sound.InternalBuffer = nil) then Exit; |
| 103 |
|
| 104 |
Parameters := TPlaySoundParameters.Create; |
| 105 |
try |
| 106 |
Parameters.Buffer := Sound.InternalBuffer; |
| 107 |
Parameters.Spatial := false; |
| 108 |
Parameters.Loop := true; |
| 109 |
Parameters.Priority := Sound.Priority; |
| 110 |
Parameters.Pitch := Pitch * Sound.Pitch; |
| 111 |
Parameters.Volume := Volume * Sound.Volume; |
| 112 |
{$warnings off} // using unportable symbols knowingly |
| 113 |
Parameters.MinGain := Sound.MinGain; |
| 114 |
Parameters.MaxGain := Sound.MaxGain; |
| 115 |
{$warnings on} |
| 116 |
FAllocatedSource := FEngine.PlaySound(Parameters); |
| 117 |
finally FreeAndNil(Parameters) end; |
| 118 |
|
| 119 |
if FAllocatedSource <> nil then |
| 120 |
FAllocatedSource.OnRelease := |
| 121 |
{$ifdef FPC}@{$endif} AllocatedSourceRelease |
| 122 |
else |
| 123 |
{ Make a warning only if FEngine.Enabled. |
| 124 |
When FEngine.Enabled = false (like when --no-sound was passed) this is normal. } |
| 125 |
if FEngine.Enabled then |
| 126 |
WritelnWarning('Could not allocate sound source, to play looping sound %s', [ |
| 127 |
Sound.Name |
| 128 |
]); |
| 129 |
end; |
| 130 |
|
| 131 |
procedure TLoopingChannel.SetSound(const Value: TCastleSound); |
| 132 |
begin |
| 133 |
if Value <> FSound then |
| 134 |
begin |
| 135 |
if FSound <> nil then |
| 136 |
begin |
| 137 |
FSound.InternalRemoveChangeNotification({$ifdef FPC}@{$endif}SoundChange); |
| 138 |
FSound.RemoveFreeNotification(Self); |
| 139 |
if FAllocatedSource <> nil then |
| 140 |
begin |
| 141 |
FAllocatedSource.Release; |
| 142 |
{ AllocatedSourceRelease should set FAllocatedSource to nil. } |
| 143 |
Assert(FAllocatedSource = nil); |
| 144 |
end; |
| 145 |
end; |
| 146 |
|
| 147 |
FSound := Value; |
| 148 |
|
| 149 |
if FSound <> nil then |
| 150 |
begin |
| 151 |
FSound.InternalAddChangeNotification({$ifdef FPC}@{$endif}SoundChange); |
| 152 |
FSound.FreeNotification(Self); |
| 153 |
AllocateSource; |
| 154 |
end; |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
procedure TLoopingChannel.Notification(AComponent: TComponent; Operation: TOperation); |
| 159 |
begin |
| 160 |
inherited; |
| 161 |
{ set to nil by setter to clean nicely, and stop playing } |
| 162 |
if (Operation = opRemove) and (AComponent = FSound) then |
| 163 |
Sound := nil; |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TLoopingChannel.SoundChange(const Sender: TCastleSound; const Change: TSoundChange); |
| 167 |
begin |
| 168 |
case Change of |
| 169 |
scBeforeBufferDestroyed: |
| 170 |
begin |
| 171 |
if FAllocatedSource <> nil then |
| 172 |
FAllocatedSource.Release; |
| 173 |
Assert(FAllocatedSource = nil); |
| 174 |
end; |
| 175 |
scAfterBufferCreated: |
| 176 |
AllocateSource; |
| 177 |
scAfterOther: |
| 178 |
begin |
| 179 |
if FAllocatedSource <> nil then |
| 180 |
begin |
| 181 |
FAllocatedSource.Priority := Sound.Priority; |
| 182 |
FAllocatedSource.Pitch := Pitch * Sound.Pitch; |
| 183 |
FAllocatedSource.Volume := Volume * Sound.Volume; |
| 184 |
{$warnings off} // using unportable symbols knowingly |
| 185 |
FAllocatedSource.MinGain := Sound.MinGain; |
| 186 |
FAllocatedSource.MaxGain := Sound.MaxGain; |
| 187 |
{$warnings on} |
| 188 |
end; |
| 189 |
end; |
| 190 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 191 |
else raise EInternalError.Create('TLoopingChannel.SoundChange:Change'); |
| 192 |
{$endif} |
| 193 |
end; |
| 194 |
end; |
| 195 |
|
| 196 |
procedure TLoopingChannel.AllocatedSourceRelease(Sender: TInternalSoundSource); |
| 197 |
begin |
| 198 |
Assert(Sender = FAllocatedSource); |
| 199 |
FAllocatedSource := nil; |
| 200 |
end; |
| 201 |
|
| 202 |
function TLoopingChannel.GetVolume: Single; |
| 203 |
begin |
| 204 |
Result := FVolume; |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TLoopingChannel.SetVolume(const Value: Single); |
| 208 |
begin |
| 209 |
if Value <> FVolume then |
| 210 |
begin |
| 211 |
FVolume := Value; |
| 212 |
if FAllocatedSource <> nil then |
| 213 |
FAllocatedSource.Volume := Volume * Sound.Volume; |
| 214 |
end; |
| 215 |
end; |
| 216 |
|
| 217 |
function TLoopingChannel.GetPitch: Single; |
| 218 |
begin |
| 219 |
Result := FPitch; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure TLoopingChannel.SetPitch(const Value: Single); |
| 223 |
begin |
| 224 |
if Value <> FPitch then |
| 225 |
begin |
| 226 |
FPitch := Value; |
| 227 |
if FAllocatedSource <> nil then |
| 228 |
FAllocatedSource.Pitch := Pitch * Sound.Pitch; |
| 229 |
end; |
| 230 |
end; |
| 231 |
|
| 232 |
{$endif read_implementation} |