File size: 14,948 Bytes
6eac6e1
 
 
 
 
 
 
 
 
 
 
d7672e1
b78fc58
d081a94
 
6eac6e1
d081a94
6eac6e1
 
 
 
 
 
 
 
 
 
 
3fbd4a0
6eac6e1
 
 
 
 
 
 
 
 
 
3fbd4a0
6eac6e1
3fbd4a0
6eac6e1
 
 
 
3fbd4a0
 
 
 
 
6eac6e1
3fbd4a0
6eac6e1
3fbd4a0
6eac6e1
3fbd4a0
 
 
d7672e1
6eac6e1
 
 
 
 
 
 
 
 
 
 
 
a4df331
 
428894f
a4df331
 
501163c
 
 
 
97b11e9
a4df331
 
6eac6e1
 
3fbd4a0
6eac6e1
 
2775a80
6eac6e1
 
 
 
3fbd4a0
6eac6e1
3fbd4a0
6eac6e1
 
 
 
 
 
 
 
 
428894f
6eac6e1
2457c1e
6eac6e1
 
2457c1e
6eac6e1
 
2457c1e
6eac6e1
 
 
 
 
 
 
3fbd4a0
2457c1e
3fbd4a0
d7672e1
 
 
 
 
3fbd4a0
 
 
 
6eac6e1
d081a94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6eac6e1
 
d7672e1
 
 
 
6eac6e1
 
 
 
d7672e1
501163c
 
d081a94
a6cd2a1
 
 
 
2457c1e
a6cd2a1
 
501163c
 
a6cd2a1
339799c
a6cd2a1
d081a94
d7672e1
 
 
 
2457c1e
 
d7672e1
501163c
 
2457c1e
501163c
 
2775a80
9622192
428894f
d081a94
 
 
 
d7672e1
9454331
d081a94
501163c
d081a94
 
 
3a081bb
d081a94
 
501163c
9454331
 
428894f
 
f375b6c
428894f
 
bb4972a
428894f
f375b6c
2457c1e
09eb27e
d081a94
339799c
d081a94
 
9454331
d081a94
 
 
 
 
501163c
d081a94
 
 
 
 
 
 
9454331
 
d081a94
501163c
d081a94
 
428894f
d081a94
 
 
3b10964
9454331
 
 
 
 
 
d7672e1
 
 
 
 
6eac6e1
d081a94
501163c
d081a94
3fbd4a0
 
 
a6cd2a1
 
 
3fbd4a0
d7672e1
6eac6e1
3fbd4a0
6eac6e1
380e75f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import sys
import importlib.util
import site
import json
import torch
import gradio as gr
import torchaudio
import numpy as np
from huggingface_hub import snapshot_download, hf_hub_download
import subprocess
import uuid
import soundfile as sf
import spaces
import librosa

# --- 1. نصب و راه‌اندازی ---
downloaded_resources = {
    "configs": False,
    "tokenizer_vq8192": False,
    "fmt_Vq8192ToMels": False,
    "vocoder": False
}

def install_espeak():
    try:
        result = subprocess.run(["which", "espeak-ng"], capture_output=True, text=True)
        if result.returncode != 0:
            print("Installing espeak-ng...")
            subprocess.run(["apt-get", "update"], check=True)
            subprocess.run(["apt-get", "install", "-y", "espeak-ng", "espeak-ng-data"], check=True)
    except Exception as e:
        print(f"Error installing espeak-ng: {e}")

install_espeak()

def patch_langsegment_init():
    try:
        spec = importlib.util.find_spec("LangSegment")
        if spec is None or spec.origin is None: return
        init_path = os.path.join(os.path.dirname(spec.origin), '__init__.py')
        with open(init_path, 'r') as f: lines = f.readlines()
        modified = False
        new_lines = []
        target_line_prefix = "from .LangSegment import"
        for line in lines:
            if line.strip().startswith(target_line_prefix) and ('setLangfilters' in line or 'getLangfilters' in line):
                mod_line = line.replace(',setLangfilters', '').replace(',getLangfilters', '')
                mod_line = mod_line.replace('setLangfilters,', '').replace('getLangfilters,', '').rstrip(',') 
                new_lines.append(mod_line + '\n')
                modified = True
            else:
                new_lines.append(line)
        if modified:
            with open(init_path, 'w') as f: f.writelines(new_lines)
            try:
                import LangSegment
                importlib.reload(LangSegment)
            except: pass
    except: pass

