Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TWMAfile - for extracting information from WMA 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.0 (29 April 2002) }
13 { - Support for Windows Media Audio (versions 7, 8) }
14 { - File info: file size, channel mode, sample rate, duration, bit rate }
15 { - WMA tag info: title, artist, album, track, year, genre, comment }
16 { }
17 { This library is free software; you can redistribute it and/or }
18 { modify it under the terms of the GNU Lesser General Public }
19 { License as published by the Free Software Foundation; either }
20 { version 2.1 of the License, or (at your option) any later version. }
21 { }
22 { This library is distributed in the hope that it will be useful, }
23 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
24 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
25 { Lesser General Public License for more details. }
26 { }
27 { You should have received a copy of the GNU Lesser General Public }
28 { License along with this library; if not, write to the Free Software }
29 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
30 { }
31 { *************************************************************************** }
32
33 unit WMAfile;
34
35 interface
36
37 uses
38 Classes, SysUtils, LazUTF8, DCClassesUtf8;
39
40 const
41 { Channel modes }
42 WMA_CM_UNKNOWN = 0; { Unknown }
43 WMA_CM_MONO = 1; { Mono }
44 WMA_CM_STEREO = 2; { Stereo }
45
46 { Channel mode names }
47 WMA_MODE: array [0..2] of string = ('Unknown', 'Mono', 'Stereo');
48
49 type
50 { Class TWMAfile }
51 TWMAfile = class(TObject)
52 private
53 { Private declarations }
54 FValid: Boolean;
55 FFileSize: Integer;
56 FChannelModeID: Byte;
57 FSampleRate: Integer;
58 FDuration: Double;
59 FBitRate: Integer;
60 FTitle: String;
61 FArtist: String;
62 FAlbum: String;
63 FTrack: Integer;
64 FYear: String;
65 FGenre: String;
66 FComment: String;
67 procedure FResetData;
68 function FGetChannelMode: string;
69 public
70 { Public declarations }
71 constructor Create; { Create object }
72 function ReadFromFile(const FileName: String): Boolean; { Load data }
73 property Valid: Boolean read FValid; { True if valid data }
74 property FileSize: Integer read FFileSize; { File size (bytes) }
75 property ChannelModeID: Byte read FChannelModeID; { Channel mode code }
76 property ChannelMode: string read FGetChannelMode; { Channel mode name }
77 property SampleRate: Integer read FSampleRate; { Sample rate (hz) }
78 property Duration: Double read FDuration; { Duration (seconds) }
79 property BitRate: Integer read FBitRate; { Bit rate (kbit) }
80 property Title: String read FTitle; { Song title }
81 property Artist: String read FArtist; { Artist name }
82 property Album: String read FAlbum; { Album name }
83 property Track: Integer read FTrack; { Track number }
84 property Year: String read FYear; { Year }
85 property Genre: String read FGenre; { Genre name }
86 property Comment: String read FComment; { Comment }
87 end;
88
89 implementation
90
91 const
92 { Object IDs }
93 WMA_HEADER_ID =
94 #48#38#178#117#142#102#207#17#166#217#0#170#0#98#206#108;
95 WMA_FILE_PROPERTIES_ID =
96 #161#220#171#140#71#169#207#17#142#228#0#192#12#32#83#101;
97 WMA_STREAM_PROPERTIES_ID =
98 #145#7#220#183#183#169#207#17#142#230#0#192#12#32#83#101;
99 WMA_CONTENT_DESCRIPTION_ID =
100 #51#38#178#117#142#102#207#17#166#217#0#170#0#98#206#108;
101 WMA_EXTENDED_CONTENT_DESCRIPTION_ID =
102 #64#164#208#210#7#227#210#17#151#240#0#160#201#94#168#80;
103
104 { Max. number of supported comment fields }
105 WMA_FIELD_COUNT = 7;
106
107 { Names of supported comment fields }
108 WMA_FIELD_NAME: array [1..WMA_FIELD_COUNT] of UnicodeString =
109 ('WM/TITLE', 'WM/AUTHOR', 'WM/ALBUMTITLE', 'WM/TRACK', 'WM/YEAR',
110 'WM/GENRE', 'WM/DESCRIPTION');
111
112 { Max. number of characters in tag field }
113 WMA_MAX_STRING_SIZE = 250;
114
115 type
116 { Object ID }
117 ObjectID = array [1..16] of Char;
118
119 { Tag data }
120 TagData = array [1..WMA_FIELD_COUNT] of UnicodeString;
121
122 { File data - for internal use }
123 FileData = record
124 FileSize: Integer; { File size (bytes) }
125 MaxBitRate: Integer; { Max. bit rate (bps) }
126 Channels: Word; { Number of channels }
127 SampleRate: Integer; { Sample rate (hz) }
128 ByteRate: Integer; { Byte rate }
129 Tag: TagData; { WMA tag information }
130 end;
131
132 { ********************* Auxiliary functions & procedures ******************** }
133
134 function ReadFieldString(const Source: TStream; DataSize: Word): UnicodeString;
135 var
136 Iterator, StringSize: Integer;
137 FieldData: array [1..WMA_MAX_STRING_SIZE * 2] of Byte;
138 begin
139 { Read field data and convert to Unicode string }
140 Result := '';
141 StringSize := DataSize div 2;
142 if StringSize > WMA_MAX_STRING_SIZE then StringSize := WMA_MAX_STRING_SIZE;
143 Source.ReadBuffer(FieldData, StringSize * 2);
144 Source.Seek(DataSize - StringSize * 2, soFromCurrent);
145 for Iterator := 1 to StringSize do
146 Result := Result +
147 WideChar(FieldData[Iterator * 2 - 1] + (FieldData[Iterator * 2] shl 8));
148 end;
149
150 { --------------------------------------------------------------------------- }
151
152 procedure ReadTagStandard(const Source: TStream; var Tag: TagData);
153 var
154 Iterator: Integer;
155 FieldSize: array [1..5] of Word;
156 FieldValue: UnicodeString;
157 begin
158 { Read standard tag data }
159 Source.ReadBuffer(FieldSize, SizeOf(FieldSize));
160 for Iterator := 1 to 5 do
161 if FieldSize[Iterator] > 0 then
162 begin
163 { Read field value }
164 FieldValue := ReadFieldString(Source, FieldSize[Iterator]);
165 { Set corresponding tag field if supported }
166 case Iterator of
167 1: Tag[1] := FieldValue;
168 2: Tag[2] := FieldValue;
169 4: Tag[7] := FieldValue;
170 end;
171 end;
172 end;
173
174 { --------------------------------------------------------------------------- }
175
176 procedure ReadTagExtended(const Source: TStream; var Tag: TagData);
177 var
178 Iterator1, Iterator2, FieldCount, DataSize, DataType: Word;
179 FieldName, FieldValue: UnicodeString;
180 begin
181 { Read extended tag data }
182 Source.ReadBuffer(FieldCount, SizeOf(FieldCount));
183 for Iterator1 := 1 to FieldCount do
184 begin
185 { Read field name }
186 Source.ReadBuffer(DataSize, SizeOf(DataSize));
187 FieldName := ReadFieldString(Source, DataSize);
188 { Read value data type }
189 Source.ReadBuffer(DataType, SizeOf(DataType));
190 { Read field value only if string }
191 if DataType = 0 then
192 begin
193 Source.ReadBuffer(DataSize, SizeOf(DataSize));
194 FieldValue := ReadFieldString(Source, DataSize);
195 end
196 else
197 Source.Seek(DataSize, soFromCurrent);
198 { Set corresponding tag field if supported }
199 for Iterator2 := 1 to WMA_FIELD_COUNT do
200 if UpperCase(Trim(FieldName)) = WMA_FIELD_NAME[Iterator2] then
201 Tag[Iterator2] := FieldValue;
202 end;
203 end;
204
205 { --------------------------------------------------------------------------- }
206
207 procedure ReadObject(const ID: ObjectID; Source: TStream; var Data: FileData);
208 begin
209 { Read data from header object if supported }
210 if ID = WMA_FILE_PROPERTIES_ID then
211 begin
212 { Read file properties }
213 Source.Seek(80, soFromCurrent);
214 Source.ReadBuffer(Data.MaxBitRate, SizeOf(Data.MaxBitRate));
215 end;
216 if ID = WMA_STREAM_PROPERTIES_ID then
217 begin
218 { Read stream properties }
219 Source.Seek(60, soFromCurrent);
220 Source.ReadBuffer(Data.Channels, SizeOf(Data.Channels));
221 Source.ReadBuffer(Data.SampleRate, SizeOf(Data.SampleRate));
222 Source.ReadBuffer(Data.ByteRate, SizeOf(Data.ByteRate));
223 end;
224 if ID = WMA_CONTENT_DESCRIPTION_ID then
225 begin
226 { Read standard tag data }
227 Source.Seek(4, soFromCurrent);
228 ReadTagStandard(Source, Data.Tag);
229 end;
230 if ID = WMA_EXTENDED_CONTENT_DESCRIPTION_ID then
231 begin
232 { Read extended tag data }
233 Source.Seek(4, soFromCurrent);
234 ReadTagExtended(Source, Data.Tag);
235 end;
236 end;
237
238 { --------------------------------------------------------------------------- }
239
240 function ReadData(const FileName: String; var Data: FileData): Boolean;
241 var
242 Source: TFileStreamEx;
243 ID: ObjectID;
244 Iterator, ObjectCount, ObjectSize, Position: Integer;
245 begin
246 { Read file data }
247 try
248 Source := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
249 Data.FileSize := Source.Size;
250 { Check for existing header }
251 Source.ReadBuffer(ID, SizeOf(ID));
252 if ID = WMA_HEADER_ID then
253 begin
254 Source.Seek(8, soFromCurrent);
255 Source.ReadBuffer(ObjectCount, SizeOf(ObjectCount));
256 Source.Seek(2, soFromCurrent);
257 { Read all objects in header and get needed data }
258 for Iterator := 1 to ObjectCount do
259 begin
260 Position := Source.Position;
261 Source.ReadBuffer(ID, SizeOf(ID));
262 Source.ReadBuffer(ObjectSize, SizeOf(ObjectSize));
263 ReadObject(ID, Source, Data);
264 Source.Seek(Position + ObjectSize, soFromBeginning);
265 end;
266 end;
267 Source.Free;
268 Result := true;
269 except
270 Result := false;
271 end;
272 end;
273
274 { --------------------------------------------------------------------------- }
275
276 function IsValid(const Data: FileData): Boolean;
277 begin
278 { Check for data validity }
279 Result :=
280 (Data.MaxBitRate > 0) and (Data.MaxBitRate < 320000) and
281 ((Data.Channels = WMA_CM_MONO) or (Data.Channels = WMA_CM_STEREO)) and
282 (Data.SampleRate >= 8000) and (Data.SampleRate <= 96000) and
283 (Data.ByteRate > 0) and (Data.ByteRate < 40000);
284 end;
285
286 { --------------------------------------------------------------------------- }
287
288 function ExtractTrack(const TrackString: UnicodeString): Integer;
289 var
290 Value, Code: Integer;
291 begin
292 { Extract track from string }
293 Result := 0;
294 Val(TrackString, Value, Code);
295 if Code = 0 then Result := Value;
296 end;
297
298 { ********************** Private functions & procedures ********************* }
299
300 procedure TWMAfile.FResetData;
301 begin
302 { Reset variables }
303 FValid := false;
304 FFileSize := 0;
305 FChannelModeID := WMA_CM_UNKNOWN;
306 FSampleRate := 0;
307 FDuration := 0;
308 FBitRate := 0;
309 FTitle := '';
310 FArtist := '';
311 FAlbum := '';
312 FTrack := 0;
313 FYear := '';
314 FGenre := '';
315 FComment := '';
316 end;
317
318 { --------------------------------------------------------------------------- }
319
320 function TWMAfile.FGetChannelMode: string;
321 begin
322 { Get channel mode name }
323 Result := WMA_MODE[FChannelModeID];
324 end;
325
326 { ********************** Public functions & procedures ********************** }
327
328 constructor TWMAfile.Create;
329 begin
330 { Create object }
331 inherited;
332 FResetData;
333 end;
334
335 { --------------------------------------------------------------------------- }
336
337 function TWMAfile.ReadFromFile(const FileName: String): Boolean;
338 var
339 Data: FileData;
340 begin
341 { Reset variables and load file data }
342 FResetData;
343 FillChar(Data, SizeOf(Data), 0);
344 Result := ReadData(FileName, Data);
345 { Process data if loaded and valid }
346 if Result and IsValid(Data) then
347 begin
348 FValid := true;
349 { Fill properties with loaded data }
350 FFileSize := Data.FileSize;
351 FChannelModeID := Data.Channels;
352 FSampleRate := Data.SampleRate;
353 FDuration := Data.FileSize * 8 / Data.MaxBitRate;
354 FBitRate := Data.ByteRate * 8 div 1000;
355 FTitle := UTF16ToUTF8(Trim(Data.Tag[1]));
356 FArtist := UTF16ToUTF8(Trim(Data.Tag[2]));
357 FAlbum := UTF16ToUTF8(Trim(Data.Tag[3]));
358 FTrack := ExtractTrack(Trim(Data.Tag[4]));
359 FYear := UTF16ToUTF8(Trim(Data.Tag[5]));
360 FGenre := UTF16ToUTF8(Trim(Data.Tag[6]));
361 FComment := UTF16ToUTF8(Trim(Data.Tag[7]));
362 end;
363 end;
364
365 end.