mirror of
https://github.com/denizsafak/abogen.git
synced 2026-07-18 13:40:27 +02:00
normalizing the weight to 1.0 of the total weight of mixed voices
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
from constants import VOICES_INTERNAL
|
from constants import VOICES_INTERNAL
|
||||||
|
|
||||||
# Calls parsing and loads the voice to gpu or cpu
|
# Calls parsing and loads the voice to gpu or cpu
|
||||||
@@ -11,24 +12,26 @@ def get_new_voice(pipeline, formula, use_gpu):
|
|||||||
|
|
||||||
# Parse the formula and get the combined voice tensor
|
# Parse the formula and get the combined voice tensor
|
||||||
def parse_voice_formula(pipeline, formula):
|
def parse_voice_formula(pipeline, formula):
|
||||||
"""Parse the voice formula string and return the combined voice tensor."""
|
|
||||||
if not formula.strip():
|
if not formula.strip():
|
||||||
raise ValueError("Empty voice formula")
|
raise ValueError("Empty voice formula")
|
||||||
|
|
||||||
# Initialize the weighted sum
|
# Initialize the weighted sum
|
||||||
weighted_sum = None
|
weighted_sum = None
|
||||||
|
|
||||||
|
total_weight = calculate_sum_from_formula(formula)
|
||||||
|
|
||||||
# Split the formula into terms
|
# Split the formula into terms
|
||||||
terms = formula.split('+')
|
voices = formula.split('+')
|
||||||
|
|
||||||
for term in terms:
|
for term in voices:
|
||||||
# Parse each term (format: "0.333 * voice_name")
|
# Parse each term (format: "0.333 * voice_name")
|
||||||
weight, voice_name = term.strip().split('*')
|
weight, voice_name = term.strip().split('*')
|
||||||
weight = float(weight.strip())
|
weight = float(weight.strip())
|
||||||
|
# normalize the weight
|
||||||
|
weight /= total_weight if total_weight > 0 else 1.0
|
||||||
voice_name = voice_name.strip()
|
voice_name = voice_name.strip()
|
||||||
|
|
||||||
# Get the voice tensor
|
# Get the voice tensor
|
||||||
# use VOICES_INTERNAL
|
|
||||||
if voice_name not in VOICES_INTERNAL:
|
if voice_name not in VOICES_INTERNAL:
|
||||||
raise ValueError(f"Unknown voice: {voice_name}")
|
raise ValueError(f"Unknown voice: {voice_name}")
|
||||||
|
|
||||||
@@ -40,4 +43,9 @@ def parse_voice_formula(pipeline, formula):
|
|||||||
else:
|
else:
|
||||||
weighted_sum += weight * voice_tensor
|
weighted_sum += weight * voice_tensor
|
||||||
|
|
||||||
return weighted_sum
|
return weighted_sum
|
||||||
|
|
||||||
|
def calculate_sum_from_formula(formula):
|
||||||
|
weights = re.findall(r'([\d.]+) \*', formula)
|
||||||
|
total_sum = sum(float(weight) for weight in weights)
|
||||||
|
return total_sum
|
||||||
Reference in New Issue
Block a user