VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsTitleAudioInfo"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' -------------------------------------------------------------------------------
'
'   AVStoDVD - by MrC
'   File: clsTitleAudioInfo.cls
'   Purpose: Class that handle Title Info properties
'   Requires: clsMediaInfo.cls, modStr.bas
'
' -------------------------------------------------------------------------------


Option Explicit
Option Compare Binary
Option Base 0


Public Enum DVD_Audio_Compl
    Not_Audio_Compl
    DTS_Compl
    AC3_Compl
    MP2_Compl
    WAV_Compl
End Enum


Private AudioPathname As String
Private AudioID As String
Private AudioStreamOrder As String          'for Audio Demuxing: FFmpeg and MKVexctract use different tracks numbering
Private AudioTime As String
Private AudioCompression As String
Private AudioBitRateMode As String
Private AudioAvgBitRate As String
Private AudioChannels As String
Private AudioSampleRate As String
Private AudioResolution As String
Private AudioLanguage As String
Private AudioDelay As String


Public Function Initialize(SourceMediaInfo As clsMediaInfo, Optional AudioTrack As Integer) As Boolean

    Dim aStream
    
    With SourceMediaInfo
        AudioPathname = .PathName
        AudioID = .AudioID(AudioTrack)
        If Val(.VideoTracksCount) = 0 Then
            AudioStreamOrder = "0"
        Else
            If IsNumeric(.AudioStreamOrder(AudioTrack)) = True Then
                AudioStreamOrder = .AudioStreamOrder(AudioTrack)
            ElseIf InStr(1, .AudioStreamOrder(AudioTrack), "-") <> 0 Then
                aStream = Split(.AudioStreamOrder(AudioTrack), "-")
                If IsNumeric(aStream(UBound(aStream))) = True Then
                    AudioStreamOrder = aStream(UBound(aStream))
                End If
            End If
        End If
        AudioTime = .AudioTime(AudioTrack)
        AudioCompression = .AudioCompression(AudioTrack)
        AudioBitRateMode = .AudioBitRateMode(AudioTrack)
        AudioAvgBitRate = .AudioAvgBitRate(AudioTrack)
        AudioChannels = .AudioChannels(AudioTrack)
        AudioSampleRate = .AudioSampleRate(AudioTrack)
        AudioResolution = .AudioResolution(AudioTrack)
        AudioLanguage = .AudioLanguage(AudioTrack)
        AudioDelay = .AudioDelay(AudioTrack)
    End With
    Initialize = CBool(Len(AudioCompression))

End Function

Public Property Get ID() As String

    'Dim aID
    
    'aID = Split(AudioID, "-")
    'If UBound(aID) > 0 Then
    '    ID = aID(UBound(aID))
    'Else
        ID = AudioID
    'End If

End Property
    
Public Property Let ID(value As String)

    AudioID = value
    
End Property

Public Property Get StreamOrder() As String

    StreamOrder = AudioStreamOrder

End Property
    
Public Property Let StreamOrder(value As String)

    AudioStreamOrder = value
    
End Property

Public Property Get Time() As String

    Time = AudioTime

End Property
    
Public Property Let Time(value As String)

    AudioTime = value
    
End Property

Public Property Get Compression() As String

    Compression = AudioCompression

End Property
    
Public Property Let Compression(value As String)

    AudioCompression = value
    
End Property

Public Property Get BitRateMode() As String

    BitRateMode = AudioBitRateMode

End Property
    
Public Property Let BitRateMode(value As String)

    AudioBitRateMode = value
    
End Property

Public Property Get AvgBitRate() As String

    AvgBitRate = AudioAvgBitRate

End Property
    
Public Property Let AvgBitRate(value As String)

    AudioAvgBitRate = value
    
End Property

Public Property Get Channels() As String

    Channels = AudioChannels

End Property
    
Public Property Let Channels(value As String)

    AudioChannels = value
    
End Property

Public Property Get SampleRate() As String

    SampleRate = AudioSampleRate

End Property
    
Public Property Let SampleRate(value As String)

    AudioSampleRate = value
    
End Property

Public Property Get Resolution() As String

    Resolution = AudioResolution

End Property
    
Public Property Let Resolution(value As String)

    AudioResolution = value
    
End Property

Public Property Get Language() As String

    Language = AudioLanguage

End Property
    
Public Property Let Language(value As String)

    AudioLanguage = value
    
End Property

Public Property Get LanguageCode() As String

    If Len(AudioLanguage) >= 2 Then
        LanguageCode = UCase(Left(AudioLanguage, 2))
    End If

End Property
    
Public Property Get Delay() As String

    Delay = AudioDelay

End Property
    
Public Property Let Delay(value As String)

    AudioDelay = value
    
End Property

Public Property Get Summary() As String
                        
    If AudioCompression <> "" Then
        Summary = AudioCompression & " - " & _
                  Round(Val(AudioAvgBitRate) / 1000) & " kbps - " & _
                  AudioBitRateMode & " - " & _
                  AudioChannels & " ch - " & _
                  AudioSampleRate & " Hz - " & _
                  IIf(AudioResolution = "", "", AudioResolution & " bit - ") & _
                  VisualTime(Val(AudioTime) / 1000) & " (" & _
                  Int(Val(AudioDelay)) & " ms delay)" & _
                  IIf(LanguageCode = "", "", " - " & LanguageCode)
    End If
    
