Debian Codegraph @

plugins/wdx/audioinfo/src/atl/DTS.pas

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TDTS - for manipulating with DTS Files }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2005 by Gambit }
10 { }
11 { Version 1.1 (April 2005) by Gambit }
12 { - updated to unicode file access }
13 { }
14 { Version 1.0 (10 January 2005) }
15 { }
16 { This library is free software; you can redistribute it and/or }
17 { modify it under the terms of the GNU Lesser General Public }
18 { License as published by the Free Software Foundation; either }
19 { version 2.1 of the License, or (at your option) any later version. }
20 { }
21 { This library is distributed in the hope that it will be useful, }
22 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
23 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
24 { Lesser General Public License for more details. }
25 { }
26 { You should have received a copy of the GNU Lesser General Public }
27 { License along with this library; if not, write to the Free Software }
28 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
29 { }
30 { *************************************************************************** }
31
32 unit DTS;
33
34 interface
35
36 uses
37 Classes, SysUtils, DCClassesUtf8;
38
39 const
40 BIRATES: array[0..31] of Integer = (32, 56, 64, 96, 112, 128, 192, 224, 256,
41 320, 384, 448, 512, 576, 640, 768, 960,
42 1024, 1152, 1280, 1344, 1408, 1411, 1472,
43 1536, 1920, 2048, 3072, 3840, 0, -1, 1);
44 //open, variable, lossless
45 type
46 { Class TDTS }
47 TDTS = class(TObject)
48 private
49 { Private declarations }
50 FFileSize: Int64;
51 FValid: Boolean;
52
53 FChannels: Cardinal;
54 FBits: Cardinal;
55 FSampleRate: Cardinal;
56
57 FBitrate: Word;
58 FDuration: Double;
59
60 function FGetRatio: Double;
61 procedure FResetData;
62
63 public
64 { Public declarations }
65 constructor Create; { Create object }
66 destructor Destroy; override; { Destroy object }
67
68 function ReadFromFile(const FileName: String): Boolean; { Load header }
69
70 property FileSize: Int64 read FFileSize;
71 property Valid: Boolean read FValid;
72
73 property Channels: Cardinal read FChannels;
74 property Bits: Cardinal read FBits;
75 property SampleRate: Cardinal read FSampleRate;
76
77 property Bitrate: Word read FBitrate;
78 property Duration: Double read FDuration;
79 property Ratio: Double read FGetRatio; { Compression ratio (%) }
80 end;
81
82 implementation
83
84
85 { ********************** Private functions & procedures ********************* }
86
87 procedure TDTS.FResetData;
88 begin
89 { Reset all data }
90 FFileSize := 0;
91 FValid := False;
92
93 FChannels := 0;
94 FBits := 0;
95 FSampleRate := 0;
96
97 FBitrate := 0;
98 FDuration := 0;
99 end;
100
101
102 { ********************** Public functions & procedures ********************** }
103
104 constructor TDTS.Create;
105 begin
106 { Create object }
107 inherited;
108 FResetData;
109 end;
110
111 (* -------------------------------------------------------------------------- *)
112
113 destructor TDTS.Destroy;
114 begin
115 inherited;
116 end;
117
118 (* -------------------------------------------------------------------------- *)
119
120 function TDTS.ReadFromFile(const FileName: String): Boolean;
121 var
122 f: TFileStreamEx;
123 SignatureChunk: Cardinal;
124 tehWord: Word;
125 gayDTS: array[0..7] of Byte;
126 begin
127 Result := False;
128 FResetData;
129
130 f:=nil;
131
132 try
133 f := TFileStreamEx.create(FileName, fmOpenRead or fmShareDenyWrite);
134 //0x7FFE8001
135 if (f.Read(SignatureChunk, SizeOf(SignatureChunk)) = SizeOf(SignatureChunk)) and (SignatureChunk = 25230975) then
136 begin
137 FillChar(gayDTS, SizeOf(gayDTS),0);
138 f.Seek(3, soFromCurrent);
139 f.Read(gayDTS, SizeOf(gayDTS));
140
141 FFileSize := f.Size;
142 FValid := TRUE;
143
144 tehWord := gayDTS[1] or (gayDTS[0] shl 8);
145
146 case ((tehWord and $0FC0) shr 6) of
147 0: FChannels := 1;
148 1..4: FChannels := 2;
149 5..6: FChannels := 3;
150 7..8: FChannels := 4;
151 9: FChannels := 5;
152 10..12: FChannels := 6;
153 13: FChannels := 7;
154 14..15: FChannels := 8;
155 else FChannels := 0;
156 end;
157
158 case ((tehWord and $3C) shr 2) of
159 1: FSampleRate := 8000;
160 2: FSampleRate := 16000;
161 3: FSampleRate := 32000;
162 6: FSampleRate := 11025;
163 7: FSampleRate := 22050;
164 8: FSampleRate := 44100;
165 11: FSampleRate := 12000;
166 12: FSampleRate := 24000;
167 13: FSampleRate := 48000;
168 else FSampleRate := 0;
169 end;
170
171 tehWord := 0;
172 tehWord := gayDTS[2] or (gayDTS[1] shl 8);
173
174 FBitrate := BIRATES[(tehWord and $03E0) shr 5];
175
176 tehWord := 0;
177 tehWord := gayDTS[7] or (gayDTS[6] shl 8);
178
179 case ((tehWord and $01C0) shr 6) of
180 0..1: FBits := 16;
181 2..3: FBits := 20;
182 4..5: FBits := 24;
183 else FBits := 16;
184 end;
185
186 FDuration := FFileSize * 8 / 1000 / FBitrate;
187
188 Result := True;
189 end;
190
191 finally
192 f.free;
193 end;
194 end;
195
196 (* -------------------------------------------------------------------------- *)
197
198 function TDTS.FGetRatio: Double;
199 begin
200 { Get compression ratio }
201 if FValid then
202 Result := FFileSize / ((FDuration * FSampleRate) * (FChannels * FBits / 8) + 44) * 100
203 else
204 Result := 0;
205 end;
206
207 (* -------------------------------------------------------------------------- *)
208
209 end.