text_to_speech

text_to_speech(string text, string output_path, string method = "api", string voice = "", string model = "tts-1", string api_key = "", int timeout = 60): bool

Преобразовать текст в звук через OpenAI API или локальные инструменты (piper-tts)

Команда на вход принимает параметры:
  • text – текст для преобразования
  • output_path – путь к выходному аудиофайлу
  • method – метод преобразования ("api" по умолчанию или "local")
  • voice – голос для синтеза (для api: "alloy", "echo", "fable", "onyx", "nova", "shimmer", по умолчанию "alloy"; для local: имя модели голоса, по умолчанию "ru_RU-denis-medium")
  • model – модель для api ("tts-1" или "tts-1-hd", по умолчанию "tts-1")
  • api_key – ключ OpenAI API (если не указан, берется из настроек)
  • timeout – таймаут выполнения в секундах (по умолчанию 60)
После отработки команда возвращает результат своей работы в робот:
  • true/false - Успешно? Да/Нет



Пример использования (php)

<?php
// Scenario: Convert text to speech using various methods and voices
 
$xhe_host = "127.0.0.1:7010";
// Connect functional objects if not already connected
if (!isset($path)){
  $path = "../../../Templates/init.php";
  require($path);
}
// info
echo "\n<span >sound->" . basename (__FILE__) . "</span>\n";
 
// Example 1: Convert text to speech using API (default method)
// Input data
$text1 = "Hello, this is a test message.";
$outputFile1 = "test/tts_api_output.mp3";
 
echo("\n\nExample 1. Convert text to speech using API (default method): ");
$result1 = SYSTEM::$sound->text_to_speech($text1, $outputFile1);
 
if ($result1) {
    echo("true\n");
} else {
    echo("false\n");
}
 
// Example 2: Convert text to speech using API with specified voice
// Input data
$text2 = "Привет, это тестовое сообщение.";
$outputFile2 = "test/tts_api_voice.mp3";
$method2 = "api";
$voice2 = "nova";
 
echo("\n\nExample 2. Convert text to speech using API with specified voice: ");
$result2 = SYSTEM::$sound->text_to_speech($text2, $outputFile2, $method2, $voice2);
 
if ($result2) {
    echo("true\n");
} else {
    echo("false\n");
}
 
// Example 3: Convert text to speech using local method (piper-tts)
// Input data
$text3 = "Test message for local TTS.";
$outputFile3 = "test/tts_local_output.wav";
$method3 = "local";
 
echo("\n\nExample 3. Convert text to speech using local method (piper-tts): ");
$result3 = SYSTEM::$sound->text_to_speech($text3, $outputFile3, $method3);
 
if ($result3) {
    echo("true\n");
} else {
    echo("false\n");
}
 
// Example 4: Convert text to speech using local method with specified voice model
// Input data
$text4 = "Тестовое сообщение для локального синтеза.";
$outputFile4 = "test/tts_local_voice.wav";
$method4 = "local";
$voice4 = "ru_RU-denis-medium";
 
echo("\n\nExample 4. Convert text to speech using local method with specified voice model: ");
$result4 = SYSTEM::$sound->text_to_speech($text4, $outputFile4, $method4, $voice4);
 
if ($result4) {
    echo("true\n");
} else {
    echo("false\n");
}
 
// Quit the application
WINDOW::$app->quit();
?>