2cc02ad2aec4672815a071e1deafc2064b61495f
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
;
217 e_LogWritefln('PlaySounAtRect: Pan = %s | Volume = %s | Result = %s', [Pan
, Volume
, Result
]);
220 function PlaySoundAt(X
, Y
: Integer; out Pan
: Single; out Volume
: Single; InVolume
: Single = 1.0): Boolean;
222 Result
:= PlaySoundAtRect(X
, Y
, 0, 0, Pan
, Volume
, InVolume
)
225 function g_Sound_PlayAt(ID
: DWORD
; X
, Y
: Integer): Boolean;
229 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
230 Result
:= (e_PlaySoundPanVolume(ID
, Pan
, Vol
* (gSoundLevel
/255.0)) >= 0)
235 function g_Sound_PlayExAt(SoundName
: ShortString; X
, Y
: Integer): Boolean;
242 if SoundArray
= nil then
245 for a
:= 0 to High(SoundArray
) do
246 if SoundArray
[a
].Name
= SoundName
then
248 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
249 Result
:= (e_PlaySoundPanVolume(SoundArray
[a
].ID
, Pan
, Vol
* (gSoundLevel
/255.0)) >= 0);
253 e_WriteLog(Format(_lc
[I_GAME_ERROR_SOUND
], [SoundName
]), TMsgType
.Warning
);
256 function g_Sound_CreateFile(var ID
: DWORD
; FileName
: string; isMusic
: Boolean = False): Boolean;
258 Result
:= e_LoadSound(FileName
, ID
, isMusic
);
261 function g_Sound_CreateFileEx(SoundName
: ShortString; FileName
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
267 find_id
:= FindSound();
269 if not e_LoadSound(FileName
, SoundArray
[find_id
].ID
, isMusic
, ForceNoLoop
) then
272 SoundArray
[find_id
].Name
:= SoundName
;
273 SoundArray
[find_id
].IsMusic
:= isMusic
;
278 function g_Sound_CreateWAD(var ID
: DWORD
; Resource
: string; isMusic
: Boolean = False): Boolean;
289 // e_WriteLog('Loading sound: ' + Resource, MSG_NOTIFY);
290 FileName
:= g_ExtractWadName(Resource
);
292 WAD
:= TWADFile
.Create();
293 WAD
.ReadFile(FileName
);
295 if WAD
.GetResource(g_ExtractFilePathName(Resource
), SoundData
, ResLength
) then
297 if e_LoadSoundMem(SoundData
, ResLength
, ID
, isMusic
) then
304 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
312 e_WriteLog(Format('Error loading music %s', [Resource
]), TMsgType
.Warning
)
314 e_WriteLog(Format('Error loading sound %s', [Resource
]), TMsgType
.Warning
);
321 function g_Sound_CreateWADEx(SoundName
: ShortString; Resource
: string; isMusic
: Boolean = False; ForceNoLoop
: Boolean = False): Boolean;
333 // e_WriteLog('Loading sound: ' + Resource, MSG_NOTIFY);
334 FileName
:= g_ExtractWadName(Resource
);
336 find_id
:= FindSound();
338 WAD
:= TWADFile
.Create();
339 WAD
.ReadFile(FileName
);
341 if WAD
.GetResource(g_ExtractFilePathName(Resource
), SoundData
, ResLength
) then
343 if e_LoadSoundMem(SoundData
, ResLength
, SoundArray
[find_id
].ID
, isMusic
, ForceNoLoop
) then
345 SoundArray
[find_id
].Name
:= SoundName
;
346 SoundArray
[find_id
].IsMusic
:= isMusic
;
354 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
362 e_WriteLog(Format('Error loading music %s', [Resource
]), TMsgType
.Warning
)
364 e_WriteLog(Format('Error loading sound %s', [Resource
]), TMsgType
.Warning
);
371 procedure g_Sound_Delete(SoundName
: ShortString);
375 if (SoundArray
= nil) or (SoundName
= '') then
378 for a
:= 0 to High(SoundArray
) do
379 if SoundArray
[a
].Name
= SoundName
then
381 e_DeleteSound(SoundArray
[a
].ID
);
382 SoundArray
[a
].Name
:= '';
383 SoundArray
[a
].ID
:= 0;
384 SoundArray
[a
].IsMusic
:= False;
388 function g_Sound_Exists(SoundName
: string): Boolean;
394 if SoundName
= '' then
397 if SoundArray
<> nil then
398 for a
:= 0 to High(SoundArray
) do
399 if SoundArray
[a
].Name
= SoundName
then
406 function g_Sound_Get(var ID
: DWORD
; SoundName
: ShortString): Boolean;
412 if SoundName
= '' then
415 if SoundArray
<> nil then
416 for a
:= 0 to High(SoundArray
) do
417 if SoundArray
[a
].Name
= SoundName
then
419 ID
:= SoundArray
[a
].ID
;
425 procedure g_Sound_SetupAllVolumes(SoundVol
, MusicVol
: Byte);
430 Mvol
:= 0; // shut up, compiler
431 if (gSoundLevel
= SoundVol
) and (gMusicLevel
= MusicVol
) then
434 if gSoundLevel
> 0 then
436 Svol
:= SoundVol
/ gSoundLevel
;
441 Svol
:= SoundVol
/ 255.0;
445 if gMusic
<> nil then
446 if gMusicLevel
> 0 then
447 Mvol
:= gMusic
.GetVolume() * MusicVol
/ gMusicLevel
449 Mvol
:= MusicVol
/ 255.0;
451 e_ModifyChannelsVolumes(Svol
, sm
);
453 if gMusic
<> nil then
454 gMusic
.SetVolume(Mvol
);
456 gSoundLevel
:= SoundVol
;
457 gMusicLevel
:= MusicVol
;
462 constructor TPlayableSound
.Create();
468 destructor TPlayableSound
.Destroy();
473 function TPlayableSound
.Play(Force
: Boolean = False): Boolean;
475 if Force
or not IsPlaying() then
478 Result
:= RawPlay(0.0, gSoundLevel
/255.0, FPosition
);
484 function TPlayableSound
.PlayAt(X
, Y
: Integer): Boolean;
488 if PlaySoundAt(X
, Y
, Pan
, Vol
) then
491 Result
:= RawPlay(Pan
, Vol
* (gSoundLevel
/255.0), FPosition
);
497 function TPlayableSound
.PlayPanVolume(Pan
, Volume
: Single; Force
: Boolean = False): Boolean;
499 if Force
or not IsPlaying() then
502 Result
:= RawPlay(Pan
, Volume
* (gSoundLevel
/255.0), FPosition
);
508 function TPlayableSound
.PlayVolumeAtRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
509 var Pan
, Vol
: Single;
512 if PlaySoundAtRect(X
, Y
, W
, H
, Pan
, Vol
, Volume
) then
515 Result
:= RawPlay(Pan
, Volume
* Vol
* (gSoundLevel
/ 255.0), FPosition
)
519 function TPlayableSound
.PlayVolumeAt (X
, Y
: Integer; Volume
: Single): Boolean;
521 Result
:= Self
.PlayVolumeAtRect(X
, Y
, 0, 0, Volume
)
524 function TPlayableSound
.SetCoordsRect (X
, Y
, W
, H
: Integer; Volume
: Single): Boolean;
525 var Pan
, Vol
: Single;
527 if PlaySoundAtRect(X
, Y
, W
, H
, Pan
, Vol
, Volume
) then
529 SetVolume(Volume
* Vol
* (gSoundLevel
/ 255.0));
541 function TPlayableSound
.SetCoords(X
, Y
: Integer; Volume
: Single): Boolean;
543 Result
:= Self
.SetCoordsRect(X
, Y
, 0, 0, Volume
)
546 function TPlayableSound
.SetByName(SN
: String): Boolean;
550 if g_Sound_Get(id
, SN
) then
562 constructor TMusic
.Create();
570 destructor TMusic
.Destroy();
575 function TMusic
.Play(Force
: Boolean = False): Boolean;
583 if Force
or not IsPlaying() then
586 Result
:= RawPlay(0.0, gMusicLevel
/255.0, FPosition
);
589 if Result
and FSpecPause
then
596 function TMusic
.SetByName(SN
: String): Boolean;
607 if g_Sound_Get(id
, SN
) then
619 function TMusic
.IsPaused(): Boolean;
621 Result
:= inherited IsPaused();
622 Result
:= Result
or FSpecPause
;
625 procedure TMusic
.Pause(Enable
: Boolean);
627 // Îòêëþ÷àåì ïàóçó, òîëüêî åñëè íå áûëî ñïåö-ïàóçû:
628 if Enable
or (not FSpecPause
) then
629 inherited Pause(Enable
);
632 procedure TMusic
.SetSpecPause(Enable
: Boolean);
634 FSpecPause
:= Enable
;