Spaces:
Running
Running
| 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 | |