{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
unit Main;

interface

{$IFDEF KOL_MCK}
uses Windows, Messages, KOL, AudioWav, AudioWAVPack, AudioMonkey, AudioOGG, AudioAC3, AudioAAC, AudioDTS, AudioTTA, AudioWMA, AudioOptimFrog, AudioFLAC, AudioTwinVQ, AudioCDATrack, AudioMPEG, AudioMPEGplus, AudioMP4, TagID3v1, TagID3v1Lyrics, TagID3v2, TagAPE, TagWMA, TagVorbis, TagTwinVQ, TagMp4, AudioMidi, AudioVerify {$IFNDEF KOL_MCK}, mirror, Classes, Controls, mckControls, mckObjs, Graphics,  mckCtrls,  MCKATL {$ENDIF}, ATLCommon;
{$ELSE}
{$I uses.inc}
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, mirror;
{$ENDIF}

type
  {$IFDEF KOL_MCK}
  {$I MCKfakeClasses.inc}
  {$IFDEF KOLCLASSES} {$I MainFormclass.inc} {$ELSE OBJECTS} PMainForm = ^TMainForm; {$ENDIF CLASSES/OBJECTS}
  {$IFDEF KOLCLASSES}{$I TMainForm.inc}{$ELSE} TMainForm = object(TObj) {$ENDIF}
    Form: PControl;
  {$ELSE not_KOL_MCK}
  TMainForm = class(TForm)
  {$ENDIF KOL_MCK}
    KOLProject: TKOLProject;
    KOLForm: TKOLForm;
    WavAudioInfo: TWavAudioInfo;
    WAVPackAudioInfo: TWAVPackAudioInfo;
    MonkeyAudioInfo: TMonkeyAudioInfo;
    OggAudioInfo: TOggAudioInfo;
    AC3AudioInfo: TAC3AudioInfo;
    AACAudioInfo: TAACAudioInfo;
    DTSAudioInfo: TDTSAudioInfo;
    TTAAudioInfo: TTTAAudioInfo;
    WMAAudioInfo: TWMAAudioInfo;
    OptimFrogAudioInfo: TOptimFrogAudioInfo;
    FLACAudioInfo: TFLACAudioInfo;
    TwinVQAudioInfo: TTwinVQAudioInfo;
    CDATrackAudioInfo: TCDATrackAudioInfo;
    MPEGAudioInfo: TMPEGAudioInfo;
    MPEGplusAudioInfo: TMPEGplusAudioInfo;
    MP4AudioInfo: TMP4AudioInfo;
    btnOpenAudio: TKOLButton;
    OpenSaveDialog: TKOLOpenSaveDialog;
    ID3v1Tag: TID3v1Tag;
    ID3v1LyricsTag: TID3v1LyricsTag;
    ID3v2Tag: TID3v2Tag;
    APETag: TAPETag;
    WMATag: TWMATag;
    ExWMATag: TExWMATag;
    VorbisTag: TVorbisTag;
    TwinVQTag: TTwinVQTag;
    Mp4Tag: TMp4Tag;
    gbTags: TKOLGroupBox;
    RadioBox1: TKOLRadioBox;
    RadioBox2: TKOLRadioBox;
    RadioBox3: TKOLRadioBox;
    RadioBox4: TKOLRadioBox;
    RadioBox5: TKOLRadioBox;
    RadioBox6: TKOLRadioBox;
    RadioBox7: TKOLRadioBox;
    RadioBox8: TKOLRadioBox;
    RadioBox9: TKOLRadioBox;
    btnOpenTag: TKOLButton;
    MIDIPlayer: TMIDIPlayer;
    GroupBox1: TKOLGroupBox;
    btnPlayMidi: TKOLButton;
    Button1: TKOLButton;
    Button2: TKOLButton;
    Button3: TKOLButton;
    btnVerifyAudio: TKOLButton;
    AudioVerify: TAudioVerify;
    Memo: TKOLMemo;
    procedure btnOpenAudioClick(Sender: PObj);
    procedure btnOpenTagClick(Sender: PObj);
    procedure btnPlayMidiClick(Sender: PObj);
    procedure Button1Click(Sender: PObj);
    procedure Button2Click(Sender: PObj);
    procedure Button3Click(Sender: PObj);
    procedure btnVerifyAudioClick(Sender: PObj);
  private
    procedure ShowAudio(AudioInfo: PAudioInfo; FileName: String; Read: Boolean);
    procedure ShowTag(TagInfo: PTagInfo; FileName: String; Read: Boolean);
  public
  end;

var
  MainForm {$IFDEF KOL_MCK} : PMainForm {$ELSE} : TMainForm {$ENDIF} ;

{$IFDEF KOL_MCK}
procedure NewMainForm( var Result: PMainForm; AParent: PControl );
{$ENDIF}

implementation

uses ATLConstEx;

{$IFNDEF KOL_MCK} {$R *.DFM} {$ENDIF}

{$IFDEF KOL_MCK}
{$I Main_1.inc}
{$ENDIF}

const _FormatAI = 'File: %s'#13#10'File type: %s'#13#10'File size: %s'#13#10+
                 'Bits: %s'#13#10'SampleRate: %s'#13#10'Samples: %s'#13#10+
                 'Bitrate: %s'#13#10'BitrateType: %s'#13#10'Duration: %s'#13#10+
                 'Ratio: %s'#13#10'ChannelMode: %s'#13#10'Channels: %s'#13#10'%s';
     _BitrateType: array [TBitrateType] of String = ('', 'CBR', 'VBR');

procedure TMainForm.ShowAudio(AudioInfo: PAudioInfo; FileName: String; Read: Boolean);
begin
    if Read then
      if not AudioInfo.ReadFromFile(FileName) or not AudioInfo.Valid then Exit;
    Memo.Text:= Memo.Text + Format(_FormatAI, [FileName, GetAudioVendorName(AudioInfo.AudioVendor), Int2Str(AudioInfo.FileSize),
                           Int2Str(AudioInfo.Bits), Int2Str(AudioInfo.SampleRate),
                           Int2Str(AudioInfo.Samples), Int2Str(AudioInfo.Bitrate), _BitrateType[AudioInfo.BitrateType],
                           Sec2Time(AudioInfo.Duration), Int2Str(Round(AudioInfo.Ratio)),
                           AudioInfo.ChannelMode, Int2Str(AudioInfo.Channels), AudioInfo.OtherProps.Text]);
end;

procedure TMainForm.btnOpenAudioClick(Sender: PObj);
var FileName: String;
begin
    OpenSaveDialog.Filter:= 'Audio files|'+GetAudioFileMaskAll;
    if OpenSaveDialog.Execute then begin
      Memo.Text:= '';
      FileName:= OpenSaveDialog.Filename;
      case Ext2AudioVendor(ExtractFileExt(FileName)) of
        aWav:        ShowAudio(WavAudioInfo, FileName, True);
        aWAVPack:    ShowAudio(WAVPackAudioInfo, FileName, True);
        aMonkey:     ShowAudio(MonkeyAudioInfo, FileName, True);
        aOGG:        ShowAudio(OGGAudioInfo, FileName, True);
        aAC3:        ShowAudio(AC3AudioInfo, FileName, True);
        aAAC:        ShowAudio(AACAudioInfo, FileName, True);
        aDTS:        ShowAudio(DTSAudioInfo, FileName, True);
        aTTA:        ShowAudio(TTAAudioInfo, FileName, True);
        aWMA:        ShowAudio(WMAAudioInfo, FileName, True);
        aOptimFrog:  ShowAudio(OptimFrogAudioInfo, FileName, True);
        aFLAC:       ShowAudio(FLACAudioInfo, FileName, True);
        aTwinVQ:     ShowAudio(TwinVQAudioInfo, FileName, True);
        aCDATrack:   ShowAudio(CDATrackAudioInfo, FileName, True);
        aMPEG:       ShowAudio(MPEGAudioInfo, FileName, True);
        aMPEGplus:   ShowAudio(MPEGplusAudioInfo, FileName, True);
        aMP4:        ShowAudio(MP4AudioInfo, FileName, True);
        else Exit;
      end;
    end;
end;


const _FormatTI = 'File: %s'#13#10'Tag type: %s'#13#10'Tag size: %s'#13#10+
                  'Version: %s'#13#10'Track: %s'#13#10'Title: %s'#13#10 +
                  'Artist: %s'#13#10'Album: %s'#13#10'Year: %s'#13#10 +
                  'Comment: %s'#13#10'Genre: %s'#13#10;

procedure TMainForm.ShowTag(TagInfo: PTagInfo; FileName: String; Read: Boolean);
begin
    if Read then
      if not TagInfo.ReadFromFile(FileName) or not TagInfo.Exists then Exit;
    Memo.Text:= Memo.Text + Format(_FormatTI, [FileName, GetTagVendorName(TagInfo.TagVendor), Int2Str(TagInfo.TagSize),
                          TagInfo.Version, TagInfo.Track, TagInfo.Title, TagInfo.Artist,
                          TagInfo.Album, TagInfo.Year, TagInfo.Comment, TagInfo.Genre]);
end;

procedure TMainForm.btnOpenTagClick(Sender: PObj);
var FileName: String;
    i: Integer;
    TagVendor: TTagVendor;
begin
    TagVendor:= tUnknown;
    for i:= 0 to gbTags.ChildCount-1 do
      if gbTags.Children[i].Tag > 0 then
        if gbTags.Children[i].Checked then begin
          TagVendor:= TTagVendor(gbTags.Children[i].Tag);
          Break;
        end;
    if TagVendor = tUnknown then Exit;
    OpenSaveDialog.Filter:= 'Audio files|'+GetAudioFileMask(TagVendor2AudioVendors(TagVendor));
    if OpenSaveDialog.Execute then begin
      Memo.Text:= '';
      FileName:= OpenSaveDialog.Filename;

      case TagVendor of
        tID3v1:        ShowTag(ID3v1Tag, FileName, True);
        tID3v1Lyrics:  ShowTag(ID3v1LyricsTag, FileName, True);
        tID3v2:        ShowTag(ID3v2Tag, FileName, True);
        tAPE:          ShowTag(APETag, FileName, True);
        tVorbis:       ShowTag(VorbisTag, FileName, True);
        tTwinVQ:       ShowTag(TwinVQTag, FileName, True);
        tWMA:          ShowTag(WMATag, FileName, True);
        tExWMA:        ShowTag(ExWMATag, FileName, True);
        tMp4:          ShowTag(Mp4Tag, FileName, True);
        else Exit;
      end;
    end;
end;

procedure TMainForm.btnPlayMidiClick(Sender: PObj);
begin
    MidiPlayer.Rewind;
    OpenSaveDialog.Filter:= 'Midi files|*.mid;*.rmi;*.riff';
    if OpenSaveDialog.Execute then
      if MidiPlayer.LoadFromFile(OpenSaveDialog.Filename) then MidiPlayer.Play;
end;

procedure TMainForm.Button1Click(Sender: PObj);
begin
    MidiPlayer.Pause;
end;

procedure TMainForm.Button2Click(Sender: PObj);
begin
    MidiPlayer.Stop;
end;


procedure TMainForm.Button3Click(Sender: PObj);
begin
    MidiPlayer.Play;
end;

procedure TMainForm.btnVerifyAudioClick(Sender: PObj);
var t: TTagVendor;
begin
    OpenSaveDialog.Filter:= 'Audio files|'+GetAudioFileMaskAll;
    if OpenSaveDialog.Execute then begin
      Memo.Text:= '';
      if AudioVerify.VerifyAudio(OpenSaveDialog.Filename) then begin
        ShowAudio(AudioVerify.AudioInfo, OpenSaveDialog.Filename, False);
        for t:= Low(TTagVendor) to High(TTagVendor) do
          if t in AudioVerify.AudioInfo.TagExists then
           if AudioVerify.AudioInfo.TagInfo[t].Exists then
             ShowTag(AudioVerify.AudioInfo.TagInfo[t], OpenSaveDialog.Filename, False);
      end;
    end;
end;

end.
































