Spaces:
Running
Running
File size: 1,938 Bytes
3ee1b47 d4e069a 3ee1b47 |
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 |
import re
import numpy as np
import soundfile as sf
import os
import tempfile
from pydub import AudioSegment
import io
class ScriptProcessor:
def __init__(self, engine):
self.engine = engine
def split_text_into_chunks(self, text, max_chars=500):
"""
Splits text into chunks based on sentence boundaries.
"""
# Clean text
text = text.replace('\n', ' ').strip()
# Split by sentence boundaries but keep the punctuation
sentences = re.split('(?<=[.!?]) +', text)
chunks = []
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) < max_chars:
current_chunk += " " + sentence
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def process_long_script(self, text, voice, speed=1.0, lang='a'):
"""
Processes a long script by chunking, generating audio for each, and merging.
"""
chunks = self.split_text_into_chunks(text)
print(f"Split script into {len(chunks)} chunks.")
combined_audio = []
for i, chunk in enumerate(chunks):
print(f"Processing chunk {i+1}/{len(chunks)}...")
audio, _ = self.engine.generate(chunk, voice=voice, speed=speed, lang=lang)
combined_audio.append(audio)
# Concatenate numpy arrays
final_audio = np.concatenate(combined_audio)
return final_audio, 24000
def save_audio(self, audio_data, sample_rate, output_path):
"""
Saves numpy audio data to a file.
"""
sf.write(output_path, audio_data, sample_rate)
return output_path
|