| 1 |
{%MainUnit castlesoundengine.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2021 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
//type // allow type declaration to continue |
| 20 |
|
| 21 |
{ Sound buffer represents contents of a sound file, like Wav or OggVorbis, |
| 22 |
that (may be) played. The loading parameters, like streaming and URL, |
| 23 |
cannot change after loading buffer. |
| 24 |
|
| 25 |
It can be only allocated by @link(TSoundEngine.LoadBuffer) |
| 26 |
and freed by @link(TSoundEngine.FreeBuffer). |
| 27 |
You should first stop all the sound sources using this buffer before freeing it |
| 28 |
(with OpenAL, not doing it may cause a warning: OpenAL error at alDeleteBuffers). |
| 29 |
@bold(Do not create or free TInternalSoundBuffer instances yourself.) |
| 30 |
|
| 31 |
This is internal now. Use TCastleSound to load a sound comfortably, |
| 32 |
it hides dealing with TInternalSoundBuffer from you (and TCastleSound has more properties, |
| 33 |
and allows to change TCastleSound.Url / TCastleSound.Stream at any point). |
| 34 |
|
| 35 |
@exclude |
| 36 |
} |
| 37 |
TInternalSoundBuffer = class |
| 38 |
private |
| 39 |
FUrl: String; |
| 40 |
FSoundLoading: TSoundLoading; |
| 41 |
References: Cardinal; |
| 42 |
Backend: TSoundBufferBackend; |
| 43 |
BackendIsOpen: Boolean; |
| 44 |
procedure ContextOpen(const ExceptionOnError: boolean); |
| 45 |
procedure ContextClose; |
| 46 |
public |
| 47 |
constructor Create(const SoundEngineBackend: TSoundEngineBackend; |
| 48 |
const SoundLoading: TSoundLoading); |
| 49 |
destructor Destroy; override; |
| 50 |
|
| 51 |
{ Duration of the sound, in seconds. -1 if not loaded yet. } |
| 52 |
function Duration: TFloatTime; |
| 53 |
|
| 54 |
{ Absolute sound file URL. |
| 55 |
Never empty (do not create TInternalSoundBuffer instances for invalid / empty URL, |
| 56 |
like the ones that can be created by TRepoSoundEngine for not defined sounds.) } |
| 57 |
property Url: String read FUrl; |
| 58 |
|
| 59 |
{ Data format (bits per sample, stereo or mono) of the loaded sound file. |
| 60 |
Typical applications don't need this value, this is just an information |
| 61 |
about the loaded sound file. |
| 62 |
Undefined if backend is not loaded. } |
| 63 |
function DataFormat: TSoundDataFormat; |
| 64 |
|
| 65 |
{ Frequency (sample rate) of the loaded sound file. |
| 66 |
Typical applications don't need this value, this is just an information |
| 67 |
about the loaded sound file. |
| 68 |
Undefined if backend is not loaded. } |
| 69 |
function Frequency: Cardinal; |
| 70 |
end; |
| 71 |
|
| 72 |
TSoundBuffer = TInternalSoundBuffer deprecated 'use TInternalSoundBuffer'; |
| 73 |
|
| 74 |
{$endif read_interface} |
| 75 |
|
| 76 |
{$ifdef read_implementation} |
| 77 |
|
| 78 |
{ TInternalSoundBuffer --------------------------------------------------------------- } |
| 79 |
|
| 80 |
constructor TInternalSoundBuffer.Create(const SoundEngineBackend: TSoundEngineBackend; |
| 81 |
const SoundLoading: TSoundLoading); |
| 82 |
begin |
| 83 |
inherited Create; |
| 84 |
FSoundLoading := SoundLoading; |
| 85 |
Backend := SoundEngineBackend.CreateBuffer(SoundLoading); |
| 86 |
end; |
| 87 |
|
| 88 |
function TInternalSoundBuffer.DataFormat: TSoundDataFormat; |
| 89 |
begin |
| 90 |
if BackendIsOpen then |
| 91 |
Result := Backend.DataFormat |
| 92 |
else |
| 93 |
Result := Default(TSoundDataFormat); |
| 94 |
end; |
| 95 |
|
| 96 |
function TInternalSoundBuffer.Frequency: Cardinal; |
| 97 |
begin |
| 98 |
if BackendIsOpen then |
| 99 |
Result := Backend.Frequency |
| 100 |
else |
| 101 |
Result := 0; |
| 102 |
end; |
| 103 |
|
| 104 |
function TInternalSoundBuffer.Duration: TFloatTime; |
| 105 |
begin |
| 106 |
if BackendIsOpen then |
| 107 |
Result := Backend.Duration |
| 108 |
else |
| 109 |
Result := -1; |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TInternalSoundBuffer.ContextOpen(const ExceptionOnError: boolean); |
| 113 |
|
| 114 |
procedure OpenCore; |
| 115 |
begin |
| 116 |
FUrl := Url; |
| 117 |
Backend.ContextOpen(Url); |
| 118 |
BackendIsOpen := true; |
| 119 |
end; |
| 120 |
|
| 121 |
begin |
| 122 |
if BackendIsOpen then |
| 123 |
Exit; // do not initialize already-initialized |
| 124 |
|
| 125 |
if ExceptionOnError then |
| 126 |
begin |
| 127 |
OpenCore; |
| 128 |
end else |
| 129 |
try |
| 130 |
OpenCore; |
| 131 |
except |
| 132 |
on E: Exception do |
| 133 |
begin |
| 134 |
WritelnWarning('Sound', Format('Sound file "%s" cannot be loaded: %s', |
| 135 |
[UriDisplay(Url), E.Message])); |
| 136 |
end; |
| 137 |
end; |
| 138 |
end; |
| 139 |
|
| 140 |
procedure TInternalSoundBuffer.ContextClose; |
| 141 |
begin |
| 142 |
if BackendIsOpen then |
| 143 |
begin |
| 144 |
Backend.ContextClose; |
| 145 |
BackendIsOpen := false; |
| 146 |
end; |
| 147 |
end; |
| 148 |
|
| 149 |
var |
| 150 |
ValidSoundBufferFree: Cardinal; |
| 151 |
|
| 152 |
destructor TInternalSoundBuffer.Destroy; |
| 153 |
begin |
| 154 |
if ValidSoundBufferFree = 0 then |
| 155 |
raise EInvalidSoundBufferFree.Create('Do not free TInternalSoundBuffer instance directly, use SoundEngine.FreeBuffer'); |
| 156 |
ContextClose; |
| 157 |
FreeAndNil(Backend); |
| 158 |
inherited; |
| 159 |
end; |
| 160 |
|
| 161 |
{$endif read_implementation} |