-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathApus.Engine.SoundSDL.pas
More file actions
367 lines (334 loc) · 10.9 KB
/
Copy pathApus.Engine.SoundSDL.pas
File metadata and controls
367 lines (334 loc) · 10.9 KB
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
365
366
367
// Copyright (C) Ivan Polyacov, Apus Software (ivan@apus-software.com)
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
// SDL2_mixer sound backend: covers level 1 of the audio requirements
// (play samples and stream music as-is + volume control). Its ceiling is known:
// no pitch, no loop points, panning for samples only and a single music stream
// (so music crossfade is impossible here) - these belong to the miniaudio
// backend. See Work/R-28_audio_activation.md.
unit Apus.Engine.SoundSDL;
interface
uses Apus.Engine.Sound;
type
TSoundLibSDL=class(TInterfacedObject,ISoundLib)
procedure Init(windowHandle:THandle=0);
procedure SetVolume(volumeType:TVolumeType;volume:single); // 0..1
function OpenMediaFile(fname:string;mode:TMediaLoadingMode):TMediaFile;
function PlayMedia(media:TMediaFile;const settings:TPlaySettings):TChannel;
procedure StopChannel(var channel:TChannel);
procedure SetChannelAttribute(channel:TChannel;attr:TChannelAttribute;value:single);
procedure SlideChannel(channel:TChannel;attr:TChannelAttribute;newValue:single;timeInterval:single);
procedure Pause(pause:boolean);
procedure Done;
function CanSlide:TChannelAttributes;
function CanFadeMusic:boolean;
function HasSingleMusicStream:boolean;
function IsPlaying(channel:TChannel):boolean;
end;
implementation
uses SysUtils, SDL2, sdl2_mixer,
Apus.Core,
Apus.Log,
Apus.Types;
const
// Number of simultaneously playing samples
SAMPLE_CHANNELS = 32;
// Sample channel index marking the (single) music stream
MUSIC_CHANNEL = -1;
type
TMediaFileSDL=class(TMediaFile)
chunk:PMix_Chunk; // loaded sample
music:PMix_Music; // music stream
destructor Destroy; override;
end;
// Channel objects are owned by the backend and reused: one per SDL_mixer
// channel plus one for the music stream. StopChannel only clears the caller's
// reference, it never frees the object.
TChannelSDL=class(TChannel)
sampleChannel:integer; // MUSIC_CHANNEL for the music stream
relVolume:single; // volume requested for this channel (before the global one)
constructor Create(channelIndex:integer);
end;
var
globalMusicVolume:single=1.0;
globalSoundVolume:single=1.0;
channels:array[0..SAMPLE_CHANNELS-1] of TChannelSDL;
musicChannel:TChannelSDL;
ownAudioSubsystem:boolean; // did this backend initialize SDL's audio subsystem?
{ TMediaFileSDL }
destructor TMediaFileSDL.Destroy;
begin
if chunk<>nil then Mix_FreeChunk(chunk);
if music<>nil then Mix_FreeMusic(music);
chunk:=nil;
music:=nil;
inherited;
end;
{ TChannelSDL }
constructor TChannelSDL.Create(channelIndex:integer);
begin
sampleChannel:=channelIndex;
relVolume:=1.0;
end;
{ Helpers }
// Panning: -1 = full left, 0 = center, 1 = full right
procedure SetPanning(channel:integer;pan:single);
var
left,right:integer;
begin
right:=Clamp(round(255*(1+pan)),0,255);
left:=Clamp(round(255*(1-pan)),0,255);
Mix_SetPanning(channel,left,right);
end;
procedure ApplyChannelVolume(ch:TChannelSDL);
begin
if ch.sampleChannel=MUSIC_CHANNEL then
Mix_VolumeMusic(round(ch.relVolume*globalMusicVolume*MIX_MAX_VOLUME))
else
Mix_Volume(ch.sampleChannel,round(ch.relVolume*globalSoundVolume*MIX_MAX_VOLUME));
end;
// Report which decoders are actually available in the linked SDL2_mixer build
procedure InitDecoders;
var
wanted,got:integer;
st:String8;
begin
wanted:=MIX_INIT_OGG or MIX_INIT_MP3 or MIX_INIT_FLAC or MIX_INIT_MOD or MIX_INIT_OPUS;
got:=Mix_Init(wanted);
st:='';
if got and MIX_INIT_OGG>0 then st:=st+' ogg';
if got and MIX_INIT_MP3>0 then st:=st+' mp3';
if got and MIX_INIT_FLAC>0 then st:=st+' flac';
if got and MIX_INIT_MOD>0 then st:=st+' mod';
if got and MIX_INIT_OPUS>0 then st:=st+' opus';
// wav is decoded by SDL itself, so a missing decoder is a warning, not a failure
if got=0 then
Log.Warn('[SDL_MIX] No optional decoders available: '+Mix_GetError)
else begin
Log.Info('[SDL_MIX] Decoders:'+st);
if got<>wanted then Log.Info('[SDL_MIX] Some decoders are missing: '+Mix_GetError);
end;
end;
{ TSoundLibSDL }
function TSoundLibSDL.CanSlide:TChannelAttributes;
begin
result:=[]; // SDL_mixer can only fade out, see SlideChannel
end;
function TSoundLibSDL.CanFadeMusic:boolean;
begin
result:=true;
end;
function TSoundLibSDL.HasSingleMusicStream:boolean;
begin
result:=true; // SDL_mixer mixes many samples, but plays one music stream
end;
function TSoundLibSDL.IsPlaying(channel:TChannel):boolean;
var
ch:integer;
begin
result:=false;
if channel=nil then exit;
ASSERT(channel is TChannelSDL);
ch:=TChannelSDL(channel).sampleChannel;
if ch=MUSIC_CHANNEL then
result:=Mix_PlayingMusic<>0
else
result:=Mix_Playing(ch)<>0;
end;
procedure TSoundLibSDL.Init(windowHandle:THandle);
var
i,freq,chan:integer;
format:word;
ver:PSDL_Version;
begin
Log.Info('[SDL_MIX] Init');
// The audio subsystem may already be up if the SDL platform backend is used
if SDL_WasInit(SDL_INIT_AUDIO)=0 then begin
if SDL_InitSubSystem(SDL_INIT_AUDIO)<0 then
raise EError.Create('[SDL_MIX] cannot initialize SDL audio: '+SDL_GetError);
ownAudioSubsystem:=true;
end;
InitDecoders;
if Mix_OpenAudio(44100,AUDIO_S16,2,1024)<>0 then
raise EError.Create('[SDL_MIX] open audio error: '+Mix_GetError);
Mix_AllocateChannels(SAMPLE_CHANNELS);
for i:=0 to SAMPLE_CHANNELS-1 do
if channels[i]=nil then channels[i]:=TChannelSDL.Create(i);
if musicChannel=nil then musicChannel:=TChannelSDL.Create(MUSIC_CHANNEL);
// Diagnostics: what the device actually gave us
ver:=Mix_Linked_Version;
if ver<>nil then
Log.Info('[SDL_MIX] SDL2_mixer %d.%d.%d (headers: %d.%d.%d)',
[ver.major,ver.minor,ver.patch,SDL_MIXER_MAJOR_VERSION,SDL_MIXER_MINOR_VERSION,SDL_MIXER_PATCHLEVEL]);
freq:=0; format:=0; chan:=0;
if Mix_QuerySpec(@freq,@format,@chan)<>0 then
Log.Info('[SDL_MIX] Device: driver=%s, %d Hz, format $%x, %d channels, %d mixing channels',
[string(SDL_GetCurrentAudioDriver),freq,format,chan,SAMPLE_CHANNELS])
else
Log.Warn('[SDL_MIX] Device query failed: '+Mix_GetError);
end;
procedure TSoundLibSDL.Done;
var
i:integer;
begin
Log.Info('[SDL_MIX] stopping');
Mix_HaltChannel(-1);
Mix_HaltMusic;
Mix_CloseAudio;
Mix_Quit;
if ownAudioSubsystem then begin
SDL_QuitSubSystem(SDL_INIT_AUDIO);
ownAudioSubsystem:=false;
end;
for i:=0 to SAMPLE_CHANNELS-1 do FreeAndNil(channels[i]);
FreeAndNil(musicChannel);
end;
function TSoundLibSDL.OpenMediaFile(fname:string;mode:TMediaLoadingMode):TMediaFile;
var
st:String8;
chunk:PMix_Chunk;
music:PMix_Music;
media:TMediaFileSDL;
ext:string;
begin
result:=nil;
st:=fname;
ext:=LowerCase(ExtractFileExt(fName));
media:=TMediaFileSDL.Create;
if (mode=mlmLoadUnpack) and ((ext='.wav') or (ext='.ogg')) then begin
// Load as sample
chunk:=Mix_LoadWAV(PAnsiChar(st));
if chunk=nil then begin
Log.Error('[SDL_MIX] Failed to load media file %s: %s',[fName,string(Mix_GetError)]);
media.Free;
exit(nil);
end;
media.chunk:=chunk;
end else begin
// Load as music
music:=Mix_LoadMUS(PAnsiChar(st));
if music=nil then begin
Log.Error('[SDL_MIX] Failed to load music file %s: %s',[fname,string(Mix_GetError)]);
media.Free;
exit(nil);
end;
media.music:=music;
end;
media.source:=fName;
// Fill in numChannels/sampleRate/bitDepth: the engine needs sampleRate to
// convert the "freq=" playback parameter into a speed factor
try
media.DetectParams(fName);
except
on e:Exception do
Log.Warn('[SDL_MIX] Cannot detect params of %s: %s',[fName,ExceptionMsg(e)]);
end;
result:=media;
end;
function TSoundLibSDL.PlayMedia(media:TMediaFile;const settings:TPlaySettings):TChannel;
var
m:TMediaFileSDL;
loops,res:integer;
begin
ASSERT(media is TMediaFileSDL);
m:=TMediaFileSDL(media);
if settings.loop then loops:=-1 else loops:=0;
if m.chunk<>nil then begin
// Play sample
res:=Mix_PlayChannel(-1,m.chunk,loops);
if res<0 then begin
Log.Error('[SDL_MIX] failed to play sample %s: %s',[m.source,string(Mix_GetError)]);
exit(nil);
end;
channels[res].relVolume:=settings.volume;
ApplyChannelVolume(channels[res]);
SetPanning(res,settings.pan);
result:=channels[res];
end else begin
// Play music (SDL_mixer has a single music stream).
// A fade-out started for the previous track keeps its own timer running: it
// would halt whatever plays when it expires, i.e. this new track. Stop the
// old stream explicitly instead of letting the fade finish on its own.
if Mix_FadingMusic<>MIX_NO_FADING then Mix_HaltMusic;
if Mix_PlayMusic(m.music,loops)<>0 then begin
Log.Error('[SDL_MIX] failed to play music %s: %s',[m.source,string(Mix_GetError)]);
exit(nil);
end;
musicChannel.relVolume:=settings.volume;
ApplyChannelVolume(musicChannel);
result:=musicChannel;
end;
end;
procedure TSoundLibSDL.SetChannelAttribute(channel:TChannel;attr:TChannelAttribute;value:single);
var
ch:TChannelSDL;
begin
if channel=nil then exit;
ASSERT(channel is TChannelSDL);
ch:=TChannelSDL(channel);
case attr of
caVolume:begin
ch.relVolume:=value;
ApplyChannelVolume(ch);
end;
caPanning:
if ch.sampleChannel<>MUSIC_CHANNEL then SetPanning(ch.sampleChannel,value);
// caSpeed is not supported by SDL_mixer
end;
end;
procedure TSoundLibSDL.SetVolume(volumeType:TVolumeType;volume:single);
var
i:integer;
begin
case volumeType of
vtSounds:begin
globalSoundVolume:=volume;
// Per-channel volumes are relative to the global one, so reapply them
for i:=0 to SAMPLE_CHANNELS-1 do
if (channels[i]<>nil) and (Mix_Playing(i)<>0) then ApplyChannelVolume(channels[i]);
end;
vtMusic:begin
globalMusicVolume:=volume;
if musicChannel<>nil then ApplyChannelVolume(musicChannel);
end;
end;
end;
procedure TSoundLibSDL.SlideChannel(channel:TChannel;attr:TChannelAttribute;newValue,timeInterval:single);
var
ch:integer;
begin
// SDL_mixer has no generic slide: only a fade-out is available (CanSlide=[])
if channel=nil then exit;
ASSERT(channel is TChannelSDL);
if (attr<>caVolume) or (newValue>0) then exit;
ch:=TChannelSDL(channel).sampleChannel;
if ch=MUSIC_CHANNEL then
Mix_FadeOutMusic(round(timeInterval*1000))
else
Mix_FadeOutChannel(ch,round(timeInterval*1000));
end;
procedure TSoundLibSDL.Pause(pause:boolean);
begin
if pause then begin
Mix_Pause(-1);
Mix_PauseMusic;
end else begin
Mix_Resume(-1);
Mix_ResumeMusic;
end;
end;
procedure TSoundLibSDL.StopChannel(var channel:TChannel);
var
ch:integer;
begin
if channel=nil then exit;
ASSERT(channel is TChannelSDL);
ch:=TChannelSDL(channel).sampleChannel;
if ch=MUSIC_CHANNEL then begin
Log.Info('[SDL_MIX] halt music');
Mix_HaltMusic;
end else
Mix_HaltChannel(ch);
channel:=nil; // the object itself is owned by the backend
end;
end.