Attribute VB_Name = "cdrtools"
Option Explicit

Public cdda2wavPath As String
Public ConvertirDespues As Byte
Public EnProceso As Byte
Public CancelarProceso As Byte
Public OutTrackDir As String
Private ParDevice As String

Public Sub CDRip()
Dim TrackList() As String, i As Byte
If ScriptObj.ExisteArchivo(cdda2wavPath) = False Then
Lang.MsgFileNotFound ("cdda2wav.exe")
Exit Sub
End If

Form1.Visible = False

Load Form2
With Form2
.Frame4.Left = .Frame1.Left
.Frame4.Top = .Frame1.Top
.Command2.Visible = False
.Command3.Visible = True
.Command3.Left = .Command2.Left
.Command3.Top = .Command2.Top
End With

Lang.StringCDRipObtainTrackList
EnProceso = 1
Form2.Show
TrackList = ObtenerTrackList
EnProceso = 0

If CancelarProceso = 1 Then
CancelarProceso = 0
Unload Form2
Exit Sub
End If

If TrackList(1) = "" Then
Lang.MsgCDRipCanotObtTtrackList
Unload Form2
Exit Sub
End If

With Form2
For i = 1 To UBound(TrackList): .List1.AddItem TrackList(i): Next
.Combo1.ListIndex = 0
If ConvertirDespues = 1 Then
.Text1.Enabled = False
.Combo1.Enabled = False
.Command1.Enabled = False
.IconoCtrl1.Visible = False
.IconoCtrl2.Visible = True
End If
.Frame4.Visible = False
.Text2.Text = ""
.Command3.Visible = False
.Command2.Visible = True
End With
End Sub

Private Function ObtenerTrackList() As String()
'Devuelve una lista con los títulos de los tracks contenidos en el CD
'Devuelve una lista vacía si no se pudo obtener la información
Dim Ret() As String, Comandos As String, ConsoleOut As String, i As Byte
Dim Posicion1 As Long, Posicion2 As Long, NumTrack As String, k As Integer
Dim TrackTitle As String, Unidades() As String, Encontradas As Byte
ReDim Ret(1 To 1)
Unidades = BuscarDisco()
If Unidades(1) = "" Then GoTo Salir
Encontradas = 0
k = 1
While Encontradas = 0 And k <= UBound(Unidades)
Comandos = Chr(34) & cdda2wavPath & Chr(34) & " -N -D " & Unidades(k)
Call CaptureConsole.ExecAndCapture(Comandos, , , , , , , ConsoleOut)
For i = 1 To 99
NumTrack = Trim(CStr(i))
If Len(NumTrack) = 1 Then NumTrack = " " & NumTrack
Posicion1 = InStr(1, ConsoleOut, vbCrLf & "Track " & NumTrack & ": '")
If Posicion1 > 0 Then
Posicion1 = Posicion1 + 13
Posicion2 = InStr(Posicion1, ConsoleOut, "'")
If Posicion2 > 0 And Posicion2 > Posicion1 Then TrackTitle = Mid(ConsoleOut, Posicion1, Posicion2 - Posicion1) Else TrackTitle = "Track " & NumTrack
ReDim Preserve Ret(1 To i)
Ret(i) = TrackTitle
Else
Exit For
End If
Next
If i > 1 Then
Encontradas = 1
ParDevice = " -D " & Unidades(k)
End If
k = k + 1
Wend
Salir:
ObtenerTrackList = Ret
End Function

