File size: 2,717 Bytes
47acec0
 
 
43dc84c
47acec0
 
 
 
 
 
 
 
 
0372022
 
 
 
 
47acec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import gradio as gr
import spaces

from audio_processing import AudioProcessingError, convert_voice


DEFAULT_PITCH = 0
DEFAULT_LOW_CUT = 20
DEFAULT_HIGH_CUT = 7_900


@spaces.GPU(duration=1)
def zero_gpu_startup_probe():
    return None


def run_conversion(audio_input, pitch_semitones, low_cut_hz, high_cut_hz):
    try:
        sample_rate, waveform, message = convert_voice(
            audio_input,
            pitch_semitones,
            low_cut_hz,
            high_cut_hz,
        )
        return (sample_rate, waveform), message
    except AudioProcessingError as error:
        return None, str(error)
    except Exception as error:
        return None, f"音声変換中にエラーが発生しました: {error}"


def reset_settings():
    return DEFAULT_PITCH, DEFAULT_LOW_CUT, DEFAULT_HIGH_CUT


with gr.Blocks(title="音声パラメータ実験室") as demo:
    gr.Markdown(
        """
        # 音声パラメータ実験室 / Voice Parameter Playground

        音声をアップロードまたは録音し、声の高さと通過させる周波数帯域を変更できます。
        スライダーを調整して「音声を変換」を押してください。
        """
    )

    audio_input = gr.Audio(
        label="入力音声",
        sources=["upload", "microphone"],
        type="numpy",
    )

    pitch_slider = gr.Slider(
        minimum=-12,
        maximum=12,
        value=DEFAULT_PITCH,
        step=1,
        label="声の高さ(半音)",
    )
    low_cut_slider = gr.Slider(
        minimum=20,
        maximum=3_000,
        value=DEFAULT_LOW_CUT,
        step=10,
        label="低域カットオフ周波数(Hz)",
    )
    high_cut_slider = gr.Slider(
        minimum=500,
        maximum=7_900,
        value=DEFAULT_HIGH_CUT,
        step=100,
        label="高域カットオフ周波数(Hz)",
    )

    with gr.Row():
        convert_button = gr.Button("音声を変換", variant="primary")
        reset_button = gr.Button("設定をリセット")

    audio_output = gr.Audio(label="加工後音声", type="numpy")
    status_output = gr.Textbox(label="処理内容またはエラーメッセージ", lines=4)

    convert_button.click(
        fn=run_conversion,
        inputs=[
            audio_input,
            pitch_slider,
            low_cut_slider,
            high_cut_slider,
        ],
        outputs=[audio_output, status_output],
        show_progress=True,
    )
    reset_button.click(
        fn=reset_settings,
        inputs=None,
        outputs=[pitch_slider, low_cut_slider, high_cut_slider],
        show_progress=False,
    )


if __name__ == "__main__":
    demo.launch()