unit fdbtPluginMain;

interface

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



var
  // ジャンル情報
  GenreListData: TTagListData;        

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



  FDBT_FIELD_VENDOR      = 'Vendor';

  FDBT_FIELD_LENGTH      = 'length';
  FDBT_FIELD_BITRATE     = 'bitrate';
  FDBT_FIELD_SAMPLERATE  = 'SampleRate';
  FDBT_FIELD_CHANNELMODE = 'ChannelMode';

  FDBT_FIELD_RATIO       = 'Ratio';
  FDBT_FIELD_ENCODER     = 'Encoder';



  FDBTP_FIELDCOUNT = 15;
  FDBTP_FIELDS: array[0..FDBTP_FIELDCOUNT-1] of TTagField = (
        (
        Name:        FDBT_FIELD_TITLE;
        DisplayName: 'タイトル';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ARTIST;
        DisplayName: 'アーティスト';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_ALBUM;
        DisplayName: 'アルバム';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_TRACK;
        DisplayName: 'トラック';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_DURATION;
        DisplayName: '演奏時間';
        ValueType:   FDBT_VALUE_TYPE_DOUBLE;
        Editable:    False;      Visible: False;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_GENRE;
        DisplayName: 'ジャンル';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_YEAR;
        DisplayName: '日付';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_COMMENT;
        DisplayName: 'コメント';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    True;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_VENDOR;
        DisplayName: 'ベンダー';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),

        (
        Name:        FDBT_FIELD_ENCODER;
        DisplayName: 'エンコーダ';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        Editable:    False;      Visible: True;
        Data:        nil;
        ),
        (
        Name:        FDBT_FIELD_LENGTH;
        DisplayName: '時間';
        ValueType:   FDBT_VALUE_TYPE_STRING;
        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_CHANNELMODE;
        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
  ov: TOggVorbis;
begin
  ov:=TOggVorbis.Create;

  if ov.ReadFromFile(FileName) then
  begin                                          
    SetTagValue(Tag^[0], FDBT_FIELD_TITLE,    ov.Title);
    SetTagValue(Tag^[1], FDBT_FIELD_ARTIST,   ov.Artist);
    SetTagValue(Tag^[2], FDBT_FIELD_ALBUM,    ov.Album);
    SetTagValue(Tag^[3], FDBT_FIELD_TRACK,    IntToStr(ov.Track));
    SetTagValue(Tag^[4], FDBT_FIELD_YEAR,     ov.Date);
    SetTagValue(Tag^[5], FDBT_FIELD_GENRE,    ov.Genre);
    SetTagValue(Tag^[6], FDBT_FIELD_COMMENT,  ov.Comment);
    SetTagValue(Tag^[7], FDBT_FIELD_DURATION, ov.Duration);

    SetTagValue(Tag^[8],  FDBT_FIELD_VENDOR,  ov.Vendor);
    SetTagValue(Tag^[9],  FDBT_FIELD_LENGTH,  DurationToStr(Round(ov.Duration), False));
    SetTagValue(Tag^[10], FDBT_FIELD_BITRATE, IntToStr(ov.Bitrate)+' kbit/s');
    SetTagValue(Tag^[11], FDBT_FIELD_SAMPLERATE,  IntToStr(ov.SampleRate)+' hz');
    SetTagValue(Tag^[12], FDBT_FIELD_CHANNELMODE, ov.ChannelMode);
    SetTagValue(Tag^[13], FDBT_FIELD_RATIO,    Format('%f %%', [ov.Ratio]));
    SetTagValue(Tag^[14], FDBT_FIELD_ENCODER,  ov.Encoder);

    Result:=16;
  end else
    Result:=0;

  ov.Free;
end;

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

  ov:=TOggVorbis.Create;
  c:=Length(Tag^);

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

    if fld=FDBT_FIELD_TITLE   then ov.Title    := v;
    if fld=FDBT_FIELD_ARTIST  then ov.Artist   := v;
    if fld=FDBT_FIELD_ALBUM   then ov.Album    := v;
    if fld=FDBT_FIELD_TRACK   then ov.Track    := StrToIntDef(v, 0);
    if fld=FDBT_FIELD_GENRE   then ov.Genre    := v;
    if fld=FDBT_FIELD_YEAR    then ov.Date     := v;
    if fld=FDBT_FIELD_COMMENT then ov.Comment  := v;
  end;

  if ov.SaveTag(FileName) then
    Result:=FDBT_SUCCESS
  else
    Result:=FDBT_INTERNAL_ERROR;

  ov.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.
