CloudBuilder/CloudBuilder.AI/Policy/ChineseTtsVoicePolicy.cs
owenchen cb3fdca3e1 ow
2026-06-04 08:16:37 +08:00

267 lines
9.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.AI.Policy
{
public enum ChineseTtsVoice
{
/// <summary>
/// 普通话(中国大陆) 娇柔(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 喜佳(女性)-0")]
Sherpa0,
/// <summary>
/// 普通话(中国大陆)- 柔美(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 柔美(女性)-2")]
Sherpa2,
/// <summary>
/// 普通话(中国大陆)- 知性(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 知性(男性)-4")]
Sherpa4,
/// <summary>
/// 普通话(中国大陆)- 知青(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 知青(男性)-3")]
Sherpa3,
/// <summary>
/// 普通话(中国大陆)- 中青(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 中青(男性)-1")]
Sherpa1,
/// <summary>
/// 普通话(中国大陆)- 爱莎(女性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 爱莎(女性)-6")]
Aaishe6,
/// <summary>
/// 普通话(中国大陆)- 爱莎(女性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 爱莎(女性)-14")]
Aaishe14,
/// <summary>
/// 普通话(中国大陆)- 爱莎(女性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 爱莎(女性)-18")]
Aaishe18,
/// <summary>
/// 普通话(中国大陆)- 云健(男性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云健(男性)-10")]
Aaishe10,
/// <summary>
/// 普通话(中国大陆)- 爱莎(女性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 爱莎(女性)-1")]
Aaishe1,
/// <summary>
/// 普通话(中国大陆)- 云健(男性)- 文件小速度快
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云健(男性)-15")]
Aaishe15,
/// 普通话(中国大陆) 云阳(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云阳(男性)-10")]
Fanchen10,
/// <summary>
/// 普通话(中国大陆) 云阳(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云阳(男性)-110")]
Fanchen110,
/// 普通话(中国大陆) 喜佳(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云夏(女性)-6")]
Fanchen6,
/// <summary>
/// 普通话(中国大陆) 云阳(男性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 云阳(男性)-15")]
Fanchen15,
/// <summary>
/// 普通话(中国大陆) 喜佳(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 喜佳(女性)-2")]
Fanchen2,
/// <summary>
/// 普通话(中国大陆) 喜佳(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 喜佳(女性)-14")]
Fanchen14,
/// <summary>
/// 普通话(中国大陆) 喜佳(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 喜佳(女性)-20")]
Fanchen20,
/// <summary>
/// 普通话(中国大陆) 喜佳(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 喜佳(女性)-36")]
Fanchen36,
/// <summary>
/// 粤语(香港)- 女性(男性)- 文件大
/// </summary>
[System.ComponentModel.Description("粤语(香港) 梅隆 (女性)-0")]
Melo,
/// <summary>
/// 普通话(中国大陆)- 神精女(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 神精女(女性)-0")]
Eula,
/// <summary>
/// 普通话(中国大陆)- 小神精女(女性)
/// </summary>
[System.ComponentModel.Description("普通话(中国大陆) 小神精女(女性)-27")]
Theresa,
}
public static class ChineseTtsVoiceRandom
{
private static readonly Random _random = new Random();
private static ChineseTtsVoice Default = ChineseTtsVoice.Sherpa2;
private static ChineseTtsVoice DefaultFemale = ChineseTtsVoice.Sherpa0;
private static ChineseTtsVoice DefaultMale = ChineseTtsVoice.Sherpa4;
// 所有男性列表(只定义一次)
private static readonly List<ChineseTtsVoice> _allMales = new()
{
ChineseTtsVoice.Sherpa4,
ChineseTtsVoice.Sherpa3,
ChineseTtsVoice.Sherpa1,
ChineseTtsVoice.Aaishe10,
ChineseTtsVoice.Aaishe15,
ChineseTtsVoice.Fanchen10,
ChineseTtsVoice.Fanchen110,
ChineseTtsVoice.Fanchen15
};
// 所有女性列表
private static readonly List<ChineseTtsVoice> _allFemales = new()
{
ChineseTtsVoice.Sherpa0,
ChineseTtsVoice.Sherpa2,
ChineseTtsVoice.Aaishe6,
ChineseTtsVoice.Aaishe14,
ChineseTtsVoice.Aaishe18,
ChineseTtsVoice.Aaishe1,
ChineseTtsVoice.Fanchen6,
ChineseTtsVoice.Fanchen2,
ChineseTtsVoice.Fanchen14,
ChineseTtsVoice.Fanchen20,
ChineseTtsVoice.Fanchen36,
ChineseTtsVoice.Melo,
ChineseTtsVoice.Eula,
ChineseTtsVoice.Theresa
};
/// <summary>
/// 百分比越大取前面5个的机会越大
/// </summary>
/// <param name="percentage">0~100推荐 50~100</param>
/// <summary>
/// 百分比越大取前面5个的机会越大
/// </summary>
/// <param name="percentage">0~100越大越容易选前5个</param>
public static ChineseTtsVoice GetRandom(int percentage = 80)
{
var all = Enum.GetValues(typeof(ChineseTtsVoice)).Cast<ChineseTtsVoice>().ToList();
int totalCount = all.Count;
// 安全限制
percentage = Math.Clamp(percentage, 0, 100);
if (totalCount == 0) return default;
// 前5个
int topCount = 5;
var topItems = all.Take(topCount).ToList();
// 百分比越高前5个被选中的概率越高
double topChance = percentage / 100.0;
// 随机决定是否从前5个拿
if (_random.NextDouble() < topChance && topItems.Any())
{
return topItems[_random.Next(topItems.Count)];
}
else
{
// 否则全量随机
return all[_random.Next(all.Count)];
}
}
// 2. 随机女性 + 百分比权重越高越容易选前5个
public static ChineseTtsVoice GetRandomFemale(int weightPercentage = 80)
{
return GetWeightedRandom(_allFemales, weightPercentage);
}
// 3. 随机男性 + 百分比权重
public static ChineseTtsVoice GetRandomMale(int weightPercentage = 80)
{
return GetWeightedRandom(_allMales, weightPercentage);
}
// 4. 随机男性(排除指定)+ 百分比权重
public static ChineseTtsVoice GetRandomMaleExclude(int weightPercentage = 80, ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa4)
{
var filtered = _allMales.Where(m => m != excludeVoice).ToList();
return GetWeightedRandom(filtered, weightPercentage);
}
// 5. 随机女性(排除指定)+ 百分比权重
public static ChineseTtsVoice GetRandomFemaleExclude(int weightPercentage = 80, ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa0)
{
var filtered = _allFemales.Where(f => f != excludeVoice).ToList();
return GetWeightedRandom(filtered, weightPercentage);
}
// 1. 随机任意一个(排除默认)+ 百分比权重
public static ChineseTtsVoice GetRandomExclude(int weightPercentage = 80)
{
var all = Enum.GetValues(typeof(ChineseTtsVoice)).Cast<ChineseTtsVoice>().ToList();
all = all.Where(f => f != DefaultFemale && f != DefaultMale).ToList();
return GetWeightedRandom(all, weightPercentage);
}
/// <summary>
/// 加权随机百分比越高前5个概率越大
/// </summary>
private static ChineseTtsVoice GetWeightedRandom(List<ChineseTtsVoice> list, int percentage)
{
if (list == null || list.Count == 0)
return default;
// 确保 0~100 之间
percentage = Math.Clamp(percentage, 0, 100);
const int topCount = 5; // 固定前5个
var topItems = list.Take(topCount).ToList();
// 百分比越高越容易选前5个
if (_random.NextDouble() < percentage / 100.0 && topItems.Any())
{
return topItems[_random.Next(topItems.Count)];
}
// 否则全列表随机
return list[_random.Next(list.Count)];
}
}
}