unit AudioCDAtrack;

interface

uses KOL, ATLCommon;

type TCDATrackAudioInfo = PAudioInfo;

function NewAudioCDATrack: TCDATrackAudioInfo;

implementation

uses ATLLib;

type
  TTrackData = packed record
    RIFFHeader: array [1..4] of Char;                         { Always "RIFF" }
    FileSize: Integer;                            { Always "RealFileSize - 8" }
    CDDAHeader: array [1..8] of Char;                     { Always "CDDAfmt " }
    FormatSize: Integer;                                          { Always 24 }
    FormatID: Word;                                                { Always 1 }
    TrackNumber: Word;                                         { Track number }
    Serial: Integer;              { CD serial number (stored in cdplayer.ini) }
    PositionHSG: Integer;                      { Track position in HSG format }
    DurationHSG: Integer;                      { Track duration in HSG format }
    PositionRB: Integer;                  { Track position in Red-Book format }
    DurationRB: Integer;                  { Track duration in Red-Book format }
  end;

type
  TAudioCDATrack = object(TAudioInfo)
  private
  end;

function AudioCDATrackReader(const AFileName: KOLstring; AudioInfo: TCDATrackAudioInfo): Boolean; forward;

function NewAudioCDATrack: TCDATrackAudioInfo;
begin
    New(Result, Create);
    with TAudioCDATrack(Result^) do begin
      FAudioVendor:= aCDATrack;
      FExistsProps:= [apBits, apSampleRate, apChannels, apDuration];
      FReader:= AudioCDATrackReader;
    end;
end;

function AudioCDATrackReader(const AFileName: KOLstring; AudioInfo: TCDATrackAudioInfo): Boolean;
var Data: TTrackData;
    Source: PStream;
begin
    with TAudioCDATrack(AudioInfo^) do begin
      Result:= False;
      try
        ResetData;
        try
          Source := OpenFileRead(AFileName);
          FillChar(Data, SizeOf(Data), 0);
          if (Source.Read(Data, 44) = 44) and (Data.RIFFHeader = 'RIFF') and (Data.CDDAHeader = 'CDDAfmt ') then begin
            FValid := True;
            {$IFDEF USE_OtherAudioProps}
            OtherProps.Add('Track='+Int2Str(Data.TrackNumber));
            OtherProps.Add('Serial='+Int2Str(Data.Serial));
            OtherProps.Add('Position='+Double2Str(Data.PositionHSG / 75));
            {$ENDIF}
            FChannels:= 2;
            FBits:= 16;
            FSampleRate:= 44100;
            FDuration := Data.DurationHSG / 75;
            Result:= True;
          end;
        finally
          Source.Free;
        end;
      except
      end;
    end;
end;

end.
