Spaces:
Runtime error
Runtime error
Update flask_api_full_song.py
Browse files- flask_api_full_song.py +19 -6
flask_api_full_song.py
CHANGED
@@ -65,27 +65,40 @@ def infer(audio_path, tran, spk, wav_format, task_id):
|
|
65 |
audio_data, audio_sr = slicer.chunks2audio(audio_path, chunks)
|
66 |
|
67 |
audio = []
|
68 |
-
for (slice_tag, data) in audio_data:
|
69 |
-
print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
|
70 |
|
71 |
-
|
|
|
72 |
if slice_tag:
|
73 |
print('jump empty segment')
|
74 |
_audio = np.zeros(length)
|
75 |
else:
|
76 |
# padd
|
77 |
pad_len = int(audio_sr * 0.5)
|
78 |
-
|
79 |
raw_path = io.BytesIO()
|
80 |
-
soundfile.write(raw_path,
|
81 |
raw_path.seek(0)
|
82 |
out_audio, out_audio_shape, out_sr = svc_model.infer(spk, tran, raw_path)
|
83 |
svc_model.clear_empty()
|
84 |
_audio = out_audio.cpu().numpy()
|
85 |
pad_len = int(svc_model.target_sample * 0.5)
|
86 |
_audio = _audio[pad_len:-pad_len]
|
87 |
-
|
88 |
audio.extend(list(infer_tool.pad_array(_audio, length)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
out_wav_path = "/tmp/" + audio_name
|
90 |
soundfile.write(out_wav_path, audio, svc_model.target_sample, format=wav_format)
|
91 |
|
|
|
65 |
audio_data, audio_sr = slicer.chunks2audio(audio_path, chunks)
|
66 |
|
67 |
audio = []
|
|
|
|
|
68 |
|
69 |
+
def process_chunk(chunk_data, audio_sr, slice_tag, svc_model, audio):
|
70 |
+
length = int(np.ceil(len(chunk_data) / audio_sr * svc_model.target_sample))
|
71 |
if slice_tag:
|
72 |
print('jump empty segment')
|
73 |
_audio = np.zeros(length)
|
74 |
else:
|
75 |
# padd
|
76 |
pad_len = int(audio_sr * 0.5)
|
77 |
+
chunk_data = np.concatenate([np.zeros([pad_len]), chunk_data, np.zeros([pad_len])])
|
78 |
raw_path = io.BytesIO()
|
79 |
+
soundfile.write(raw_path, chunk_data, audio_sr, format="wav")
|
80 |
raw_path.seek(0)
|
81 |
out_audio, out_audio_shape, out_sr = svc_model.infer(spk, tran, raw_path)
|
82 |
svc_model.clear_empty()
|
83 |
_audio = out_audio.cpu().numpy()
|
84 |
pad_len = int(svc_model.target_sample * 0.5)
|
85 |
_audio = _audio[pad_len:-pad_len]
|
|
|
86 |
audio.extend(list(infer_tool.pad_array(_audio, length)))
|
87 |
+
|
88 |
+
for (slice_tag, data) in audio_data:
|
89 |
+
print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
|
90 |
+
|
91 |
+
# Check if the segment is longer than 30 seconds
|
92 |
+
segment_length = len(data) / audio_sr
|
93 |
+
if segment_length > 30:
|
94 |
+
# Split the segment into chunks of 30 seconds or less
|
95 |
+
num_chunks = int(np.ceil(segment_length / 30))
|
96 |
+
chunk_length = int(len(data) / num_chunks)
|
97 |
+
for i in range(num_chunks):
|
98 |
+
chunk_data = data[i * chunk_length:(i + 1) * chunk_length]
|
99 |
+
process_chunk(chunk_data, audio_sr, slice_tag, svc_model, audio)
|
100 |
+
else:
|
101 |
+
process_chunk(data, audio_sr, slice_tag, svc_model, audio)
|
102 |
out_wav_path = "/tmp/" + audio_name
|
103 |
soundfile.write(out_wav_path, audio, svc_model.target_sample, format=wav_format)
|
104 |
|