-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.SoundSDL.pas
201 lines (178 loc) · 5.25 KB
/
Apus.Engine.SoundSDL.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
// Copyright (C) Ivan Polyacov, Apus Software ([email protected])
// 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/)
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 Done;
function CanSlide:TChannelAttributes;
function CanFadeMusic:boolean;
end;
implementation
uses Apus.Common, SysUtils, SDL2, sdl2_mixer;
type
TMediaFileSDL=class(TMediaFile)
chunk,music:pointer;
end;
TChannelSDL=class(TChannel)
sampleChannel:integer; // negative - music
end;
var
globalMusicVolume,curMusicVolume,globalSoundVolume:single;
{ TSoundLibSDL }
function TSoundLibSDL.CanSlide: TChannelAttributes;
begin
result:=[];
end;
function TSoundLibSDL.CanFadeMusic:boolean;
begin
result:=true;
end;
procedure TSoundLibSDL.Init(windowHandle: THandle);
var
res,flags:integer;
begin
LogMessage('[SDL_MIX] Init');
flags:=MIX_INIT_OGG;
res:=Mix_Init(flags);
if res<>flags then raise EError.Create('[SDL_MIX] init failed: '+Mix_GetError);
res:=Mix_OpenAudio(44100,AUDIO_S16,2,1764);
if res<>0 then raise EError.Create('[SDL_MIX] open audio error '+Mix_GetError);
end;
procedure TSoundLibSDL.Done;
begin
LogMessage('[SDL_MIX] stopping');
Mix_CloseAudio;
Mix_Quit;
end;
function TSoundLibSDL.OpenMediaFile(fname: string;
mode: TMediaLoadingMode): TMediaFile;
var
st:String8;
chunk,music:pointer;
media:TMediaFileSDL;
ext:string;
begin
result:=nil;
st:=fname;
ext:=Lowercase(ExtractFileExt(fName));
if (mode=mlmLoadUnpack) and ((ext='.wav') or (ext='.ogg')) then begin
// Load as sample
chunk:=Mix_LoadWAV(PAnsiChar(st));
if chunk=nil then begin
LogMessage('[SDL_MIX] Failed to load media file %s: %s ',[fName,Mix_GetError]);
exit(nil);
end;
media:=TMediaFileSDL.Create;
media.chunk:=chunk;
end else begin
// Load as music
music:=Mix_LoadMUS(PAnsiChar(st));
if music=nil then begin
LogMessage('[SDL_MIX] Failed to load music file %s: %s ',[fname,Mix_GetError]);
exit(nil);
end;
media:=TMediaFileSDL.Create;
media.music:=music;
end;
media.source:=fName;
result:=media;
end;
procedure UpdateCurMusicVolume;
begin
Mix_VolumeMusic(round(curMusicVolume*globalMusicVolume*MIX_MAX_VOLUME));
end;
function TSoundLibSDL.PlayMedia(media: TMediaFile;
const settings: TPlaySettings): TChannel;
var
m:TMediaFileSDL;
loops:integer;
res:integer;
ch:TChannelSDL;
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=-1 then begin
ForceLogMessage('[SDL_MIX] failed to play sample: '+m.source);
exit(nil);
end;
Mix_Volume(res,round(settings.volume*globalSoundVolume*MIX_MAX_VOLUME));
end else begin
// Play music
res:=Mix_PlayMusic(m.music,loops);
if res<>0 then begin
ForceLogMessage('[SDL_MIX] failed to play music: '+m.source);
exit(nil);
end;
curMusicVolume:=settings.volume;
UpdateCurMusicVolume;
res:=-999;
end;
ch:=TChannelSDL.Create;
ch.sampleChannel:=res;
result:=ch;
end;
procedure TSoundLibSDL.SetChannelAttribute(channel: TChannel;
attr: TChannelAttribute; value: single);
var
ch:integer;
left,right:byte;
begin
ASSERT(channel is TChannelSDL);
ch:=TChannelSDL(channel).sampleChannel;
if ch<0 then begin
if attr=caVolume then begin
curMusicVolume:=value;
UpdateCurMusicVolume;
end;
end else begin
if attr=caVolume then MIX_Volume(ch,round(value*globalSoundVolume*MIX_MAX_VOLUME));
if attr=caPanning then begin
right:=clamp(round(255*(1+value)),0,255);
left:=clamp(round(255*(1-value)),0,255);
Mix_SetPanning(ch,left,right);
end;
end;
end;
procedure TSoundLibSDL.SetVolume(volumeType: TVolumeType; volume: single);
begin
case volumeType of
vtSounds:globalSoundVolume:=volume;
vtMusic:begin
globalMusicVolume:=volume;
UpdateCurMusicVolume;
end;
end;
end;
procedure TSoundLibSDL.SlideChannel(channel: TChannel; attr: TChannelAttribute;
newValue, timeInterval: single);
begin
if (channel is TChannelSDL) and (attr=caVolume) then begin
if newValue=0 then Mix_FadeOutMusic(round(timeInterval*1000));
end;
end;
procedure TSoundLibSDL.StopChannel(var channel: TChannel);
var
ch:TChannelSDL;
begin
ASSERT(channel is TChannelSDL);
ch:=TChannelSDL(channel);
if ch.sampleChannel>=0 then
Mix_HaltChannel(ch.sampleChannel)
else
Mix_HaltMusic;
end;
end.