fix what you see wrong dont change important things, and return the script fully fixed…
- User
fix what you see wrong dont change important things, and return the script fully fixed thanks
public async Task ConvertVocals(string selectedVoicePath, string singingVoiceFile, int qualityValue) { const string outputsDir = @"C:\AI\UVR5_UI\outputs"; const string finalOutputPath = @"C:\output\folder\NewVoice.wav";
// Clear output directory try { if (Directory.Exists(outputsDir)) { foreach (var file in Directory.GetFiles(outputsDir)) { try { File.Delete(file); } catch (Exception ex) { MessageBox.Show($"Failed to delete file '{file}': {ex.Message}"); } } } else { MessageBox.Show($"Outputs directory does not exist: {outputsDir}"); } } catch (Exception ex) { MessageBox.Show($"Failed to clear output directory: {ex.Message}"); } using var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan }; try { // Validate input files existence before opening if (!File.Exists(singingVoiceFile)) { MessageBox.Show($"Singing voice file not found: {singingVoiceFile}"); return; } if (!File.Exists(selectedVoicePath)) { MessageBox.Show($"Selected voice path not found: {selectedVoicePath}"); return; } using var sourceStream = File.OpenRead(singingVoiceFile); using var referenceStream = File.OpenRead(selectedVoicePath); using var sourceContent = new StreamContent(sourceStream); sourceContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"source_file\"", FileName = "\"source.wav\"" }; using var referenceContent = new StreamContent(referenceStream); referenceContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"reference_file\"", FileName = "\"reference.wav\"" }; using var form = new MultipartFormDataContent{ { sourceContent, "source_file", "source.wav" }, { referenceContent, "reference_file", "reference.wav" }, { new StringContent(qualityValue.ToString()), "diffusion_steps" }, { new StringContent("1.0"), "length_adjust" }, { new StringContent("0.7"), "inference_cfg_rate" }, { new StringContent("true"), "auto_f0_adjust" }, { new StringContent("0"), "pitch_shift" } };
HttpResponseMessage response = await client.PostAsync("http://localhost:6576/convert", form); if (!response.IsSuccessStatusCode) { MessageBox.Show($"Server error: {response.StatusCode}"); return; } var jsonString = await response.Content.ReadAsStringAsync(); using var jsonDoc = JsonDocument.Parse(jsonString); var root = jsonDoc.RootElement; if (!root.TryGetProperty("windows_path", out var pathElem) && !root.TryGetProperty("file_path", out pathElem)) { MessageBox.Show("Server response missing 'windows_path' or 'file_path'"); return; } string serverFilePath = pathElem.GetString(); if (string.IsNullOrWhiteSpace(serverFilePath) || !File.Exists(serverFilePath)) { MessageBox.Show($"Output file from server not found: {serverFilePath}"); return; } // Delete old vocals file (if exists) var oldFile = GetFirstVocalsWavFile(Path.GetDirectoryName(finalOutputPath)); if (!string.IsNullOrEmpty(oldFile) && File.Exists(oldFile)) { try { File.Delete(oldFile); } catch (Exception ex) { MessageBox.Show($"Failed to delete old vocal file '{oldFile}': {ex.Message}"); // Continue after this failure } } // Delete existing final output file to avoid locks during copy if (File.Exists(finalOutputPath)) { try { File.Delete(finalOutputPath); } catch (Exception ex) { MessageBox.Show($"Failed to delete existing final output file '{finalOutputPath}': {ex.Message}"); return; } } // Copy new converted vocals File.Copy(serverFilePath, finalOutputPath); // Assuming CombineWavs is synchronous CombineWavs(); } catch (TaskCanceledException) { MessageBox.Show("Request was canceled. It may have taken too long."); } catch (System.Text.Json.JsonException jEx) { MessageBox.Show($"Failed to parse server response: {jEx.Message}"); } catch (Exception ex) { MessageBox.Show($"Error during conversion: {ex.Message}"); } - Assistant