Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TTwinVQ - for extracting information from TwinVQ file header }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2000-2002 by Jurgen Faul }
10 { Copyright (c) 2003-2005 by The MAC Team }
11 { }
12 { Version 1.3 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 1.2 (April 2004) by Gambit }
16 { - Added Ratio property }
17 { }
18 { Version 1.1 (13 August 2002) }
19 { - Added property Album }
20 { - Support for Twin VQ 2.0 }
21 { }
22 { Version 1.0 (6 August 2001) }
23 { - File info: channel mode, bit rate, sample rate, file size, duration }
24 { - Tag info: title, comment, author, copyright, compressed file name }
25 { }
26 { This library is free software; you can redistribute it and/or }
27 { modify it under the terms of the GNU Lesser General Public }
28 { License as published by the Free Software Foundation; either }
29 { version 2.1 of the License, or (at your option) any later version. }
30 { }
31 { This library is distributed in the hope that it will be useful, }
32 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
33 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
34 { Lesser General Public License for more details. }
35 { }
36 { You should have received a copy of the GNU Lesser General Public }
37 { License along with this library; if not, write to the Free Software }
38 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
39 { }
40 { *************************************************************************** }
41
42 unit TwinVQ;
43
44 interface
45
46 uses
47 Classes, SysUtils, DCClassesUtf8;
48
49 const
50 { Used with ChannelModeID property }
51 TWIN_CM_MONO = 1; { Index for mono mode }
52 TWIN_CM_STEREO = 2; { Index for stereo mode }
53
54 { Channel mode names }
55 TWIN_MODE: array [0..2] of string = ('Unknown', 'Mono', 'Stereo');
56
57 type
58 { Class TTwinVQ }
59 TTwinVQ = class(TObject)
60 private
61 { Private declarations }
62 FValid: Boolean;
63 FChannelModeID: Byte;
64 FBitRate: Byte;
65 FSampleRate: Word;
66 FFileSize: Cardinal;
67 FDuration: Double;
68 FTitle: string;
69 FComment: string;
70 FAuthor: string;
71 FCopyright: string;
72 FOriginalFile: string;
73 FAlbum: string;
74 procedure FResetData;
75 function FGetChannelMode: string;
76 function FIsCorrupted: Boolean;
77 function FGetRatio: Double;
78 public
79 { Public declarations }
80 constructor Create; { Create object }
81 function ReadFromFile(const FileName: String): Boolean; { Load header }
82 property Valid: Boolean read FValid; { True if header valid }
83 property ChannelModeID: Byte read FChannelModeID; { Channel mode code }
84 property ChannelMode: string read FGetChannelMode; { Channel mode name }
85 property BitRate: Byte read FBitRate; { Total bit rate }
86 property SampleRate: Word read FSampleRate; { Sample rate (hz) }
87 property FileSize: Cardinal read FFileSize; { File size (bytes) }
88 property Duration: Double read FDuration; { Duration (seconds) }
89 property Title: string read FTitle; { Title name }
90 property Comment: string read FComment; { Comment }
91 property Author: string read FAuthor; { Author name }
92 property Copyright: string read FCopyright; { Copyright }
93 property OriginalFile: string read FOriginalFile; { Original file name }
94 property Album: string read FAlbum; { Album title }
95 property Corrupted: Boolean read FIsCorrupted; { True if file corrupted }
96 property Ratio: Double read FGetRatio; { Compression ratio (%) }
97 end;
98
99 implementation
100
101 const
102 { Twin VQ header ID }
103 TWIN_ID = 'TWIN';
104
105 { Max. number of supported tag-chunks }
106 TWIN_CHUNK_COUNT = 6;
107
108 { Names of supported tag-chunks }
109 TWIN_CHUNK: array [1..TWIN_CHUNK_COUNT] of string =
110 ('NAME', 'COMT', 'AUTH', '(c) ', 'FILE', 'ALBM');
111
112 type
113 { TwinVQ chunk header }
114 ChunkHeader = record
115 ID: array [1..4] of Char; { Chunk ID }
116 Size: Cardinal; { Chunk size }
117 end;
118
119 { File header data - for internal use }
120 HeaderInfo = record
121 { Real structure of TwinVQ file header }
122 ID: array [1..4] of Char; { Always "TWIN" }
123 Version: array [1..8] of Char; { Version ID }
124 Size: Cardinal; { Header size }
125 Common: ChunkHeader; { Common chunk header }
126 ChannelMode: Cardinal; { Channel mode: 0 - mono, 1 - stereo }
127 BitRate: Cardinal; { Total bit rate }
128 SampleRate: Cardinal; { Sample rate (khz) }
129 SecurityLevel: Cardinal; { Always 0 }
130 { Extended data }
131 FileSize: Cardinal; { File size (bytes) }
132 Tag: array [1..TWIN_CHUNK_COUNT] of string; { Tag information }
133 end;
134
135 { ********************* Auxiliary functions & procedures ******************** }
136
137 function ReadHeader(const FileName: String; var Header: HeaderInfo): Boolean;
138 var
139 SourceFile: TFileStreamEx;
140 Transferred: Integer;
141 begin
142 try
143 Result := true;
144 { Set read-access and open file }
145 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
146 { Read header and get file size }
147 Transferred := SourceFile.Read(Header, 40);
148 Header.FileSize := SourceFile.Size;
149 SourceFile.Free;
150 { if transfer is not complete }
151 if Transferred < 40 then Result := false;
152 except
153 { Error }
154 Result := false;
155 end;
156 end;
157
158 { --------------------------------------------------------------------------- }
159
160 function GetChannelModeID(const Header: HeaderInfo): Byte;
161 begin
162 { Get channel mode from header }
163 case Swap(Header.ChannelMode shr 16) of
164 0: Result := TWIN_CM_MONO;
165 1: Result := TWIN_CM_STEREO
166 else Result := 0;
167 end;
168 end;
169
170 { --------------------------------------------------------------------------- }
171
172 function GetBitRate(const Header: HeaderInfo): Byte;
173 begin
174 { Get bit rate from header }
175 Result := Swap(Header.BitRate shr 16);
176 end;
177
178 { --------------------------------------------------------------------------- }
179
180 function GetSampleRate(const Header: HeaderInfo): Word;
181 begin
182 { Get real sample rate from header }
183 Result := Swap(Header.SampleRate shr 16);
184 case Result of
185 11: Result := 11025;
186 22: Result := 22050;
187 44: Result := 44100;
188 else Result := Result * 1000;
189 end;
190 end;
191
192 { --------------------------------------------------------------------------- }
193
194 function GetDuration(const Header: HeaderInfo): Double;
195 begin
196 { Get duration from header }
197 Result := Abs((Header.FileSize - Swap(Header.Size shr 16) - 20)) / 125 /
198 Swap(Header.BitRate shr 16);
199 end;
200
201 { --------------------------------------------------------------------------- }
202
203 function HeaderEndReached(const Chunk: ChunkHeader): Boolean;
204 begin
205 { Check for header end }
206 Result := (Ord(Chunk.ID[1]) < 32) or
207 (Ord(Chunk.ID[2]) < 32) or
208 (Ord(Chunk.ID[3]) < 32) or
209 (Ord(Chunk.ID[4]) < 32) or
210 (Chunk.ID = 'DATA');
211 end;
212
213 { --------------------------------------------------------------------------- }
214
215 procedure SetTagItem(const ID, Data: string; var Header: HeaderInfo);
216 var
217 Iterator: Byte;
218 begin
219 { Set tag item if supported tag-chunk found }
220 for Iterator := 1 to TWIN_CHUNK_COUNT do
221 if TWIN_CHUNK[Iterator] = ID then Header.Tag[Iterator] := Data;
222 end;
223
224 { --------------------------------------------------------------------------- }
225
226 procedure ReadTag(const FileName: String; var Header: HeaderInfo);
227 var
228 SourceFile: TFileStreamEx;
229 Chunk: ChunkHeader;
230 Data: array [1..250] of Char;
231 begin
232 try
233 { Set read-access, open file }
234 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
235 SourceFile.Seek(16, soFromBeginning);
236 repeat
237 begin
238 FillChar(Data, SizeOf(Data), 0);
239 { Read chunk header }
240 SourceFile.Read(Chunk, 8);
241 { Read chunk data and set tag item if chunk header valid }
242 if HeaderEndReached(Chunk) then break;
243 SourceFile.Read(Data, Swap(Chunk.Size shr 16) mod SizeOf(Data));
244 SetTagItem(Chunk.ID, Data, Header);
245 end;
246 until SourceFile.Position >= SourceFile.Size;
247 SourceFile.Free;
248 except
249 end;
250 end;
251
252 { ********************** Private functions & procedures ********************* }
253
254 procedure TTwinVQ.FResetData;
255 begin
256 FValid := false;
257 FChannelModeID := 0;
258 FBitRate := 0;
259 FSampleRate := 0;
260 FFileSize := 0;
261 FDuration := 0;
262 FTitle := '';
263 FComment := '';
264 FAuthor := '';
265 FCopyright := '';
266 FOriginalFile := '';
267 FAlbum := '';
268 end;
269
270 { --------------------------------------------------------------------------- }
271
272 function TTwinVQ.FGetChannelMode: string;
273 begin
274 Result := TWIN_MODE[FChannelModeID];
275 end;
276
277 { --------------------------------------------------------------------------- }
278
279 function TTwinVQ.FIsCorrupted: Boolean;
280 begin
281 { Check for file corruption }
282 Result := (FValid) and
283 ((FChannelModeID = 0) or
284 (FBitRate < 8) or (FBitRate > 192) or
285 (FSampleRate < 8000) or (FSampleRate > 44100) or
286 (FDuration < 0.1) or (FDuration > 10000));
287 end;
288
289 { ********************** Public functions & procedures ********************** }
290
291 constructor TTwinVQ.Create;
292 begin
293 inherited;
294 FResetData;
295 end;
296
297 { --------------------------------------------------------------------------- }
298
299 function TTwinVQ.ReadFromFile(const FileName: String): Boolean;
300 var
301 Header: HeaderInfo;
302 begin
303 { Reset data and load header from file to variable }
304 FResetData;
305 Result := ReadHeader(FileName, Header);
306 { Process data if loaded and header valid }
307 if (Result) and (Header.ID = TWIN_ID) then
308 begin
309 FValid := true;
310 { Fill properties with header data }
311 FChannelModeID := GetChannelModeID(Header);
312 FBitRate := GetBitRate(Header);
313 FSampleRate := GetSampleRate(Header);
314 FFileSize := Header.FileSize;
315 FDuration := GetDuration(Header);
316 { Get tag information and fill properties }
317 ReadTag(FileName, Header);
318 FTitle := Trim(Header.Tag[1]);
319 FComment := Trim(Header.Tag[2]);
320 FAuthor := Trim(Header.Tag[3]);
321 FCopyright := Trim(Header.Tag[4]);
322 FOriginalFile := Trim(Header.Tag[5]);
323 FAlbum := Trim(Header.Tag[6]);
324 end;
325 end;
326
327 { --------------------------------------------------------------------------- }
328
329 function TTwinVQ.FGetRatio: Double;
330 begin
331 { Get compression ratio }
332 if FValid then
333 Result := FFileSize / ((FDuration * FSampleRate) * (FChannelModeID * 16 / 8) + 44) * 100
334 else
335 Result := 0;
336 end;
337
338 { --------------------------------------------------------------------------- }
339
340 end.