Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TCDAtrack - for getting information for CDDA track }
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.1 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 1.0 (4 November 2002) }
16 { - Using cdplayer.ini }
17 { - Track info: title, artist, album, duration, track number, position }
18 { }
19 { This library is free software; you can redistribute it and/or }
20 { modify it under the terms of the GNU Lesser General Public }
21 { License as published by the Free Software Foundation; either }
22 { version 2.1 of the License, or (at your option) any later version. }
23 { }
24 { This library is distributed in the hope that it will be useful, }
25 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
26 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
27 { Lesser General Public License for more details. }
28 { }
29 { You should have received a copy of the GNU Lesser General Public }
30 { License along with this library; if not, write to the Free Software }
31 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
32 { }
33 { *************************************************************************** }
34
35 unit CDAtrack;
36
37 interface
38
39 uses
40 Classes, SysUtils, IniFiles, DCClassesUtf8;
41
42 type
43 { Class TCDAtrack }
44 TCDAtrack = class(TObject)
45 private
46 { Private declarations }
47 FValid: Boolean;
48 FTitle: String;
49 FArtist: String;
50 FAlbum: String;
51 FDuration: Double;
52 FTrack: Word;
53 FPosition: Double;
54 procedure FResetData;
55 public
56 { Public declarations }
57 constructor Create; { Create object }
58 function ReadFromFile(const FileName: String): Boolean; { Load data }
59 property Valid: Boolean read FValid; { True if valid format }
60 property Title: String read FTitle; { Song title }
61 property Artist: String read FArtist; { Artist name }
62 property Album: String read FAlbum; { Album name }
63 property Duration: Double read FDuration; { Duration (seconds) }
64 property Track: Word read FTrack; { Track number }
65 property Position: Double read FPosition; { Track position (seconds) }
66 end;
67
68 implementation
69
70 type
71 { CDA track data }
72 TrackData = packed record
73 RIFFHeader: array [1..4] of Char; { Always "RIFF" }
74 FileSize: Integer; { Always "RealFileSize - 8" }
75 CDDAHeader: array [1..8] of Char; { Always "CDDAfmt " }
76 FormatSize: Integer; { Always 24 }
77 FormatID: Word; { Always 1 }
78 TrackNumber: Word; { Track number }
79 Serial: Integer; { CD serial number (stored in cdplayer.ini) }
80 PositionHSG: Integer; { Track position in HSG format }
81 DurationHSG: Integer; { Track duration in HSG format }
82 PositionRB: Integer; { Track position in Red-Book format }
83 DurationRB: Integer; { Track duration in Red-Book format }
84 Title: string; { Song title }
85 Artist: string; { Artist name }
86 Album: string; { Album name }
87 end;
88
89 { ********************* Auxiliary functions & procedures ******************** }
90
91 function ReadData(const FileName: String; var Data: TrackData): Boolean;
92 var
93 SourceFile: TFileStreamEx;
94 CDData: TIniFile;
95 begin
96 { Read track data }
97 Result := false;
98 try
99 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
100 SourceFile.Read(Data, 44);
101 SourceFile.Free;
102 Result := true;
103 { Try to get song info }
104 CDData := TIniFile.Create('cdplayer.ini');
105 Data.Title := CDData.ReadString(IntToHex(Data.Serial, 2),
106 IntToStr(Data.TrackNumber), '');
107 Data.Artist := CDData.ReadString(IntToHex(Data.Serial, 2),
108 'artist', '');
109 Data.Album := CDData.ReadString(IntToHex(Data.Serial, 2),
110 'title', '');
111 CDData.Free;
112 except
113 end;
114 end;
115
116 { --------------------------------------------------------------------------- }
117
118 function IsValid(const Data: TrackData): Boolean;
119 begin
120 { Check for format correctness }
121 Result := (Data.RIFFHeader = 'RIFF') and (Data.CDDAHeader = 'CDDAfmt ');
122 end;
123
124 { ********************** Private functions & procedures ********************* }
125
126 procedure TCDAtrack.FResetData;
127 begin
128 { Reset variables }
129 FValid := false;
130 FTitle := '';
131 FArtist := '';
132 FAlbum := '';
133 FDuration := 0;
134 FTrack := 0;
135 FPosition := 0;
136 end;
137
138 { ********************** Public functions & procedures ********************** }
139
140 constructor TCDAtrack.Create;
141 begin
142 { Create object }
143 inherited;
144 FResetData;
145 end;
146
147 { --------------------------------------------------------------------------- }
148
149 function TCDAtrack.ReadFromFile(const FileName: String): Boolean;
150 var
151 Data: TrackData;
152 begin
153 { Reset variables and load file data }
154 FResetData;
155 FillChar(Data, SizeOf(Data), 0);
156 Result := ReadData(FileName, Data);
157 { Process data if loaded and valid }
158 if Result and IsValid(Data) then
159 begin
160 FValid := true;
161 { Fill properties with loaded data }
162 FTitle := Data.Title;
163 FArtist := Data.Artist;
164 FAlbum := Data.Album;
165 FDuration := Data.DurationHSG / 75;
166 FTrack := Data.TrackNumber;
167 FPosition := Data.PositionHSG / 75;
168 end;
169 end;
170
171 { --------------------------------------------------------------------------- }
172
173 end.