unit fdbtPluginMain;

interface

uses Windows, SysUtils, Classes, TagEditorLibConsts, TagEditorLibAddinConsts,
  fdbtPluginUtils, fdbtPlugin, WAVFile;



const
  FDBTP_INFO: array[0..FDBT_AUTHOR] of string = (
        '1.0',
        'WAVE',
        '.wav',
        'のらだいこ'
        );



  FDBT_FIELD_FORMAT      = 'format';
  FDBT_FIELD_LENGTH      = 'length';
  FDBT_FIELD_BITRATE     = 'bitrate';
  FDBT_FIELD_SAMPLERATE  = 'SampleRate';
  FDBT_FIELD_CHANNELS    = 'channels';
  FDBT_FIELD_RATIO       = 'Ratio';


  FDBTP_FIELDCOUNT = 14;
  FDBTP_FIELDS: array[0..FDBTP_FIELDCOUNT-1] of TTagField = (
        (
        Name:        FDBT_FIELD_TITLE;
        DisplayName: 'タイトル';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ARTIST;
        DisplayName: 'アーティスト';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ALBUM;
        DisplayName: 'アルバム';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_TRACK;
        DisplayName: 'トラック';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_GENRE;
        DisplayName: 'ジャンル';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_YEAR;
        DisplayName: '西暦';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_COMMENT;
        DisplayName: 'コメント';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: False;
        Data:        nil;
        ),

        (
        Name:        FDBT_FIELD_DURATION;
        DisplayName: '演奏時間';
        ValueType:   FDBT_VALUE_TYPE_DOUBLE;
        Editable:    False;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_LENGTH;
        DisplayName: '時間';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;     Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_FORMAT;
        DisplayName: 'フォーマット';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_CHANNELS;
        DisplayName: 'チャンネル数';
        ValueType:   FDBT_VALUE_TYPE_INTEGER;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_SAMPLERATE;
        DisplayName: 'サンプルレート';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_BITRATE;
        DisplayName: 'ビットレート';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;     Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_RATIO;
        DisplayName: '圧縮率';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        )
        );


type

TFdbtPluginMain = class(TFdbtPlugin)
private
  FAddinMenu: TAddinMenu;
  FGetTrackDataInfo: TGetTrackDataInfo;
public
  TrackTools: TTrackDataInfo;
  function Initialize(info: PFdbtPluginInitInfo): integer; override;
  function GetLibInfo(InfoNo: Integer; Buffer: PChar; Size: Integer): Integer; override;
  function ShowConfig: integer; override;
  function GetTagInfo(FileName: PWideChar; Tag: PTagInfo; Size: integer): integer; override;
  function SetTagInfo(FileName: PWideChar; Tag: PTagInfo): integer; override;
  function GetTagFieldInfo(fl: PTagFieldList; size: integer): integer; override;

  function AddinMenu(place: integer; Identify: PChar; DisplayName: PChar; ProcExe: TAddinProc; ImageType: integer; Image: cardinal): integer;
  function GetTrackDataInfo: TTrackDataInfo;
end;


implementation



function TFdbtPluginMain.Initialize(info: PFdbtPluginInitInfo): integer;
begin
  inherited Initialize(info);
  @FAddinMenu:=InitInfo.GetService('AddinMenu');
  @FGetTrackDataInfo:=InitInfo.GetService('GetTrackDataInfo');

  TrackTools:=GetTrackDataInfo();

  Result:=FDBT_SUCCESS;
end;


function TFdbtPluginMain.GetLibInfo(InfoNo: Integer; Buffer: PChar; Size: Integer): Integer;
var
  PlugInfo : string;
begin
  PlugInfo:=FDBTP_INFO[InfoNo];
  lstrcpyn(Buffer, PChar(PlugInfo), Size);
  Result:=Length(Buffer);
end;

function TFdbtPluginMain.ShowConfig: integer;
begin
  Result:=FDBT_NOTIMPLEMENTED;
end;

function TFdbtPluginMain.GetTagInfo(FileName: PWideChar; Tag: PTagInfo; Size: integer): integer;
var
  tg: TWAVfile;
begin
  tg:=TWAVfile.Create;

  if tg.ReadFromFile(FileName) then
  begin
      SetTagValue(Tag^[0], FDBT_FIELD_DURATION,     tg.Duration);

      SetTagValue(Tag^[1],  FDBT_FIELD_FORMAT,      tg.Format);
      SetTagValue(Tag^[2],  FDBT_FIELD_CHANNELS,    tg.ChannelNumber);
      SetTagValue(Tag^[3],  FDBT_FIELD_LENGTH,      DurationToStr(Round(tg.Duration), False));
      SetTagValue(Tag^[4], FDBT_FIELD_BITRATE,     Format('%f kbit/s', [(tg.BitsPerSample/1000)]));
      SetTagValue(Tag^[5], FDBT_FIELD_SAMPLERATE,  Format('%d hz', [tg.SampleRate]));
      SetTagValue(Tag^[6], FDBT_FIELD_RATIO,       Format('%f %%', [tg.Ratio]));

    Result:=7;
  end else
    Result:=0;

  tg.Free;
end;

function TFdbtPluginMain.SetTagInfo(FileName: PWideChar; Tag: PTagInfo): integer;
var
  tg: TWAVfile;
  i,c: integer;
  fld, v: string;
begin

  tg:=TWAVfile.Create;
  c:=Length(Tag^);

  for i:=0 to c-1 do
  begin
    fld := Tag^[i].Field;
    v   := UTF8Encode(Value2String(Tag^[i].Value));
  end;

  Result:=FDBT_SUCCESS;
  tg.Free;
end;

function TFdbtPluginMain.GetTagFieldInfo(fl: PTagFieldList; size: integer): integer;
var
  i: integer;
begin
  for i:=0 to FDBTP_FIELDCOUNT-1 do
    fl^[i]:=FDBTP_FIELDS[i];

  Result:=FDBTP_FIELDCOUNT;
end;

function TFdbtPluginMain.AddinMenu(place: integer; Identify: PChar; DisplayName: PChar; ProcExe: TAddinProc; ImageType: integer; Image: cardinal): integer;
begin
  if Assigned(FAddinMenu) then
    Result:=FAddinMenu(place, Identify, DisplayName, ProcExe, ImageType, Image)
  else
    Result:=FDBT_NOTIMPLEMENTED;
end;

function TFdbtPluginMain.GetTrackDataInfo: TTrackDataInfo;
begin
  if Assigned(FGetTrackDataInfo) then Result:=FGetTrackDataInfo();
end;



initialization


end.
