VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   7785
   ClientLeft      =   60
   ClientTop       =   405
   ClientWidth     =   13620
   LinkTopic       =   "Form1"
   ScaleHeight     =   7785
   ScaleWidth      =   13620
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text1 
      Height          =   315
      Left            =   60
      TabIndex        =   2
      Text            =   "Text1"
      Top             =   6540
      Width           =   13095
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Generate Samples"
      Height          =   315
      Left            =   4140
      TabIndex        =   1
      Top             =   7200
      Width           =   1995
   End
   Begin Project1.ucFilterList lv 
      Height          =   6195
      Left            =   60
      TabIndex        =   0
      Top             =   120
      Width           =   13215
      _ExtentX        =   23310
      _ExtentY        =   7964
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim fso As New CFileSystem2
Dim cmd As New CCmdOutput
Dim py As String
Dim tts As String
Dim samples As String
Dim selItem As ListItem

Private Const SND_APPLICATION = &H80         '  look for application specific association
Private Const SND_ALIAS = &H10000     '  name is a WIN.INI [sounds] entry
Private Const SND_ALIAS_ID = &H110000    '  name is a WIN.INI [sounds] entry identifier
Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_LOOP = &H8         '  loop the sound until next sndPlaySound
Private Const SND_MEMORY = &H4         '  lpszSoundName points to a memory file
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found
Private Const SND_NOSTOP = &H10        '  don't stop any currently playing sound
Private Const SND_NOWAIT = &H2000      '  don't wait if the driver is busy
Private Const SND_PURGE = &H40               '  purge non-static events for task
Private Const SND_RESOURCE = &H40004     '  name is a resource name or atom
Private Const SND_SYNC = &H0         '  play synchronously (default)

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long



Private Sub Command1_Click()
    Dim li As ListItem, cmdline As String, mp4 As String, wav As String
    Dim ffmpeg As String
    
    If Len(py) = 0 Or Len(tts) = 0 Or Len(samples) = 0 Then
        MsgBox "Not configured to gen samples"
        Exit Sub
    End If
    
    ffmpeg = fso.GetParentFolder(tts) & "\ffmpeg\bin\ffmpeg.exe"
    If Not fso.FileExists(ffmpeg) Then
        Debug.Print "Can not convert to wav"
    End If
    
    'tts.py voice outfile "text to speak"
    'ffmpeg -i input.mp4 output.wav
    For Each li In lv.ListItems
        
        cmdline = """" & tts & """ " & li.Text & " """ & samples & "\" & li.Text & ".mp4"" " & """I am not sure what to say but I think you can hear me now"""
        cmd.GetCommandOutput py, cmdline
        Debug.Print li.Text & " " & cmd.result
        
        mp4 = samples & "\" & li.Text & ".mp4"
        wav = Replace(mp4, ".mp4", ".wav")
        
        If fso.FileExists(mp4) Then
            cmdline = "-i """ & mp4 & """ """ & wav & """"
            cmd.GetCommandOutput ffmpeg, cmdline
            
            If fso.FileExists(wav) Then
                If FileLen(wav) = 0 Then
                    Kill wav
                Else
                    Kill mp4
                End If
            End If
        End If
        
    Next
        
    MsgBox "complete"
    
        
End Sub

Function playwav(fpath)
    PlaySound fpath, ByVal 0&, SND_FILENAME Or SND_ASYNC
End Function

Private Sub lv_DblClick()
    Dim wav As String
    
    If Not fso.FolderExists(samples) Then
        MsgBox "No sample folder? " & samples
        Exit Sub
    End If
    
    If selItem Is Nothing Then
        MsgBox "Nothing selected"
        Exit Sub
    End If
    
    wav = samples & "\" & selItem.Text & ".wav"
    If Not fso.FileExists(wav) Then
        MsgBox "Could not find desired sample: " & wav
        Exit Sub
    End If
    
    playwav wav
    
    
End Sub

Private Sub Form_Load()
    Dim tmp, tt, a As Long, b As Long, x() As String, li As ListItem
    
    tmp = App.path & "\..\dump_voices.py"
    If Not fso.FileExists(CStr(tmp)) Then
        tmp = App.path & "\dump_voices.py"
        If Not fso.FileExists(CStr(tmp)) Then
            MsgBox "Could not find dump_voices.py"
            End
        End If
    End If
    
    samples = App.path & "\samples"
    If Not fso.FolderExists(samples) Then samples = App.path & "\voices\samples"

    tts = fso.GetParentFolder(tmp) & "\tts.py" 'text to speech script
    
    py = "c:\py311\python.exe" 'just for me for clip generation..
   
    If Not fso.FileExists(py) Then py = Empty
    If Not fso.FileExists(tts) Then tts = Empty
    If Not fso.FolderExists(samples) Then samples = Empty
    
    tmp = fso.ReadFile(tmp)
    a = InStr(1, tmp, "Rachel")
    If a < 1 Then Exit Sub
    b = InStr(a, tmp, String(3, """"))
    If b < 1 Then Exit Sub
   
'   "labels": {
'        "accent": "american",
'        "description": "calm",
'        "age": "young",
'        "gender": "female",
'        "use case": "narration"
'    },
        
    lv.SetColumnHeaders "name,id,accent,desc,age,gender,use,sample "
    tmp = Mid(tmp, a, b - a)
    tmp = Split(tmp, vbCrLf)
    For Each tt In tmp
        ' Rachel, 21m00Tcm4TlvDq8ikWAM, american, calm, young, female, narration
        tt = Trim(tt)
        If Len(tt) > 0 Then
            x = Split(Trim(tt), ",")
            Set li = lv.AddAryItem(x)
            If Not li Is Nothing Then
                If fso.FileExists(samples & "\" & li.Text & ".wav") Then li.subItems(7) = "Yes"
                Debug.Print "combo1.additem """ & li.Text & "," & li.subItems(1) & """"
            End If
        End If
    Next
    
    Command1.Visible = IsIde()
    
End Sub



Function IsIde() As Boolean
' Brad Martinez  http://www.mvps.org/ccrp
    On Error GoTo out
    Debug.Print 1 / 0
out: IsIde = Err
End Function

Private Sub lv_ItemClick(ByVal Item As MSComctlLib.ListItem)
    Set selItem = Item
    Text1 = Item.Text & " " & Item.subItems(1)
End Sub
