home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mp3osr05.zip / src / tagedit.pas < prev    next >
Pascal/Delphi Source File  |  2000-01-06  |  6KB  |  239 lines

  1. (*
  2.  * tag editing form
  3.  *  (c) 1999 by Alexander Trunov, 2:5069/10, jnc@mail.ru
  4.  *)
  5.  
  6. unit TagEdit;
  7.  
  8. interface
  9.  
  10. uses Dialogs, Objects, Drivers, Views, ID3v1
  11.   ;
  12.  
  13. const
  14.   ResourceSize = 1180; { !!!!!!!!!!!!!! you need to change this if the
  15.                          genre-list has been modified !!!!!!!!!!!!!!!! }
  16.  
  17. type
  18.   PTagEditor = ^TTagEditor;
  19.   TTagEditor = object(TDialog)
  20.     iSongname,
  21.       iArtist,
  22.       iAlbum,
  23.       iYear,
  24.       iComment: PInputLine;
  25.     iGenre: PListBox;
  26.     iCodepage: PRadioButtons;
  27.     constructor Init(AFilename: string);
  28.     destructor Done; virtual;
  29.     procedure HandleEvent(var Event: TEvent); virtual;
  30.   private
  31.     Genres: PStringCollection;
  32.     stream: PDosStream;
  33.     tag: PTagData;
  34.     srcCodepage: Byte;
  35.   end;
  36.  
  37. function MakeGenres: PStringCollection;
  38.  
  39. implementation
  40.  
  41. uses Codepage, StdDlg, MsgBox, DOS, App
  42.   {$IFNDEF fpc}, EditChD{$ENDIF}
  43.   {$IFDEF fpc}, Commands{$ENDIF}
  44.   ;
  45.  
  46. {$I nonfpc.inc}
  47.  
  48. function LoCase(c: Char): Char;
  49. begin
  50.   case c of
  51.     'A'..'Z': c := Char(Byte(c) + (97 - 65));
  52.     'Ç'..'Å': c := Char(Byte(c) + (160 - 128));
  53.     'É'..'ƒ': c := Char(Byte(c) + (224 - 144));
  54.     '≡': c := '±';
  55.   end;
  56.   LoCase := c;
  57. end;
  58.  
  59. function stLoCase(S: string): string;
  60. var
  61.   k: Byte;
  62. begin
  63.   for k := 1 to Length(S) do
  64.     S[k] := locase(S[k]);
  65.   stLocase := S;
  66. end;
  67.  
  68. function MakeGenres: PStringCollection;
  69. var
  70.   res: PResourceFile;
  71.   stream: PDosStream;
  72. begin
  73.   stream := New(PDosStream, Init(ExtractDir(ParamStr(0)) + 'tagger.res',
  74.     stOpenRead));
  75.  
  76.   stream^.Seek(stream^.GetSize - ResourceSize);
  77.   res := New(PResourceFile, Init(stream));
  78.  
  79.   MakeGenres := PStringCollection(res^.Get('genres'));
  80.  
  81.   Dispose(res, Done);
  82. end;
  83.  
  84. constructor TTagEditor.Init(AFilename: string);
  85. var
  86.   R: TRect;
  87. begin
  88.   R.Assign(0, 0, 56, 19);
  89. {$ifndef win32}
  90.   inherited Init(R, 'ID3v1 tag editing (' + stLocase(ExtractFilename(
  91.     AFilename)) + ')');
  92. {$else}
  93.   inherited Init(R, 'ID3v1 tag editing (' + Win2Alt(stLocase(ExtractFilename(
  94.     AFilename))) + ')');
  95. {$endif}
  96.  
  97.   Options := Options or ofCentered;
  98.  
  99.   R.Assign(2, 3, 34, 4);
  100.   iSongname := New(PInputLine, Init(R, 30));
  101.   Insert(iSongname);
  102.   R.Assign(2, 2, 34, 3);
  103.   Insert(New(PLabel, Init(R, ' ~S~ong name:', iSongname)));
  104.  
  105.   R.Assign(2, 6, 34, 7);
  106.   iArtist := New(PInputLine, Init(R, 30));
  107.   Insert(iArtist);
  108.   R.Assign(2, 5, 34, 6);
  109.   Insert(New(PLabel, Init(R, ' A~r~tist:', iArtist)));
  110.  
  111.   R.Assign(2, 9, 34, 10);
  112.   iAlbum := New(PInputLine, Init(R, 30));
  113.   Insert(iAlbum);
  114.   R.Assign(2, 8, 34, 9);
  115.   Insert(New(PLabel, Init(R, ' A~l~bum:', iAlbum)));
  116.  
  117.   R.Assign(2, 12, 34, 13);
  118.   iComment := New(PInputLine, Init(R, 30));
  119.   Insert(iComment);
  120.   R.Assign(2, 11, 34, 12);
  121.   Insert(New(PLabel, Init(R, ' Co~m~ment:', iComment)));
  122.  
  123.   R.Assign(2, 14, 34, 17);
  124.   iCodepage := New(PRadioButtons, Init(R,
  125.     NewSItem('Save ~u~sing 1251 codepage',
  126.     NewSItem('Save us~i~ng Koi8-r codepage',
  127.     NewSItem('Sa~v~e using Alt codepage', nil)))));
  128.   Insert(iCodepage);
  129.  
  130.   R.Assign(35, 3, 54, 13);
  131.   iGenre := New(PListBox, Init(R, 1, nil));
  132.   Insert(iGenre);
  133.   iGenre^.NewList(MakeGenres);
  134.   R.Assign(35, 2, 54, 3);
  135.   Insert(New(PLabel, Init(R, ' ~G~enre:', iGenre)));
  136.  
  137.   R.Assign(42, 14, 48, 15);
  138.   iYear := New(PInputLine, Init(R, 4));
  139.   Insert(iYear);
  140.   R.Assign(34, 14, 41, 15);
  141.   Insert(New(PLabel, Init(R, ' ~Y~ear:', iYear)));
  142.  
  143.   R.Assign(34, 16, 44, 18);
  144.   Insert(New(PButton, Init(R, 'O~K~', cmOK, bfDefault)));
  145.   R.Assign(44, 16, 54, 18);
  146.   Insert(New(PButton, Init(R, '~C~ancel', cmCancel, 0)));
  147.  
  148.   iSongname^.Select;
  149.  
  150.   tag := nil;
  151.   stream := New(PDosStream, Init(AFilename, stOpen));
  152.  
  153.   if stream^.Status <> stOk then
  154.   begin
  155.     MessageBox(#3'An error occured while trying to open the audio-file. ' +
  156.       'Therefore, there is no information to display.',
  157.       nil, mfInformation or mfOkButton);
  158.   end;
  159.  
  160.   tag := New(PTagData, Init(stream));
  161.   if IsThereATag(stream) then
  162.   begin
  163.     tag^.ReadTag;
  164.     with tag^.tag do
  165.     begin
  166.       srcCodepage := DecodeTag(@tag^.tag);
  167.       case srcCodepage of
  168.         cpWin: iCodepage^.Press(0);
  169.         cpKoi: iCodepage^.Press(1);
  170.         cpAlt: iCodepage^.Press(2);
  171.       end;
  172.       iSongname^.Data^ := Songname;
  173.       iArtist^.Data^ := Artist;
  174.       iAlbum^.Data^ := Album;
  175.       iYear^.Data^ := Year;
  176.       iComment^.Data^ := Comment;
  177.       if Genre > $7D then
  178.       begin
  179.         iGenre^.FocusItem(0);
  180.       end
  181.       else
  182.       begin
  183.         iGenre^.FocusItem(Genre + 1);
  184.       end;
  185.     end;
  186.   end;
  187. end;
  188.  
  189. procedure TTagEditor.HandleEvent(var Event: TEvent);
  190. begin
  191.   if Event.What = evCommand then
  192.   begin
  193.     case Event.Command of
  194.       cmOK:
  195.         begin
  196.           with tag^.tag do
  197.           begin
  198.             Songname := iSongname^.Data^;
  199.             Artist := iArtist^.Data^;
  200.             Album := iAlbum^.Data^;
  201.             Comment := iComment^.Data^;
  202.             Year := iYear^.Data^;
  203.             if iGenre^.Focused = 0 then
  204.               Genre := $FF
  205.             else
  206.               Genre := iGenre^.Focused - 1;
  207.             if iCodepage^.Mark(0) then
  208.             begin
  209.               Songname := Alt2Win(Songname);
  210.               Artist := Alt2Win(Artist);
  211.               Album := Alt2Win(Album);
  212.               Comment := Alt2Win(Comment);
  213.             end;
  214.             if iCodepage^.Mark(1) then
  215.             begin
  216.               Songname := Alt2Koi(Songname);
  217.               Artist := Alt2Koi(Artist);
  218.               Album := Alt2Koi(Album);
  219.               Comment := Alt2Koi(Comment);
  220.             end;
  221.           end;
  222.           tag^.WriteTag;
  223.         end;
  224.     end;
  225.   end;
  226.   inherited HandleEvent(Event);
  227. end;
  228.  
  229. destructor TTagEditor.Done;
  230. begin
  231.   if iGenre^.List <> nil then
  232.     Dispose(iGenre^.List, Done);
  233.   Dispose(stream, Done);
  234.   if tag <> nil then
  235.     Dispose(tag, Done);
  236.   inherited Done;
  237. end;
  238.  
  239. end.