miko-tts / util.py
zirobtc's picture
Uploading DART folder into model repo
478eeb0 verified
import wave
import io
import os
import json
import requests
import yaml
def load_yaml():
"""
Loads a YAML file and extracts the 'model_name' string.
"""
file_path = "../config.yaml"
try:
with open(file_path, 'r') as file:
config = yaml.safe_load(file)
return config
except FileNotFoundError:
print(f"Error: The file '{file_path}' was not found.")
return None
except yaml.YAMLError as e:
print(f"Error parsing YAML file: {e}")
return None
config = load_yaml()
SYNC_PORT = config["SYNC_PORT"]
MOTION_SYNC_URL = f"http://localhost:{SYNC_PORT}/update_duration"
def update_motion_generator_duration(scene_id: str, motion_index: int, duration: float):
"""Sends the actual audio duration to the motion generator's sync server."""
try:
payload = {
"sceneId": scene_id,
"motionIndex": motion_index,
"duration": duration
}
# This sends the data to the server running inside the motion generator script
response = requests.post(MOTION_SYNC_URL, json=payload, timeout=5)
if response.status_code == 200:
print(f"[ sent duration for {scene_id}:{motion_index} ]")
else:
print(f"⚠️ Failed to notify motion generator for {scene_id}_{motion_index}. Status: {response.status_code}, Response: {response.text}")
except requests.exceptions.RequestException as e:
print(f"⚠️ Error connecting to motion generator sync server: {e}")
def calculate_duration_from_bytes(audio_bytes: bytes) -> float:
"""Calculates audio duration in seconds directly from WAV bytes in memory."""
try:
with wave.open(io.BytesIO(audio_bytes), 'rb') as wf:
frames = wf.getnframes()
rate = wf.getframerate()
return frames / float(rate) if rate > 0 else 0.0
except (wave.Error, ZeroDivisionError) as e:
print(f"⚠️ Could not calculate duration from bytes: {e}")
return 0.0