Debian Codegraph @

src/audio/castleinternalsoxsoundbackend.pas

raw
1 {
2 Copyright 2019-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Dummy sound engine backend using "sox" command-line.
17 This is not for production use, a lot of sound engine features are not supported.
18 Really, it can only just read and play a sound file.
19
20 To test it, use this unit and call UseSOXSoundBackend. }
21 unit CastleInternalSoxSoundBackend;
22
23 {$I castleconf.inc}
24
25 {$ifndef FPC}
26 {$message fatal 'This internal unit is only for FPC, not Delphi.'}
27 {$endif}
28
29 interface
30
31 { Use this to set sound engine backend to SOX. }
32 procedure UseSOXSoundBackend;
33
34 implementation
35
36 uses SysUtils, Classes, Math, Process, StrUtils,
37 CastleVectors, CastleTimeUtils, CastleXMLConfig,
38 CastleClassUtils, CastleStringUtils, CastleInternalSoundFile,
39 CastleInternalAbstractSoundBackend, CastleSoundBase, CastleSoundEngine,
40 CastleLog, CastleUtils, CastleUriUtils, CastleFilesUtils;
41
42 { sound backend classes interface -------------------------------------------- }
43
44 type
45 TSoxSoundBufferBackend = class(TSoundBufferBackendFromSoundFile)
46 private
47 FileName: String;
48 public
49 procedure ContextOpenFromSoundFile(const SoundFile: TSoundFile); override;
50 end;
51
52 TSoxSoundSourceBackend = class(TSoundSourceBackend)
53 strict private
54 FBuffer: TSoxSoundBufferBackend;
55 FPlayStart: TTimerResult;
56 FPlayStarted: Boolean;
57 FPlayProcess: TProcess;
58 public
59 procedure ContextOpen; override;
60 procedure ContextClose; override;
61 function PlayingOrPaused: boolean; override;
62 procedure Play(const BufferChangedRecently: Boolean); override;
63 procedure Stop; override;
64 procedure SetPosition(const Value: TVector3); override;
65 procedure SetVelocity(const Value: TVector3); override;
66 procedure SetLoop(const Value: boolean); override;
67 procedure SetSpatial(const Value: boolean); override;
68 procedure SetVolume(const Value: Single); override;
69 procedure SetMinGain(const Value: Single); override;
70 procedure SetMaxGain(const Value: Single); override;
71 procedure SetBuffer(const Value: TSoundBufferBackend); override;
72 procedure SetPitch(const Value: Single); override;
73 procedure SetReferenceDistance(const Value: Single); override;
74 procedure SetMaxDistance(const Value: Single); override;
75 procedure SetPriority(const Value: Single); override;
76 function GetOffset: Single; override;
77 procedure SetOffset(const Value: Single); override;
78 end;
79
80 TSoxSoundEngineBackend = class(TSoundEngineBackend)
81 private
82 SoxCommand: String;
83 public
84 function ContextOpen(const ADevice: String; out Information, InformationSummary: String): Boolean; override;
85 procedure ContextClose; override;
86 function CreateBuffer(const SoundLoading: TSoundLoading): TSoundBufferBackend; override;
87 function CreateSource: TSoundSourceBackend; override;
88 procedure SetVolume(const Value: Single); override;
89 procedure SetDistanceModel(const Value: TSoundDistanceModel); override;
90 procedure SetDopplerFactor(const Value: Single); override;
91 procedure SetListener(const Position, Direction, Up: TVector3); override;
92 end;
93
94 { TSoxSoundBufferBackend -------------------------------------------------- }
95
96 procedure TSoxSoundBufferBackend.ContextOpenFromSoundFile(const SoundFile: TSoundFile);
97 begin
98 inherited;
99
100 { Note that we don't load contents from SoundFile in case of SOX backend.
101 Loading sound contents in case of SOX was useless.
102 However we need TSoundFile to know the Duration of the sound correctly,
103 which we will later use for PlayingOrPaused implementation. }
104
105 FileName := UriToFilenameSafe(SoundFile.URL);
106 { Workaround sox on Windows being unable to process filenames with backslashes. }
107 {$ifdef MSWINDOWS}
108 FileName := SReplaceChars(FileName, '\', '/');
109 {$endif}
110 if FileName = '' then
111 raise ESoundFileError.CreateFmt('URL "%s" does not translate to a filename, and SOX can only play local files',
112 [SoundFile.URL]);
113 end;
114
115 { TSoxSoundSourceBackend -------------------------------------------------- }
116
117 procedure TSoxSoundSourceBackend.ContextOpen;
118 begin
119 end;
120
121 procedure TSoxSoundSourceBackend.ContextClose;
122 begin
123 Stop;
124 end;
125
126 function TSoxSoundSourceBackend.PlayingOrPaused: boolean;
127 begin
128 Result := (FBuffer <> nil) and FPlayStarted and (FPlayStart.ElapsedTime < FBuffer.Duration);
129 end;
130
131 procedure TSoxSoundSourceBackend.Play(const BufferChangedRecently: Boolean);
132 begin
133 Stop;
134
135 FPlayProcess := TProcess.Create(nil);
136 { We invoke "sox xxx.wav --default-device" instead of "play xxx.wav",
137 because on Windows (Cygwin) the "play" is only a symbolic link,
138 so we would have to execute it through bash, making the code more complicated. }
139 FPlayProcess.Executable := (SoundEngine as TSoxSoundEngineBackend).SoxCommand;
140 FPlayProcess.Parameters.Add(FBuffer.FileName);
141 FPlayProcess.Parameters.Add('--default-device');
142 FPlayProcess.Execute;
143
144 FPlayStarted := true;
145 FPlayStart := Timer;
146 end;
147
148 procedure TSoxSoundSourceBackend.Stop;
149 begin
150 { Terminate and free FPlayProcess.
151 Otherwise on Unix the sox process may be left hanging forever,
152 even after the main CGE application terminated. }
153 if FPlayProcess <> nil then
154 begin
155 FPlayProcess.Terminate(0);
156 FreeAndNil(FPlayProcess);
157 end;
158 end;
159
160 procedure TSoxSoundSourceBackend.SetPosition(const Value: TVector3);
161 begin
162 end;
163
164 procedure TSoxSoundSourceBackend.SetVelocity(const Value: TVector3);
165 begin
166 end;
167
168 procedure TSoxSoundSourceBackend.SetLoop(const Value: boolean);
169 begin
170 end;
171
172 procedure TSoxSoundSourceBackend.SetSpatial(const Value: boolean);
173 begin
174 end;
175
176 procedure TSoxSoundSourceBackend.SetVolume(const Value: Single);
177 begin
178 end;
179
180 procedure TSoxSoundSourceBackend.SetMinGain(const Value: Single);
181 begin
182 end;
183
184 procedure TSoxSoundSourceBackend.SetMaxGain(const Value: Single);
185 begin
186 end;
187
188 procedure TSoxSoundSourceBackend.SetBuffer(const Value: TSoundBufferBackend);
189 begin
190 FBuffer := Value as TSoxSoundBufferBackend;
191 end;
192
193 procedure TSoxSoundSourceBackend.SetPitch(const Value: Single);
194 begin
195 end;
196
197 procedure TSoxSoundSourceBackend.SetReferenceDistance(const Value: Single);
198 begin
199 end;
200
201 procedure TSoxSoundSourceBackend.SetMaxDistance(const Value: Single);
202 begin
203 end;
204
205 procedure TSoxSoundSourceBackend.SetPriority(const Value: Single);
206 begin
207 end;
208
209 function TSoxSoundSourceBackend.GetOffset: Single;
210 begin
211 Result := 0;
212 end;
213
214 procedure TSoxSoundSourceBackend.SetOffset(const Value: Single);
215 begin
216 end;
217
218 { TSoxSoundEngineBackend -------------------------------------------------- }
219
220 function TSoxSoundEngineBackend.ContextOpen(const ADevice: String;
221 out Information, InformationSummary: String): Boolean;
222 var
223 SoxVersion: String;
224 begin
225 SoxCommand := FindExe('sox');
226 if SoxCommand = '' then
227 begin
228 Information := 'SOX executable not found on $PATH';
229 InformationSummary := Information;
230 Exit(false);
231 end;
232 if not RunCommand(SoxCommand, ['--version'], SoxVersion) then
233 begin
234 Information := 'Failed to execute SOX executable with --version';
235 InformationSummary := Information;
236 Exit(false);
237 end;
238
239 SoxVersion := Trim(SoxVersion); // remove final newline
240
241 Result := true;
242 Information := 'SOX command found:' + NL +
243 'Executable path: ' + SoxCommand + NL +
244 'Version: ' + SoxVersion;
245 InformationSummary := 'SOX ' + SoxVersion;
246 end;
247
248 procedure TSoxSoundEngineBackend.ContextClose;
249 begin
250 end;
251
252 procedure TSoxSoundEngineBackend.SetVolume(const Value: Single);
253 begin
254 end;
255
256 procedure TSoxSoundEngineBackend.SetDistanceModel(const Value: TSoundDistanceModel);
257 begin
258 end;
259
260 procedure TSoxSoundEngineBackend.SetDopplerFactor(const Value: Single);
261 begin
262 end;
263
264 procedure TSoxSoundEngineBackend.SetListener(const Position, Direction, Up: TVector3);
265 begin
266 end;
267
268 function TSoxSoundEngineBackend.CreateBuffer(const SoundLoading: TSoundLoading): TSoundBufferBackend;
269 begin
270 // Ignore SoundLoading
271 Result := TSoxSoundBufferBackend.Create(Self);
272 end;
273
274 function TSoxSoundEngineBackend.CreateSource: TSoundSourceBackend;
275 begin
276 Result := TSoxSoundSourceBackend.Create(Self);
277 end;
278
279 { globals -------------------------------------------------------------------- }
280
281 procedure UseSOXSoundBackend;
282 begin
283 SoundEngine.InternalBackend := TSoxSoundEngineBackend.Create;
284 end;
285
286 end.