This commit is contained in:
owenchen 2026-06-23 15:32:23 +08:00
parent 2c6475b2d5
commit 5fe7c6260e
4 changed files with 21 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View File

@ -22,10 +22,19 @@ namespace CloudBuilder.Topshelf.Python
if (!File.Exists(file)) return;
string init = File.ReadAllText(file, Encoding.UTF8);
File.AppendAllText("log.txt", "SoVITS:" + file + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
using (Py.GIL())
{
python.Exec(init);
try
{
python.Exec(init);
}
catch (Exception ex)
{
File.AppendAllText("log.txt", "SoVITS-Exec:" + ex.Message + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
}
File.AppendAllText("log.txt", "SoVITS-Exec:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
}
}

View File

@ -28,13 +28,14 @@ namespace CloudBuilder.Topshelf.Task.TTS
public SoVITSTtsTask(IApplicationService service)
{
File.AppendAllText("log.txt", "SoVITSTtsTask:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
this.service = service;
}
public void Run(Dictionary<string, string> bodyDict)
{
File.AppendAllText("log.txt", "Run:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
ConsoleOutput.HideStdError();
IRepository<AiSentenceViewEntity> repository = service.GetRepository<IRepository<AiSentenceViewEntity>>();
string bookId = string.Empty;
@ -58,9 +59,8 @@ namespace CloudBuilder.Topshelf.Task.TTS
&& (string.IsNullOrEmpty(from_chapter_id) || x.ChapterId >= Convert.ToInt32(from_chapter_id))
&& (string.IsNullOrEmpty(to_chapter_id) || x.ChapterId <= Convert.ToInt32(to_chapter_id)
)).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray();
if (ents == null || ents.Length == 0) return;
File.AppendAllText("log.txt", "SoVITSTtsVoiceRandom:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
string save_path = string.Empty, text = string.Empty, voice = SoVITSTtsVoiceRandom.GetRandomYoungFemale().ToString(), actor = string.Empty, actor_voice = SoVITSTtsVoiceRandom.GetRandomYoungMale().ToString();
if (bodyDict.ContainsKey("actor"))
actor = bodyDict["actor"];
@ -78,18 +78,23 @@ namespace CloudBuilder.Topshelf.Task.TTS
if (bodyDict.ContainsKey("speed"))
speed = Convert.ToSingle(bodyDict["speed"]);
File.AppendAllText("log.txt", "GenerateAndMergeChapterAudio:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
GenerateAndMergeChapterAudio(ents, speed, actor, actor_voice, voice, save_path);
}
public void GenerateAndMergeChapterAudio(AiSentenceViewEntity[] ents, float speed, string actor, string actor_voice, string mainVoice, string save_path)
{
IRepository<AiBookPersonEntity> repositoryAiBookPersonEntity = service.GetRepository<IRepository<AiBookPersonEntity>>();
File.AppendAllText("log.txt", "BookId:" + ents[0].BookId + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
AiBookPersonEntity[] bps = repositoryAiBookPersonEntity.DetachedEntities.Where(x => x.BookId == ents[0].BookId).ToArray();
File.AppendAllText("log.txt", "repositoryAiBookPersonEntity:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
Python39Command python39Command = new Python39Command(service);
File.AppendAllText("log.txt", "Python39Command:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
SoVITS soVITS = new SoVITS(python39Command);
File.AppendAllText("log.txt", "SoVITS:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
// 1. 按 BookId + ChapterId 分组(关键!)
var chapterGroups = ents
.Where(ent => ent.Content.Any(c => c >= 0x4E00 && c <= 0x9FFF)) // 只保留含中文的句子
@ -179,6 +184,7 @@ namespace CloudBuilder.Topshelf.Task.TTS
string fileName = string.Format("{0}_{1}_{2}_{3}.mp3", ent.BookId, ent.ChapterId, ent.ParagraphId, ent.SentenceId);
string filePath = Path.Combine(save_path, fileName);
File.AppendAllText("log.txt", "Generate:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
// 生成音频
soVITS.Generate(ent.Content, soVITS.Voices[EnumHelper.ToEnum<SoVITSTtsVoice>(voice)], filePath);