Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TTTA - for manipulating with TTA Files }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2004-2005 by Gambit }
10 { }
11 { Version 1.1 (April 2005) by Gambit }
12 { - updated to unicode file access }
13 { }
14 { Version 1.0 (12 August 2004) }
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 TTA;
33
34 interface
35
36 uses
37 Classes, SysUtils, ID3v1, ID3v2, APEtag, DCClassesUtf8;
38
39 type
40
41 tta_header = packed record
42 //TTAid: array[0..3] of Char;
43 AudioFormat: Word;
44 NumChannels: Word;
45 BitsPerSample: Word;
46 SampleRate: Longword;
47 DataLength: Longword;
48 CRC32: Longword;
49 end;
50
51 { Class TTTA }
52 TTTA = class(TObject)
53 private
54 { Private declarations }
55 FFileSize: Int64;
56 FValid: Boolean;
57
58 FAudioFormat: Cardinal;
59 FChannels: Cardinal;
60 FBits: Cardinal;
61 FSampleRate: Cardinal;
62 FSamples: Cardinal;
63 FCRC32: Cardinal;
64
65 FBitrate: Double;
66 FDuration: Double;
67
68 FID3v1: TID3v1;
69 FID3v2: TID3v2;
70 FAPEtag: TAPEtag;
71
72 function FGetRatio: Double;
73 procedure FResetData;
74
75 public
76 { Public declarations }
77 constructor Create; { Create object }
78 destructor Destroy; override; { Destroy object }
79
80 function ReadFromFile(const FileName: String): Boolean; { Load header }
81
82 property FileSize: Int64 read FFileSize;
83 property Valid: Boolean read FValid;
84
85 property AudioFormat: Cardinal read FAudioFormat;
86 property Channels: Cardinal read FChannels;
87 property Bits: Cardinal read FBits;
88 property SampleRate: Cardinal read FSampleRate;
89 property Samples: Cardinal read FSamples; { Number of samples }
90 property CRC32: Cardinal read FCRC32;
91
92 property Bitrate: Double read FBitrate;
93 property Duration: Double read FDuration;
94 property Ratio: Double read FGetRatio; { Compression ratio (%) }
95
96 property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data }
97 property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data }
98 property APEtag: TAPEtag read FAPEtag; { APE tag data }
99 end;
100
101 implementation
102
103
104 { ********************** Private functions & procedures ********************* }
105
106 procedure TTTA.FResetData;
107 begin
108 { Reset all data }
109 FFileSize := 0;
110 FValid := False;
111
112 FAudioFormat := 0;
113 FChannels := 0;
114 FBits := 0;
115 FSampleRate := 0;
116 FSamples := 0;
117 FCRC32 := 0;
118
119 FBitrate := 0;
120 FDuration := 0;
121
122 FID3v1.ResetData;
123 FID3v2.ResetData;
124 FAPEtag.ResetData;
125 end;
126
127
128 { ********************** Public functions & procedures ********************** }
129
130 constructor TTTA.Create;
131 begin
132 { Create object }
133 inherited;
134 FID3v1 := TID3v1.Create;
135 FID3v2 := TID3v2.Create;
136 FAPEtag := TAPEtag.Create;
137 FResetData;
138 end;
139
140 (* -------------------------------------------------------------------------- *)
141
142 destructor TTTA.Destroy;
143 begin
144 FID3v1.Free;
145 FID3v2.Free;
146 FAPEtag.Free;
147 inherited;
148 end;
149
150 (* -------------------------------------------------------------------------- *)
151
152 function TTTA.ReadFromFile(const FileName: String): Boolean;
153 var
154 f: TFileStreamEx;
155 SignatureChunk: array[0..3] of Char;
156 ttaheader: tta_header;
157 TagSize: Int64;
158 begin
159 Result := False;
160 FResetData;
161 // load tags first
162 FID3v2.ReadFromFile(FileName);
163 FID3v1.ReadFromFile(FileName);
164 FAPEtag.ReadFromFile(FileName);
165 // calulate total tag size
166 TagSize := 0;
167 if FID3v1.Exists then inc(TagSize,128);
168 if FID3v2.Exists then inc(TagSize, FID3v2.Size);
169 if FAPEtag.Exists then inc(TagSize, FAPETag.Size);
170 // begin reading data from file
171 f:=nil;
172
173 try
174 f := TFileStreamEx.create(FileName, fmOpenRead or fmShareDenyWrite);
175 // seek past id3v2-tag
176 if FID3v2.Exists then
177 begin
178 f.Seek(FID3v2.Size, soFromBeginning);
179 end;
180
181 if (f.Read(SignatureChunk, SizeOf(SignatureChunk)) = SizeOf(SignatureChunk)) and (StrLComp(SignatureChunk,'TTA1',4) = 0) then
182 begin
183 // start looking for chunks
184 FillChar(ttaheader, SizeOf(ttaheader),0);
185 f.Read(ttaheader, SizeOf(ttaheader));
186
187 FFileSize := f.Size;
188 FValid := TRUE;
189
190 FAudioFormat := ttaheader.AudioFormat;
191 FChannels := ttaheader.NumChannels;
192 FBits := ttaheader.BitsPerSample;
193 FSampleRate := ttaheader.SampleRate;
194 FSamples := ttaheader.DataLength;
195 FCRC32 := ttaheader.CRC32;
196
197 FBitrate := FFileSize * 8 / (FSamples / FSampleRate) / 1000;
198 FDuration := ttaheader.DataLength / ttaheader.SampleRate;
199
200 Result := True;
201 end;
202
203 finally
204 f.free;
205 end;
206 end;
207
208 (* -------------------------------------------------------------------------- *)
209
210 function TTTA.FGetRatio: Double;
211 begin
212 { Get compression ratio }
213 if FValid then
214 Result := FFileSize / (FSamples * (FChannels * FBits / 8) + 44) * 100
215 else
216 Result := 0;
217 end;
218
219 (* -------------------------------------------------------------------------- *)
220
221 end.