patch_langsegment_init()

if not os.path.exists("Amphion"):
    subprocess.run(["git", "clone", "https://github.com/open-mmlab/Amphion.git"])
    os.chdir("Amphion")
if os.path.dirname(os.path.abspath("Amphion")) not in sys.path:
    sys.path.append(os.path.dirname(os.path.abspath("Amphion")))

os.makedirs("wav", exist_ok=True)
os.makedirs("ckpts/Vevo", exist_ok=True)

from models.vc.vevo.vevo_utils import VevoInferencePipeline

def save_audio_pcm16(waveform, output_path, sample_rate=24000):
    try:
        if isinstance(waveform, torch.Tensor):
            waveform = waveform.detach().cpu()
            if waveform.dim() == 2 and waveform.shape[0] == 1:
                waveform = waveform.squeeze(0)
            waveform = waveform.numpy()
        sf.write(output_path, waveform, sample_rate, subtype='PCM_16')
    except Exception as e:
        print(f"Save error: {e}")

def setup_configs():
    if downloaded_resources["configs"]: return
    config_path = "models/vc/vevo/config"
    os.makedirs(config_path, exist_ok=True)
    config_files = ["Vq8192ToMels.json", "Vocoder.json"]
    for file in config_files:
        file_path = f"{config_path}/{file}"
        if not os.path.exists(file_path):
            try:
                file_data = hf_hub_download(repo_id="amphion/Vevo", filename=f"config/{file}", repo_type="model")
                subprocess.run(["cp", file_data, file_path])
            except Exception as e: print(f"Error downloading config {file}: {e}")
    downloaded_resources["configs"] = True

setup_configs()
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

inference_pipelines = {}

def preload_all_resources():
    setup_configs()
    global downloaded_content_style_tokenizer_path, downloaded_fmt_path, downloaded_vocoder_path
    if not downloaded_resources["tokenizer_vq8192"]:
        downloaded_content_style_tokenizer_path = snapshot_download(repo_id="amphion/Vevo", repo_type="model", cache_dir="./ckpts/Vevo", allow_patterns=["tokenizer/vq8192/*"])
        downloaded_resources["tokenizer_vq8192"] = True
    if not downloaded_resources["fmt_Vq8192ToMels"]:
        downloaded_fmt_path = snapshot_download(repo_id="amphion/Vevo", repo_type="model", cache_dir="./ckpts/Vevo", allow_patterns=["acoustic_modeling/Vq8192ToMels/*"])
        downloaded_resources["fmt_Vq8192ToMels"] = True
    if not downloaded_resources["vocoder"]:
        downloaded_vocoder_path = snapshot_download(repo_id="amphion/Vevo", repo_type="model", cache_dir="./ckpts/Vevo", allow_patterns=["acoustic_modeling/Vocoder/*"])
        downloaded_resources["vocoder"] = True

downloaded_content_style_tokenizer_path = None
downloaded_fmt_path = None
downloaded_vocoder_path = None
preload_all_resources()

def get_pipeline():
    if "timbre" in inference_pipelines: return inference_pipelines["timbre"]
    pipeline = VevoInferencePipeline(
        content_style_tokenizer_ckpt_path=os.path.join(downloaded_content_style_tokenizer_path, "tokenizer/vq8192"),
        fmt_cfg_path="./models/vc/vevo/config/Vq8192ToMels.json",
        fmt_ckpt_path=os.path.join(downloaded_fmt_path, "acoustic_modeling/Vq8192ToMels"),
        vocoder_cfg_path="./models/vc/vevo/config/Vocoder.json",
        vocoder_ckpt_path=os.path.join(downloaded_vocoder_path, "acoustic_modeling/Vocoder"),
        device=device,
    )
    inference_pipelines["timbre"] = pipeline
    return pipeline

