This commit is contained in:
owenchen committed 2026-06-04 08:16:37 +08:00
1 parent fd48c2eb7d
commit cb3fdca3e1
8 files changed
+339 -52

No files matched your search

+72 -18
View File
@@ -168,45 +168,99 @@ namespace CloudBuilder.AI.Policy
ChineseTtsVoice.Theresa
};
// 1. 随机取任意一个
public static ChineseTtsVoice GetRandom()
/// <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();
return all[_random.Next(all.Count)];
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. 随机女性
public static ChineseTtsVoice GetRandomFemale()
// 2. 随机女性 + 百分比权重(越高越容易选前5个)
public static ChineseTtsVoice GetRandomFemale(int weightPercentage = 80)
{
return _allFemales[_random.Next(_allFemales.Count)];
return GetWeightedRandom(_allFemales, weightPercentage);
}
// 3. 随机男性
public static ChineseTtsVoice GetRandomMale()
// 3. 随机男性 + 百分比权重
public static ChineseTtsVoice GetRandomMale(int weightPercentage = 80)
{
return _allMales[_random.Next(_allMales.Count)];
return GetWeightedRandom(_allMales, weightPercentage);
}
// 4. 随机男性(传入要排除的声音,通用版 ✅)
public static ChineseTtsVoice GetRandomMaleExclude(ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa4)
// 4. 随机男性(排除指定)+ 百分比权重
public static ChineseTtsVoice GetRandomMaleExclude(int weightPercentage = 80, ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa4)
{
var filtered = _allMales.Where(m => m != excludeVoice).ToList();
return filtered[_random.Next(filtered.Count)];
return GetWeightedRandom(filtered, weightPercentage);
}
// 5. 随机女性(传入要排除的声音,通用版 ✅)
public static ChineseTtsVoice GetRandomFemaleExclude(ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa0)
// 5. 随机女性(排除指定)+ 百分比权重
public static ChineseTtsVoice GetRandomFemaleExclude(int weightPercentage = 80, ChineseTtsVoice excludeVoice = ChineseTtsVoice.Sherpa0)
{
var filtered = _allFemales.Where(f => f != excludeVoice).ToList();
return filtered[_random.Next(filtered.Count)];
return GetWeightedRandom(filtered, weightPercentage);
}
// 1. 随机取任意一个
public static ChineseTtsVoice GetRandomExclude()
// 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 all[_random.Next(all.Count)];
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)];
}
}
}