fix: proper Seed-VC generator handling and audio output
This commit is contained in:
@@ -107,8 +107,8 @@ class SeedVCService:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Perform voice conversion using the wrapper
|
# Perform voice conversion using the wrapper
|
||||||
# Returns numpy array of audio samples
|
# Returns generator that yields tuples, final result is in StopIteration.value
|
||||||
audio_array = self.wrapper.convert_voice(
|
result_gen = self.wrapper.convert_voice(
|
||||||
source=source_audio_path,
|
source=source_audio_path,
|
||||||
target=reference_audio_path,
|
target=reference_audio_path,
|
||||||
diffusion_steps=diffusion_steps,
|
diffusion_steps=diffusion_steps,
|
||||||
@@ -119,6 +119,27 @@ class SeedVCService:
|
|||||||
pitch_shift=pitch_shift,
|
pitch_shift=pitch_shift,
|
||||||
stream_output=False # We want the full audio array, not streaming
|
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
|
# Determine sample rate based on f0_condition
|
||||||
sr = 44100 if f0_condition else 22050
|
sr = 44100 if f0_condition else 22050
|
||||||
@@ -129,6 +150,20 @@ class SeedVCService:
|
|||||||
# If wrapper returns tuple, extract the array
|
# If wrapper returns tuple, extract the array
|
||||||
audio_array = audio_array[0]
|
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
|
# Ensure numpy array
|
||||||
audio_array = np.asarray(audio_array)
|
audio_array = np.asarray(audio_array)
|
||||||
|
|
||||||
@@ -148,6 +183,10 @@ class SeedVCService:
|
|||||||
# Ensure float32 format for soundfile
|
# Ensure float32 format for soundfile
|
||||||
audio_array = audio_array.astype(np.float32)
|
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
|
# Save output
|
||||||
sf.write(output_path, audio_array, sr)
|
sf.write(output_path, audio_array, sr)
|
||||||
logger.info(f"Voice conversion complete: {output_path}")
|
logger.info(f"Voice conversion complete: {output_path}")
|
||||||
|
|||||||
Reference in New Issue
Block a user