Public Sub CDExtract()
Dim Comandos As String, DirectorioSalida As String, i As Byte
Dim SelectedTracks As Byte, TrackName As String, FileList() As String
SelectedTracks = 0
For i = 0 To Form2.List1.ListCount - 1
If Form2.List1.Selected(i) = True Then
SelectedTracks = 1
Exit For
End If
Next
If SelectedTracks = 0 Then
Lang.MsgCDRipNoTracksSelect
Exit Sub
End If
If Form2.Text1.Enabled = True Then
If OutTrackDir = "" Then
Lang.MsgCDRipGetOutDir
Exit Sub
End If
DirectorioSalida = OutTrackDir & "\CDRip"
DirectorioSalida = ScriptObj.ResolverConflictoDeNombres(DirectorioSalida)
If ScriptObj.CrearCarpeta(DirectorioSalida) = False Then
Lang.MsgCanotCreateDir
Exit Sub
End If
Else
DirectorioSalida = Aplicacion.CrearDirectorioTemporal
If DirectorioSalida = "" Then Exit Sub
End If
Comandos = Chr(34) & cdda2wavPath & Chr(34) & ParDevice & " -B -x -s -H -no-infofile -O " & Form2.Combo1.List(Form2.Combo1.ListIndex)
If Form2.Check1.Value = 1 Then Comandos = Comandos & " -w"
Form2.Frame4.Visible = True
Form2.Command2.Visible = False
Form2.Command3.Visible = True
EnProceso = 1
For i = 0 To Form2.List1.ListCount - 1
If Form2.List1.Selected(i) = True Then
Call CaptureConsole.ExecAndCapture(Comandos & " -t " & CStr(i + 1) & "+" & CStr(i + 1), DirectorioSalida, , , Form2.Text2, , 1, , 0)
If ScriptObj.ExisteArchivo(DirectorioSalida & "\Audio" & "." & Form2.Combo1.List(Form2.Combo1.ListIndex)) = True Then
TrackName = ScriptObj.NombreBase(ScriptObj.ResolverConflictoDeNombres(DirectorioSalida & "\" & Form2.List1.List(i) & "." & Form2.Combo1.List(Form2.Combo1.ListIndex)))
If ScriptObj.CambiarNombreBaseArchivo(DirectorioSalida & "\Audio" & "." & Form2.Combo1.List(Form2.Combo1.ListIndex), TrackName) = False Then
Lang.MsgCanotRenameFile (DirectorioSalida & "\Audio" & "." & Form2.Combo1.List(Form2.Combo1.ListIndex))
CancelarProceso = 1
End If
End If
If CancelarProceso = 1 Then GoTo Finaliza
End If
Next
If Form2.Text1.Enabled = False Then
FileList = ScriptObj.ListarArchivosDeDir(DirectorioSalida, Form2.Combo1.List(Form2.Combo1.ListIndex) & ":")
If FileList(1) <> "" Then
For i = 1 To UBound(FileList)
Aplicacion.InFilesCount = Aplicacion.InFilesCount + 1
ReDim Preserve Aplicacion.InFiles(1 To Aplicacion.InFilesCount)
ReDim Preserve Aplicacion.InFilesMap(1 To Aplicacion.InFilesCount)
Aplicacion.InFiles(Aplicacion.InFilesCount) = FileList(i)
Next
Aplicacion.RefreshInListBox
EnProceso = 0
CancelarProceso = 0
Unload Form2
Exit Sub
End If
End If
Finaliza:
Form2.Frame4.Visible = False
Form2.Command2.Visible = True
Form2.Command3.Visible = False
EnProceso = 0
CancelarProceso = 0
End Sub

Private Function BuscarDisco() As String()
Dim BufferConsola As String, Comandos As String
Dim Devices() As String, k As Integer, NoMasD As Byte, i As Integer
Dim Posiciones() As Long, CDDevices() As String, j As Integer
Dim Posicion1 As Long, Posicion2 As Long
ReDim BuscarDisco(1 To 1)
ParDevice = ""
Comandos = Chr(34) & cdda2wavPath & Chr(34) & " -scanbus"
Call CaptureConsole.ExecAndCapture(Comandos, , , , , , , BufferConsola)
If BufferConsola = "" Then Exit Function
Comandos = vbNullString
NoMasD = 0
k = 0
i = 0
While NoMasD = 0
Posicion1 = InStr(1, BufferConsola, vbCrLf & "scsibus" & Trim(CStr(i)) & ":" & vbCrLf)
If Posicion1 > 0 Then
k = k + 1
ReDim Preserve Posiciones(1 To k)
Posiciones(k) = Posicion1
Else
NoMasD = 1
End If
i = i + 1
Wend
If k = 0 Then
Exit Function
Else
k = k + 1
ReDim Preserve Posiciones(1 To k)
Posiciones(k) = Len(BufferConsola)
End If
ReDim Devices(1 To UBound(Posiciones) - 1)
For k = LBound(Posiciones) To UBound(Posiciones) - 1
Devices(k) = Mid(BufferConsola, Posiciones(k), Posiciones(k + 1) - Posiciones(k))
Next
BufferConsola = vbNullString
Erase Posiciones
j = 0
For k = LBound(Devices) To UBound(Devices)
If InStr(1, Devices(k), "Removable CD-Rom", vbTextCompare) > 0 Then
Posicion1 = InStr(1, Devices(k), vbTab)
If Posicion1 > 0 Then
Posicion1 = Posicion1 + 1
Posicion2 = InStr(Posicion1, Devices(k), vbTab)
If Posicion2 > 0 Then
j = j + 1
ReDim Preserve CDDevices(1 To j)
CDDevices(j) = Mid(Devices(k), Posicion1, Posicion2 - Posicion1)
End If
End If
End If
Next
If j = 0 Then Exit Function
BuscarDisco = CDDevices
End Function