# --- 2. الگوریتم برش فوق هوشمند ---
def find_advanced_split_points(audio_np, sr):
    """
    پیدا کردن نقاط برش با استراتژی فال‌بک (Fallback Strategy):
    ۱. تلاش برای پیدا کردن سکوت در بازه ۸ تا ۱۲ ثانیه.
    ۲. اگر نشد، تلاش در بازه وسیع‌تر ۶ تا ۱۴ ثانیه.
    ۳. انتخاب نقطه با کمترین انرژی (حتی اگر سکوت نباشد).
    ۴. تنظیم دقیق روی نزدیک‌ترین Zero-Crossing.
    """
    total_samples = len(audio_np)
    
    # تنظیمات بازه جستجو
    MIN_PREFERRED = 8.0
    MAX_PREFERRED = 12.0
    MIN_HARD = 6.0
    MAX_HARD = 15.0
    
    split_points = [0]
    current_pos = 0
    
    hop_length = 512
    frame_length = 1024
    
    while current_pos < total_samples:
        # استراتژی ۱: بازه ایده‌آل
        start_search = current_pos + int(MIN_PREFERRED * sr)
        end_search = current_pos + int(MAX_PREFERRED * sr)
        
        # اگر به انتهای فایل نزدیکیم
        if start_search >= total_samples:
            split_points.append(total_samples)
            break
        
        end_search = min(end_search, total_samples)
        
        # استراتژی ۲: اگر بازه ایده‌آل خیلی کوتاه است (ته فایل)، گسترش بده
        if end_search - start_search < sr:
             # استفاده از بازه سخت (وسیع)
             start_search = current_pos + int(MIN_HARD * sr)
             end_search = current_pos + int(MAX_HARD * sr)
             start_search = min(start_search, total_samples)
             end_search = min(end_search, total_samples)

        # برش منطقه جستجو
        region = audio_np[start_search:end_search]
        
        if len(region) == 0:
            split_points.append(total_samples)
            break

        # محاسبه انرژی
        rms = librosa.feature.rms(y=region, frame_length=frame_length, hop_length=hop_length)[0]
        
        # پیدا کردن کم‌انرژی‌ترین نقطه (Local Minimum)
        min_idx = np.argmin(rms)
        local_cut_sample = min_idx * hop_length
        
        # --- تکنیک Zero Crossing ---
        # نقطه برش تقریبی را پیدا کردیم. حالا باید دقیقاً روی محور صفر برش دهیم
        # تا صدای "کلیک" ایجاد نشود.
        
        cut_absolute_approx = start_search + local_cut_sample
        
        # جستجو در اطراف نقطه تقریبی (±500 نمونه) برای پیدا کردن صفر
        search_radius = 500
        zc_start = max(0, cut_absolute_approx - search_radius)
        zc_end = min(total_samples, cut_absolute_approx + search_radius)
        
        zc_region = audio_np[zc_start:zc_end]
        
        # پیدا کردن نزدیک‌ترین عبور از صفر
        # (جایی که علامت عدد تغییر می‌کند)
        zero_crossings = np.where(np.diff(np.signbit(zc_region)))[0]
        
        if len(zero_crossings) > 0:
            # نزدیک‌ترین صفر به وسط بازه جستجو
            closest_zc = zero_crossings[np.argmin(np.abs(zero_crossings - search_radius))]
            best_cut_absolute = zc_start + closest_zc
        else:
            # اگر صفر پیدا نشد (خیلی بعید)، همان نقطه کم‌انرژی را بگیر
            best_cut_absolute = cut_absolute_approx
            
        split_points.append(best_cut_absolute)
        current_pos = best_cut_absolute
        
    return split_points

