| 1 |
{ |
| 2 |
Copyright 2010-2019 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 basic types. } |
| 17 |
unit CastleSoundBase; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Generics.Collections; |
| 24 |
|
| 25 |
type |
| 26 |
ENoMoreSources = class(Exception); |
| 27 |
ESoundFileError = class(Exception); |
| 28 |
|
| 29 |
{ How does distance affect spatial sounds, used for @link(TSoundEngine.DistanceModel). } |
| 30 |
TSoundDistanceModel = ( |
| 31 |
{ Sound is fully audible (with @link(TCastleSound.Volume)) at the distance |
| 32 |
@link(TCastleSound.ReferenceDistance), and then it drops down with distance |
| 33 |
following a realistic curve. |
| 34 |
Sound is not attenuated more when it is further than @link(TCastleSound.MaxDistance). |
| 35 |
|
| 36 |
This is the default distance model, that is realistic and is easy to use. |
| 37 |
|
| 38 |
The description above is not 100% precise, because different sound backends have |
| 39 |
different details how is this realized. |
| 40 |
For OpenAL, this means using default AL_INVERSE_DISTANCE_CLAMPED |
| 41 |
(the exact equation is on https://www.openal.org/documentation/openal-1.1-specification.pdf ). |
| 42 |
For FMOD, this means using default FMOD_3D_INVERSEROLLOFF |
| 43 |
(see https://www.fmod.com/resources/documentation-api?version=2.01&page=core-api-common.html#fmod_3d_inverserolloff , |
| 44 |
https://www.fmod.com/resources/documentation-api?version=2.01&page=white-papers-3d-sounds.html#inverse , |
| 45 |
"for every doubling of this mindistance, the sound volume will halve" -- so it is inverse squared). } |
| 46 |
dmInverse, |
| 47 |
|
| 48 |
{ Sound is fully audible (with @link(TCastleSound.Volume)) at the distance |
| 49 |
@link(TCastleSound.ReferenceDistance), and then it drops down linearly to 0 |
| 50 |
at @link(TCastleSound.MaxDistance). |
| 51 |
Sound is no more audible at @link(TCastleSound.MaxDistance). |
| 52 |
|
| 53 |
It is easy to control, but remember to set @link(TCastleSound.MaxDistance) |
| 54 |
to sensibly large value. |
| 55 |
|
| 56 |
This matches the X3D sound model most. } |
| 57 |
dmLinear |
| 58 |
); |
| 59 |
|
| 60 |
TSoundDevice = class |
| 61 |
private |
| 62 |
FName, FCaption: string; |
| 63 |
public |
| 64 |
{ Short device name, used for @link(TSoundEngine.Device). } |
| 65 |
property Name: string read FName; |
| 66 |
{ Nice device name to show user. } |
| 67 |
property Caption: string read FCaption; |
| 68 |
end; |
| 69 |
|
| 70 |
TSoundDeviceList = class({$ifdef FPC}specialize{$endif} TObjectList<TSoundDevice>) |
| 71 |
procedure Add(const AName, ACaption: string); reintroduce; |
| 72 |
end; |
| 73 |
|
| 74 |
{ Sound sample format. |
| 75 |
|
| 76 |
8-bit data is unsigned. |
| 77 |
Just like in case of 8-bit WAV files, and OpenAL AL_FORMAT_MONO8 / AL_FORMAT_STEREO8: |
| 78 |
It is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero. |
| 79 |
|
| 80 |
16-bit data is signed. |
| 81 |
Just like in case of 16-bit WAV files, and OpenAL AL_FORMAT_MONO16 / AL_FORMAT_STEREO16: |
| 82 |
It is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero. |
| 83 |
|
| 84 |
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample. |
| 85 |
} |
| 86 |
TSoundDataFormat = ( |
| 87 |
sfMono8, |
| 88 |
sfMono16, |
| 89 |
sfStereo8, |
| 90 |
sfStereo16 |
| 91 |
); |
| 92 |
|
| 93 |
{ How to load a sound buffer. } |
| 94 |
TSoundLoading = ( |
| 95 |
{ Load entire sound file at once. |
| 96 |
The advantage is that once the sound buffer is loaded, there's zero overhead at runtime |
| 97 |
for playing it, and loading the sound buffer multiple times uses the cache properly. |
| 98 |
The disadvantage is that loading time may be long, for longer files. } |
| 99 |
slComplete, |
| 100 |
|
| 101 |
{ Decompress the sound (like OggVorbis) during playback. |
| 102 |
It allows for much quicker sound loading (almost instant, if you use streaming |
| 103 |
for everything) but means that sounds will be loaded (in parts) |
| 104 |
during playback. |
| 105 |
In general case, we advise to use it for longer sounds (like music tracks). } |
| 106 |
slStreaming |
| 107 |
); |
| 108 |
|
| 109 |
function DataFormatToStr(const DataFormat: TSoundDataFormat): string; |
| 110 |
|
| 111 |
implementation |
| 112 |
|
| 113 |
{ TSoundDeviceList ----------------------------------------------------------- } |
| 114 |
|
| 115 |
procedure TSoundDeviceList.Add(const AName, ACaption: string); |
| 116 |
var |
| 117 |
D: TSoundDevice; |
| 118 |
begin |
| 119 |
D := TSoundDevice.Create; |
| 120 |
D.FName := AName; |
| 121 |
D.FCaption := ACaption; |
| 122 |
inherited Add(D); |
| 123 |
end; |
| 124 |
|
| 125 |
{ global functions ----------------------------------------------------------- } |
| 126 |
|
| 127 |
function DataFormatToStr(const DataFormat: TSoundDataFormat): string; |
| 128 |
const |
| 129 |
DataFormatStr: array [TSoundDataFormat] of String = ( |
| 130 |
'mono 8', |
| 131 |
'mono 16', |
| 132 |
'stereo 8', |
| 133 |
'stereo 16' |
| 134 |
); |
| 135 |
begin |
| 136 |
Result := DataFormatStr[DataFormat]; |
| 137 |
end; |
| 138 |
|
| 139 |
end. |