8623b5f7ebd5294487650f3cb1c7a16b64617f77
1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
29 TPlayableSound
= class(TBasicSound
)
35 destructor Destroy(); override;
36 function Play(Force
: Boolean = False): Boolean;
37 function PlayAt(X
, Y
: Integer): Boolean;
38 function PlayPanVolume(Pan
, Volume
: Single; Force
: Boolean = False): Boolean;
39 function PlayVolumeAt(X
, Y
: Integer; Volume
: Single): Boolean;
40 function PlayVolumeAtRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
41 function SetByName(SN
: String): Boolean;
42 function SetCoords(X
, Y
: Integer; Volume
: Single): Boolean;
43 function SetCoordsRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
45 property Loop
: Boolean read FMusic write FMusic
;
46 property Name
: String read FName
;
49 TMusic
= class(TBasicSound
)
52 FSpecPause
: Boolean; // Ñïåö-ïàóçà. "Ñèëüíåå" îáû÷íîé
55 procedure SetSpecPause(Enable
: Boolean);
59 destructor Destroy(); override;
60 function Play(Force
: Boolean = False): Boolean;
61 function SetByName(SN
: String): Boolean;
62 function IsPaused(): Boolean;
63 procedure Pause(Enable
: Boolean);
65 property Name
: String read FName
;
66 property SpecPause
: Boolean read FSpecPause write SetSpecPause
;
67 property NoMusic
: Boolean read FNoMusic
;
70 function g_Sound_PlayEx(SoundName
: ShortString): Boolean;
71 function g_Sound_PlayExPanVolume(SoundName
: ShortString; Pan
: Single; Volume
: Single): Boolean;
72 function g_Sound_PlayAt(ID
: DWORD
; X
, Y
: Integer): Boolean;
73 function g_Sound_PlayExAt(SoundName
: ShortString; X
, Y
: Integer): Boolean;
75 function g_Sound_CreateWAD(var ID
: DWORD
; Resource
: string; isMusic
: Boolean = False): Boolean;
76 function g_Sound_CreateWADEx(SoundName
: ShortString; Resource
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
77 function g_Sound_CreateFile(var ID
: DWORD
; FileName
: string; isMusic
: Boolean = False): Boolean;
78 function g_Sound_CreateFileEx(SoundName
: ShortString; FileName
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
80 procedure g_Sound_Delete(SoundName
: ShortString);
81 function g_Sound_Exists(SoundName
: string): Boolean;
82 function g_Sound_Get(var ID
: DWORD
; SoundName
: ShortString): Boolean;
84 procedure g_Sound_SetupAllVolumes(SoundVol
, MusicVol
: Byte);
89 e_log
, SysUtils
, g_console
, g_options
, wadreader
,
90 g_game
, g_basic
, g_items
, g_map
, Math
,
101 SoundArray
: Array of TGameSound
;
102 //SoundsMuted: Boolean = False;
105 function FindSound(): DWORD
;
109 if SoundArray
<> nil then
110 for i
:= 0 to High(SoundArray
) do
111 if SoundArray
[i
].Name
= '' then
117 if SoundArray
= nil then
119 SetLength(SoundArray
, 8);
124 Result
:= High(SoundArray
) + 1;
125 SetLength(SoundArray
, Length(SoundArray
) + 8);
129 function g_Sound_PlayEx(SoundName
: ShortString): Boolean;
134 if SoundArray
= nil then
137 for a
:= 0 to High(SoundArray
) do
138 if SoundArray
[a
].Name
= SoundName
then
140 Result
:= (e_PlaySoundVolume(SoundArray
[a
].ID
, gSoundLevel
/255.0) >= 0);
144 e_WriteLog(Format(_lc
[I_GAME_ERROR_SOUND
], [SoundName
]), TMsgType
.Warning
);
147 function g_Sound_PlayExPanVolume(SoundName
: ShortString; Pan
: Single; Volume
: Single): Boolean;
152 if SoundArray
= nil then
155 for a
:= 0 to High(SoundArray
) do
156 if SoundArray
[a
].Name
= SoundName
then
158 Result
:= (e_PlaySoundPanVolume(SoundArray
[a
].ID
, Pan
, Volume
* (gSoundLevel
/255.0)) >= 0);
162 e_WriteLog(Format(_lc
[I_GAME_ERROR_SOUND
], [SoundName
]), TMsgType
.Warning
);
165 function PlaySoundAtRect (X
, Y
, W
, H
: Integer; out Pan
, Volume
: Single; InVolume
: Single = 1.0): Boolean;
171 procedure CalcDest (const p
: THearPoint
; out pan
: Single; out len
: Integer);
172 var XX
, YY
, lx
, rx
: Integer;
174 pan
:= 0.0; len
:= gMaxDist
;
177 XX
:= Max(X
, Min(X
+ W
, p
.Coords
.X
));
178 YY
:= Max(Y
, Min(Y
+ H
, p
.Coords
.Y
));
179 len
:= Round(Hypot(XX
- p
.Coords
.X
, YY
- p
.Coords
.Y
));
180 if sMaxDist
< SOUND_MINDIST
then
182 lx
:= X
- SOUND_MINDIST
;
183 rx
:= X
+ W
+ SOUND_MINDIST
;
184 if p
.Coords
.X
< lx
then
185 pan
:= (lx
- p
.Coords
.X
) / sMaxDist
186 else if p
.Coords
.X
> rx
then
187 pan
:= (rx
- p
.Coords
.X
) / sMaxDist
193 ASSERT((W
>= 0) and (H
>= 0));
194 ASSERT((InVolume
>= 0.0) and (InVolume
<= 1.0));
195 sMaxDist
:= SOUND_MAXDIST
* InVolume
;
196 X
:= Max(0, Min(X
, gMapInfo
.Width
));
197 Y
:= Max(0, Min(Y
, gMapInfo
.Height
));
198 CalcDest(gHearPoint1
, pan1
, len1
);
199 CalcDest(gHearPoint2
, pan2
, len2
);
205 if len1
>= sMaxDist
then
214 Volume
:= 1.0 - len1
/ sMaxDist
;
219 function PlaySoundAt(X
, Y
: Integer; out Pan
: Single; out Volume
: Single; InVolume
: Single = 1.0): Boolean;
221 Result
:= PlaySoundAtRect(X
, Y
, 0, 0, Pan
, Volume
, InVolume
)
224 function g_Sound_PlayAt(ID
: DWORD
; X
, Y
: Integer): Boolean;
228 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
229 Result
:= (e_PlaySoundPanVolume(ID
, Pan
, Vol
* (gSoundLevel
/255.0)) >= 0)
234 function g_Sound_PlayExAt(SoundName
: ShortString; X
, Y
: Integer): Boolean;
241 if SoundArray
= nil then
244 for a
:= 0 to High(SoundArray
) do
245 if SoundArray
[a
].Name
= SoundName
then
247 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
248 Result
:= (e_PlaySoundPanVolume(SoundArray
[a
].ID
, Pan
, Vol
* (gSoundLevel
/255.0)) >= 0);
252 e_WriteLog(Format(_lc
[I_GAME_ERROR_SOUND
], [SoundName
]), TMsgType
.Warning
);
255 function g_Sound_CreateFile(var ID
: DWORD
; FileName
: string; isMusic
: Boolean = False): Boolean;
257 Result
:= e_LoadSound(FileName
, ID
, isMusic
);
260 function g_Sound_CreateFileEx(SoundName
: ShortString; FileName
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
266 find_id
:= FindSound();
268 if not e_LoadSound(FileName
, SoundArray
[find_id
].ID
, isMusic
, ForceNoLoop
) then
271 SoundArray
[find_id
].Name
:= SoundName
;
272 SoundArray
[find_id
].IsMusic
:= isMusic
;
277 function g_Sound_CreateWAD(var ID
: DWORD
; Resource
: string; isMusic
: Boolean = False): Boolean;
288 // e_WriteLog('Loading sound: ' + Resource, MSG_NOTIFY);
289 FileName
:= g_ExtractWadName(Resource
);
291 WAD
:= TWADFile
.Create();
292 WAD
.ReadFile(FileName
);
294 if WAD
.GetResource(g_ExtractFilePathName(Resource
), SoundData
, ResLength
) then
296 if e_LoadSoundMem(SoundData
, ResLength
, ID
, isMusic
) then
303 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
311 e_WriteLog(Format('Error loading music %s', [Resource
]), TMsgType
.Warning
)
313 e_WriteLog(Format('Error loading sound %s', [Resource
]), TMsgType
.Warning
);
320 function g_Sound_CreateWADEx(SoundName
: ShortString; Resource
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
332 // e_WriteLog('Loading sound: ' + Resource, MSG_NOTIFY);
333 FileName
:= g_ExtractWadName(Resource
);
335 find_id
:= FindSound();
337 WAD
:= TWADFile
.Create();
338 WAD
.ReadFile(FileName
);
340 if WAD
.GetResource(g_ExtractFilePathName(Resource
), SoundData
, ResLength
) then
342 if e_LoadSoundMem(SoundData
, ResLength
, SoundArray
[find_id
].ID
, isMusic
, ForceNoLoop
) then
344 SoundArray
[find_id
].Name
:= SoundName
;
345 SoundArray
[find_id
].IsMusic
:= isMusic
;
353 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
361 e_WriteLog(Format('Error loading music %s', [Resource
]), TMsgType
.Warning
)
363 e_WriteLog(Format('Error loading sound %s', [Resource
]), TMsgType
.Warning
);
370 procedure g_Sound_Delete(SoundName
: ShortString);
374 if (SoundArray
= nil) or (SoundName
= '') then
377 for a
:= 0 to High(SoundArray
) do
378 if SoundArray
[a
].Name
= SoundName
then
380 e_DeleteSound(SoundArray
[a
].ID
);
381 SoundArray
[a
].Name
:= '';
382 SoundArray
[a
].ID
:= 0;
383 SoundArray
[a
].IsMusic
:= False;
387 function g_Sound_Exists(SoundName
: string): Boolean;
393 if SoundName
= '' then
396 if SoundArray
<> nil then
397 for a
:= 0 to High(SoundArray
) do
398 if SoundArray
[a
].Name
= SoundName
then
405 function g_Sound_Get(var ID
: DWORD
; SoundName
: ShortString): Boolean;
411 if SoundName
= '' then
414 if SoundArray
<> nil then
415 for a
:= 0 to High(SoundArray
) do
416 if SoundArray
[a
].Name
= SoundName
then
418 ID
:= SoundArray
[a
].ID
;
424 procedure g_Sound_SetupAllVolumes(SoundVol
, MusicVol
: Byte);
429 Mvol
:= 0; // shut up, compiler
430 if (gSoundLevel
= SoundVol
) and (gMusicLevel
= MusicVol
) then
433 if gSoundLevel
> 0 then
435 Svol
:= SoundVol
/ gSoundLevel
;
440 Svol
:= SoundVol
/ 255.0;
444 if gMusic
<> nil then
445 if gMusicLevel
> 0 then
446 Mvol
:= gMusic
.GetVolume() * MusicVol
/ gMusicLevel
448 Mvol
:= MusicVol
/ 255.0;
450 e_ModifyChannelsVolumes(Svol
, sm
);
452 if gMusic
<> nil then
453 gMusic
.SetVolume(Mvol
);
455 gSoundLevel
:= SoundVol
;
456 gMusicLevel
:= MusicVol
;
461 constructor TPlayableSound
.Create();
467 destructor TPlayableSound
.Destroy();
472 function TPlayableSound
.Play(Force
: Boolean = False): Boolean;
474 if Force
or not IsPlaying() then
477 Result
:= RawPlay(0.0, gSoundLevel
/255.0, FPosition
);
483 function TPlayableSound
.PlayAt(X
, Y
: Integer): Boolean;
487 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
490 Result
:= RawPlay(Pan
, Vol
* (gSoundLevel
/255.0), FPosition
);
496 function TPlayableSound
.PlayPanVolume(Pan
, Volume
: Single; Force
: Boolean = False): Boolean;
498 if Force
or not IsPlaying() then
501 Result
:= RawPlay(Pan
, Volume
* (gSoundLevel
/255.0), FPosition
);
507 function TPlayableSound
.PlayVolumeAtRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
508 var Pan
, Vol
: Single;
511 if PlaySoundAtRect(X
, Y
, W
, H
, Pan
, Vol
, Volume
) then
514 Result
:= RawPlay(Pan
, Volume
* Vol
* (gSoundLevel
/ 255.0), FPosition
)
518 function TPlayableSound
.PlayVolumeAt (X
, Y
: Integer; Volume
: Single): Boolean;
520 Result
:= Self
.PlayVolumeAtRect(X
, Y
, 0, 0, Volume
)
523 function TPlayableSound
.SetCoordsRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
524 var Pan
, Vol
: Single;
526 if PlaySoundAtRect(X
, Y
, W
, H
, Pan
, Vol
, Volume
) then
528 SetVolume(Volume
* Vol
* (gSoundLevel
/ 255.0));
540 function TPlayableSound
.SetCoords(X
, Y
: Integer; Volume
: Single): Boolean;
542 Result
:= Self
.SetCoordsRect(X
, Y
, 0, 0, Volume
)
545 function TPlayableSound
.SetByName(SN
: String): Boolean;
549 if g_Sound_Get(id
, SN
) then
561 constructor TMusic
.Create();
569 destructor TMusic
.Destroy();
574 function TMusic
.Play(Force
: Boolean = False): Boolean;
582 if Force
or not IsPlaying() then
585 Result
:= RawPlay(0.0, gMusicLevel
/255.0, FPosition
);
588 if Result
and FSpecPause
then
595 function TMusic
.SetByName(SN
: String): Boolean;
606 if g_Sound_Get(id
, SN
) then
618 function TMusic
.IsPaused(): Boolean;
620 Result
:= inherited IsPaused();
621 Result
:= Result
or FSpecPause
;
624 procedure TMusic
.Pause(Enable
: Boolean);
626 // Îòêëþ÷àåì ïàóçó, òîëüêî åñëè íå áûëî ñïåö-ïàóçû:
627 if Enable
or (not FSpecPause
) then
628 inherited Pause(Enable
);
631 procedure TMusic
.SetSpecPause(Enable
: Boolean);
633 FSpecPause
:= Enable
;