@spaces.GPU()
def vevo_timbre(content_wav, reference_wav):
    session_id = str(uuid.uuid4())[:8]
    temp_content_path = f"wav/c_{session_id}.wav"
    temp_reference_path = f"wav/r_{session_id}.wav"
    output_path = f"wav/out_{session_id}.wav"
    
    if content_wav is None or reference_wav is None:
        raise ValueError("Please upload audio files")
    
    try:
        SR = 24000
        
        # --- ورودی ---
        if isinstance(content_wav, tuple):
            content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
        else:
            content_sr, content_data = content_wav
        if len(content_data.shape) > 1: content_data = np.mean(content_data, axis=1)
        
        content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
        if content_sr != SR:
            content_tensor = torchaudio.functional.resample(content_tensor, content_sr, SR)
        content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
        content_full_np = content_tensor.squeeze().numpy()

        # --- رفرنس ---
        if isinstance(reference_wav, tuple):
            ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
        else:
            ref_sr, ref_data = reference_wav
        if len(ref_data.shape) > 1: ref_data = np.mean(ref_data, axis=1)
        
        ref_tensor = torch.FloatTensor(ref_data).unsqueeze(0)
        if ref_sr != SR:
            ref_tensor = torchaudio.functional.resample(ref_tensor, ref_sr, SR)
        ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
        if ref_tensor.shape[1] > SR * 20: ref_tensor = ref_tensor[:, :SR * 20]
        save_audio_pcm16(ref_tensor, temp_reference_path, SR)
        
        pipeline = get_pipeline()
        
        # --- تقسیم‌بندی پیشرفته ---
        print(f"[{session_id}] Finding best energy split points (Zero-Crossing)...")
        split_points = find_advanced_split_points(content_full_np, SR)
        print(f"[{session_id}] Split into {len(split_points)-1} chunks.")
        
        final_output = []
        PADDING_SAMPLES = int(2.5 * SR) # کمی پدینگ بیشتر برای اطمینان
        
        for i in range(len(split_points) - 1):
            start = split_points[i]
            end = split_points[i+1]
            
            read_start = max(0, start - PADDING_SAMPLES)
            read_end = end
            
            chunk_input = content_full_np[read_start:read_end]
            save_audio_pcm16(torch.FloatTensor(chunk_input).unsqueeze(0), temp_content_path, SR)
            
            try:
                gen = pipeline.inference_fm(
                    src_wav_path=temp_content_path,
                    timbre_ref_wav_path=temp_reference_path,
                    flow_matching_steps=32,
                )
                if torch.isnan(gen).any(): gen = torch.nan_to_num(gen, nan=0.0)
                gen_np = gen.detach().cpu().squeeze().numpy()
                
                trim_amount = start - read_start
                
                if len(gen_np) > trim_amount:
                    valid_audio = gen_np[trim_amount:]
                    
                    # اتصال
                    if len(final_output) > 0:
                        # اگر برش روی سکوت نبوده (اجباری)، باید کمی بیشتر کراس‌فید کنیم
                        # تا تغییر ناگهانی لحن مخفی شود.
                        fade_len = int(0.03 * SR) # 30ms standard
                        
                        if len(final_output[-1]) > fade_len and len(valid_audio) > fade_len:
                            fade_out = np.linspace(1, 0, fade_len)
                            fade_in = np.linspace(0, 1, fade_len)
                            
                            prev_tail = final_output[-1][-fade_len:]
                            curr_head = valid_audio[:fade_len]
                            
                            mixed = (prev_tail * fade_out) + (curr_head * fade_in)
                            final_output[-1][-fade_len:] = mixed
                            valid_audio = valid_audio[fade_len:]
                            
                    final_output.append(valid_audio)
                    
            except Exception as e:
                print(f"Error segment {i}: {e}")
                # پر کردن جای خالی با سکوت برای به هم نریختن تایم
                final_output.append(np.zeros(end - start))

        if len(final_output) > 0:
            full_audio = np.concatenate(final_output)
        else:
            full_audio = np.zeros(SR)
            
        save_audio_pcm16(full_audio, output_path, SR)
        return output_path

    finally:
        if os.path.exists(temp_content_path): os.remove(temp_content_path)
        if os.path.exists(temp_reference_path): os.remove(temp_reference_path)

with gr.Blocks(title="Vevo-Timbre (Pro Logic)") as demo:
    gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
    gr.Markdown("Robust Splitting: Uses Minimum Energy + Zero Crossing detection to handle fast speech without glitches.")
    
    with gr.Row():
        with gr.Column():
            timbre_content = gr.Audio(label="Source Audio", type="numpy")
            timbre_reference = gr.Audio(label="Target Timbre", type="numpy")
            timbre_button = gr.Button("Generate", variant="primary")
        with gr.Column():
            timbre_output = gr.Audio(label="Result")
            
    timbre_button.click(vevo_timbre, inputs=[timbre_content, timbre_reference], outputs=timbre_output)

demo.launch()