forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.Sound.pas
364 lines (310 loc) · 10 KB
/
Game.Sound.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
unit Game.Sound;
{$include lem_directives.inc}
interface
uses
Dialogs,
LCLIntf,// SDL,
Classes, Contnrs, SysUtils, Math,
Base.Utils,
Prog.Types, Prog.Base, Prog.Data,
BASS;
type
TSound = class;
TMusic = class;
SoundLibrary = class
strict private
class var fLibEnabled : Boolean;
private
// sound handles
class function CreateSoundHandle(aSound: TSound): DWORD; static;
class procedure FreeSoundHandle(aSound: TSound); static;
// music handles
class function CreateMusicHandle(aMusic: TMusic; aType: TMusicStreamType): DWORD; static;
class procedure FreeMusicHandle(aMusic: TMusic); static;
// play
class procedure PlaySound(aSound: TSound); static;
// class procedure StopSound(aSound: TSound); static; not (yet) used so commented out interface and implementation
class procedure PlayMusic(aMusic: TMusic); static;
class procedure StopMusic(aMusic: TMusic); static;
class procedure SetMusicVolume(aMusic: TMusic; aVolume: Single); static;
public
class procedure Init; static; // must be called at startup
class procedure Done; static;
class property LibEnabled: Boolean read fLibEnabled;
end;
TAbstractSound = class abstract
strict protected
fDataPtr : Pointer; // owned by class
fDataSize : Integer;
fHandle : DWORD; // owned by soundlibrary (BASS)
public
end;
TSound = class sealed(TAbstractSound)
public
constructor Create(const aFileName: string);
destructor Destroy; override;
procedure Play; inline;
property DataPtr: Pointer read fDataPtr;
property DataSize: Integer read fDataSize;
property Handle: DWORD read fHandle;
end;
TMusic = class(TAbstractSound)
private
fVolume: Single;
fStreamType: TMusicStreamType;
fIsPlaying: Boolean;
procedure SetVolume(aValue: Single); inline;
public
constructor Create(const aFileName: string; aType: TMusicStreamType; aVolume: Single = 0.2);
destructor Destroy; override;
procedure Play; inline;
procedure Stop; inline;
property Volume: Single read fVolume write SetVolume;
property DataPtr: Pointer read fDataPtr;
property DataSize: Integer read fDataSize;
property Handle: DWORD read fHandle;
property IsPlaying: Boolean read fIsPlaying;
end;
TSoundList = class(TFastObjectList<TSound>);
TMusicList = class(TFastObjectList<TMusic>);
// we create, manage and play sounds from here (by index)
TSoundMgr = class
private
fSounds : TSoundList;
fMusics : TMusicList;
function GetMusicIsPlaying(index: Integer): Boolean; inline;
protected
public
constructor Create;
destructor Destroy; override;
// sounds
function AddSoundFromFileName(const aFileName: string): Integer;
procedure ClearSounds;
procedure PlaySound(index: Integer);
procedure StopSound(index: Integer);
// musics
function AddMusicFromFileName(const aFileName: string; aType: TMusicStreamType; aVolume: Single = 0.2): Integer;
procedure ClearMusics;
procedure PlayMusic(index: Integer);
procedure StopMusic(index: Integer);
function GetMusicVolumne(index: Integer): Single;
procedure SetMusicVolume(index: Integer; const aVolume: Single);
property MusicIsPlaying[index: Integer]: Boolean read GetMusicIsPlaying;
end;
implementation
{ SoundLibrary }
class procedure SoundLibrary.Init;
begin
// check version
if (HI(BASS_GetVersion) <> BASSVERSION) then
Exit;
// Initialize audio: default device, 44100hz, stereo, 16 bits
fLibEnabled := BASS_Init(-1, 44100, 0, 0, nil);
end;
class procedure SoundLibrary.Done;
begin
if fLibEnabled then
BASS_Free;
end;
class function SoundLibrary.CreateSoundHandle(aSound: TSound): DWORD;
// bass load from memory pointer. Note: bass owns the handle, TSound owns the memory
begin
if not fLibEnabled or (aSound.DataPtr = nil) or (aSound.DataSize = 0) then
Exit(0);
Result := BASS_StreamCreateFile(True, aSound.DataPtr, 0, aSound.DataSize, BASS_UNICODE);
end;
class procedure SoundLibrary.FreeSoundHandle(aSound: TSound);
begin
if not fLibEnabled or (aSound.Handle = 0) then
Exit;
BASS_StreamFree(aSound.Handle);
end;
class function SoundLibrary.CreateMusicHandle(aMusic: TMusic; aType: TMusicStreamType): DWORD;
// bass load from memory pointer. Note: bass owns the handle, TMusic owns the memory
begin
Result := 0;
if not fLibEnabled or (aMusic.DataPtr = nil) or (aMusic.DataSize = 0) then
Exit;
case aType of
TMusicStreamType.&MOD:
Result := BASS_MusicLoad(True, aMusic.DataPtr, 0, aMusic.DataSize, BASS_MUSIC_RAMP or BASS_MUSIC_LOOP or BASS_MUSIC_SURROUND or BASS_UNICODE, 1);
TMusicStreamType.MP3:
Result := BASS_StreamCreateFile(True, aMusic.DataPtr, 0, aMusic.DataSize, BASS_UNICODE);//BASS_MUSIC_RAMP or BASS_MUSIC_LOOP or BASS_MUSIC_SURROUND or BASS_UNICODE);
end;
end;
class procedure SoundLibrary.FreeMusicHandle(aMusic: TMusic);
begin
if not fLibEnabled or (aMusic.Handle = 0) then
Exit;
BASS_MusicFree(aMusic.Handle);
end;
class procedure SoundLibrary.PlaySound(aSound: TSound);
var
tmpHandle: DWORD;
begin
if (aSound.DataPtr = nil) or (aSound.DataSize <= 0) then
Exit;
if fLibEnabled then begin
// overlapping sounds
if (BASS_ChannelIsActive(aSound.Handle) = BASS_ACTIVE_PLAYING)
and (BASS_ChannelGetPosition(aSound.Handle, BASS_POS_BYTE) >= BASS_ChannelSeconds2Bytes(aSound.Handle, 0.1)) then begin
tmpHandle := BASS_StreamCreateFile(True, aSound.DataPtr, 0, aSound.DataSize, BASS_UNICODE or BASS_STREAM_AUTOFREE);
BASS_ChannelPlay(tmpHandle, True);
Exit;
end;
BASS_ChannelPlay(aSound.Handle, True);
end;
//else
// Winapi.MMSystem.PlaySound(aSound.DataPtr, 0, SND_ASYNC or SND_MEMORY); // fallback for sounds on the winapi
end;
//class procedure SoundLibrary.StopSound(aSound: TSound);
//begin
// if (aSound.DataPtr = nil) or (aSound.DataSize <= 0) then
// Exit;
// if fEnabled then
// BASS_ChannelStop(aSound.Handle)
// else
// Winapi.MMSystem.PlaySound(nil, 0, 0); // fallback for sounds on the winapi
//end;
class procedure SoundLibrary.PlayMusic(aMusic: TMusic);
begin
if fLibEnabled and (aMusic.DataPtr <> nil) and (aMusic.DataSize > 0) then begin
BASS_ChannelPlay(aMusic.Handle, True);
BASS_ChannelSetAttribute(aMusic.Handle, BASS_ATTRIB_VOL, aMusic.Volume);
end;
end;
class procedure SoundLibrary.StopMusic(aMusic: TMusic);
begin
if fLibEnabled and (aMusic.DataPtr <> nil) and (aMusic.DataSize > 0) then begin
BASS_ChannelStop(aMusic.Handle); // todo: try sliding down
end;
end;
class procedure SoundLibrary.SetMusicVolume(aMusic: TMusic; aVolume: Single);
begin
EnsureRange(aVolume, 0.0, 1.0);
BASS_ChannelSetAttribute(aMusic.Handle, BASS_ATTRIB_VOL, aVolume);
end;
{ TSound }
constructor TSound.Create(const aFileName: string);
begin
inherited Create;
fDataPtr := TData.CreatePointer(Consts.StyleName, aFileName, TDataType.Sound, {out}fDataSize);
fHandle := SoundLibrary.CreateSoundHandle(Self);
end;
destructor TSound.Destroy;
begin
SoundLibrary.FreeSoundHandle(Self);
if Assigned(fDataPtr) then
FreeMem(fDataPtr);
inherited;
end;
procedure TSound.Play;
begin
SoundLibrary.PlaySound(Self);
end;
{ TMusic }
constructor TMusic.Create(const aFileName: string; aType: TMusicStreamType; aVolume: Single = 0.2);
begin
inherited Create;
fStreamType := aType;
fDataPtr := TData.CreatePointer(Consts.StyleName, aFileName, TDataType.Music, {out} fDataSize, True); // do not cache these
fHandle := SoundLibrary.CreateMusicHandle(Self, fStreamType);
fVolume := aVolume;
end;
destructor TMusic.Destroy;
begin
SoundLibrary.FreeMusicHandle(Self);
if Assigned(fDataPtr) then
FreeMem(fDataPtr);
inherited;
end;
procedure TMusic.Play;
begin
SoundLibrary.PlayMusic(Self);
fIsPlaying := True;
end;
procedure TMusic.Stop;
begin
SoundLibrary.StopMusic(Self);
fIsPlaying := False;
end;
procedure TMusic.SetVolume(aValue: Single);
begin
Restrict(aValue, 0.0, 1.0);
if fVolume = aValue then
Exit;
fVolume := aValue;
SoundLibrary.SetMusicVolume(Self, fVolume);
end;
{ TSoundMgr }
constructor TSoundMgr.Create;
begin
inherited Create;
fSounds := TSoundList.Create;
fMusics := TMusicList.Create;
end;
destructor TSoundMgr.Destroy;
begin
fSounds.Free;
fMusics.Free;
inherited Destroy;
end;
function TSoundMgr.AddSoundFromFileName(const aFileName: string): Integer;
var
snd: TSound;
begin
snd := TSound.Create(aFileName);
Result := fSounds.Add(snd);
end;
function TSoundMgr.GetMusicIsPlaying(index: Integer): Boolean;
begin
Result := fMusics.ValidIndex(index) and fMusics[index].IsPlaying;
end;
procedure TSoundMgr.PlaySound(index: Integer);
begin
if fSounds.ValidIndex(index) then
fSounds[index].Play;
end;
procedure TSoundMgr.StopSound(Index: Integer);
begin
if fMusics.ValidIndex(index) then
SoundLibrary.StopMusic(fMusics[index]);
end;
procedure TSoundMgr.PlayMusic(index: Integer);
begin
if fMusics.ValidIndex(index) then
fMusics[index].Play;
end;
function TSoundMgr.GetMusicVolumne(index: Integer): Single;
begin
if fMusics.ValidIndex(index) then
Result := fMusics[index].Volume
else
Result := 0.0;
end;
procedure TSoundMgr.SetMusicVolume(index: Integer; const aVolume: Single);
begin
if fMusics.ValidIndex(index) then
fMusics[index].Volume := aVolume;
end;
procedure TSoundMgr.StopMusic(index: Integer);
begin
if fMusics.ValidIndex(index) then
fMusics[index].Stop;
end;
function TSoundMgr.AddMusicFromFileName(const aFileName: string; aType: TMusicStreamType; aVolume: Single = 0.2): Integer;
var
M: TMusic;
begin
M := TMusic.Create(aFileName, aType, aVolume);
Result := fMusics.Add(M);
end;
procedure TSoundMgr.ClearMusics;
begin
fMusics.Clear;
end;
procedure TSoundMgr.ClearSounds;
begin
fSounds.Clear;
end;
end.