Spaces:
Running
on
L4
Running
on
L4
wanglamao
commited on
Commit
·
71d4610
1
Parent(s):
f1fdc79
add trim logic
Browse files
app.py
CHANGED
|
@@ -53,12 +53,16 @@ def preprocess_audio(audio_path):
|
|
| 53 |
# Validate audio duration
|
| 54 |
is_valid, duration = validate_audio_duration(audio_path)
|
| 55 |
if not is_valid:
|
| 56 |
-
logger.
|
| 57 |
-
raise ValueError(f"Audio duration cannot exceed {MAX_AUDIO_DURATION}s, current is {duration:.2f}s")
|
| 58 |
|
| 59 |
# Load audio with librosa: automatically resamples to sr=16000 and converts to mono
|
| 60 |
y, _ = librosa.load(audio_path, sr=16000, mono=True)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Save processed audio to a new file to avoid conflicts
|
| 63 |
dir_name = os.path.dirname(audio_path)
|
| 64 |
base_name = os.path.basename(audio_path)
|
|
@@ -110,8 +114,8 @@ def process_tts_a(text, ref_audio):
|
|
| 110 |
# Validate text length
|
| 111 |
is_valid, text_len = validate_text_length(text)
|
| 112 |
if not is_valid:
|
| 113 |
-
logger.
|
| 114 |
-
|
| 115 |
|
| 116 |
# Preprocess audio
|
| 117 |
ref_audio = preprocess_audio(ref_audio)
|
|
|
|
| 53 |
# Validate audio duration
|
| 54 |
is_valid, duration = validate_audio_duration(audio_path)
|
| 55 |
if not is_valid:
|
| 56 |
+
logger.warning(f"Audio duration {duration:.2f}s exceeds max limit {MAX_AUDIO_DURATION}s. Truncating.")
|
|
|
|
| 57 |
|
| 58 |
# Load audio with librosa: automatically resamples to sr=16000 and converts to mono
|
| 59 |
y, _ = librosa.load(audio_path, sr=16000, mono=True)
|
| 60 |
|
| 61 |
+
# Truncate if exceeds max duration
|
| 62 |
+
max_samples = int(MAX_AUDIO_DURATION * 16000)
|
| 63 |
+
if len(y) > max_samples:
|
| 64 |
+
y = y[:max_samples]
|
| 65 |
+
|
| 66 |
# Save processed audio to a new file to avoid conflicts
|
| 67 |
dir_name = os.path.dirname(audio_path)
|
| 68 |
base_name = os.path.basename(audio_path)
|
|
|
|
| 114 |
# Validate text length
|
| 115 |
is_valid, text_len = validate_text_length(text)
|
| 116 |
if not is_valid:
|
| 117 |
+
logger.warning(f"Text length {text_len} exceeds max limit {MAX_TEXT_LENGTH}. Truncating.")
|
| 118 |
+
text = text[:MAX_TEXT_LENGTH]
|
| 119 |
|
| 120 |
# Preprocess audio
|
| 121 |
ref_audio = preprocess_audio(ref_audio)
|