⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jvqid3v2base.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{******************************************************************************}
{* WARNING:  JEDI VCL To CLX Converter generated unit.                        *}
{*           Manual modifications will be lost on next release.               *}
{******************************************************************************}

{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: JvID3v2Base.PAS, released on 2003-04-16.

The Initial Developer of the Original Code is Remko Bonte [remkobonte att myrealbox dott com]
Portions created by Remko Bonte are Copyright (C) 2003 Remko Bonte.
All Rights Reserved.

Contributor(s):

You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net

Known Issues:
  * Encryption, compression not supported
  * Footer in v2.4 tags not supported
  * Some tags are not supported, see var DefaultFrameClasses. Values nil in that
    list indicate not supported frames.
-----------------------------------------------------------------------------}
// $Id: JvQID3v2Base.pas,v 1.18 2004/09/07 23:11:17 asnepvangers Exp $

unit JvQID3v2Base;

{$I jvcl.inc}

interface

uses
  Classes, SysUtils,
  JclUnicode,
  JvQComponent, JvQID3v2Types, JvQID3v1;

const
  { Only v2.2, v2.3 and v2.4 are supported }
  CSupportedVersions = [ive2_2, ive2_3, ive2_4];

type
  EJvID3Error = class(Exception);

  TJvID3ActivateChangeEvent = procedure(Sender: TObject; Activated: Boolean) of object;

  TJvID3HandleError = (heAutoCorrect, heRaise, heBoolean);

  TJvMPEGLayer = (mlNotDefined, mlLayerIII, mlLayerII, mlLayerI);
  TJvMPEGVersion = (mvVersion25, mvReserved, mvVersion2, mvVersion1);
  TJvMPEGChannelMode = (mcStereo, mcJointStereo, mcDualChannel, mcSingleChannel);
  TJvMPEGBit = (mbProtection, mbPrivate, mbCopyrighted, mbOriginal);
  TJvMPEGBits = set of TJvMPEGBit;
  TJvMPEGEmphasis = (meNone, me5015ms, meReserved, meCCITJ17);
  TJvMPEGModeExtension = (meModeExt0, meModeExt1, meModeExt2, meModeExt3);

  TJvID3ControllerOption = (coAutoCorrect, coRemoveEmptyFrames);
  TJvID3ControllerOptions = set of TJvID3ControllerOption;

  TJvID3Event = (
    { Fired when the content of 1 or more frames in a tag changes }
    ideFrameChange,
    { Fired when the whole tag has changed, because of reading/writing }
    ideID3Change,
    { Fired when frames are added, deleted etc. }
    ideFrameListChange);

  TJvID3Controller = class;

  TJvID3Stream = class(TMemoryStream)
  private
    FReadingFrame: Boolean;
    FWritingFrame: Boolean;
    FSourceEncoding: TJvID3Encoding;
    FDestEncoding: TJvID3Encoding;

    FAllowedEncodings: TJvID3Encodings;

    FStartPosition: Integer;
    FCurrentFrameSize: Integer;
    procedure MoveToNextFrame;
    function GetBytesTillEndOfTag: Longint;
    function GetBytesTillEndOfFrame: Longint;
    procedure UpdateDestEncoding;
    procedure SetSourceEncoding(const Value: TJvID3Encoding);
  protected
    { ISO-8859-1 }
    function ReadStringA(var SA: string): Longint;
    function ReadUserStringA(var SA1, SA2: string): Longint;
    function WriteStringA(const SA: string): Longint;
    function WriteUserStringA(const SA1, SA2: string): Longint;
    function WriteTerminatorA: Longint;
    { UTF-16 & UTF-16BE }
    function ReadStringW(var SW: WideString): Longint;
    function ReadUserStringW(var SW1, SW2: WideString): Longint;
    function WriteStringW(const SW: WideString): Longint;
    function WriteUserStringW(const SW1, SW2: WideString): Longint;
    function WriteTerminatorW: Longint;
    { UTF-8 }
    function ReadStringUTF8(var SW: WideString): Longint;
    function ReadUserStringUTF8(var SW1, SW2: WideString): Longint;
    function WriteStringUTF8(const SW: WideString): Longint;
    function WriteUserStringUTF8(const SW1, SW2: WideString): Longint;
  public
    procedure BeginReadFrame(const AFrameSize: Integer);
    procedure BeginWriteFrame(const AFrameSize: Integer);

    procedure EndReadFrame;
    procedure EndWriteFrame;

    { Inits FAllowedEncodings depending on the wanted version and encoding }
    procedure InitAllowedEncodings(const AVersion: TJvID3Version;
      const AEncoding: TJvID3ForceEncoding);

    { Checks whether ACount bytes can be read }
    function CanRead(const ACount: Cardinal): Boolean;
    { Checks whether we are still in the frame }
    function InFrame(P: PChar): Boolean;

    { Read }
    function ReadDate(var ADate: TDateTime): Longint;
    function ReadLanguage(var Language: string): Longint;
    function ReadNumber(var AValue: Cardinal): Longint;
    function ReadEnc(var AEncoding: TJvID3Encoding): Longint;
    function ReadStringEnc(var S: TJvID3StringPair): Longint;
    function ReadUserString(var S1, S2: TJvID3StringPair): Longint;
    { Only for v2.2 }
    function ReadFixedNumber3(var AValue: Cardinal): Longint;
    { Only for v2.3 }
    function ReadFixedNumber(var AValue: Cardinal): Longint;
    { Only for v2.4 }
    function ReadSyncSafeInteger(var AInt: Cardinal): Longint; overload;
    function ReadSyncSafeInteger(var AInt: Cardinal; const ASize: Byte): Longint; overload;
    function ReadSyncSafeInteger(var AInt: Int64; const ASize: Byte = 4): Longint; overload;

    procedure ReadFromStream(AStream: TStream; const ASize: Integer);

    { Write }
    function WriteDate(const ADate: TDateTime): Longint;
    function WriteLanguage(const Language: string): Longint;
    function WriteNumber(AValue: Cardinal): Longint;
    function WriteEnc: Longint;
    function WritePadding(const Count: Longint): Longint;
    function WriteStringEnc(const S: TJvID3StringPair): Longint;
    function WriteUserString(const S1, S2: TJvID3StringPair): Longint;
    function WriteTerminatorEnc: Longint;
    { Only for v2.2 }
    function WriteFixedNumber3(AValue: Cardinal): Longint;
    { Only for v2.3 }
    function WriteFixedNumber(AValue: Cardinal): Longint;
    { Only for v2.4 }
    function WriteSyncSafeInteger(const AInt: Int64; const ASize: Byte = 4): Longint; overload;
    function WriteSyncSafeInteger(const AInt: Cardinal; const ASize: Byte): Longint; overload;
    function WriteSyncSafeInteger(const AInt: Cardinal): Longint; overload;

    property BytesTillEndOfFrame: Longint read GetBytesTillEndOfFrame;
    property BytesTillEndOfTag: Longint read GetBytesTillEndOfTag;

    { SourceEncoding =
        - When reading: encoding of the ID3 stream
        - When writing: encoding of current frame in the TJvID3Controller }
    property SourceEncoding: TJvID3Encoding read FSourceEncoding write SetSourceEncoding;
    { DestEncoding =
        - When reading: encoding of current frame in the TJvID3Controller
        - When writing: encoding of the ID3 stream }
    property DestEncoding: TJvID3Encoding read FDestEncoding;
    property AllowedEncodings: TJvID3Encodings read FAllowedEncodings;
  end;

  TJvID3Frame = class;
  TJvID3Frames = class;

  TJvID3FrameClass = class of TJvID3Frame;

  { Base component for TJvID3Header & TJvID3ExtendedHeader }
  TJvID3Base = class(TPersistent)
  private
    FController: TJvID3Controller;
    function GetStream: TJvID3Stream;
  protected
    procedure Read; virtual; abstract;
    procedure Write; virtual; abstract;
    procedure Reset; virtual; abstract;

    property Stream: TJvID3Stream read GetStream;
  public
    constructor Create(AController: TJvID3Controller); virtual;
    procedure AfterConstruction; override;
    procedure ChangeToVersion(const ANewVersion: TJvID3Version); virtual; abstract;
    procedure Assign(Source: TPersistent); override;
    property Controller: TJvID3Controller read FController;
  end;

  TJvID3Header = class(TJvID3Base)
  private
    FFlags: TJvID3HeaderFlags;
    FHasTag: Boolean;
    FMajorVersion: Byte;
    FRevisionNumber: Byte;
    FSize: Cardinal;
    procedure SetFlags(const Value: TJvID3HeaderFlags);
  protected
    procedure Read; override;
    procedure Write; override;
    procedure Reset; override;
  public
    procedure Assign(Source: TPersistent); override;
    procedure ChangeToVersion(const ANewVersion: TJvID3Version); override;
  published
    property MajorVersion: Byte read FMajorVersion;
    property RevisionNumber: Byte read FRevisionNumber;
    property HasTag: Boolean read FHasTag;
    property Flags: TJvID3HeaderFlags read FFlags write SetFlags;
    property Size: Cardinal read FSize;
  end;

  TJvID3ExtendedHeader = class(TJvID3Base)
  private
    FFlags: TJvID3HeaderExtendedFlags;
    FRestrictions: TJvID3Restrictions;
    FSizeOfPadding: Cardinal;
    FTotalFrameCRC: Cardinal;
    function GetSize: Cardinal;
    function GetSizeForVersion(const AVersion: TJvID3Version): Cardinal;
    procedure SetFlags(const Value: TJvID3HeaderExtendedFlags);
  protected
    procedure Read; override;
    procedure Write; override;
    procedure Reset; override;
  public
    procedure Assign(Source: TPersistent); override;
    procedure ChangeToVersion(const ANewVersion: TJvID3Version); override;
    property Size: Cardinal read GetSize;
  published
    property TotalFrameCRC: Cardinal read FTotalFrameCRC write FTotalFrameCRC;
    property SizeOfPadding: Cardinal read FSizeOfPadding;
    property Flags: TJvID3HeaderExtendedFlags read FFlags write SetFlags;
  end;

  { Base class for all frames }
  { TODO : Change to TPersistent? }
  TJvID3Frame = class(TComponent)
  private
    FController: TJvID3Controller;
    FFrames: TJvID3Frames;
    FFrameID: TJvID3FrameID;
    FFrameIDStr: string;
    FFrameSize: Cardinal;

    FDataLengthIndicator: Cardinal; { v2.4 }
    FDecompressedSize: Cardinal;
    FEncoding: TJvID3Encoding;
    FEncryptionID: Byte;
    FFlags: TJvID3FrameHeaderFlags;
    FGroupID: Byte;

    function GetFrameName: string;
    function GetFrameIDStrForVersion(const Version: TJvID3Version): string;
    function GetIndex: Integer;
    function GetStream: TJvID3Stream;
    procedure SetController(const AController: TJvID3Controller);
    procedure SetEncoding(const Value: TJvID3Encoding);
    procedure SetFlags(const Value: TJvID3FrameHeaderFlags);
    procedure SetFrameID(const Value: TJvID3FrameID);
    procedure SetFrameName(NewFrameName: string);
    procedure SetIndex(const Value: Integer);
  protected
    procedure Read;
    procedure Write;

    procedure ReadEncoding;
    procedure ReadFrame; virtual; abstract;
    procedure ReadFrameHeader;
    procedure WriteEncoding;
    procedure WriteFrame; virtual; abstract;
    procedure WriteFrameHeader(const AFrameSize: Cardinal);
    procedure WriteID;

    procedure ChangeToVersion(const ANewVersion: TJvID3Version); virtual;
    function SupportsVersion(const AVersion: TJvID3Version): Boolean; virtual;

    { Checks whether this frame is empty, thus can be removed }
    function GetIsEmpty: Boolean; virtual;

    { Checks whether there are no other frames with the same unique
      identifier as this frame }
    function CheckIsUnique: Boolean;

    procedure CheckFrameID(const AFrameID: TJvID3FrameID);
    procedure CheckFrameIDStr(const S: string);

    { Checks whether Frame has the same unique identifier as this frame }
    function SameUniqueIDAs(const Frame: TJvID3Frame): Boolean; virtual;

    function GetFrameSize(const ToEncoding: TJvID3Encoding): Cardinal; virtual; abstract;
    procedure UpdateFrameSize;

    procedure DataChanged;
    procedure Changed; virtual;

    procedure Error(const Msg: string);
    procedure ErrorFmt(const Msg: string; const Args: array of const);

    property Stream: TJvID3Stream read GetStream;
  public
    constructor Create(AOwner: TComponent; const AFrameID: TJvID3FrameID;
      const AFrameIDStr: string = ''); reintroduce; virtual;
    destructor Destroy; override;

    class function CanAddFrame(AController: TJvID3Controller; AFrameID: TJvID3FrameID): Boolean; virtual;
    function CheckFrame(const HandleError: TJvID3HandleError): Boolean; virtual;

    procedure Assign(Source: TPersistent); override;
    procedure Clear; virtual;
    property Controller: TJvID3Controller read FController write SetController stored False;
    property FrameSize: Cardinal read FFrameSize;

    property IsEmpty: Boolean read GetIsEmpty;
  published
    property Encoding: TJvID3Encoding read FEncoding write SetEncoding;
    property EncryptionID: Byte read FEncryptionID write FEncryptionID;
    property Flags: TJvID3FrameHeaderFlags read FFlags write SetFlags;
    property FrameID: TJvID3FrameID read FFrameID write SetFrameID;
    property FrameName: string read GetFrameName write SetFrameName;
    property GroupID: Byte read FGroupID write FGroupID;
    property Index: Integer read GetIndex write SetIndex stored False;
  end;

  TJvID3Frames = class(TJvID3Base)
  private
    FList: TList;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -