Update app.py
Browse files
app.py
CHANGED
@@ -9,11 +9,35 @@ from pathlib import Path
|
|
9 |
from pydub import AudioSegment
|
10 |
|
11 |
def get_silence(duration_ms=1000):
|
12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Get all available voices
|
15 |
async def get_voices():
|
16 |
-
|
|
|
17 |
|
18 |
async def text_to_speech_segment(text_segment, voice, rate, pitch):
|
19 |
"""Processes a single text segment for voice commands and generates audio."""
|
|
|
9 |
from pydub import AudioSegment
|
10 |
|
11 |
def get_silence(duration_ms=1000):
|
12 |
+
# Create silent audio segment with specified parameters
|
13 |
+
silent_audio = AudioSegment.silent(
|
14 |
+
duration=duration_ms,
|
15 |
+
frame_rate=24000 # 24kHz sampling rate
|
16 |
+
)
|
17 |
+
|
18 |
+
# Set audio parameters
|
19 |
+
silent_audio = silent_audio.set_channels(1) # Mono
|
20 |
+
silent_audio = silent_audio.set_sample_width(4) # 32-bit (4 bytes per sample)
|
21 |
+
|
22 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
23 |
+
# Export with specific bitrate and codec parameters
|
24 |
+
silent_audio.export(
|
25 |
+
tmp_file.name,
|
26 |
+
format="mp3",
|
27 |
+
bitrate="48k",
|
28 |
+
parameters=[
|
29 |
+
"-ac", "1", # Mono
|
30 |
+
"-ar", "24000", # Sample rate
|
31 |
+
"-sample_fmt", "s32", # 32-bit samples
|
32 |
+
"-codec:a", "libmp3lame" # MP3 codec
|
33 |
+
]
|
34 |
+
)
|
35 |
+
return tmp_file.name
|
36 |
|
37 |
# Get all available voices
|
38 |
async def get_voices():
|
39 |
+
voices = await edge_tts.list_voices()
|
40 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
41 |
|
42 |
async def text_to_speech_segment(text_segment, voice, rate, pitch):
|
43 |
"""Processes a single text segment for voice commands and generates audio."""
|