Sakalti commited on
Commit
60b21a4
·
verified ·
1 Parent(s): cfb63ba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pyttsx3
3
+ import tempfile
4
+ from pydub import AudioSegment
5
+
6
+ # テンプレート設定
7
+ TEMPLATES = {
8
+ "標準 (ピッチ1.0, 速度1.0)": (1.0, 1.0),
9
+ "パラオボール (高め, 少し速い)": (1.3, 1.2),
10
+ "ウサギモード (超高め, 超速い)": (2.0, 2.0),
11
+ "クマモード (低め, ゆっくり)": (0.8, 0.8),
12
+ "スローリーバード (普通ピッチ, めっちゃゆっくり)": (1.0, 0.5)
13
+ }
14
+
15
+ def change_pitch_speed(sound, pitch_factor=1.0, speed_factor=1.0):
16
+ new_sample_rate = int(sound.frame_rate * pitch_factor)
17
+ sound = sound._spawn(sound.raw_data, overrides={"frame_rate": new_sample_rate})
18
+ sound = sound.set_frame_rate(44100)
19
+ sound = sound.speedup(playback_speed=speed_factor)
20
+ return sound
21
+
22
+ def read_text(text, template_name, custom_pitch, custom_speed, use_custom):
23
+ # テンプレート選択 or カスタム
24
+ if use_custom:
25
+ pitch = custom_pitch
26
+ speed = custom_speed
27
+ else:
28
+ pitch, speed = TEMPLATES.get(template_name, (1.0, 1.0))
29
+
30
+ # 音声合成
31
+ engine = pyttsx3.init()
32
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
33
+ tts_filename = fp.name
34
+ engine.save_to_file(text, tts_filename)
35
+ engine.runAndWait()
36
+
37
+ sound = AudioSegment.from_file(tts_filename)
38
+ modified_sound = change_pitch_speed(sound, pitch_factor=pitch, speed_factor=speed)
39
+
40
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as out_fp:
41
+ output_filename = out_fp.name
42
+ modified_sound.export(output_filename, format="mp3")
43
+
44
+ return output_filename
45
+
46
+ # Gradioインターフェース
47
+ iface = gr.Interface(
48
+ fn=read_text,
49
+ inputs=[
50
+ gr.Textbox(label="読み上げるテキスト"),
51
+ gr.Dropdown(choices=list(TEMPLATES.keys()), value="標準 (ピッチ1.0, 速度1.0)", label="テンプレートを選択"),
52
+ gr.Slider(minimum=0.5, maximum=3.0, step=0.05, value=1.0, label="カスタムピッチ(使う場合のみ)"),
53
+ gr.Slider(minimum=0.5, maximum=3.0, step=0.05, value=1.0, label="カスタム速度(使う場合のみ)"),
54
+ gr.Checkbox(label="カスタム設定を使う(オンなら上のスライダー反映)", value=False)
55
+ ],
56
+ outputs=gr.Audio(label="生成された音声"),
57
+ title="パラオボール声 読み上げ機 テンプレート版",
58
+ description="テンプレートを選ぶか、カスタムでピッチ・速度を指定して、自由に音声を作ろう!"
59
+ )
60
+
61
+ iface.launch()