End Property
                        
Public Property Get SummaryLong() As String
                        
    If AudioCompression <> "" Then
        If AudioID <> "" Then
            SummaryLong = "Track ID: " & AudioID & vbCrLf & _
                          "Stream Order: " & AudioStreamOrder & vbCrLf
        End If
        SummaryLong = SummaryLong & _
                      "Time: " & VisualTime(Val(AudioTime / 1000), True, True) & vbCrLf & _
                      "Compression: " & AudioCompression & vbCrLf & _
                      "BitRate Mode: " & AudioBitRateMode & vbCrLf & _
                      "Avg BitRate: " & IIf(AudioAvgBitRate = "", "", Round(Val(AudioAvgBitRate) / 1000) & " kbps") & vbCrLf & _
                      "Channels: " & AudioChannels & vbCrLf & _
                      "SampleRate: " & AudioSampleRate & " Hz" & vbCrLf & _
                      "Resolution: " & IIf(AudioResolution = "", "", AudioResolution & " bit") & vbCrLf & _
                      "Language: " & AudioLanguage & vbCrLf & _
                      "Delay: " & IIf(AudioDelay = "", "", AudioDelay & " ms")
    End If

End Property

Public Property Get DVDCompliant() As DVD_Audio_Compl

    'http://en.wikipedia.org/wiki/DVD-Video#Audio_data

    Dim BitRate_Array As Variant
    Dim jj As Integer
    Dim bFlag As Boolean
    
    If AudioBitRateMode = "CBR" Then
        Select Case UCase(Extension)
        Case "DTS"
            Select Case Val(AudioSampleRate)
            Case 48000, 96000
                Select Case AudioChannels
                Case "2"
                    BitRate_Array = Array(377250, 503250)
                    DVDCompliant = DTS_Compl
                Case "5", "6"
                    BitRate_Array = Array(754500, 1509750)
                    DVDCompliant = DTS_Compl
                End Select
            End Select
        Case "AC3"
            If Val(AudioSampleRate) = 48000 Then
                Select Case AudioChannels
                Case "1", "2", "5", "6"
                    BitRate_Array = Array(64000, 96000, 128000, 160000, 192000, 224000, 256000, 384000, 448000)
                    DVDCompliant = AC3_Compl
                End Select
            End If
        Case "MP2"
            If Val(AudioSampleRate) = 48000 Then
                Select Case AudioChannels
                Case "1", "2"
                    BitRate_Array = Array(64000, 96000, 128000, 160000, 192000, 224000, 256000)
                    DVDCompliant = MP2_Compl
                End Select
            End If
        Case "WAV"
            If Val(AudioSampleRate) = 48000 Then
                Select Case AudioChannels
                Case "1", "2"
                    BitRate_Array = Array(768000, 1536000)
                    DVDCompliant = WAV_Compl
                End Select
            End If
        End Select
        If IsEmpty(BitRate_Array) = False Then
            For jj = LBound(BitRate_Array) To UBound(BitRate_Array)
                If Val(AudioAvgBitRate) = BitRate_Array(jj) Then
                    bFlag = True
                End If
            Next jj
        End If
    End If
    If bFlag = False Then DVDCompliant = Not_Audio_Compl
    
End Property

Public Property Get Extension() As String

    Dim sCompr As String
    Dim jj As Integer
    Dim bFlag As Boolean
    
        sCompr = UCase(AudioCompression)
        If InStr(1, sCompr, "AAC") <> 0 Then
            Extension = "aac"
        ElseIf InStr(1, sCompr, "MPEG") <> 0 And InStr(1, sCompr, "LAYER 2") <> 0 Then
            Extension = "mp2"
        ElseIf InStr(1, sCompr, "MPEG") <> 0 And InStr(1, sCompr, "LAYER 3") <> 0 Then
            Extension = "mp3"
        ElseIf InStr(1, sCompr, "PCM") <> 0 Then
            Extension = "wav"
        ElseIf InStr(1, sCompr, "AMR") <> 0 Then
            Extension = "amr"
        ElseIf InStr(1, sCompr, "VORBIS") <> 0 Then
            Extension = "ogg"
        ElseIf InStr(1, sCompr, "AC3") <> 0 Then
            Extension = "ac3"
        ElseIf InStr(1, sCompr, "DTS") <> 0 Then
            Extension = "dts"
        ElseIf InStr(1, sCompr, "WMA") <> 0 Then
            Extension = "wma"
        Else
            If AudioPathname <> "" Then
                jj = 0
                Do
                    jj = jj + 1
                    Extension = Right(AudioPathname, jj)
                    If Left(Extension, 1) = "." Then
                        Extension = Right(Extension, Len(Extension) - 1)
                        bFlag = True
                    End If
                Loop Until bFlag = True
            End If
        End If

End Function


