-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_monkey.pas
270 lines (235 loc) · 9.21 KB
/
file_monkey.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{
This file is part of OvoTag
Copyright (C) 2011 Marco Caselli
OvoTag is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
}
{$I ovotag.inc}
unit file_Monkey;
interface
uses
Classes, SysUtils, AudioTag, baseTag, tag_APE;
const
MonkeyFileMask: string = '*.ape;';
type
{ TMonkeyReader }
TMonkeyReader = class(TTagReader)
private
fTags: TTags;
FSampleRate: integer;
FSamples: int64;
protected
function GetDuration: int64; override;
function GetTags: TTags; override;
public
function isUpdateable: boolean; override;
function LoadFromFile(AFileName: Tfilename): boolean; override;
function SaveToFile(AFileName: Tfilename): boolean; override;
end;
implementation
uses tag_id3v2;
type
{ Real structure of Monkey's Audio header }
// common header for all versions
APE_HEADER = packed record
cID: array[0..3] of byte; // should equal 'MAC '
nVersion: word; // version number * 1000 (3.81 = 3810)
end;
// old header for <= 3.97
APE_HEADER_OLD = packed record
nCompressionLevel, // the compression level
nFormatFlags, // any format flags (for future use)
nChannels: word; // the number of channels (1 or 2)
nSampleRate, // the sample rate (typically 44100)
nHeaderBytes, // the bytes after the MAC header that compose the WAV header
nTerminatingBytes, // the bytes after that raw data (for extended info)
nTotalFrames, // the number of frames in the file
nFinalFrameBlocks: longword; // the number of samples in the final frame
nInt: integer;
end;
// new header for >= 3.98
APE_HEADER_NEW = packed record
nCompressionLevel: word; // the compression level (see defines I.E. COMPRESSION_LEVEL_FAST)
nFormatFlags: word; // any format flags (for future use) Note: NOT the same flags as the old header!
nBlocksPerFrame: longword; // the number of audio blocks in one frame
nFinalFrameBlocks: longword; // the number of audio blocks in the final frame
nTotalFrames: longword; // the total number of frames
nBitsPerSample: word; // the bits per sample (typically 16)
nChannels: word; // the number of channels (1 or 2)
nSampleRate: longword; // the sample rate (typically 44100)
end;
// data descriptor for >= 3.98
APE_DESCRIPTOR = packed record
padded: word; // padding/reserved (always empty)
nDescriptorBytes, // the number of descriptor bytes (allows later expansion of this header)
nHeaderBytes, // the number of header APE_HEADER bytes
nSeekTableBytes, // the number of bytes of the seek table
nHeaderDataBytes, // the number of header data bytes (from original file)
nAPEFrameDataBytes, // the number of bytes of APE frame data
nAPEFrameDataBytesHigh, // the high order number of APE frame data bytes
nTerminatingDataBytes: longword; // the terminating data of the file (not including tag data)
cFileMD5: array[0..15] of byte; // the MD5 hash of the file (see notes for usage... it's a littly tricky)
end;
Const { Compression level codes }
//MONKEY_COMPRESSION_FAST = 1000; { Fast (poor) }
//MONKEY_COMPRESSION_NORMAL = 2000; { Normal (good) }
//MONKEY_COMPRESSION_HIGH = 3000; { High (very good) }
MONKEY_COMPRESSION_EXTRA_HIGH = 4000; { Extra high (best) }
//MONKEY_COMPRESSION_INSANE = 5000; { Insane }
//MONKEY_COMPRESSION_BRAINDEAD = 6000; { BrainDead }
{ TMonkeyReader }
function TMonkeyReader.GetDuration: int64;
begin
if (FSampleRate > 0) then
begin
Result := trunc((FSamples / FSampleRate) * 1000);
end
else
begin
Result := 0;
end;
end;
function TMonkeyReader.GetTags: TTags;
begin
Result := fTags;
end;
function TMonkeyReader.isUpdateable: boolean;
begin
Result := True;
end;
function TMonkeyReader.LoadFromFile(AFileName: Tfilename): boolean;
var
fStream: TFileStream;
tempTags: TTags;
HaveID3V2: boolean;
BlocksPerFrame: Integer;
APE: APE_HEADER; // common header
APE_OLD: APE_HEADER_OLD; // old header <= 3.97
APE_NEW: APE_HEADER_NEW; // new header >= 3.98
APE_DESC: APE_DESCRIPTOR; // extra header >= 3.98
begin
Result := inherited LoadFromFile(AFileName);
fStream := TFileStream.Create(fileName, fmOpenRead or fmShareDenyNone);
try
tempTags := TID3Tags.Create;
HaveID3V2 := tempTags.ReadFromStream(fStream);
fStream.Position := 0;
ftags := TAPETags.Create;
Result := ftags.ReadFromStream(fStream);
if (not Result) and HaveID3V2 then
begin
fTags.Free;
fTags := tempTags;
end
else
tempTags.free;
if HaveID3V2 then
fStream.Seek(TID3Tags(tempTags).Size, soFromBeginning)
else
fStream.Seek(0, soFromBeginning);
FillByte(APE, sizeof(APE), 0);
if (fStream.Read(APE, sizeof(APE)) = sizeof(APE)) and (StrLComp(@APE.cID[0], 'MAC ', 4) = 0) then
begin
if APE.nVersion >= 3980 then
begin
FillByte(APE_DESC, sizeof(APE_DESC), 0);
if (fStream.Read(APE_DESC, sizeof(APE_DESC)) = sizeof(APE_DESC)) then
begin
// seek past description header
if APE_DESC.nDescriptorBytes <> 52 then
fStream.Seek(APE_DESC.nDescriptorBytes - 52, soFromCurrent);
// load new ape_header
if APE_DESC.nHeaderBytes > sizeof(APE_NEW) then
APE_DESC.nHeaderBytes := sizeof(APE_NEW);
fillchar(APE_NEW, sizeof(APE_NEW), 0);
if (longword(fStream.Read(APE_NEW, APE_DESC.nHeaderBytes)) = APE_DESC.nHeaderBytes) then
begin
// based on MAC SDK 3.98a1 (APEinfo.h)
FSampleRate := APE_NEW.nSampleRate;
if APE_NEW.nTotalFrames > 0 then
begin
FSamples :=
int64(APE_NEW.nBlocksPerFrame) * int64(APE_NEW.nTotalFrames - 1) +
int64(APE_NEW.nFinalFrameBlocks);
end;
end;
end;
end
else
begin
// Old Monkey <= 3.97
fillchar(APE_OLD, sizeof(APE_OLD), 0);
if (fStream.Read(APE_OLD, sizeof(APE_OLD)) = sizeof(APE_OLD)) then
begin
FSampleRate := APE_OLD.nSampleRate;
// based on MAC_SDK_397 (APEinfo.cpp)
if (APE.nVersion >= 3950) then
BlocksPerFrame := 73728 * 4
else if (APE.nVersion >= 3900) or ((APE.nVersion >= 3800) and
(APE_OLD.nCompressionLevel = MONKEY_COMPRESSION_EXTRA_HIGH)) then
BlocksPerFrame := 73728
else
BlocksPerFrame := 9216;
// calculate total uncompressed samples
if APE_OLD.nTotalFrames > 0 then
begin
FSamples :=
int64(APE_OLD.nTotalFrames - 1) * int64(BlocksPerFrame) +
int64(APE_OLD.nFinalFrameBlocks);
end;
end;
end;
end;
finally
fstream.Free;
end;
end;
function TMonkeyReader.SaveToFile(AFileName: Tfilename): boolean;
var
SourceStream: TFileStream;
DestStream: TFileStream;
v1rec: TID3V1Record;
offset: cardinal;
header: TAPEHeader;
begin
Result := inherited SaveToFile(AFileName);
SourceStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
DestStream := TFileStream.Create(AFileName, fmCreate or fmOpenReadWrite or fmShareDenyNone);
try
SourceStream.Seek(SourceStream.Size - SizeOf(TID3V1Record), soFromBeginning);
SourceStream.Read(V1Rec, SizeOf(TID3V1Record));
if V1Rec.Header <> 'TAG' then
Offset := 0
else
Offset := SizeOf(V1Rec);
SourceStream.Seek(SourceStream.Size - SizeOf(TAPEHeader) - offset, soFromBeginning);
SourceStream.Read(Header, sizeof(TAPEHeader));
if string(Header.Marker) = APE_IDENTIFIER then
begin
Offset := offset + header.TagSize;
SourceStream.Seek(SourceStream.Size - SizeOf(Header) - offset, soFromBeginning);
SourceStream.Read(Header, sizeof(Header));
if string(Header.Marker) = APE_IDENTIFIER then
Offset := offset + sizeof(Header);
end;
SourceStream.Position := 0;
DestStream.CopyFrom(SourceStream, SourceStream.Size - offset);
Tags.WriteToStream(DestStream);
Result := True;
finally
SourceStream.Free;
DestStream.Free;
end;
end;
initialization
RegisterTagReader(MonkeyFileMask, TMonkeyReader);
end.