Debian Codegraph @

plugins/wdx/audioinfo/src/audiodata.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 This content plugin can show information about audio files
5
6 Copyright (C) 2016-2023 Alexander Koblov ([email protected])
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 }
22
23 unit AudioData;
24
25 {$mode objfpc}{$H+}
26
27 interface
28
29 uses
30 Classes, SysUtils, DCStrUtils, MPEGaudio, Musepack, OggVorbis, ID3v1, ID3v2,
31 APEtag, FLACfile, Monkey, AACfile, CDAtrack, WMAfile, WAVfile, TTA, TwinVQ,
32 AC3, DTS, WAVPackfile, OptimFROG, MP4file;
33
34 type
35
36 { TAudioData }
37
38 TAudioData = class
39 private
40 FDTS: TDTS;
41 FAC3: TAC3;
42 FTTA: TTTA;
43 FTwinVQ: TTwinVQ;
44 FMonkey: TMonkey;
45 FMP4file: TMP4file;
46 FAACfile: TAACfile;
47 FWMAfile: TWMAfile;
48 FWAVfile: TWAVfile;
49 FFLACfile: TFLACfile;
50 FMPEGplus: TMPEGplus;
51 FCDAtrack: TCDAtrack;
52 FMPEGaudio: TMPEGaudio;
53 FOggVorbis: TOggVorbis;
54 FOptimFrog: TOptimFrog;
55 FWAVPackfile: TWAVPackfile;
56 private
57 procedure Clear;
58 procedure ReadID3v1(ID3v1: TID3v1);
59 procedure ReadID3v2(ID3v2: TID3v2);
60 procedure ReadAPEtag(APEtag: TAPEtag);
61 procedure AppendTag(const ATag: String);
62 function FormatChannels(AChannels: Integer): String;
63 function FormatDuration(ADuration: Integer): String;
64 procedure UpdateValue(var AValue: Integer; AData: Integer);
65 procedure UpdateValue(var AValue: String; const AData: String);
66 protected
67 FFileName: String;
68 function ReadDTS: Boolean;
69 function ReadAC3: Boolean;
70 function ReadTTA: Boolean;
71 function ReadTwinVQ: Boolean;
72 function ReadMonkey: Boolean;
73 function ReadMP4file: Boolean;
74 function ReadAACfile: Boolean;
75 function ReadWMAfile: Boolean;
76 function ReadWAVfile: Boolean;
77 function ReadFLACfile: Boolean;
78 function ReadMPEGplus: Boolean;
79 function ReadCDAtrack: Boolean;
80 function ReadMPEGaudio: Boolean;
81 function ReadOggVorbis: Boolean;
82 function ReadOptimFrog: Boolean;
83 function ReadWAVPackfile: Boolean;
84 public
85 Album, Artist, Title: String;
86 Bits,
87 Track,
88 Duration,
89 SampleRate,
90 BitRate: Integer;
91 DurationHMS,
92 BitRateType,
93 Channels,
94 Date, Genre, Comment, Tags,
95 Encoder, Composer, Copyright, URL: String;
96 FullText: UnicodeString;
97 public
98 constructor Create;
99 destructor Destroy; override;
100 function LoadFromFile(const FileName: String): Boolean;
101 end;
102
103 implementation
104
105 uses
106 LazUTF8;
107
108 { TAudioData }
109
110 procedure TAudioData.Clear;
111 begin
112 Bits:= 0;
113 Track:= 0;
114 BitRate:= 0;
115 Duration:= 0;
116 SampleRate:= 0;
117 URL:= EmptyStr;
118 Tags:= EmptyStr;
119 Date:= EmptyStr;
120 Genre:= EmptyStr;
121 Title:= EmptyStr;
122 Album:= EmptyStr;
123 Artist:= EmptyStr;
124 Comment:= EmptyStr;
125 Encoder:= EmptyStr;
126 Channels:= EmptyStr;
127 Composer:= EmptyStr;
128 Copyright:= EmptyStr;
129 DurationHMS:= EmptyStr;
130 FullText:= EmptyWideStr;
131 BitRateType:= 'Unknown';
132 end;
133
134 procedure TAudioData.ReadID3v1(ID3v1: TID3v1);
135 begin
136 if ID3v1.Exists then
137 begin
138 UpdateValue(Date, ID3v1.Year);
139 UpdateValue(Track, ID3v1.Track);
140 UpdateValue(Album, ID3v1.Album);
141 UpdateValue(Title, ID3v1.Title);
142 UpdateValue(Genre, ID3v1.Genre);
143 UpdateValue(Artist, ID3v1.Artist);
144 UpdateValue(Comment, ID3v1.Comment);
145 case ID3v1.VersionID of
146 TAG_VERSION_1_0: AppendTag('ID3v1.0');
147 TAG_VERSION_1_1: AppendTag('ID3v1.1');
148 else AppendTag('ID3v1');
149 end;
150 end;
151 end;
152
153 procedure TAudioData.ReadID3v2(ID3v2: TID3v2);
154 begin
155 if ID3v2.Exists then
156 begin
157 UpdateValue(URL, ID3v2.Link);
158 UpdateValue(Date, ID3v2.Year);
159 UpdateValue(Track, ID3v2.Track);
160 UpdateValue(Album, ID3v2.Album);
161 UpdateValue(Title, ID3v2.Title);
162 UpdateValue(Genre, ID3v2.Genre);
163 UpdateValue(Artist, ID3v2.Artist);
164 UpdateValue(Comment, ID3v2.Comment);
165 UpdateValue(Encoder, ID3v2.Encoder);
166 UpdateValue(Composer, ID3v2.Composer);
167 UpdateValue(Copyright, ID3v2.Copyright);
168 case ID3v2.VersionID of
169 TAG_VERSION_2_2: AppendTag('ID3v2.2');
170 TAG_VERSION_2_3: AppendTag('ID3v2.3');
171 TAG_VERSION_2_4: AppendTag('ID3v2.4');
172 else AppendTag('ID3v2');
173 end;
174 end;
175 end;
176
177 procedure TAudioData.ReadAPEtag(APEtag: TAPEtag);
178 begin
179 if APEtag.Exists then
180 begin
181 UpdateValue(Date, APEtag.Year);
182 UpdateValue(Track, APEtag.Track);
183 UpdateValue(Album, APEtag.Album);
184 UpdateValue(Title, APEtag.Title);
185 UpdateValue(Genre, APEtag.Genre);
186 UpdateValue(Artist, APEtag.Artist);
187 UpdateValue(Comment, APEtag.Comment);
188 UpdateValue(Composer, APEtag.Composer);
189 UpdateValue(Copyright, APEtag.Copyright);
190 AppendTag('APEv' + IntToStr(APEtag.Version div 1000));
191 end;
192 end;
193
194 procedure TAudioData.AppendTag(const ATag: String);
195 begin
196 if Length(Tags) = 0 then
197 Tags:= ATag
198 else begin
199 Tags:= Tags + ' ' + ATag;
200 end;
201 end;
202
203 function TAudioData.FormatChannels(AChannels: Integer): String;
204 begin
205 case AChannels of
206 0: Result:= 'Unknown';
207 1: Result:= 'Mono';
208 2: Result:= 'Stereo'
209 else Result:= IntToStr(AChannels) + ' ch';
210 end;
211 end;
212
213 function TAudioData.FormatDuration(ADuration: Integer): String;
214 var
215 AHour, AMinute, ASecond: Integer;
216 begin
217 AHour:= ADuration div 3600;
218 AMinute:= ADuration mod 3600 div 60;
219 ASecond:= ADuration mod 60;
220 Result:= Format('%.2d:%.2d:%.2d', [AHour, AMinute, ASecond]);
221 end;
222
223 procedure TAudioData.UpdateValue(var AValue: Integer; AData: Integer);
224 begin
225 if AValue = 0 then AValue:= AData;
226 end;
227
228 procedure TAudioData.UpdateValue(var AValue: String; const AData: String);
229 begin
230 if Length(AValue) = 0 then AValue:= AData;
231 end;
232
233 function TAudioData.ReadDTS: Boolean;
234 begin
235 Result:= FDTS.ReadFromFile(FFileName) and FDTS.Valid;
236 if Result then
237 begin
238 BitRate:= FDTS.BitRate;
239 Bits:= Integer(FDTS.Bits);
240 Duration:= Round(FDTS.Duration);
241 DurationHMS:= FormatDuration(Duration);
242 Channels:= FormatChannels(FDTS.Channels);
243 SampleRate:= FDTS.SampleRate;
244 end;
245 end;
246
247 function TAudioData.ReadAC3: Boolean;
248 begin
249 Result:= FAC3.ReadFromFile(FFileName) and FAC3.Valid;
250 if Result then
251 begin
252 BitRate:= FAC3.BitRate;
253 Duration:= Round(FAC3.Duration);
254 DurationHMS:= FormatDuration(Duration);
255 Channels:= FormatChannels(FAC3.Channels);
256 SampleRate:= FAC3.SampleRate;
257 end;
258 end;
259
260 function TAudioData.ReadTTA: Boolean;
261 begin
262 Result:= FTTA.ReadFromFile(FFileName) and FTTA.Valid;
263 if Result then
264 begin
265 Bits:= Integer(FTTA.Bits);
266 BitRate:= Round(FTTA.BitRate);
267 Duration:= Round(FTTA.Duration);
268 DurationHMS:= FormatDuration(Duration);
269 Channels:= FormatChannels(FTTA.Channels);
270 SampleRate:= FTTA.SampleRate;
271 ReadAPEtag(FTTA.APEtag);
272 ReadID3v2(FTTA.ID3v2);
273 ReadID3v1(FTTA.ID3v1);
274 end;
275 end;
276
277 function TAudioData.ReadTwinVQ: Boolean;
278 begin
279 Result:= FTwinVQ.ReadFromFile(FFileName) and FTwinVQ.Valid;
280 if Result then
281 begin
282 BitRate:= FTwinVQ.BitRate;
283 Channels:= FTwinVQ.ChannelMode;
284 Duration:= Round(FTwinVQ.Duration);
285 DurationHMS:= FormatDuration(Duration);
286 SampleRate:= FTwinVQ.SampleRate;
287
288 Album:= FTwinVQ.Album;
289 Title:= FTwinVQ.Title;
290 Artist:= FTwinVQ.Author;
291 Comment:= FTwinVQ.Comment;
292 end;
293 end;
294
295 function TAudioData.ReadMonkey: Boolean;
296 begin
297 Result:= FMonkey.ReadFromFile(FFileName) and FMonkey.Valid;
298 if Result then
299 begin
300 Bits:= FMonkey.Bits;
301 Channels:= FMonkey.ChannelMode;
302 BitRate:= Round(FMonkey.BitRate) div 1000000;
303 Duration:= Round(FMonkey.Duration);
304 DurationHMS:= FormatDuration(Duration);
305 SampleRate:= FMonkey.SampleRate;
306 ReadAPEtag(FMonkey.APEtag);
307 ReadID3v2(FMonkey.ID3v2);
308 ReadID3v1(FMonkey.ID3v1);
309 end;
310 end;
311
312 function TAudioData.ReadMP4file: Boolean;
313 begin
314 Result:= FMP4file.ReadFromFile(FFileName) and FMP4file.Valid;
315 if Result then
316 begin
317 SampleRate:= FMP4file.SampleRate;
318 BitRate:= Round(FMP4file.BitRate);
319 Duration:= Round(FMP4file.Duration);
320 DurationHMS:= FormatDuration(Duration);
321 Channels:= FormatChannels(FMP4file.Channels);
322
323 Date:= FMP4file.Year;
324 Track:= FMP4file.Track;
325 Genre:= FMP4file.Genre;
326 Title:= FMP4file.Title;
327 Album:= FMP4file.Album;
328 Artist:= FMP4file.Artist;
329 Comment:= FMP4file.Comment;
330 Encoder:= FMP4file.Encoder;
331 Composer:= FMP4file.Composer;
332 Copyright:= FMP4file.Copyright;
333 end;
334 end;
335
336 function TAudioData.ReadAACfile: Boolean;
337 begin
338 Result:= FAACfile.ReadFromFile(FFileName) and FAACfile.Valid;
339 if Result then
340 begin
341 BitRate:= FAACfile.BitRate;
342 BitRateType:= FAACfile.BitRateType;
343 Duration:= Round(FAACfile.Duration);
344 DurationHMS:= FormatDuration(Duration);
345 Channels:= FormatChannels(FAACfile.Channels);
346 SampleRate:= FAACfile.SampleRate;
347 ReadID3v2(FAACfile.ID3v2);
348 ReadID3v1(FAACfile.ID3v1);
349 ReadAPEtag(FAACfile.APEtag);
350 end;
351 end;
352
353 function TAudioData.ReadWMAfile: Boolean;
354 begin
355 Result:= FWMAfile.ReadFromFile(FFileName) and FWMAfile.Valid;
356 if Result then
357 begin
358 BitRate:= FWMAfile.BitRate;
359 Channels:= FWMAfile.ChannelMode;
360 Duration:= Round(FWMAfile.Duration);
361 DurationHMS:= FormatDuration(Duration);
362 SampleRate:= FWMAfile.SampleRate;
363
364 Date:= FWMAfile.Year;
365 Track:= FWMAfile.Track;
366 Album:= FWMAfile.Album;
367 Title:= FWMAfile.Title;
368 Genre:= FWMAfile.Genre;
369 Artist:= FWMAfile.Artist;
370 Comment:= FWMAfile.Comment;
371 end;
372 end;
373
374 function TAudioData.ReadWAVfile: Boolean;
375 begin
376 Result:= FWAVfile.ReadFromFile(FFileName) and FWAVfile.Valid;
377 if Result then
378 begin
379 Bits:= FWAVfile.BitsPerSample;
380 BitRate:= Round(FWAVfile.BitRate);
381 Channels:= FWAVfile.ChannelMode;
382 Duration:= Round(FWAVfile.Duration);
383 DurationHMS:= FormatDuration(Duration);
384 SampleRate:= FWAVfile.SampleRate;
385 end;
386 end;
387
388 function TAudioData.ReadFLACfile: Boolean;
389 begin
390 Result:= FFLACfile.ReadFromFile(FFileName) and FFLACfile.Valid;
391 if Result then
392 begin
393 BitRate:= FFLACfile.BitRate;
394 Bits:= FFLACfile.BitsPerSample;
395 Channels:= FFLACfile.ChannelMode;
396 Duration:= Round(FFLACfile.Duration);
397 DurationHMS:= FormatDuration(Duration);
398 SampleRate:= FFLACfile.SampleRate;
399
400 URL:= FFLACfile.Link;
401 Date:= FFLACfile.Year;
402 Track:= FFLACfile.Track;
403 Album:= FFLACfile.Album;
404 Title:= FFLACfile.Title;
405 Genre:= FFLACfile.Genre;
406 Artist:= FFLACfile.Artist;
407 Comment:= FFLACfile.Comment;
408 Encoder:= FFLACfile.Encoder;
409 Composer:= FFLACfile.Composer;
410 Copyright:= FFLACfile.Copyright;
411 end;
412 end;
413
414 function TAudioData.ReadMPEGplus: Boolean;
415 begin
416 Result:= FMPEGplus.ReadFromFile(FFileName) and FMPEGplus.Valid;
417 if Result then
418 begin
419 BitRate:= FMPEGplus.BitRate;
420 Channels:= FMPEGplus.ChannelMode;
421 Duration:= Round(FMPEGplus.Duration);
422 DurationHMS:= FormatDuration(Duration);
423 SampleRate:= FMPEGplus.SampleRate;
424 ReadID3v2(FMPEGplus.ID3v2);
425 ReadID3v1(FMPEGplus.ID3v1);
426 ReadAPEtag(FMPEGplus.APEtag);
427 end;
428 end;
429
430 function TAudioData.ReadCDAtrack: Boolean;
431 begin
432 Result:= FCDAtrack.ReadFromFile(FFileName) and FCDAtrack.Valid;
433 if Result then
434 begin
435 Duration:= Round(FCDAtrack.Duration);
436 DurationHMS:= FormatDuration(Duration);
437 Track:= FCDAtrack.Track;
438 Album:= FCDAtrack.Album;
439 Title:= FCDAtrack.Title;
440 Artist:= FCDAtrack.Artist;
441 end;
442 end;
443
444 function TAudioData.ReadMPEGaudio: Boolean;
445 begin
446 Result:= FMPEGaudio.ReadFromFile(FFileName) and FMPEGaudio.Valid;
447 if Result then
448 begin
449 if FMPEGaudio.VBR.Found then
450 BitRateType:= 'VBR'
451 else begin
452 BitRateType:= 'CBR';
453 end;
454 BitRate:= FMPEGaudio.BitRate;
455 Channels:= FMPEGaudio.ChannelMode;
456 Duration:= Round(FMPEGaudio.Duration);
457 DurationHMS:= FormatDuration(Duration);
458 SampleRate:= FMPEGaudio.SampleRate;
459 ReadID3v2(FMPEGaudio.ID3v2);
460 ReadID3v1(FMPEGaudio.ID3v1);
461 end;
462 end;
463
464 function TAudioData.ReadOggVorbis: Boolean;
465 begin
466 Result:= FOggVorbis.ReadFromFile(FFileName) and FOggVorbis.Valid;
467 if Result then
468 begin
469 BitRate:= FOggVorbis.BitRate;
470 Channels:= FOggVorbis.ChannelMode;
471 Duration:= Round(FOggVorbis.Duration);
472 DurationHMS:= FormatDuration(Duration);
473 SampleRate:= FOggVorbis.SampleRate;
474
475 Date:= FOggVorbis.Date;
476 Track:= FOggVorbis.Track;
477 Album:= FOggVorbis.Album;
478 Title:= FOggVorbis.Title;
479 Genre:= FOggVorbis.Genre;
480 Artist:= FOggVorbis.Artist;
481 Comment:= FOggVorbis.Comment;
482 Encoder:= FOggVorbis.Encoder;
483 end;
484 end;
485
486 function TAudioData.ReadOptimFrog: Boolean;
487 begin
488 Result:= FOptimFrog.ReadFromFile(FFileName) and FOptimFrog.Valid;
489 if Result then
490 begin
491 Bits:= FOptimFrog.Bits;
492 BitRate:= FOptimFrog.BitRate;
493 Channels:= FOptimFrog.ChannelMode;
494 Duration:= Round(FOptimFrog.Duration);
495 DurationHMS:= FormatDuration(Duration);
496 SampleRate:= FOptimFrog.SampleRate;
497 ReadID3v2(FOptimFrog.ID3v2);
498 ReadID3v1(FOptimFrog.ID3v1);
499 ReadAPEtag(FOptimFrog.APEtag);
500 end;
501 end;
502
503 function TAudioData.ReadWAVPackfile: Boolean;
504 begin
505 Result:= FWAVPackfile.ReadFromFile(FFileName) and FWAVPackfile.Valid;
506 if Result then
507 begin
508 Bits:= FWAVPackfile.Bits;
509 Encoder:= FWAVPackfile.Encoder;
510 Channels:= FWAVPackfile.ChannelMode;
511 BitRate:= Round(FWAVPackfile.BitRate);
512 Duration:= Round(FWAVPackfile.Duration);
513 DurationHMS:= FormatDuration(Duration);
514 SampleRate:= FWAVPackfile.SampleRate;
515 ReadAPEtag(FWAVPackfile.APEtag);
516 end;
517 end;
518
519 constructor TAudioData.Create;
520 begin
521 FDTS:= TDTS.Create;
522 FAC3:= TAC3.Create;
523 FTTA:= TTTA.Create;
524 FTwinVQ:= TTwinVQ.Create;
525 FMonkey:= TMonkey.Create;
526 FMP4file:= TMP4file.Create;
527 FAACfile:= TAACfile.Create;
528 FWMAfile:= TWMAfile.Create;
529 FWAVfile:= TWAVfile.Create;
530 FCDAtrack:= TCDAtrack.Create;
531 FMPEGplus:= TMPEGplus.Create;
532 FFLACfile:= TFLACfile.Create;
533 FMPEGaudio:= TMPEGaudio.Create;
534 FOggVorbis:= TOggVorbis.Create;
535 FOptimFrog:= TOptimFrog.Create;
536 FWAVPackfile:= TWAVPackfile.Create;
537 end;
538
539 destructor TAudioData.Destroy;
540 begin
541 FDTS.Free;
542 FAC3.Free;
543 FTTA.Free;
544 FTwinVQ.Free;
545 FMonkey.Free;
546 FMP4file.Free;
547 FAACfile.Free;
548 FWMAfile.Free;
549 FWAVfile.Free;
550 FCDAtrack.Free;
551 FMPEGplus.Free;
552 FFLACfile.Free;
553 FMPEGaudio.Free;
554 FOggVorbis.Free;
555 FOptimFrog.Free;
556 FWAVPackfile.Free;
557 inherited Destroy;
558 end;
559
560 function TAudioData.LoadFromFile(const FileName: String): Boolean;
561 var
562 FileExt: String;
563 begin
564 Clear;
565
566 FFileName:= FileName;
567 FileExt:= LowerCase(ExtractOnlyFileExt(FileName));
568
569 if (FileExt = 'mp3') or (FileExt = 'mp2') or (FileExt = 'mp1') then
570 begin
571 Result:= ReadMPEGaudio;
572 end
573 else if (FileExt = 'mpc') then
574 begin
575 Result:= ReadMPEGplus;
576 end
577 else if (FileExt = 'ogg') or (FileExt = 'opus') then
578 begin
579 Result:= ReadOggVorbis;
580 end
581 else if (FileExt = 'flac') then
582 begin
583 Result:= ReadFLACfile;
584 end
585 else if (FileExt = 'ape') then
586 begin
587 Result:= ReadMonkey;
588 end
589 else if (FileExt = 'aac') then
590 begin
591 Result:= ReadAACfile;
592 end
593 else if (FileExt = 'cda') then
594 begin
595 Result:= ReadCDAtrack;
596 end
597 else if (FileExt = 'wma') then
598 begin
599 Result:= ReadWMAfile;
600 end
601 else if (FileExt = 'wav') then
602 begin
603 Result:= ReadWAVfile;
604 end
605 else if (FileExt = 'tta') then
606 begin
607 Result:= ReadTTA;
608 end
609 else if (FileExt = 'vqf') then
610 begin
611 Result:= ReadTwinVQ;
612 end
613 else if (FileExt = 'ac3') then
614 begin
615 Result:= ReadAC3;
616 end
617 else if (FileExt = 'dts') then
618 begin
619 Result:= ReadDTS;
620 end
621 else if (FileExt = 'wv') or (FileExt = 'wvc') then
622 begin
623 Result:= ReadWAVPackfile;
624 end
625 else if (FileExt = 'ofr') or (FileExt = 'ofs') then
626 begin
627 Result:= ReadOptimFrog;
628 end
629 else if (FileExt = 'mp4') or (FileExt = 'm4a') then
630 begin
631 Result:= ReadMP4file;
632 end
633 else Result:= False;
634
635 if Result then
636 begin
637 FullText:= UTF8ToUTF16(Title + LineEnding + Artist + LineEnding + Album + LineEnding +
638 Comment + LineEnding + Composer + LineEnding + Copyright + LineEnding +
639 URL + LineEnding + Encoder + LineEnding);
640 end;
641 end;
642
643 end.
644