| 1 |
{ API of vorbisfile / tremolo library. |
| 2 |
Usually called libvorbisfile.so under Unixes or vorbisfile.dll under Windows. |
| 3 |
On ARM devices (Android, iOS) this uses the Tremolo, |
| 4 |
http://wss.co.uk/pinknoise/tremolo/ , which is like vorbisfile but better for mobile devices. |
| 5 |
This started as a translation of /usr/include/vorbis/vorbisfile.h header. |
| 6 |
|
| 7 |
@exclude (This unit is only a C header translation (it has no nice PasDoc docs), |
| 8 |
and also this is internal.) } |
| 9 |
unit CastleInternalVorbisFile; |
| 10 |
|
| 11 |
{ Define this to link statically to Tremolo. |
| 12 |
This is automatically defined by the build tool |
| 13 |
when building a game for iOS using OggVorbis. } |
| 14 |
{.$define CASTLE_TREMOLO_STATIC} |
| 15 |
|
| 16 |
{$ifdef FPC} |
| 17 |
{$packrecords C} |
| 18 |
{$else} |
| 19 |
{$ALIGN 4} |
| 20 |
{$endif} |
| 21 |
|
| 22 |
{$i castleconf.inc} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
uses CTypes, CastleInternalVorbisCodec, CastleInternalOgg; |
| 27 |
|
| 28 |
const |
| 29 |
NOTOPEN = 0; |
| 30 |
PARTOPEN = 1; |
| 31 |
OPENED = 2; |
| 32 |
STREAMSET = 3; |
| 33 |
INITSET = 4; |
| 34 |
|
| 35 |
type |
| 36 |
TSizeT = CUInt32; |
| 37 |
|
| 38 |
TVorbisFileReadFunc = function (ptr: Pointer; Size: TSizeT; nmemb: TSizeT; DataSource: Pointer): TSizeT; cdecl; |
| 39 |
TVorbisFileSeekFunc = function (DataSource: Pointer; offset: Int64; whence: CInt): CInt; cdecl; |
| 40 |
TVorbisFileCloseFunc = function (DataSource: Pointer): CInt; cdecl; |
| 41 |
TVorbisFileTellFunc = function (DataSource: Pointer): CLong; cdecl; |
| 42 |
|
| 43 |
Tov_callbacks = record |
| 44 |
read_func: TVorbisFileReadFunc; |
| 45 |
seek_func: TVorbisFileSeekFunc; |
| 46 |
close_func: TVorbisFileCloseFunc; |
| 47 |
tell_func: TVorbisFileTellFunc; |
| 48 |
end; |
| 49 |
Pov_callbacks = ^Tov_callbacks; |
| 50 |
|
| 51 |
TOggVorbis_File = record |
| 52 |
datasource: Pointer; {* Pointer to a FILE *, etc. *} |
| 53 |
seekable: Cint; |
| 54 |
offset: Int64; |
| 55 |
_end: Int64; |
| 56 |
oy: Togg_sync_state; |
| 57 |
|
| 58 |
links: Cint; |
| 59 |
offsets: PInt64; |
| 60 |
dataoffsets: PInt64; |
| 61 |
serialnos: PCLong; |
| 62 |
pcmlengths: PInt64; {* overloaded to maintain binary |
| 63 |
compatability; x2 size, stores both |
| 64 |
beginning and end values *} |
| 65 |
vi: Pvorbis_info; |
| 66 |
vc: Pvorbis_comment; |
| 67 |
|
| 68 |
{* Decoding working state local storage *} |
| 69 |
pcm_offset: Int64; |
| 70 |
ready_state: CInt; |
| 71 |
current_serialno: Clong; |
| 72 |
current_link: CInt; |
| 73 |
|
| 74 |
bittrack: double; |
| 75 |
samptrack: double; |
| 76 |
|
| 77 |
os: Togg_stream_state; {* take physical pages, weld into a logical |
| 78 |
stream of packets *} |
| 79 |
vd: Tvorbis_dsp_state; {* central working state for the packet->PCM decoder *} |
| 80 |
vb: Tvorbis_block; {* local working space for packet->PCM decode *} |
| 81 |
|
| 82 |
callbacks: Tov_callbacks; |
| 83 |
end; |
| 84 |
POggVorbis_File = ^TOggVorbis_File; |
| 85 |
|
| 86 |
{$ifdef CASTLE_TREMOLO_STATIC} |
| 87 |
|
| 88 |
// link statically to Tremolo |
| 89 |
|
| 90 |
function ov_clear(Vf: POggVorbis_File): CInt; cdecl; external; |
| 91 |
function ov_open_callbacks(DataSource: Pointer; Vf: POggVorbis_File; Initial: PChar; ibytes: CLong; callbacks: Tov_callbacks): CInt; cdecl; external; |
| 92 |
|
| 93 |
function ov_test_callbacks(DataSource: Pointer; Vf: POggVorbis_File; Initial: PChar; ibytes: CLong; callbacks: Tov_callbacks): CInt; cdecl; external; |
| 94 |
function ov_test_open(Vf: POggVorbis_File): CInt; cdecl; external; |
| 95 |
|
| 96 |
function ov_bitrate(Vf: POggVorbis_File; i: CInt): CLong; cdecl; external; |
| 97 |
function ov_bitrate_instant(Vf: POggVorbis_File): CLong; cdecl; external; |
| 98 |
function ov_streams(Vf: POggVorbis_File): CLong; cdecl; external; |
| 99 |
function ov_seekable(Vf: POggVorbis_File): CLong; cdecl; external; |
| 100 |
function ov_serialnumber(Vf: POggVorbis_File; i: CInt): CLong; cdecl; external; |
| 101 |
|
| 102 |
function ov_raw_total(Vf: POggVorbis_File; i: CInt): Int64; cdecl; external; |
| 103 |
function ov_pcm_total(Vf: POggVorbis_File; i: CInt): Int64; cdecl; external; |
| 104 |
{ Returns Int64 on Tremolo, Double on VorbisFile. |
| 105 |
Compare |
| 106 |
https://xiph.org/vorbis/doc/vorbisfile/ov_time_total.html |
| 107 |
https://svn.xiph.org/trunk/Tremor/doc/ov_time_total.html . } |
| 108 |
function ov_time_total(Vf: POggVorbis_File; i: CInt): {$ifdef CASTLE_TREMOLO} Int64 {$else} Double {$endif}; cdecl; external; |
| 109 |
|
| 110 |
function ov_raw_seek(Vf: POggVorbis_File; pos: Int64): CInt; cdecl; external; |
| 111 |
function ov_pcm_seek(Vf: POggVorbis_File; pos: Int64): CInt; cdecl; external; |
| 112 |
function ov_pcm_seek_page(Vf: POggVorbis_File; pos: Int64): CInt; cdecl; external; |
| 113 |
function ov_time_seek(Vf: POggVorbis_File; pos: Double): CInt; cdecl; external; |
| 114 |
function ov_time_seek_page(Vf: POggVorbis_File; pos: Double): CInt; cdecl; external; |
| 115 |
|
| 116 |
function ov_raw_tell(Vf: POggVorbis_File): Int64; cdecl; external; |
| 117 |
function ov_pcm_tell(Vf: POggVorbis_File): Int64; cdecl; external; |
| 118 |
function ov_time_tell(Vf: POggVorbis_File): Double; cdecl; external; |
| 119 |
|
| 120 |
function ov_info(Vf: POggVorbis_File; link: CInt): Pvorbis_info; cdecl; external; |
| 121 |
|
| 122 |
function ov_read(Vf: POggVorbis_File; var buffer; length, bigendianp, word, sgned: CInt; bitstream: PCInt): CLong; cdecl; external; |
| 123 |
|
| 124 |
{$else} |
| 125 |
|
| 126 |
var |
| 127 |
ov_clear: function (Vf: POggVorbis_File): CInt; cdecl; |
| 128 |
{ Not translatable, we don't know C stdio FILE type: |
| 129 |
extern int ov_open(FILE *f,Vf: POggVorbis_File,Initial: PChar,ibytes: CLong); cdecl;} |
| 130 |
ov_open_callbacks: function (DataSource: Pointer; Vf: POggVorbis_File; Initial: PChar; ibytes: CLong; callbacks: Tov_callbacks): CInt; cdecl; |
| 131 |
|
| 132 |
{ Not translatable, we don't know C stdio FILE type: |
| 133 |
extern int ov_test(FILE *f,Vf: POggVorbis_File,Initial: PChar,ibytes: CLong); cdecl;} |
| 134 |
ov_test_callbacks: function (DataSource: Pointer; Vf: POggVorbis_File; Initial: PChar; ibytes: CLong; callbacks: Tov_callbacks): CInt; cdecl; |
| 135 |
ov_test_open: function (Vf: POggVorbis_File): CInt; cdecl; |
| 136 |
|
| 137 |
ov_bitrate: function (Vf: POggVorbis_File; i: CInt): CLong; cdecl; |
| 138 |
ov_bitrate_instant: function (Vf: POggVorbis_File): CLong; cdecl; |
| 139 |
ov_streams: function (Vf: POggVorbis_File): CLong; cdecl; |
| 140 |
ov_seekable: function (Vf: POggVorbis_File): CLong; cdecl; |
| 141 |
ov_serialnumber: function (Vf: POggVorbis_File; i: CInt): CLong; cdecl; |
| 142 |
|
| 143 |
ov_raw_total: function (Vf: POggVorbis_File; i: CInt): Int64; cdecl; |
| 144 |
ov_pcm_total: function (Vf: POggVorbis_File; i: CInt): Int64; cdecl; |
| 145 |
ov_time_total: function (Vf: POggVorbis_File; i: CInt): {$ifdef CASTLE_TREMOLO} Int64 {$else} Double {$endif}; cdecl; |
| 146 |
|
| 147 |
ov_raw_seek: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; |
| 148 |
ov_pcm_seek: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; |
| 149 |
ov_pcm_seek_page: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; |
| 150 |
ov_time_seek: function (Vf: POggVorbis_File; pos: Double): CInt; cdecl; |
| 151 |
ov_time_seek_page: function (Vf: POggVorbis_File; pos: Double): CInt; cdecl; |
| 152 |
|
| 153 |
// ov_raw_seek_lap: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; // not available in libtremolo |
| 154 |
// ov_pcm_seek_lap: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; // not available in libtremolo |
| 155 |
// ov_pcm_seek_page_lap: function (Vf: POggVorbis_File; pos: Int64): CInt; cdecl; // not available in libtremolo |
| 156 |
// ov_time_seek_lap: function (Vf: POggVorbis_File; pos: Double): CInt; cdecl; // not available in libtremolo |
| 157 |
// ov_time_seek_page_lap: function (Vf: POggVorbis_File; pos: Double): CInt; cdecl; // not available in libtremolo |
| 158 |
|
| 159 |
ov_raw_tell: function (Vf: POggVorbis_File): Int64; cdecl; |
| 160 |
ov_pcm_tell: function (Vf: POggVorbis_File): Int64; cdecl; |
| 161 |
ov_time_tell: function (Vf: POggVorbis_File): Double; cdecl; |
| 162 |
|
| 163 |
ov_info: function (Vf: POggVorbis_File; link: CInt): Pvorbis_info; cdecl; |
| 164 |
{ Not translated yet } |
| 165 |
//extern vorbis_comment *ov_comment(Vf: POggVorbis_File,int link); cdecl; |
| 166 |
|
| 167 |
//ov_read_float: function (Vf: POggVorbis_File,float ***pcm_channels,int samples, int *bitstream): CLong; cdecl; |
| 168 |
ov_read: function (Vf: POggVorbis_File; var buffer; length, bigendianp, word, sgned: CInt; bitstream: PCInt): CLong; cdecl; |
| 169 |
//ov_crosslap: function (Vf: POggVorbis_File1,Vf: POggVorbis_File2): CInt; cdecl; |
| 170 |
|
| 171 |
//ov_halfrate: function (Vf: POggVorbis_File,int flag): CInt; cdecl; |
| 172 |
//ov_halfrate_p: function (Vf: POggVorbis_File): CInt; cdecl; |
| 173 |
|
| 174 |
{$endif CASTLE_TREMOLO_STATIC} |
| 175 |
|
| 176 |
{ Is the vorbisfile shared library (with all necessary symbols) available. } |
| 177 |
function VorbisFileInitialized: boolean; |
| 178 |
|
| 179 |
procedure VorbisFileInitialization; |
| 180 |
|
| 181 |
implementation |
| 182 |
|
| 183 |
{$ifdef CASTLE_TREMOLO_STATIC} |
| 184 |
|
| 185 |
function VorbisFileInitialized: boolean; |
| 186 |
begin |
| 187 |
Result := true; |
| 188 |
end; |
| 189 |
|
| 190 |
procedure VorbisFileInitialization; |
| 191 |
begin |
| 192 |
end; |
| 193 |
|
| 194 |
end. |
| 195 |
|
| 196 |
{$else CASTLE_TREMOLO_STATIC} |
| 197 |
|
| 198 |
uses SysUtils, CastleUtils, CastleDynLib, CastleFilesUtils; |
| 199 |
|
| 200 |
var |
| 201 |
VorbisFileLibrary: TDynLib; |
| 202 |
|
| 203 |
function VorbisFileInitialized: boolean; |
| 204 |
begin |
| 205 |
Result := VorbisFileLibrary <> nil; |
| 206 |
end; |
| 207 |
|
| 208 |
procedure VorbisFileInitialization; |
| 209 |
begin |
| 210 |
FreeAndNil(VorbisFileLibrary); |
| 211 |
|
| 212 |
VorbisFileLibrary := |
| 213 |
|
| 214 |
{$ifdef UNIX} |
| 215 |
{$ifdef DARWIN} |
| 216 |
TDynLib.Load('libvorbisfile.3.dylib', false); |
| 217 |
if VorbisFileLibrary = nil then |
| 218 |
VorbisFileLibrary := TDynLib.Load('libvorbisfile.dylib', false); |
| 219 |
if (VorbisFileLibrary = nil) and (BundlePath <> '') then |
| 220 |
VorbisFileLibrary := TDynLib.Load(BundlePath + 'Contents/MacOS/libvorbisfile.3.dylib', false); |
| 221 |
if (VorbisFileLibrary = nil) and (BundlePath <> '') then |
| 222 |
VorbisFileLibrary := TDynLib.Load(BundlePath + 'Contents/MacOS/libvorbisfile.dylib', false); |
| 223 |
|
| 224 |
{$else} |
| 225 |
TDynLib.Load('libvorbisfile.so.3', false); |
| 226 |
if VorbisFileLibrary = nil then |
| 227 |
VorbisFileLibrary := TDynLib.Load('libvorbisfile.so', false); |
| 228 |
// fallback on Tremolo, necessary for Android, may also work on other OSes |
| 229 |
if VorbisFileLibrary = nil then |
| 230 |
VorbisFileLibrary := TDynLib.Load('libtremolo.so', false); |
| 231 |
if VorbisFileLibrary = nil then |
| 232 |
VorbisFileLibrary := TDynLib.Load('libtremolo-low-precision.so', false); |
| 233 |
{$endif} |
| 234 |
{$endif} |
| 235 |
|
| 236 |
{$ifdef MSWINDOWS} |
| 237 |
TDynLib.Load('vorbisfile.dll', false); |
| 238 |
{$endif} |
| 239 |
|
| 240 |
if VorbisFileLibrary <> nil then |
| 241 |
begin |
| 242 |
Pointer({$ifndef FPC}@{$endif} ov_clear) := VorbisFileLibrary.Symbol('ov_clear'); |
| 243 |
//Pointer({$ifndef FPC}@{$endif} ov_open) := VorbisFileLibrary.Symbol('ov_open'); |
| 244 |
Pointer({$ifndef FPC}@{$endif} ov_open_callbacks) := VorbisFileLibrary.Symbol('ov_open_callbacks'); |
| 245 |
|
| 246 |
//Pointer({$ifndef FPC}@{$endif} ov_test) := VorbisFileLibrary.Symbol('ov_test'); |
| 247 |
Pointer({$ifndef FPC}@{$endif} ov_test_callbacks) := VorbisFileLibrary.Symbol('ov_test_callbacks'); |
| 248 |
Pointer({$ifndef FPC}@{$endif} ov_test_open) := VorbisFileLibrary.Symbol('ov_test_open'); |
| 249 |
|
| 250 |
Pointer({$ifndef FPC}@{$endif} ov_bitrate) := VorbisFileLibrary.Symbol('ov_bitrate'); |
| 251 |
Pointer({$ifndef FPC}@{$endif} ov_bitrate_instant) := VorbisFileLibrary.Symbol('ov_bitrate_instant'); |
| 252 |
Pointer({$ifndef FPC}@{$endif} ov_streams) := VorbisFileLibrary.Symbol('ov_streams'); |
| 253 |
Pointer({$ifndef FPC}@{$endif} ov_seekable) := VorbisFileLibrary.Symbol('ov_seekable'); |
| 254 |
Pointer({$ifndef FPC}@{$endif} ov_serialnumber) := VorbisFileLibrary.Symbol('ov_serialnumber'); |
| 255 |
|
| 256 |
Pointer({$ifndef FPC}@{$endif} ov_raw_total) := VorbisFileLibrary.Symbol('ov_raw_total'); |
| 257 |
Pointer({$ifndef FPC}@{$endif} ov_pcm_total) := VorbisFileLibrary.Symbol('ov_pcm_total'); |
| 258 |
Pointer({$ifndef FPC}@{$endif} ov_time_total) := VorbisFileLibrary.Symbol('ov_time_total'); |
| 259 |
|
| 260 |
Pointer({$ifndef FPC}@{$endif} ov_raw_seek) := VorbisFileLibrary.Symbol('ov_raw_seek'); |
| 261 |
Pointer({$ifndef FPC}@{$endif} ov_pcm_seek) := VorbisFileLibrary.Symbol('ov_pcm_seek'); |
| 262 |
Pointer({$ifndef FPC}@{$endif} ov_pcm_seek_page) := VorbisFileLibrary.Symbol('ov_pcm_seek_page'); |
| 263 |
Pointer({$ifndef FPC}@{$endif} ov_time_seek) := VorbisFileLibrary.Symbol('ov_time_seek'); |
| 264 |
Pointer({$ifndef FPC}@{$endif} ov_time_seek_page) := VorbisFileLibrary.Symbol('ov_time_seek_page'); |
| 265 |
|
| 266 |
// Pointer({$ifndef FPC}@{$endif} ov_raw_seek_lap) := VorbisFileLibrary.Symbol('ov_raw_seek_lap'); // not available in libtremolo |
| 267 |
// Pointer({$ifndef FPC}@{$endif} ov_pcm_seek_lap) := VorbisFileLibrary.Symbol('ov_pcm_seek_lap'); // not available in libtremolo |
| 268 |
// Pointer({$ifndef FPC}@{$endif} ov_pcm_seek_page_lap) := VorbisFileLibrary.Symbol('ov_pcm_seek_page_lap'); // not available in libtremolo |
| 269 |
// Pointer({$ifndef FPC}@{$endif} ov_time_seek_lap) := VorbisFileLibrary.Symbol('ov_time_seek_lap'); // not available in libtremolo |
| 270 |
// Pointer({$ifndef FPC}@{$endif} ov_time_seek_page_lap) := VorbisFileLibrary.Symbol('ov_time_seek_page_lap'); // not available in libtremolo |
| 271 |
|
| 272 |
Pointer({$ifndef FPC}@{$endif} ov_raw_tell) := VorbisFileLibrary.Symbol('ov_raw_tell'); |
| 273 |
Pointer({$ifndef FPC}@{$endif} ov_pcm_tell) := VorbisFileLibrary.Symbol('ov_pcm_tell'); |
| 274 |
Pointer({$ifndef FPC}@{$endif} ov_time_tell) := VorbisFileLibrary.Symbol('ov_time_tell'); |
| 275 |
|
| 276 |
Pointer({$ifndef FPC}@{$endif} ov_info) := VorbisFileLibrary.Symbol('ov_info'); |
| 277 |
//Pointer({$ifndef FPC}@{$endif} ov_comment) := VorbisFileLibrary.Symbol('ov_comment'); |
| 278 |
|
| 279 |
//Pointer({$ifndef FPC}@{$endif} ov_read_float) := VorbisFileLibrary.Symbol('ov_read_float'); |
| 280 |
Pointer({$ifndef FPC}@{$endif} ov_read) := VorbisFileLibrary.Symbol('ov_read'); |
| 281 |
//Pointer({$ifndef FPC}@{$endif} ov_crosslap) := VorbisFileLibrary.Symbol('ov_crosslap'); |
| 282 |
|
| 283 |
//Pointer({$ifndef FPC}@{$endif} ov_halfrate) := VorbisFileLibrary.Symbol('ov_halfrate'); |
| 284 |
//Pointer({$ifndef FPC}@{$endif} ov_halfrate_p) := VorbisFileLibrary.Symbol('ov_halfrate_p'); |
| 285 |
end; |
| 286 |
end; |
| 287 |
|
| 288 |
initialization |
| 289 |
{$ifdef ALLOW_DLOPEN_FROM_UNIT_INITIALIZATION} |
| 290 |
VorbisFileInitialization; |
| 291 |
{$endif} |
| 292 |
finalization |
| 293 |
FreeAndNil(VorbisFileLibrary); |
| 294 |
end. |
| 295 |
|
| 296 |
{$endif CASTLE_TREMOLO_STATIC} |