Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TOptimFROG - for manipulating with OptimFROG file information }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2003-2005 by Erik Stenborg }
10 { }
11 { Version 1.1 (April 2005) by Gambit }
12 { - updated to unicode file access }
13 { }
14 { Version 1.0 (10 July 2003) }
15 { - Support for OptimFROG files via modification of TMonkey class by Jurgen }
16 { - Class TID3v1: reading & writing support for ID3v1 tags }
17 { - Class TID3v2: reading & writing support for ID3v2 tags }
18 { - Class TAPEtag: reading & writing support for APE tags }
19 { }
20 { This library is free software; you can redistribute it and/or }
21 { modify it under the terms of the GNU Lesser General Public }
22 { License as published by the Free Software Foundation; either }
23 { version 2.1 of the License, or (at your option) any later version. }
24 { }
25 { This library is distributed in the hope that it will be useful, }
26 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
27 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
28 { Lesser General Public License for more details. }
29 { }
30 { You should have received a copy of the GNU Lesser General Public }
31 { License along with this library; if not, write to the Free Software }
32 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
33 { }
34 { *************************************************************************** }
35
36 unit OptimFROG;
37
38 interface
39
40 uses
41 Classes, SysUtils, ID3v1, ID3v2, APEtag, DCClassesUtf8;
42
43 const
44 OFR_COMPRESSION: array [0..9] of String = ('fast', 'normal', 'high', 'extra',
45 'best', 'ultra', 'insane', 'highnew', 'extranew', 'bestnew');
46
47 OFR_BITS: array [0..10] of ShortInt = (8, 8, 16, 16, 24, 24, 32, 32,
48 -32, -32, -32); //negative value corresponds to floating point type.
49
50 OFR_CHANNELMODE: array [0..1] of String = ('Mono', 'Stereo');
51
52 type
53 { Real structure of OptimFROG header }
54 TOfrHeader = packed record
55 ID: array [1..4] of Char; { Always 'OFR ' }
56 Size: Cardinal;
57 Length: Cardinal;
58 HiLength: Word;
59 SampleType, ChannelMode: Byte;
60 SampleRate: Integer;
61 EncoderID: Word;
62 CompressionID: Byte;
63 end;
64
65 { Class TOptimFrog }
66 TOptimFrog = class(TObject)
67 private
68 { Private declarations }
69 FFileLength: Int64;
70 FHeader: TOfrHeader;
71 FID3v1: TID3v1;
72 FID3v2: TID3v2;
73 FAPEtag: TAPEtag;
74 procedure FResetData;
75 function FGetValid: Boolean;
76 function FGetVersion: string;
77 function FGetCompression: string;
78 function FGetBits: ShortInt;
79 function FGetChannelMode: string;
80 function FGetSamples: Int64;
81 function FGetDuration: Double;
82 function FGetRatio: Double;
83 function FGetSampleRate: Integer;
84 function FGetChannels: Byte;
85 function FGetBitrate: Integer;
86 public
87 { Public declarations }
88 constructor Create; { Create object }
89 destructor Destroy; override; { Destroy object }
90 function ReadFromFile(const FileName: String): Boolean; {Load header }
91 property FileLength: Int64 read FFileLength; { File length (bytes) }
92 property Header: TOfrHeader read FHeader; { OptimFROG header }
93 property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data }
94 property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data }
95 property APEtag: TAPEtag read FAPEtag; { APE tag data }
96 property Valid: Boolean read FGetValid; { True if header valid }
97 property Version: string read FGetVersion; { Encoder version }
98 property Compression: string read FGetCompression; { Compression level }
99 property Bits: ShortInt read FGetBits; { Bits per sample }
100 property ChannelMode: string read FGetChannelMode; { Channel mode }
101 property Samples: Int64 read FGetSamples; { Number of samples }
102 property Duration: Double read FGetDuration; { Duration (seconds) }
103 property SampleRate: Integer read FGetSampleRate; { Sample rate (Hz) }
104 property Ratio: Double read FGetRatio; { Compression ratio (%) }
105 property Channels: Byte read FGetChannels;
106 property Bitrate: Integer read FGetBitrate;
107 end;
108
109 implementation
110
111 { ********************** Private functions & procedures ********************* }
112
113 procedure TOptimFrog.FResetData;
114 begin
115 { Reset data }
116 FFileLength := 0;
117 FillChar(FHeader, SizeOf(FHeader), 0);
118 FID3v1.ResetData;
119 FID3v2.ResetData;
120 FAPEtag.ResetData;
121 end;
122
123 { --------------------------------------------------------------------------- }
124
125 function TOptimFrog.FGetValid: Boolean;
126 begin
127 Result :=
128 (FHeader.ID = 'OFR ') and
129 (FHeader.SampleRate > 0) and
130 (FHeader.SampleType in [0..10]) and
131 (FHeader.ChannelMode in [0..1]) and
132 (FHeader.CompressionID shr 3 in [0..9]);
133 end;
134
135 { --------------------------------------------------------------------------- }
136
137 function TOptimFrog.FGetVersion: string;
138 begin
139 { Get encoder version }
140 Result := Format('%5.3f', [((FHeader.EncoderID shr 4) + 4500) / 1000]);
141 end;
142
143 { --------------------------------------------------------------------------- }
144
145 function TOptimFrog.FGetCompression: string;
146 begin
147 { Get compression level }
148 Result := OFR_COMPRESSION[FHeader.CompressionID shr 3]
149 end;
150
151 { --------------------------------------------------------------------------- }
152
153 function TOptimFrog.FGetBits: ShortInt;
154 begin
155 { Get number of bits per sample }
156 Result := OFR_BITS[FHeader.SampleType]
157 end;
158
159 { --------------------------------------------------------------------------- }
160
161 function TOptimFrog.FGetChannelMode: string;
162 begin
163 { Get channel mode }
164 Result := OFR_CHANNELMODE[FHeader.ChannelMode]
165 end;
166
167 { --------------------------------------------------------------------------- }
168
169 function TOptimFrog.FGetSamples: Int64;
170 var
171 Res: array [0..1] of Cardinal absolute Result;
172 begin
173 { Get number of samples }
174 Res[0] := Header.Length shr Header.ChannelMode;
175 Res[1] := Header.HiLength shr Header.ChannelMode;
176 end;
177
178 { --------------------------------------------------------------------------- }
179
180 function TOptimFrog.FGetDuration: Double;
181 begin
182 { Get song duration }
183 if FHeader.SampleRate > 0 then
184 Result := FGetSamples / FHeader.SampleRate
185 else
186 Result := 0;
187 end;
188
189 { --------------------------------------------------------------------------- }
190
191 function TOptimFrog.FGetSampleRate: Integer;
192 begin
193 Result := Header.SampleRate;
194 end;
195
196 { --------------------------------------------------------------------------- }
197
198 function TOptimFrog.FGetRatio: Double;
199 begin
200 { Get compression ratio }
201 if FGetValid then
202 Result := FFileLength /
203 (FGetSamples * ((FHeader.ChannelMode+1) * Abs(FGetBits) / 8) + 44) * 100
204 else
205 Result := 0;
206 end;
207
208 { ********************** Public functions & procedures ********************** }
209
210 constructor TOptimFrog.Create;
211 begin
212 { Create object }
213 inherited;
214 FID3v1 := TID3v1.Create;
215 FID3v2 := TID3v2.Create;
216 FAPEtag := TAPEtag.Create;
217 FResetData;
218 end;
219
220 { --------------------------------------------------------------------------- }
221
222 destructor TOptimFrog.Destroy;
223 begin
224 { Destroy object }
225 FID3v1.Free;
226 FID3v2.Free;
227 FAPEtag.Free;
228 inherited;
229 end;
230
231 { --------------------------------------------------------------------------- }
232
233 function TOptimFrog.ReadFromFile(const FileName: String): Boolean;
234 var
235 SourceFile: TFileStreamEx;
236 begin
237 Result := False;
238 SourceFile := nil;
239 try
240 { Reset data and search for file tag }
241 FResetData;
242 FID3v1.ReadFromFile(FileName);
243 FID3v2.ReadFromFile(FileName);
244 FAPEtag.ReadFromFile(FileName);
245 { Set read-access, open file and get file length }
246 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
247 FFileLength := SourceFile.Size;
248 { Read header data }
249 SourceFile.Seek(ID3v2.Size, soFromBeginning);
250 SourceFile.Read(FHeader, SizeOf(FHeader));
251 if FHeader.ID = 'OFR ' then
252 Result := True;
253 finally
254 SourceFile.Free;
255 end;
256 end;
257
258 { --------------------------------------------------------------------------- }
259
260 function TOptimFrog.FGetChannels: Byte;
261 begin
262 Result := Header.ChannelMode + 1;
263 end;
264
265 { --------------------------------------------------------------------------- }
266
267 function TOptimFrog.FGetBitrate: Integer;
268 begin
269 Result := Round(FFileLength * 8.0 / (FGetSamples / FHeader.SampleRate * 1000));
270 end;
271
272 { --------------------------------------------------------------------------- }
273
274 end.