| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TAC3 - for manipulating with AC3 Files } |
| 5 |
{ } |
| 6 |
{ http://mac.sourceforge.net/atl/ } |
| 7 |
{ e-mail: [email protected] } |
| 8 |
{ } |
| 9 |
{ Copyright (c) 2005 by Gambit } |
| 10 |
{ } |
| 11 |
{ Version 1.1 (April 2005) by Gambit } |
| 12 |
{ - updated to unicode file access } |
| 13 |
{ } |
| 14 |
{ Version 1.0 (05 January 2005) } |
| 15 |
{ } |
| 16 |
{ This library is free software; you can redistribute it and/or } |
| 17 |
{ modify it under the terms of the GNU Lesser General Public } |
| 18 |
{ License as published by the Free Software Foundation; either } |
| 19 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 20 |
{ } |
| 21 |
{ This library is distributed in the hope that it will be useful, } |
| 22 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 23 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 24 |
{ Lesser General Public License for more details. } |
| 25 |
{ } |
| 26 |
{ You should have received a copy of the GNU Lesser General Public } |
| 27 |
{ License along with this library; if not, write to the Free Software } |
| 28 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 29 |
{ } |
| 30 |
{ *************************************************************************** } |
| 31 |
|
| 32 |
unit AC3; |
| 33 |
|
| 34 |
interface |
| 35 |
|
| 36 |
uses |
| 37 |
Classes, SysUtils, DCClassesUtf8; |
| 38 |
|
| 39 |
const |
| 40 |
BIRATES: array[0..18] of Integer = (32, 40, 48, 56, 64, 80, 96, 112, 128, 160, |
| 41 |
192, 224, 256, 320, 384, 448, 512, 576, 640); |
| 42 |
|
| 43 |
type |
| 44 |
{ Class TAC3 } |
| 45 |
TAC3 = class(TObject) |
| 46 |
private |
| 47 |
{ Private declarations } |
| 48 |
FFileSize: Int64; |
| 49 |
FValid: Boolean; |
| 50 |
|
| 51 |
FChannels: Cardinal; |
| 52 |
FBits: Cardinal; |
| 53 |
FSampleRate: Cardinal; |
| 54 |
|
| 55 |
FBitrate: Word; |
| 56 |
FDuration: Double; |
| 57 |
|
| 58 |
function FGetRatio: Double; |
| 59 |
procedure FResetData; |
| 60 |
|
| 61 |
public |
| 62 |
{ Public declarations } |
| 63 |
constructor Create; { Create object } |
| 64 |
destructor Destroy; override; { Destroy object } |
| 65 |
|
| 66 |
function ReadFromFile(const FileName: String): Boolean; { Load header } |
| 67 |
|
| 68 |
property FileSize: Int64 read FFileSize; |
| 69 |
property Valid: Boolean read FValid; |
| 70 |
|
| 71 |
property Channels: Cardinal read FChannels; |
| 72 |
property Bits: Cardinal read FBits; |
| 73 |
property SampleRate: Cardinal read FSampleRate; |
| 74 |
|
| 75 |
property Bitrate: Word read FBitrate; |
| 76 |
property Duration: Double read FDuration; |
| 77 |
property Ratio: Double read FGetRatio; { Compression ratio (%) } |
| 78 |
end; |
| 79 |
|
| 80 |
implementation |
| 81 |
|
| 82 |
|
| 83 |
{ ********************** Private functions & procedures ********************* } |
| 84 |
|
| 85 |
procedure TAC3.FResetData; |
| 86 |
begin |
| 87 |
{ Reset all data } |
| 88 |
FFileSize := 0; |
| 89 |
FValid := False; |
| 90 |
|
| 91 |
FChannels := 0; |
| 92 |
FBits := 0; |
| 93 |
FSampleRate := 0; |
| 94 |
|
| 95 |
FBitrate := 0; |
| 96 |
FDuration := 0; |
| 97 |
end; |
| 98 |
|
| 99 |
|
| 100 |
{ ********************** Public functions & procedures ********************** } |
| 101 |
|
| 102 |
constructor TAC3.Create; |
| 103 |
begin |
| 104 |
{ Create object } |
| 105 |
inherited; |
| 106 |
FResetData; |
| 107 |
end; |
| 108 |
|
| 109 |
(* -------------------------------------------------------------------------- *) |
| 110 |
|
| 111 |
destructor TAC3.Destroy; |
| 112 |
begin |
| 113 |
inherited; |
| 114 |
end; |
| 115 |
|
| 116 |
(* -------------------------------------------------------------------------- *) |
| 117 |
|
| 118 |
function TAC3.ReadFromFile(const FileName: String): Boolean; |
| 119 |
var |
| 120 |
f: TFileStreamEx; |
| 121 |
SignatureChunk: Word; |
| 122 |
tehByte: Byte; |
| 123 |
begin |
| 124 |
Result := False; |
| 125 |
FResetData; |
| 126 |
|
| 127 |
f:=nil; |
| 128 |
|
| 129 |
try |
| 130 |
f := TFileStreamEx.create(FileName, fmOpenRead or fmShareDenyWrite); |
| 131 |
//0x0B77 |
| 132 |
if (f.Read(SignatureChunk, SizeOf(SignatureChunk)) = SizeOf(SignatureChunk)) and (SignatureChunk = 30475) then |
| 133 |
begin |
| 134 |
FillChar(tehByte, SizeOf(tehByte),0); |
| 135 |
f.Seek(2, soFromCurrent); |
| 136 |
f.Read(tehByte, SizeOf(tehByte)); |
| 137 |
|
| 138 |
FFileSize := f.Size; |
| 139 |
FValid := TRUE; |
| 140 |
|
| 141 |
case (tehByte and $C0) of |
| 142 |
0: FSampleRate := 48000; |
| 143 |
$40: FSampleRate := 44100; |
| 144 |
$80: FSampleRate := 32000; |
| 145 |
else FSampleRate := 0; |
| 146 |
end; |
| 147 |
|
| 148 |
FBitrate := BIRATES[(tehByte and $3F) shr 1]; |
| 149 |
|
| 150 |
FillChar(tehByte, SizeOf(tehByte),0); |
| 151 |
f.Seek(1, soFromCurrent); |
| 152 |
f.Read(tehByte, SizeOf(tehByte)); |
| 153 |
|
| 154 |
case (tehByte and $E0) of |
| 155 |
0: FChannels := 2; |
| 156 |
$20: FChannels := 1; |
| 157 |
$40: FChannels := 2; |
| 158 |
$60: FChannels := 3; |
| 159 |
$80: FChannels := 3; |
| 160 |
$A0: FChannels := 4; |
| 161 |
$C0: FChannels := 4; |
| 162 |
$E0: FChannels := 5; |
| 163 |
else FChannels := 0; |
| 164 |
end; |
| 165 |
|
| 166 |
FBits := 16; |
| 167 |
FDuration := FFileSize * 8 / 1000 / FBitrate; |
| 168 |
|
| 169 |
Result := True; |
| 170 |
end; |
| 171 |
|
| 172 |
finally |
| 173 |
f.free; |
| 174 |
end; |
| 175 |
end; |
| 176 |
|
| 177 |
(* -------------------------------------------------------------------------- *) |
| 178 |
|
| 179 |
function TAC3.FGetRatio: Double; |
| 180 |
begin |
| 181 |
{ Get compression ratio } |
| 182 |
if FValid then |
| 183 |
Result := FFileSize / ((FDuration * FSampleRate) * (FChannels * FBits / 8) + 44) * 100 |
| 184 |
else |
| 185 |
Result := 0; |
| 186 |
end; |
| 187 |
|
| 188 |
(* -------------------------------------------------------------------------- *) |
| 189 |
|
| 190 |
end. |