  option fieldsep,|
  DIALOG CREATE,ID3 Tag Editor,-1,0,394,305
  DIALOG ADD,TEXT,tFilename,8,8,,,Filename:
  DIALOG ADD,EDIT,eFilename,8,88,264,24,,,readonly
  DIALOG ADD,BUTTON,bFilename,10,360,24,18,...
  DIALOG ADD,TEXT,tTitle,40,8,,,Title:
  DIALOG ADD,EDIT,eTitle,40,88,296,24
  DIALOG ADD,TEXT,tArtist,72,8,,,Artist:
  DIALOG ADD,EDIT,eArtist,72,88,296,24
  DIALOG ADD,TEXT,tAlbum,104,8,,,Album:
  DIALOG ADD,EDIT,eAlbum,104,88,296,24
  DIALOG ADD,TEXT,tYear,136,8,,,Year:
  DIALOG ADD,EDIT,eYear,136,88,296,24
  DIALOG ADD,TEXT,tComment,168,8,,,Comment:
  DIALOG ADD,EDIT,eComment,168,88,296,24
  DIALOG ADD,TEXT,tTrack,200,8,,,Track:
  DIALOG ADD,EDIT,eTrack,200,88,296,24
  DIALOG ADD,TEXT,tGenre,232,8,,,Genre:
  DIALOG ADD,EDIT,eGenre,232,88,296,24
  DIALOG ADD,BUTTON,bWrite,272,288,96,24,&Write changes
  DIALOG SHOW
  %%offset =
:evloop
  wait event
  goto @event()
:bFilenamebutton
  %%file = @filedlg("MP3 Files|*.mp3|All Files (*.*)|*.*",Select MP3 file)
  if %%file
    if %%offset
      binfile close,4
    end
    dialog set,eFilename,%%file
    binfile open,4,%%file
    %%offset = @diff(@file(%%file,Z),128)
    gosub readid3
    if @not(%%tag)
      %%offset = @sum(%%offset,128)
    end
    parse "eTitle;eArtist;eAlbum;eYear;eComment;eTrack;eGenre",%%title|%%artist|%%album|%%year|%%comment|%%track|%%genre
  end
  goto evloop
:bWritebutton
  if %%offset
    parse "%%title;%%artist;%%album;%%year;%%comment;%%track;%%genre",@dlgtext(eTitle)|@dlgtext(eArtist)|@dlgtext(eAlbum)|@dlgtext(eYear)|@dlgtext(eComment)|@dlgtext(eTrack)|@dlgtext(eGenre)
    gosub writeid3
  end
  goto evloop
:close
  if %%offset
    binfile close,4
  end
  exit

  rem ID3v1.0
  rem "TAG"       =    3 bytes
  rem song title  =   30 bytes
  rem artist      =   30 bytes
  rem album       =   30 bytes
  rem year        =    4 bytes
  rem comment     =   30 bytes
  rem genre       =    1 byte
  rem               ----------- +
  rem total       =  128 bytes 

  rem ID3v1.1
  rem "TAG"       =    3 bytes
  rem song title  =   30 bytes
  rem artist      =   30 bytes
  rem album       =   30 bytes
  rem year        =    4 bytes
  rem comment     =   28 bytes
  rem @chr(0)     =    1 byte
  rem album track =    1 byte
  rem genre       =    1 byte
  rem               ----------- +
  rem total       =  128 bytes 

:readid3
  rem Inputs:
  rem   - %%offset
  rem   - binfile 4
  rem Outputs:
  rem   - %%tag; %%title; %%artist; %%album; %%year; %%comment; %%track; %%genre

  binfile seek,4,%%offset
  %%tag = @binfile(read,4,text,3)
  if @equal(%%tag,TAG)
    %%max = 30
    gosub read
    %%title = %%result
    if @greater(%%max,%%count)
      %%dummy = @binfile(read,4,text,@diff(%%max,%%count))
    end

    gosub read
    %%artist = %%result
    if @greater(%%max,%%count)
      %%dummy = @binfile(read,4,text,@diff(%%max,%%count))
    end

    gosub read
    %%album = %%result
    if @greater(%%max,%%count)
      %%dummy = @binfile(read,4,text,@diff(%%max,%%count))
    end

    %%max = 4
    gosub read
    %%year = %%result
    if @greater(%%max,%%count)
      %%dummy = @binfile(read,4,text,@diff(%%max,%%count))
    end

    %%max = 30
    gosub read
    %%comment = %%result
    if @greater(%%max,%%count)
      %%dummy = @binfile(read,4,text,@diff(@pred(%%max),%%count))
      %%track = @substr(@binfile(read,4,binary,1),1,-1)
      if @equal(%%track,0)
        %%track =
      end
    end
    
    %%genre = @substr(@binfile(read,4,binary,1),1,-1)
  else
    %%tag     =
    %%title   =
    %%artist  =
    %%album   =
    %%year    =
    %%comment =
    %%track   =
    %%genre   =
  end
  exit
:read
  %i = 0
  %%result =
  %%count = 0
  repeat
    %i = @succ(%i)
    %%char = @substr(@binfile(read,4,binary,1),1,-1)
    %%count = @succ(%%count)
    if @not(@equal(%%char,0))
      %%result = %%result@chr(%%char)
    end
  until @equal(%i,%%max)@equal(%%char,0)
  %%result = @trim(%%result)
  exit
:writeid3
  rem Inputs:
  rem   - %%offset; %%title; %%artist; %%album; %%year; %%comment; %%track; %%genre
  rem   - binfile 4
  rem Outputs:
  rem   - binfile 4

  binfile seek,4,%%offset
  binfile write,4,text,TAG

  %%data = %%title
  %%length = 30
  gosub write

  %%data = %%artist
  %%length = 30
  gosub write

  %%data = %%album
  %%length = 30
  gosub write

  %%data = %%year
  %%length = 4
  gosub write

  if %%track
    %%data = %%comment
    %%length = 28
    gosub write

    binfile write,4,binary,0|
    binfile write,4,binary,%%track|
  else
    %%data = %%comment
    %%length = 30
    gosub write
  end
  if @not(%%genre)
    %%genre = 255
  end
  binfile write,4,binary,%%genre|

  exit
:write
  if @substr(%%data,1,%%length)
    binfile write,4,text,@substr(%%data,1,%%length)
  end
  %%length = @diff(%%length,@len(%%data))
  if @greater(%%length,0)
    repeat
      binfile write,4,binary,0|
      %%length = @pred(%%length)
    until @equal(%%length,0)
  end
  exit
