VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsID3"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "Allows manipulation of the ID3"
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Private mvarFilename As String
Private mvarArtist As String
Private mvarTitle As String
Private mvarAlbum As String
Private mvarComment As String
Private mvarGenre As Integer
Private mvarYear As String
Private tagData As String * 128
Public isActive As Boolean

Public Property Let Genre(ByVal vData As Integer)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If vData < 0 Or vData > 255 Then vData = 255
    Mid(tagData, 128, 1) = Chr(vData)
    mvarGenre = vData
End Property

Public Property Get Genre() As Integer
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Genre = mvarGenre
End Property



Public Property Let Comment(ByVal vData As String)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
    If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
    Mid(tagData, 98, 30) = vData
    mvarComment = vData
End Property

Public Property Get Comment() As String
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Comment = mvarComment
End Property



Public Property Let Album(ByVal vData As String)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
    If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
    Mid(tagData, 64, 30) = vData
    mvarAlbum = vData
End Property

Public Property Get Album() As String
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Album = mvarAlbum
End Property



Public Property Let Title(ByVal vData As String)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
    If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
    Mid(tagData, 4, 30) = vData
    mvarTitle = vData
End Property

Public Property Get Title() As String
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Title = mvarTitle
End Property



Public Property Let Artist(ByVal vData As String)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
    If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
    Mid(tagData, 34, 30) = vData
    mvarArtist = vData
End Property

Public Property Get Artist() As String
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Artist = mvarArtist
End Property



Public Property Let Filename(ByVal vData As String)
    On Error Resume Next
    Dim fileNum As Long, filePos As Long
    
    If Len(Dir(vData)) = 0 Then
        GoTo WRONG
    Else
        If UCase(Right(vData, 4)) <> ".MP3" Then GoTo WRONG
        fileNum = FreeFile
        filePos = FileLen(vData) - 127
        If filePos > 0 Then
            Open vData For Binary As #fileNum
                Get #fileNum, filePos, tagData
            Close #fileNum
            If Left(tagData, 3) <> "TAG" Then GoTo WRONG
            
            mvarTitle = Replace(Trim(Mid(tagData, 4, 30)), Chr(0), "")
            mvarArtist = Replace(Trim(Mid(tagData, 34, 30)), Chr(0), "")
            mvarAlbum = Replace(Trim(Mid(tagData, 64, 30)), Chr(0), "")
            mvarYear = Replace(Trim(Mid(tagData, 94, 4)), Chr(0), "")
            mvarComment = Replace(Trim(Mid(tagData, 98, 30)), Chr(0), "")
            mvarGenre = Asc(Mid(tagData, 128, 1))
        Else
            GoTo WRONG
        End If
    End If
    
    isActive = True
    mvarFilename = vData
    Exit Property
WRONG: ' Jumps here if bad file
    isActive = False
    mvarFilename = ""
End Property

Public Property Get Filename() As String
    Filename = mvarFilename
End Property



Public Property Let Year(ByVal vData As String)
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    If Len(vData) > 4 Then vData = Left(vData, 4) 'If too big, trim to 30
    If Len(vData) < 4 Then vData = vData & String(4 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
    Mid(tagData, 94, 4) = vData
    mvarYear = vData
End Property

Public Property Get Year() As String
    If (Len(Dir(mvarFilename))) = 0 Then Exit Property
    Year = mvarYear
End Property



Public Function SaveTag() As String
    Dim errDesc As String ' Error description
    Dim fileNum As Long, filePos As Long
    Dim fileData As String
    
    If Len(Dir(mvarFilename)) = 0 Then
        errDesc = "File not found"
        GoTo WRONG
    Else
        If UCase(Right(mvarFilename, 4)) <> ".MP3" Then errDesc = "Wrong file extension. Must be .mp3": GoTo WRONG
        fileNum = FreeFile
        fileData = String(FileLen(mvarFilename), Chr(0)) 'Buffer
        
        If FileLen(mvarFilename) > 128 Then
            Open mvarFilename For Binary As #fileNum
                Get #fileNum, 1, fileData
            Close #fileNum
            If Left(Right(fileData, 128), 3) <> "TAG" Then GoTo WRONG
            
            Mid(fileData, Len(fileData) - 127) = tagData

            fileNum = FreeFile
            Open mvarFilename For Binary As #fileNum
                Put #fileNum, 1, fileData
            Close #fileNum
        Else
            GoTo WRONG
        End If
    End If
    
    Exit Function
WRONG: 'Go here if error writing to file
    isActive = False
    mvarFilename = ""
    SaveTag = errDesc
End Function
