| 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 |
{ Sound engine backend using FMOD. |
| 17 |
See https://castle-engine.io/fmod |
| 18 |
about using FMOD with CGE. |
| 19 |
} |
| 20 |
unit CastleFMODSoundBackend; |
| 21 |
|
| 22 |
{$I castleconf.inc} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
{ Use this to set sound engine backend to FMOD. |
| 27 |
You can call this at any point of your application. |
| 28 |
If you plan to use FMOD for your entire application, |
| 29 |
then it is beneficial to call this before any sound loading/playing, |
| 30 |
as then the default sound backend (OpenAL on most platforms) |
| 31 |
wil not even be initialized. |
| 32 |
|
| 33 |
This does nothing (and shows a warning) if the dynamic FMOD library |
| 34 |
could not be found. Therefore, applications on platforms where FMOD |
| 35 |
is dynamic (all platforms except iOS and Nintendo Switch now) |
| 36 |
gracefully fallback from FMOD to the default backend, if FMOD library cannot be found. } |
| 37 |
procedure UseFMODSoundBackend; |
| 38 |
|
| 39 |
implementation |
| 40 |
|
| 41 |
uses SysUtils, Classes, Math, StrUtils, CTypes, |
| 42 |
CastleVectors, CastleTimeUtils, CastleLog, CastleUtils, CastleUriUtils, |
| 43 |
CastleClassUtils, CastleStringUtils, CastleInternalSoundFile, |
| 44 |
CastleInternalAbstractSoundBackend, CastleSoundBase, CastleSoundEngine, |
| 45 |
{$ifdef ANDROID} JNI, CastleAndroidNativeAppGlue, CastleAndroidInternalAssetStream, {$endif} |
| 46 |
CastleInternalFMOD; |
| 47 |
|
| 48 |
{ sound backend classes interface -------------------------------------------- } |
| 49 |
|
| 50 |
type |
| 51 |
TFMODSoundBufferBackend = class(TSoundBufferBackend) |
| 52 |
strict private |
| 53 |
FDuration: TFloatTime; |
| 54 |
FDataFormat: TSoundDataFormat; |
| 55 |
FFrequency: Cardinal; |
| 56 |
private |
| 57 |
FSoundLoading: TSoundLoading; |
| 58 |
FMODSound: PFMOD_SOUND; |
| 59 |
function FMODSystem: PFMOD_SYSTEM; |
| 60 |
public |
| 61 |
constructor Create(const ASoundEngine: TSoundEngineBackend; |
| 62 |
const ASoundLoading: TSoundLoading); |
| 63 |
procedure ContextOpen(const AUrl: String); override; |
| 64 |
procedure ContextClose; override; |
| 65 |
function Duration: TFloatTime; override; |
| 66 |
function DataFormat: TSoundDataFormat; override; |
| 67 |
function Frequency: Cardinal; override; |
| 68 |
end; |
| 69 |
|
| 70 |
TFMODSoundSourceBackend = class(TSoundSourceBackend) |
| 71 |
strict private |
| 72 |
FMODChannel: PFMOD_CHANNEL; |
| 73 |
FBuffer: TFMODSoundBufferBackend; |
| 74 |
FPosition, FVelocity: TVector3; |
| 75 |
FLoop, FSpatial: Boolean; |
| 76 |
FReferenceDistance, FMaxDistance: Single; |
| 77 |
function FMODSystem: PFMOD_SYSTEM; |
| 78 |
function Mode: TFMOD_MODE; |
| 79 |
public |
| 80 |
procedure ContextOpen; override; |
| 81 |
procedure ContextClose; override; |
| 82 |
function PlayingOrPaused: boolean; override; |
| 83 |
procedure Play(const BufferChangedRecently: Boolean); override; |
| 84 |
procedure Stop; override; |
| 85 |
procedure SetPosition(const Value: TVector3); override; |
| 86 |
procedure SetVelocity(const Value: TVector3); override; |
| 87 |
procedure SetLoop(const Value: boolean); override; |
| 88 |
procedure SetSpatial(const Value: boolean); override; |
| 89 |
procedure SetVolume(const Value: Single); override; |
| 90 |
procedure SetMinGain(const Value: Single); override; |
| 91 |
procedure SetMaxGain(const Value: Single); override; |
| 92 |
procedure SetBuffer(const Value: TSoundBufferBackend); override; |
| 93 |
procedure SetPitch(const Value: Single); override; |
| 94 |
procedure SetReferenceDistance(const Value: Single); override; |
| 95 |
procedure SetMaxDistance(const Value: Single); override; |
| 96 |
procedure SetPriority(const Value: Single); override; |
| 97 |
function GetOffset: Single; override; |
| 98 |
procedure SetOffset(const Value: Single); override; |
| 99 |
end; |
| 100 |
|
| 101 |
TFMODSoundEngineBackend = class(TSoundEngineBackend) |
| 102 |
private |
| 103 |
FMODSystem: PFMOD_SYSTEM; |
| 104 |
FDistanceModel: TSoundDistanceModel; |
| 105 |
public |
| 106 |
function ContextOpen(const ADevice: String; out Information, InformationSummary: String): Boolean; override; |
| 107 |
procedure ContextClose; override; |
| 108 |
function CreateBuffer(const SoundLoading: TSoundLoading): TSoundBufferBackend; override; |
| 109 |
function CreateSource: TSoundSourceBackend; override; |
| 110 |
|
| 111 |
procedure Update; override; |
| 112 |
procedure SetVolume(const Value: Single); override; |
| 113 |
{ TODO: This just sets default distance model for all newly created sounds. |
| 114 |
So it will only work if executed early. } |
| 115 |
procedure SetDistanceModel(const Value: TSoundDistanceModel); override; |
| 116 |
procedure SetDopplerFactor(const Value: Single); override; |
| 117 |
procedure SetListener(const Position, Direction, Up: TVector3); override; |
| 118 |
end; |
| 119 |
|
| 120 |
{ private unit helpers ------------------------------------------------------- } |
| 121 |
|
| 122 |
type |
| 123 |
EFMODError = class(Exception); |
| 124 |
|
| 125 |
procedure CheckFMOD(const FMODResult: TFMOD_RESULT; const When: String = ''; const Warning: Boolean = false); |
| 126 |
var |
| 127 |
ErrorStr: String; |
| 128 |
begin |
| 129 |
if FMODResult <> FMOD_OK then |
| 130 |
begin |
| 131 |
// FPC error "No type info available for this type", because it's an enum with assignments |
| 132 |
//ErrorStr := GetEnumName(TypeInfo(TFMOD_RESULT), Ord(FMODResult)); |
| 133 |
System.WriteStr(ErrorStr, FMODResult); |
| 134 |
ErrorStr := 'FMOD error ' + ErrorStr; |
| 135 |
if When <> '' then |
| 136 |
ErrorStr := ErrorStr + ' when doing ' + When; |
| 137 |
if Warning then |
| 138 |
WritelnWarning(ErrorStr) |
| 139 |
else |
| 140 |
raise EFMODError.Create(ErrorStr); |
| 141 |
end; |
| 142 |
end; |
| 143 |
|
| 144 |
function SoundTypeToStr(const SoundType: TFMOD_SOUND_TYPE): String; |
| 145 |
begin |
| 146 |
// FPC error "No type info available for this type", because it's an enum with assignments |
| 147 |
//Result := GetEnumName(TypeInfo(TFMOD_SOUND_TYPE), Ord(SoundType)); |
| 148 |
System.WriteStr(Result, SoundType); |
| 149 |
end; |
| 150 |
|
| 151 |
function SoundFormatToStr(const SoundFormat: TFMOD_SOUND_FORMAT): String; |
| 152 |
begin |
| 153 |
// FPC error "No type info available for this type", because it's an enum with assignments |
| 154 |
//Result := GetEnumName(TypeInfo(TFMOD_SOUND_FORMAT), Ord(SoundFormat)); |
| 155 |
System.WriteStr(Result, SoundFormat); |
| 156 |
end; |
| 157 |
|
| 158 |
{ TFMODSoundBufferBackend -------------------------------------------------- } |
| 159 |
|
| 160 |
constructor TFMODSoundBufferBackend.Create( |
| 161 |
const ASoundEngine: TSoundEngineBackend; const ASoundLoading: TSoundLoading); |
| 162 |
begin |
| 163 |
inherited Create(ASoundEngine); |
| 164 |
FSoundLoading := ASoundLoading; |
| 165 |
end; |
| 166 |
|
| 167 |
function TFMODSoundBufferBackend.FMODSystem: PFMOD_SYSTEM; |
| 168 |
begin |
| 169 |
Result := (SoundEngine as TFMODSoundEngineBackend).FMODSystem; |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TFMODSoundBufferBackend.ContextOpen(const AUrl: String); |
| 173 |
var |
| 174 |
TimeStart: TCastleProfilerTime; |
| 175 |
|
| 176 |
procedure CalculateProperties; |
| 177 |
var |
| 178 |
SoundType: TFMOD_SOUND_TYPE; |
| 179 |
SoundFormat: TFMOD_SOUND_FORMAT; |
| 180 |
SoundChannels, SoundBits: CInt; |
| 181 |
Miliseconds, PcmSamples: CUInt; |
| 182 |
begin |
| 183 |
// calculate FDuration |
| 184 |
CheckFMOD(FMOD_Sound_GetLength(FMODSound, @Miliseconds, FMOD_TIMEUNIT_MS)); |
| 185 |
if Miliseconds = $FFFFFFFF then |
| 186 |
FDuration := 0 |
| 187 |
else |
| 188 |
FDuration := Miliseconds / 1000; |
| 189 |
if FDuration = 0 then |
| 190 |
WritelnWarning('Cannot determine correct duration of sound file "%s"', [UriDisplay(AUrl)]); |
| 191 |
|
| 192 |
// calculate FFrequency. |
| 193 |
CheckFMOD(FMOD_Sound_GetLength(FMODSound, @PcmSamples, FMOD_TIMEUNIT_PCM)); |
| 194 |
// We know that PcmSamples = Miliseconds * Frequency / 1000. |
| 195 |
FFrequency := Int64(PcmSamples) * 1000 div Miliseconds; |
| 196 |
|
| 197 |
// calculate FDataFormat |
| 198 |
CheckFMOD(FMOD_Sound_GetFormat(FMODSound, @SoundType, @SoundFormat, @SoundChannels, @SoundBits)); |
| 199 |
if SoundChannels >= 2 then |
| 200 |
begin |
| 201 |
if SoundBits >= 16 then |
| 202 |
FDataFormat := sfStereo16 |
| 203 |
else |
| 204 |
FDataFormat := sfStereo8; |
| 205 |
end else |
| 206 |
begin |
| 207 |
if SoundBits >= 16 then |
| 208 |
FDataFormat := sfMono16 |
| 209 |
else |
| 210 |
FDataFormat := sfMono8; |
| 211 |
end; |
| 212 |
|
| 213 |
if LogSoundLoading then |
| 214 |
WritelnLog('FMOD loaded "%s": type %s, format: %s, channels: %d, bits: %d (%s), frequency: %d, duration: %f', [ |
| 215 |
UriDisplay(AUrl), |
| 216 |
SoundTypeToStr(SoundType), |
| 217 |
SoundFormatToStr(SoundFormat), |
| 218 |
SoundChannels, |
| 219 |
SoundBits, |
| 220 |
DataFormatToStr(FDataFormat), |
| 221 |
FFrequency, |
| 222 |
FDuration |
| 223 |
]); |
| 224 |
end; |
| 225 |
|
| 226 |
var |
| 227 |
Mode: TFMOD_MODE; |
| 228 |
FmodName: String; |
| 229 |
begin |
| 230 |
inherited; |
| 231 |
TimeStart := Profiler.Start('Loading "' + UriDisplay(AUrl) + '" (TFMODSoundBufferBackend)'); |
| 232 |
try |
| 233 |
FmodName := ResolveCastleDataUrl(Url); // resolve castle-data:/, as FMOD cannot understand it |
| 234 |
if URIProtocol(FmodName) = 'file' then |
| 235 |
FmodName := UriToFilenameSafe(FmodName) // resolve file:/, as FMOD cannot understand it |
| 236 |
{$ifdef ANDROID} |
| 237 |
else |
| 238 |
if URIProtocol(FmodName) = 'castle-android-assets' then |
| 239 |
begin |
| 240 |
{ Resolve CGE URL to Android URL that FMOD will handle. |
| 241 |
See https://www.fmod.com/resources/documentation-api?version=2.1&page=platforms-android.html#asset-manager } |
| 242 |
FmodName := 'file:///android_asset/' + URIToAssetPath(FmodName); |
| 243 |
end |
| 244 |
{$endif} |
| 245 |
; |
| 246 |
|
| 247 |
{ Note: it seems using FMOD_2D here doesn't cause any trouble |
| 248 |
to make spatial sounds, we set FMOD_3D on FMOD sound later if needed. } |
| 249 |
Mode := FMOD_DEFAULT or FMOD_2D; |
| 250 |
if FSoundLoading = slStreaming then |
| 251 |
Mode := Mode or FMOD_CREATESTREAM; |
| 252 |
|
| 253 |
CheckFMOD(FMOD_System_CreateSound(FMODSystem, PCharOrNil(FmodName), Mode, |
| 254 |
nil { @SoundInfo }, @FMODSound)); |
| 255 |
|
| 256 |
CalculateProperties; |
| 257 |
finally Profiler.Stop(TimeStart) end; |
| 258 |
end; |
| 259 |
|
| 260 |
procedure TFMODSoundBufferBackend.ContextClose; |
| 261 |
begin |
| 262 |
CheckFMOD(FMOD_Sound_Release(FMODSound)); |
| 263 |
FMODSound := nil; |
| 264 |
inherited; |
| 265 |
end; |
| 266 |
|
| 267 |
function TFMODSoundBufferBackend.Duration: TFloatTime; |
| 268 |
begin |
| 269 |
Result := FDuration; |
| 270 |
end; |
| 271 |
|
| 272 |
function TFMODSoundBufferBackend.DataFormat: TSoundDataFormat; |
| 273 |
begin |
| 274 |
Result := FDataFormat; |
| 275 |
end; |
| 276 |
|
| 277 |
function TFMODSoundBufferBackend.Frequency: Cardinal; |
| 278 |
begin |
| 279 |
Result := FFrequency; |
| 280 |
end; |
| 281 |
|
| 282 |
{ TFMODSoundSourceBackend -------------------------------------------------- } |
| 283 |
|
| 284 |
function TFMODSoundSourceBackend.FMODSystem: PFMOD_SYSTEM; |
| 285 |
begin |
| 286 |
Result := (SoundEngine as TFMODSoundEngineBackend).FMODSystem; |
| 287 |
end; |
| 288 |
|
| 289 |
procedure TFMODSoundSourceBackend.ContextOpen; |
| 290 |
begin |
| 291 |
// reflect FMOD defaults |
| 292 |
FReferenceDistance := 1; |
| 293 |
FMaxDistance := 10000; |
| 294 |
end; |
| 295 |
|
| 296 |
procedure TFMODSoundSourceBackend.ContextClose; |
| 297 |
begin |
| 298 |
Stop; |
| 299 |
end; |
| 300 |
|
| 301 |
function TFMODSoundSourceBackend.PlayingOrPaused: boolean; |
| 302 |
var |
| 303 |
B: TFMOD_BOOL; |
| 304 |
IsPlayingError: TFMOD_RESULT; |
| 305 |
begin |
| 306 |
if FMODChannel = nil then Exit(false); |
| 307 |
|
| 308 |
{ Note that Loop sound will have IsPlaying forever until it's explicitly stopped, |
| 309 |
and that's what we want. } |
| 310 |
IsPlayingError := FMOD_Channel_IsPlaying(FMODChannel, @B); |
| 311 |
|
| 312 |
// When sound stopped playing, Channel may become invalid |
| 313 |
if (IsPlayingError = FMOD_ERR_INVALID_HANDLE) or |
| 314 |
(IsPlayingError = FMOD_ERR_CHANNEL_STOLEN) then |
| 315 |
begin |
| 316 |
FMODChannel := nil; |
| 317 |
Exit(false); |
| 318 |
end; |
| 319 |
|
| 320 |
CheckFMOD(IsPlayingError); |
| 321 |
Result := B <> 0; |
| 322 |
end; |
| 323 |
|
| 324 |
procedure TFMODSoundSourceBackend.Play(const BufferChangedRecently: Boolean); |
| 325 |
begin |
| 326 |
if FMODChannel = nil then Exit; |
| 327 |
|
| 328 |
CheckFMOD(FMOD_Channel_SetPaused(FMODChannel, 0)); |
| 329 |
end; |
| 330 |
|
| 331 |
procedure TFMODSoundSourceBackend.Stop; |
| 332 |
begin |
| 333 |
if FMODChannel <> nil then |
| 334 |
begin |
| 335 |
{ This causes FMOD_ERR_INVALID_HANDLE sometimes on Windows when exiting |
| 336 |
application (destroying FMOD backend). |
| 337 |
No other problem is visible and it seems completely random, |
| 338 |
so I assume it is a random FMOD error. } |
| 339 |
CheckFMOD(FMOD_Channel_Stop(FMODChannel), 'FMOD_Channel_Stop', true); |
| 340 |
FMODChannel := nil; // FMODChannel should not be used anymore |
| 341 |
end; |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TFMODSoundSourceBackend.SetPosition(const Value: TVector3); |
| 345 |
begin |
| 346 |
FPosition := Value; |
| 347 |
if FMODChannel = nil then Exit; |
| 348 |
if FSpatial then // only if Spatial, otherwise FMOD raises error |
| 349 |
CheckFMOD(FMOD_Channel_Set3DAttributes(FMODChannel, @FPosition, @FVelocity)); |
| 350 |
end; |
| 351 |
|
| 352 |
function TFMODSoundSourceBackend.Mode: TFMOD_MODE; |
| 353 |
begin |
| 354 |
Result := 0; |
| 355 |
|
| 356 |
if FLoop then |
| 357 |
Result := Result or FMOD_LOOP_NORMAL |
| 358 |
else |
| 359 |
Result := Result or FMOD_LOOP_OFF; |
| 360 |
|
| 361 |
if FSpatial then |
| 362 |
begin |
| 363 |
Result := Result or FMOD_3D; |
| 364 |
{ These flags are only allowed with FMOD_3D } |
| 365 |
case (SoundEngine as TFMODSoundEngineBackend).FDistanceModel of |
| 366 |
dmInverse: Result := Result or FMOD_3D_INVERSEROLLOFF; |
| 367 |
dmLinear : Result := Result or FMOD_3D_LINEARROLLOFF; |
| 368 |
end; |
| 369 |
end else |
| 370 |
Result := Result or FMOD_2D; |
| 371 |
end; |
| 372 |
|
| 373 |
procedure TFMODSoundSourceBackend.SetVelocity(const Value: TVector3); |
| 374 |
begin |
| 375 |
FVelocity := Value; |
| 376 |
if FMODChannel = nil then Exit; |
| 377 |
if FSpatial then // only if Spatial, otherwise FMOD raises error |
| 378 |
CheckFMOD(FMOD_Channel_Set3DAttributes(FMODChannel, @FPosition, @FVelocity)); |
| 379 |
end; |
| 380 |
|
| 381 |
procedure TFMODSoundSourceBackend.SetLoop(const Value: boolean); |
| 382 |
begin |
| 383 |
FLoop := Value; |
| 384 |
if FMODChannel = nil then Exit; |
| 385 |
CheckFMOD(FMOD_Channel_SetMode(FMODChannel, Mode)); |
| 386 |
end; |
| 387 |
|
| 388 |
procedure TFMODSoundSourceBackend.SetSpatial(const Value: boolean); |
| 389 |
begin |
| 390 |
FSpatial := Value; |
| 391 |
if FMODChannel = nil then Exit; |
| 392 |
CheckFMOD(FMOD_Channel_SetMode(FMODChannel, Mode)); |
| 393 |
|
| 394 |
// call FMOD_Channel_Set3DXxx to update settings |
| 395 |
if FSpatial then // only if Spatial, otherwise FMOD raises error |
| 396 |
begin |
| 397 |
CheckFMOD(FMOD_Channel_Set3DAttributes(FMODChannel, @FPosition, @FVelocity)); |
| 398 |
CheckFMOD(FMOD_Channel_Set3DMinMaxDistance(FMODChannel, FReferenceDistance, FMaxDistance)); |
| 399 |
end; |
| 400 |
end; |
| 401 |
|
| 402 |
procedure TFMODSoundSourceBackend.SetVolume(const Value: Single); |
| 403 |
begin |
| 404 |
if FMODChannel = nil then Exit; |
| 405 |
|
| 406 |
CheckFMOD(FMOD_Channel_SetVolume(FMODChannel, Value)); |
| 407 |
end; |
| 408 |
|
| 409 |
procedure TFMODSoundSourceBackend.SetMinGain(const Value: Single); |
| 410 |
begin |
| 411 |
// not possible (well, not straightforward) with FMOD backend |
| 412 |
end; |
| 413 |
|
| 414 |
procedure TFMODSoundSourceBackend.SetMaxGain(const Value: Single); |
| 415 |
begin |
| 416 |
// not possible (well, not straightforward) with FMOD backend |
| 417 |
end; |
| 418 |
|
| 419 |
procedure TFMODSoundSourceBackend.SetBuffer(const Value: TSoundBufferBackend); |
| 420 |
begin |
| 421 |
FBuffer := Value as TFMODSoundBufferBackend; |
| 422 |
|
| 423 |
if FMODChannel <> nil then |
| 424 |
Stop; |
| 425 |
Assert(FMODChannel = nil); |
| 426 |
if FBuffer <> nil then |
| 427 |
begin |
| 428 |
// Start in paused state, allows to adjust parameters like loop before starting |
| 429 |
CheckFMOD(FMOD_System_PlaySound(FMODSystem, FBuffer.FMODSound, nil, |
| 430 |
{ paused } 1, @FMODChannel)); |
| 431 |
end; |
| 432 |
end; |
| 433 |
|
| 434 |
procedure TFMODSoundSourceBackend.SetPitch(const Value: Single); |
| 435 |
begin |
| 436 |
if FMODChannel = nil then Exit; |
| 437 |
|
| 438 |
CheckFMOD(FMOD_Channel_SetPitch(FMODChannel, Value)); |
| 439 |
end; |
| 440 |
|
| 441 |
procedure TFMODSoundSourceBackend.SetReferenceDistance(const Value: Single); |
| 442 |
begin |
| 443 |
FReferenceDistance := Value; |
| 444 |
if FMODChannel = nil then Exit; |
| 445 |
if not FSpatial then Exit; // apply this only if Spatial |
| 446 |
CheckFMOD(FMOD_Channel_Set3DMinMaxDistance(FMODChannel, FReferenceDistance, FMaxDistance)); |
| 447 |
end; |
| 448 |
|
| 449 |
procedure TFMODSoundSourceBackend.SetMaxDistance(const Value: Single); |
| 450 |
begin |
| 451 |
FMaxDistance := Value; |
| 452 |
if FMODChannel = nil then Exit; |
| 453 |
if not FSpatial then Exit; // apply this only if Spatial |
| 454 |
CheckFMOD(FMOD_Channel_Set3DMinMaxDistance(FMODChannel, FReferenceDistance, FMaxDistance)); |
| 455 |
end; |
| 456 |
|
| 457 |
procedure TFMODSoundSourceBackend.SetPriority(const Value: Single); |
| 458 |
begin |
| 459 |
{ TODO: although we pass it to FMOD, we also manually manage limited sources |
| 460 |
in TSoundAllocator. |
| 461 |
We should instead allocate many sound sources (MaxAllocatedSources large?) |
| 462 |
and let FMOD to do its job. } |
| 463 |
|
| 464 |
if FMODChannel = nil then Exit; |
| 465 |
CheckFMOD(FMOD_Channel_SetPriority(FMODChannel, Round(Value * 256))); |
| 466 |
end; |
| 467 |
|
| 468 |
function TFMODSoundSourceBackend.GetOffset: Single; |
| 469 |
var |
| 470 |
Pos: CUInt; |
| 471 |
begin |
| 472 |
{ Make sure to set FMODChannel to nil if not playing now, |
| 473 |
otherwise FMOD would report error FMOD_ERR_INVALID_HANDLE when the sound is near the end. |
| 474 |
Testcase: fmod-test-editor, play sound using button. } |
| 475 |
PlayingOrPaused; |
| 476 |
|
| 477 |
if FMODChannel = nil then Exit(0); |
| 478 |
CheckFMOD(FMOD_Channel_GetPosition(FMODChannel, @Pos, FMOD_TIMEUNIT_MS)); |
| 479 |
Result := Pos / 1000; |
| 480 |
end; |
| 481 |
|
| 482 |
procedure TFMODSoundSourceBackend.SetOffset(const Value: Single); |
| 483 |
var |
| 484 |
Pos: CUInt; |
| 485 |
begin |
| 486 |
if FMODChannel = nil then Exit; |
| 487 |
Pos := Round(Value * 1000); |
| 488 |
CheckFMOD(FMOD_Channel_SetPosition(FMODChannel, Pos, FMOD_TIMEUNIT_MS)); |
| 489 |
end; |
| 490 |
|
| 491 |
{ TFMODSoundEngineBackend -------------------------------------------------- } |
| 492 |
|
| 493 |
function TFMODSoundEngineBackend.CreateBuffer(const SoundLoading: TSoundLoading): TSoundBufferBackend; |
| 494 |
begin |
| 495 |
Result := TFMODSoundBufferBackend.Create(Self, SoundLoading); |
| 496 |
end; |
| 497 |
|
| 498 |
function TFMODSoundEngineBackend.CreateSource: TSoundSourceBackend; |
| 499 |
begin |
| 500 |
Result := TFMODSoundSourceBackend.Create(Self); |
| 501 |
end; |
| 502 |
|
| 503 |
function TFMODSoundEngineBackend.ContextOpen(const ADevice: String; |
| 504 |
out Information, InformationSummary: String): Boolean; |
| 505 |
var |
| 506 |
Version: CUInt; |
| 507 |
{$ifdef ANDROID} Env: PJNIEnv; {$endif} |
| 508 |
begin |
| 509 |
FmodLibraryUsingBegin; |
| 510 |
|
| 511 |
{$ifdef ANDROID} |
| 512 |
{ This is necessary, otherwise FMOD shows an error |
| 513 |
and FMOD_System_Create exits with internal error: |
| 514 |
|
| 515 |
FMOD_JNI_GetEnv: Native threads must be attached to the Java virtual machine, please call JavaVM::AttachCurrentThread before invocation. |
| 516 |
} |
| 517 |
Env := nil; // make sure uninitialized |
| 518 |
AndroidMainApp^.Activity^.VM^^.AttachCurrentThread(AndroidMainApp^.Activity^.VM, @Env, nil); |
| 519 |
{$endif} |
| 520 |
|
| 521 |
{ Uncomment FMOD_Debug_Initialize call to get additional logs. |
| 522 |
|
| 523 |
Note that FMOD_Debug_Initialize will only work if you use |
| 524 |
"logging version" of the FMOD library. |
| 525 |
Just rename libfmodL.so to libfmod.so. |
| 526 |
|
| 527 |
On Android: Run "adb logcat | grep fmod" to see useful output, |
| 528 |
it is not visible on just "castle-engine run --target=android". } |
| 529 |
// CheckFMOD(FMOD_Debug_Initialize(FMOD_DEBUG_LEVEL_LOG or FMOD_DEBUG_DISPLAY_TIMESTAMPS, |
| 530 |
// FMOD_DEBUG_MODE_TTY, nil, nil), 'FMOD_Debug_Initialize', true); |
| 531 |
|
| 532 |
CheckFMOD(FMOD_System_Create(@FMODSystem), 'FMOD_System_Create'); |
| 533 |
{ Use FMOD_INIT_3D_RIGHTHANDED, as by default FMOD uses left-handed coordinate system. } |
| 534 |
CheckFMOD(FMOD_System_Init(FMODSystem, 256, FMOD_INIT_NORMAL or FMOD_INIT_3D_RIGHTHANDED, nil), 'FMOD_System_Init'); |
| 535 |
CheckFMOD(FMOD_System_GetVersion(FMODSystem, @Version), 'FMOD_System_GetVersion'); |
| 536 |
Information := Format('FMOD %d.%d.%d', [ |
| 537 |
Version shr 16, |
| 538 |
(Version and $FF00) shr 8, |
| 539 |
Version and $FF |
| 540 |
]); |
| 541 |
InformationSummary := Information; |
| 542 |
Result := true; |
| 543 |
end; |
| 544 |
|
| 545 |
procedure TFMODSoundEngineBackend.ContextClose; |
| 546 |
begin |
| 547 |
CheckFMOD(FMOD_System_Close(FMODSystem)); |
| 548 |
CheckFMOD(FMOD_System_Release(FMODSystem)); |
| 549 |
FMODSystem := nil; |
| 550 |
FmodLibraryUsingEnd; |
| 551 |
end; |
| 552 |
|
| 553 |
procedure TFMODSoundEngineBackend.Update; |
| 554 |
begin |
| 555 |
inherited; |
| 556 |
CheckFMOD(FMOD_System_Update(FMODSystem)); |
| 557 |
end; |
| 558 |
|
| 559 |
procedure TFMODSoundEngineBackend.SetVolume(const Value: Single); |
| 560 |
var |
| 561 |
MasterChannel: PFMOD_CHANNELGROUP; |
| 562 |
begin |
| 563 |
CheckFMOD(FMOD_System_GetMasterChannelGroup(FMODSystem, @MasterChannel)); |
| 564 |
CheckFMOD(FMOD_ChannelGroup_SetVolume(MasterChannel, Value)); |
| 565 |
end; |
| 566 |
|
| 567 |
procedure TFMODSoundEngineBackend.SetDistanceModel(const Value: TSoundDistanceModel); |
| 568 |
begin |
| 569 |
FDistanceModel := Value; |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TFMODSoundEngineBackend.SetDopplerFactor(const Value: Single); |
| 573 |
begin |
| 574 |
CheckFMOD(FMOD_System_Set3DSettings(FMODSystem, Value, 1, 1)); |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TFMODSoundEngineBackend.SetListener(const Position, Direction, Up: TVector3); |
| 578 |
const |
| 579 |
ListenerVelocity: TVector3 = (X: 0; Y: 0; Z: 0); |
| 580 |
begin |
| 581 |
CheckFMOD(FMOD_System_Set3DListenerAttributes(FMODSystem, 0, @Position, @ListenerVelocity, @Direction, @Up)); |
| 582 |
end; |
| 583 |
|
| 584 |
{ globals -------------------------------------------------------------------- } |
| 585 |
|
| 586 |
procedure UseFMODSoundBackend; |
| 587 |
begin |
| 588 |
{$ifdef ANDROID} |
| 589 |
{ Calling InitializeFmodLibrary is necessary on Android to load dynamic library, |
| 590 |
otherwise InitializeFmodLibrary is never called and fmod entry points |
| 591 |
are not loaded from libfmod.so, so FmodLibraryAvailable = false. |
| 592 |
|
| 593 |
This could be fixed better (since this problem is not specific to FMOD): |
| 594 |
|
| 595 |
- Initialization in castle-engine/src/audio/fmod/castleinternalfmod_dynamic.inc |
| 596 |
should register a callback that would be later done from TCastleApplication.Run. |
| 597 |
|
| 598 |
- We could also try exposing JNI_OnLoad, and then since FPC 3.2.0 maybe we can ignore |
| 599 |
the issue and remove whole ALLOW_DLOPEN_FROM_UNIT_INITIALIZATION complication? |
| 600 |
FPC recommends it, https://wiki.freepascal.org/Android - |
| 601 |
"if you are creating a JNI shared library, always export JNI_OnLoad, even if it is empty". } |
| 602 |
InitializeFmodLibrary; |
| 603 |
{$endif} |
| 604 |
|
| 605 |
if not FmodLibraryAvailable then |
| 606 |
begin |
| 607 |
WritelnWarning('FMOD library not available, aborting setting FMOD as sound backend'); |
| 608 |
Exit; |
| 609 |
end; |
| 610 |
|
| 611 |
SoundEngine.InternalBackend := TFMODSoundEngineBackend.Create; |
| 612 |
end; |
| 613 |
|
| 614 |
initialization |
| 615 |
// our TVector3 is equal in memory to TFMOD_VECTOR, we rely on it here |
| 616 |
Assert(SizeOf(TFMOD_VECTOR) = SizeOf(TVector3)); |
| 617 |
end. |