-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandGenerator.vb
More file actions
245 lines (214 loc) · 12.7 KB
/
Copy pathCommandGenerator.vb
File metadata and controls
245 lines (214 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
Imports System.Text
Imports System.IO
Public Class CommandGenerator
Private ReadOnly builder As StringBuilder
Private ReadOnly settings As MainUI
' Constants for repeated strings
Private Const DEFAULT_IP As String = "127.0.0.1"
Private Const MODEL_SOURCE_PREFIX As String = "--model_source "
Private Const DEMUCS_MODEL_PREFIX As String = "--demucs_model "
Private Const PADDING_AUDIO_PREFIX As String = "--paddedaudio "
Private Const PORT_NUMBER_PREFIX As String = "--portnumber "
Private Const HTTPS_PREFIX As String = "--https "
Private Const SERVER_IP_PREFIX As String = "--serverip "
Public Sub New(mainForm As MainUI)
builder = New StringBuilder()
settings = mainForm
End Sub
Public Function Generate() As String
If String.IsNullOrEmpty(settings.ScriptFileLocation.Text) Then
Throw New Exception("Please select the program file.")
End If
If String.IsNullOrEmpty(settings.StreamLanguage.Text) Then
Throw New Exception("No Stream language was set.")
End If
Dim validModels As New List(Of String) From {
"htdemucs", "htdemucs_ft", "htdemucs_6s", "hdemucs_mmi", "mdx", "mdx_extra", "mdx_q", "mdx_extra_q", "hdemucs", "demucs", "DEFAULT"
}
If Not validModels.Contains(settings.demucs_model.Text) Then
MessageBox.Show($"'{settings.demucs_model.Text}' is not a model that is known.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Throw New Exception($"Unknown demucs model: {settings.demucs_model.Text}")
End If
settings.ShortCutType = If(settings.ScriptFileLocation.Text.Contains("synthalingua.py"), "Source", "Portable")
settings.PrimaryFolder = Path.GetDirectoryName(settings.ScriptFileLocation.Text)
' Basic setup
builder.Clear()
builder.AppendLine("chcp 65001")
builder.AppendLine("set PYTHONIOENCODING=utf-8")
builder.AppendLine("SET TORCHAUDIO_USE_BACKEND_DISPATCHER=1")
builder.AppendLine("SET TORIO_USE_FFMPEG=0")
builder.AppendLine().AppendLine("cls").AppendLine("@echo off").AppendLine("Echo Loading Script")
builder.AppendLine($"""{settings.PrimaryFolder}\set_up_env.exe""")
builder.AppendLine($"call ""{settings.PrimaryFolder}\ffmpeg_path.bat""")
' Script execution command
If settings.ShortCutType = "Source" Then
builder.AppendLine($"call ""{settings.PrimaryFolder}\data_whisper\Scripts\activate.bat""")
builder.Append($"python ""{settings.PrimaryFolder}\synthalingua.py"" ")
Else
builder.Append($"""{settings.PrimaryFolder}\Synthalingua.exe"" ")
End If
AppendRamSettings()
AppendAudioSourceSettings()
AppendLanguageSettings()
AppendDeviceSettings()
AppendAdditionalSettings()
AppendModelSource()
builder.AppendLine().AppendLine("pause")
Return builder.ToString()
End Function
Private Sub AppendRamSettings()
builder.Append($"--ram {settings.RamSize.Text} ")
If settings.ForceRam.Checked Then builder.Append("--ramforce ")
End Sub
Private Sub AppendModelSource()
' check to see which radio button is checked and apply the appropriate model source with "--model_source {value}"
' model_openvino = openvino, model_whisper = whisper, model_fasterwhisper = fasterwhisper
Select Case True
Case settings.model_openvino.Checked
builder.Append(MODEL_SOURCE_PREFIX & "openvino ")
Case settings.model_whisper.Checked
builder.Append(MODEL_SOURCE_PREFIX & "whisper ")
Case settings.model_fasterwhisper.Checked
builder.Append(MODEL_SOURCE_PREFIX & "fasterwhisper ")
End Select
End Sub
Private Sub AppendCapSettings()
'if the debugmode checkbox is checked, append "--debug "
If settings.debugmode.Checked Then builder.Append("--debug ")
' If compare_mode is checked, append "--makecaptions compare", else just "--makecaptions"
If settings.compare_mode IsNot Nothing AndAlso settings.compare_mode.Checked Then
builder.Append($"--makecaptions compare --file_input=""{settings.CaptionsInput.Text}"" --file_output=""{settings.CaptionsOutput.Text}"" --file_output_name=""{settings.CaptionsName.Text}"" ")
Else
builder.Append($"--makecaptions --file_input=""{settings.CaptionsInput.Text}"" --file_output=""{settings.CaptionsOutput.Text}"" --file_output_name=""{settings.CaptionsName.Text}"" ")
End If
' Add this block for print_srt_to_console
If settings.print_srt_to_console IsNot Nothing AndAlso settings.print_srt_to_console.Checked Then builder.Append("--print_srt_to_console ")
If settings.silent_detect.Checked Then builder.Append("--silent_detect ")
If settings.silent_threshold.Value <> -35 Then builder.Append($"--silent_threshold {settings.silent_threshold.Value} ")
If settings.silent_duration.Value <> 0.5 Then builder.Append($"--silent_duration {settings.silent_duration.Value} ")
If settings.intelligent_mode.Checked Then builder.Append("--intelligent_mode ")
' if timeout's value is greater than 0, append "--timeout {value}"
If settings.timeout.Value > 0 And settings.batchjobs.Value > 1 Then builder.Append($"--timeout {settings.timeout.Value} ")
If settings.word_timestamps.Checked Then builder.Append("--word_timestamps ")
If settings.silent_detect.Checked AndAlso settings.demucs_model.Text <> "DEFAULT" Then builder.Append(DEMUCS_MODEL_PREFIX & settings.demucs_model.Text & " ")
AppendSubtitleSettings()
builder.Append(BatchModeSettings())
End Sub
Private Sub AppendSubtitleSettings()
If settings.add_subs.Checked Then
If settings.subtype_burn.Checked Then builder.Append("--subtype burn ")
If settings.subtype_burn.Checked Then
' Build substyle parameter based on fontname availability
If Not String.IsNullOrEmpty(settings.substyle_fontname.Text) Then
builder.Append($"--substyle {settings.substyle_fontname.Text},{settings.substyle_fontsize.Value},{settings.substyle_color.Text} ")
Else
builder.Append($"--substyle {settings.substyle_fontsize.Value},{settings.substyle_color.Text} ")
End If
End If
If settings.subtype_embed.Checked Then builder.Append("--subtype embed ")
End If
End Sub
Private Sub AppendHlsSettings()
If Not String.IsNullOrEmpty(settings.CookiesName.Text) Then builder.Append($"--cookies {settings.CookiesName.Text} ")
If settings.cb_halspassword.Checked Then builder.Append($"--remote_hls_password_id {settings.hlspassid.Text} --remote_hls_password {settings.hlspassword.Text} ")
If settings.AutoHLS_Checkbox.Checked Then builder.Append("--auto_hls ")
If settings.SelectSource.Checked Then builder.Append("--selectsource ")
If settings.ShowOriginalText.Checked Then builder.Append("--stream_original_text ")
builder.Append($"--stream ""{settings.HLS_URL.Text}"" ")
builder.Append($"--stream_chunks {settings.ChunkSizeTrackBar.Value} ")
If settings.paddedaudio.Checked Then builder.Append(PADDING_AUDIO_PREFIX & settings.paddedaudio_value.Value & " ")
If settings.demucs_model.Text <> "DEFAULT" Then builder.Append(DEMUCS_MODEL_PREFIX & settings.demucs_model.Text & " ")
If settings.WebServerButton.Checked Then AppendWebServerSettings()
End Sub
Private Sub AppendMicSettings()
builder.Append("--microphone_enabled true ")
If settings.MicEnCheckBox.Checked Then builder.Append($"--energy_threshold {settings.EnThreshValue.Value} ")
If settings.MicCaliCheckBox.Checked Then builder.Append($"--mic_calibration_time {settings.MicCaliTime.Value} ")
If settings.RecordTimeOutCHeckBox.Checked Then builder.Append($"--record_timeout {settings.RecordTimeout.Value} ")
If settings.PhraseTimeOutCheckbox.Checked Then builder.Append($"--phrase_timeout {settings.PhraseTimeout.Value} ")
builder.Append($"--set_microphone {settings.MicID.Value} ")
builder.Append($"--mic_chunk_size {settings.mic_chunk_size.Value} ")
If settings.paddedaudio.Checked Then builder.Append(PADDING_AUDIO_PREFIX & settings.paddedaudio_value.Value & " ")
If settings.WebServerButton.Checked Then AppendWebServerSettings()
End Sub
Private Sub AppendWebServerSettings()
builder.Append(PORT_NUMBER_PREFIX & settings.PortNumber.Value & " ")
builder.Append(HTTPS_PREFIX & CInt(settings.HTTPSPortNumber.Value) & " ")
builder.Append(SERVER_IP_PREFIX & NormalizeServerIp() & " ")
End Sub
Private Sub AppendAudioSourceSettings()
If settings.CAP_RadioButton.Checked Then
AppendCapSettings()
ElseIf settings.HSL_RadioButton.Checked Then
AppendHlsSettings()
ElseIf settings.MIC_RadioButton.Checked Then
AppendMicSettings()
End If
End Sub
Private Sub AppendLanguageSettings()
Dim prefix = If(settings.HSL_RadioButton.Checked, "stream_", "")
If settings.StreamLanguage.Text <> "--Auto Detect--" Then
builder.Append($"--{prefix}language {settings.StreamLanguage.Text} ")
End If
If settings.EnglishTranslationCheckBox.Checked Then
builder.Append($"--{prefix}translate ")
End If
If settings.SecondaryTranslation.Checked Then
' builder.Append($"--{prefix}transcribe ")
builder.Append($"--{prefix}transcribe {settings.SecondaryTranslationLanguage.Text} ")
End If
End Sub
Private Sub AppendDeviceSettings()
If settings.CUDA_RadioButton.Checked Then
builder.Append("--device cuda ")
ElseIf settings.CPU_RadioButton.Checked Then
builder.Append("--device cpu ")
End If
End Sub
Private Sub AppendAdditionalSettings()
If settings.WordBlockList.Checked Then builder.Append($"--ignorelist ""{settings.WordBlockListLocation}"" ")
If settings.RepeatProtection.Checked Then builder.Append("--condition_on_previous_text ")
If Not String.IsNullOrEmpty(settings.DiscordWebHook.Text) Then builder.Append($"--discord_webhook ""{settings.DiscordWebHook.Text}"" ")
If Not String.IsNullOrEmpty(settings.modelDIr.Text) Then builder.Append($"--model_dir ""{settings.modelDIr.Text}"" ")
If settings.PrecisionCheckBox.Checked Then builder.Append("--fp16 ")
If settings.auto_blocklist.Checked Then builder.Append("--auto_blocklist ")
If settings.isolate_vocals.Checked Then
builder.Append("--isolate_vocals ")
' Add demucs_model_jobs value if greater than 0, directly after --isolate_vocals
If settings.demucs_model_jobs.Value > 0 Then
builder.Append($"{settings.demucs_model_jobs.Value} ")
End If
End If
End Sub
Private Function NormalizeServerIp() As String
Dim rawServerIp = settings.ServerIP.Text
If String.IsNullOrWhiteSpace(rawServerIp) Then Return DEFAULT_IP
Dim sanitized = rawServerIp.Replace("_", "").Replace(" ", "")
If String.IsNullOrWhiteSpace(sanitized) Then Return DEFAULT_IP
Dim segments = sanitized.Split("."c, StringSplitOptions.RemoveEmptyEntries)
If segments.Length <> 4 Then Return DEFAULT_IP
Dim normalized As New List(Of String)(4)
For Each segment In segments
Dim value As Integer
If Integer.TryParse(segment, value) AndAlso value >= 0 AndAlso value <= 255 Then
normalized.Add(value.ToString())
Else
Return DEFAULT_IP
End If
Next
Return String.Join(".", normalized)
End Function
Private Function BatchModeSettings() As String
' using the checkbox "BatchModeAuto" or "BatchModeManual" to determine if batch mode is enabled
' If Auto is checked then we use "--adaptive_batch" otherwise we use "--batchmode {value}"
' We grab the value from the track bar value "batchjobs"
' If we use automode then we use "--adaptive_batch --batchjobsize {value}", the value is grabbed from the numericupdown "batchjobsize"
' If settings.batchjobs.Value > 1 Then builder.Append($"--batchmode {settings.batchjobs.Value} ")
If settings.BatchModeAuto.Checked Then
Return $"--adaptive_batch --batchjobsize {settings.batchjobsize.Value} --cpu_batches {settings.batchjobs.Value} --max_cpu_time {settings.max_cpu_time.Value} "
ElseIf settings.BatchModeManual.Checked Then
Return $"--batchmode {settings.batchjobs.Value} "
End If
Return String.Empty
End Function
End Class