Opera8 commited on
Commit
885b401
·
verified ·
1 Parent(s): 97b11e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -86,9 +86,8 @@ os.makedirs("ckpts/Vevo", exist_ok=True)
86
 
87
  from models.vc.vevo.vevo_utils import VevoInferencePipeline
88
 
89
- # --- تابع ذخیره سازی دقیق (16-bit PCM) ---
90
- # این تابع کلید حل مشکل نویز صداست. فایل را دقیقاً مثل WAV استاندارد ذخیره می‌کند.
91
- def save_audio_pcm16(waveform, output_path, sample_rate=24000):
92
  try:
93
  if isinstance(waveform, torch.Tensor):
94
  waveform = waveform.detach().cpu()
@@ -96,7 +95,22 @@ def save_audio_pcm16(waveform, output_path, sample_rate=24000):
96
  waveform = waveform.squeeze(0)
97
  waveform = waveform.numpy()
98
 
99
- # تبدیل به فرمت 16 بیتی برای جلوگیری از نویز
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  sf.write(output_path, waveform, sample_rate, subtype='PCM_16')
101
 
102
  except Exception as e:
@@ -192,12 +206,14 @@ def vevo_timbre(content_wav, reference_wav):
192
  content_data = np.mean(content_data, axis=1)
193
 
194
  content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
195
-
196
  if content_sr != 24000:
197
  content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
198
  content_sr = 24000
199
 
200
  content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
 
 
 
201
 
202
  # --- پردازش صدای رفرنس ---
203
  if isinstance(reference_wav, tuple):
@@ -215,15 +231,13 @@ def vevo_timbre(content_wav, reference_wav):
215
 
216
  ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
217
 
218
- # *** ذخیره با فرمت PCM_16 (کلید حل مشکل نویز) ***
219
- save_audio_pcm16(content_tensor, temp_content_path, content_sr)
220
- save_audio_pcm16(ref_tensor, temp_reference_path, ref_sr)
221
 
222
  print(f"[{session_id}] Processing...")
223
 
224
  pipeline = get_pipeline()
225
 
226
- # اجرای مدل
227
  gen_audio = pipeline.inference_fm(
228
  src_wav_path=temp_content_path,
229
  timbre_ref_wav_path=temp_reference_path,
@@ -233,15 +247,17 @@ def vevo_timbre(content_wav, reference_wav):
233
  if torch.isnan(gen_audio).any() or torch.isinf(gen_audio).any():
234
  gen_audio = torch.nan_to_num(gen_audio, nan=0.0, posinf=0.95, neginf=-0.95)
235
 
236
- # ذخیره خروجی نهایی
237
- save_audio_pcm16(gen_audio, output_path, 24000)
 
 
238
  return output_path
239
 
240
  finally:
241
  if os.path.exists(temp_content_path): os.remove(temp_content_path)
242
  if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
243
 
244
- with gr.Blocks(title="Vevo-Timbre (High Quality)") as demo:
245
  gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
246
 
247
  with gr.Row():
 
86
 
87
  from models.vc.vevo.vevo_utils import VevoInferencePipeline
88
 
89
+ # --- تابع ذخیره سازی پیشرفته (حذف نویز + فرمت استاندارد) ---
90
+ def save_audio_final(waveform, output_path, sample_rate=24000, target_length=None):
 
91
  try:
92
  if isinstance(waveform, torch.Tensor):
93
  waveform = waveform.detach().cpu()
 
95
  waveform = waveform.squeeze(0)
96
  waveform = waveform.numpy()
97
 
98
+ # 1. همگام‌سازی طول (حذف نویز اضافه آخر فایل)
99
+ if target_length is not None:
100
+ if len(waveform) > target_length:
101
+ waveform = waveform[:target_length]
102
+ elif len(waveform) < target_length:
103
+ # اگر کوتاه‌تر بود، با سکوت پر کن (معمولاً پیش نمیاد)
104
+ padding = np.zeros(target_length - len(waveform))
105
+ waveform = np.concatenate([waveform, padding])
106
+
107
+ # 2. اعمال Fade Out (جلوگیری از صدای کلیک در لحظه قطع شدن)
108
+ fade_len = int(sample_rate * 0.05) # 50 میلی ثانیه
109
+ if len(waveform) > fade_len:
110
+ fade_curve = np.linspace(1, 0, fade_len)
111
+ waveform[-fade_len:] *= fade_curve
112
+
113
+ # 3. ذخیره با فرمت 16 بیتی
114
  sf.write(output_path, waveform, sample_rate, subtype='PCM_16')
115
 
116
  except Exception as e:
 
206
  content_data = np.mean(content_data, axis=1)
207
 
208
  content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
 
209
  if content_sr != 24000:
210
  content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
211
  content_sr = 24000
212
 
213
  content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
214
+
215
+ # ذخیره طول دقیق فایل ورودی برای برش نهایی
216
+ target_length_samples = content_tensor.shape[-1]
217
 
218
  # --- پردازش صدای رفرنس ---
219
  if isinstance(reference_wav, tuple):
 
231
 
232
  ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
233
 
234
+ save_audio_final(content_tensor, temp_content_path, content_sr)
235
+ save_audio_final(ref_tensor, temp_reference_path, ref_sr)
 
236
 
237
  print(f"[{session_id}] Processing...")
238
 
239
  pipeline = get_pipeline()
240
 
 
241
  gen_audio = pipeline.inference_fm(
242
  src_wav_path=temp_content_path,
243
  timbre_ref_wav_path=temp_reference_path,
 
247
  if torch.isnan(gen_audio).any() or torch.isinf(gen_audio).any():
248
  gen_audio = torch.nan_to_num(gen_audio, nan=0.0, posinf=0.95, neginf=-0.95)
249
 
250
+ # اینجا فایل را دقیقاً به اندازه ورودی برش می‌زنیم
251
+ # این کار باعث می‌شود نویز اضافه‌ای که مدل در پایان تولید کرده حذف شود
252
+ save_audio_final(gen_audio, output_path, 24000, target_length=target_length_samples)
253
+
254
  return output_path
255
 
256
  finally:
257
  if os.path.exists(temp_content_path): os.remove(temp_content_path)
258
  if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
259
 
260
+ with gr.Blocks(title="Vevo-Timbre (Clean)") as demo:
261
  gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
262
 
263
  with gr.Row():