File size: 2,140 Bytes
4efe6b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import librosa
import gradio as gr
import os
from matplotlib import pyplot as plt

from rvc.lib.predictors.F0Extractor import F0Extractor

from assets.i18n.i18n import I18nAuto

i18n = I18nAuto()


def extract_f0_curve(audio_path: str, method: str) -> tuple:
    print("Extracting F0 Curve...")
    image_path = os.path.join("logs", "f0_plot.png")
    txt_path = os.path.join("logs", "f0_curve.txt")
    y, sr = librosa.load(audio_path, sr=None)
    hop_length = 160

    librosa.note_to_hz("C1")
    librosa.note_to_hz("C8")

    f0_extractor = F0Extractor(audio_path, sample_rate=sr, method=method)
    f0 = f0_extractor.extract_f0()

    plt.figure(figsize=(10, 4))
    plt.plot(f0)
    plt.title(method)
    plt.xlabel("Time (frames)")
    plt.ylabel("Frequency (Hz)")
    plt.savefig(image_path)
    plt.close()

    with open(txt_path, "w") as txtfile:
        for i, f0_value in enumerate(f0):
            frequency = i * sr / hop_length
            txtfile.write(f"{frequency},{f0_value}\n")

    print("F0 Curve extracted successfully!")
    return image_path, txt_path


def f0_extractor_tab():
    with gr.Accordion(label=i18n("Extract F0 Curve")):
        with gr.Row():
            audio = gr.Audio(label=i18n("Upload Audio"), type="filepath")
            f0_method = gr.Radio(
                label=i18n("Pitch extraction algorithm"),
                info=i18n(
                    "Pitch extraction algorithm to use for the audio conversion. The default algorithm is rmvpe, which is recommended for most cases."
                ),
                choices=["crepe", "fcpe", "rmvpe"],
                value="rmvpe",
            )
        button = gr.Button(i18n("Extract F0 Curve"), variant="primary")

    with gr.Accordion(label=i18n("Output Information")):
        txt_output = gr.File(label="F0 Curve", type="filepath")
        image_output = gr.Image(type="filepath", interactive=False)

        button.click(
            fn=extract_f0_curve,
            inputs=[
                audio,
                f0_method,
            ],
            outputs=[image_output, txt_output],
            api_name="f0_extract",
        )