|
import os
|
|
import requests
|
|
import json
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
import argparse
|
|
|
|
def synthesize_and_save(batch, voicevox_engine_url):
|
|
for task in batch:
|
|
file_name, text, char_name, style_name, style_id = task
|
|
try:
|
|
|
|
output_folder = f"{char_name}_{style_name}"
|
|
os.makedirs(output_folder, exist_ok=True)
|
|
|
|
|
|
output_file_name = os.path.join(output_folder, f"{file_name}.wav")
|
|
|
|
|
|
if os.path.exists(output_file_name):
|
|
print(f"File {output_file_name} already exists, skipping.")
|
|
continue
|
|
|
|
|
|
query_response = requests.post(
|
|
f"{voicevox_engine_url}/audio_query",
|
|
params={"text": text, "speaker": style_id},
|
|
headers={"Content-Type": "application/json"}
|
|
)
|
|
|
|
if query_response.status_code != 200:
|
|
print(f"Failed to create audio query for {file_name} with character {char_name} and style {style_name}")
|
|
continue
|
|
|
|
query = query_response.json()
|
|
|
|
|
|
synthesis_response = requests.post(
|
|
f"{voicevox_engine_url}/synthesis",
|
|
params={"speaker": style_id},
|
|
data=json.dumps(query),
|
|
headers={"Content-Type": "application/json"}
|
|
)
|
|
|
|
if synthesis_response.status_code != 200:
|
|
print(f"Failed to synthesize audio for {file_name} with character {char_name} and style {style_name}")
|
|
continue
|
|
|
|
|
|
with open(output_file_name, "wb") as out_f:
|
|
out_f.write(synthesis_response.content)
|
|
|
|
print(f"Saved {output_file_name}")
|
|
except Exception as e:
|
|
print(f"Exception occurred for {file_name} with character {char_name} and style {style_name}: {e}")
|
|
|
|
def main(max_workers, batch_size):
|
|
|
|
VOICEVOX_ENGINE_URL = "http://localhost:50021"
|
|
|
|
|
|
with open("character_config.json", "r", encoding="utf-8") as f:
|
|
CHARACTERS = json.load(f)
|
|
|
|
|
|
|
|
with open("recitation_transcript_utf8.txt", "r", encoding="utf-8") as f:
|
|
lines = f.readlines()
|
|
|
|
tasks = []
|
|
for line in lines:
|
|
parts = line.strip().split(":")
|
|
file_name = parts[0].strip()
|
|
text_parts = parts[1].split(",")
|
|
text = text_parts[0].strip()
|
|
|
|
for character in CHARACTERS:
|
|
char_name = character["name"]
|
|
for style in character["styles"]:
|
|
style_name = style["name"]
|
|
style_id = style["id"]
|
|
tasks.append((file_name, text, char_name, style_name, style_id))
|
|
|
|
|
|
batches = [tasks[i:i + batch_size] for i in range(0, len(tasks), batch_size)]
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
|
futures = [executor.submit(synthesize_and_save, batch, VOICEVOX_ENGINE_URL) for batch in batches]
|
|
|
|
for future in as_completed(futures):
|
|
future.result()
|
|
|
|
print("All tasks completed.")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Parallel voice synthesis script")
|
|
parser.add_argument("--max_workers", type=int, default=256, help="Number of parallel workers")
|
|
parser.add_argument("--batch_size", type=int, default=30, help="Number of tasks per batch")
|
|
args = parser.parse_args()
|
|
main(args.max_workers, args.batch_size)
|
|
|