fix: proper Seed-VC generator handling and audio output

This commit is contained in:
Miru
2026-04-08 12:29:45 +00:00
parent 2b93fea7ca
commit c7d023e5ef

View File

@@ -107,8 +107,8 @@ class SeedVCService:
try:
# Perform voice conversion using the wrapper
# Returns numpy array of audio samples
audio_array = self.wrapper.convert_voice(
# Returns generator that yields tuples, final result is in StopIteration.value
result_gen = self.wrapper.convert_voice(
source=source_audio_path,
target=reference_audio_path,
diffusion_steps=diffusion_steps,
@@ -120,6 +120,27 @@ class SeedVCService:
stream_output=False # We want the full audio array, not streaming
)
# Iterate through generator to get final result
# The final return value is accessible via StopIteration exception
audio_array = None
try:
while True:
item = next(result_gen)
# item is (mp3_bytes, full_audio) tuple
if item and len(item) >= 2 and item[1] is not None:
# full_audio is (sr, audio_array) tuple
full_audio = item[1]
if isinstance(full_audio, tuple) and len(full_audio) == 2:
audio_array = full_audio[1]
else:
audio_array = full_audio
except StopIteration as e:
# The return value of the generator is in e.value
if e.value is not None:
# e.value is the numpy array (returned from convert_voice)
audio_array = e.value
logger.info(f"Voice conversion complete, audio shape: {audio_array.shape if audio_array is not None else None}")
# Determine sample rate based on f0_condition
sr = 44100 if f0_condition else 22050
@@ -129,6 +150,20 @@ class SeedVCService:
# If wrapper returns tuple, extract the array
audio_array = audio_array[0]
# Handle generator - get all chunks and concatenate
if hasattr(audio_array, '__iter__') and not isinstance(audio_array, (np.ndarray, list, tuple)):
# It's a generator, collect all chunks
chunks = []
for chunk in audio_array:
if isinstance(chunk, np.ndarray):
chunks.append(chunk)
else:
chunks.append(np.array(chunk))
if chunks:
audio_array = np.concatenate(chunks)
else:
audio_array = np.array([])
# Ensure numpy array
audio_array = np.asarray(audio_array)
@@ -148,6 +183,10 @@ class SeedVCService:
# Ensure float32 format for soundfile
audio_array = audio_array.astype(np.float32)
# Convert to absolute path
output_path = os.path.abspath(output_path)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Save output
sf.write(output_path, audio_array, sr)
logger.info(f"Voice conversion complete: {output_path}")