PHP code example of afaya / edge-tts

1. Go to this page and download the library: Download afaya/edge-tts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

afaya / edge-tts example snippets




faya\EdgeTTS\Service\EdgeTTS;

$tts = new EdgeTTS();
$tts->synthesize('Hello, world!', 'en-US-AriaNeural');

echo $tts->toFile('hello'); // hello.mp3, the extension comes from the format

$tts = new EdgeTTS([
    'wssUrl'          => 'wss://api.msedgeservices.com/tts/cognitiveservices/websocket/v1',
    'voicesUrl'       => 'https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list',
    'secMsGecVersion' => '1-155.0.4283.45',
    'headers'         => ['User-Agent' => '...'], // added to the default headers
    'timeout'         => 30,                      // seconds without data before giving up
    'voicesCacheTtl'  => 600,                     // seconds to reuse the voice list, 0 to always fetch it
]);

$voices   = $tts->getVoices();                  // every voice
$english  = $tts->getVoicesByLanguage('en');    // en-US, en-GB, en-IN...
$american = $tts->getVoicesByLanguage('en-US');
$female   = $tts->getVoicesByGender('Female');
$voice    = $tts->checkVoice('en-US-AriaNeural'); // throws InvalidArgumentException when it does not exist

foreach ($voices as $voice) {
    echo $voice['ShortName'] . ' ' . $voice['FriendlyName'] . PHP_EOL; // en-US-AriaNeural Aria (Neural) - en-US
}

use Afaya\EdgeTTS\Config\Constants;

$tts->synthesize('Hello, world!', 'en-US-AriaNeural', [
    'rate'         => '+50%',  // -100% and up
    'volume'       => '-10%',  // -100% to +100%
    'pitch'        => '+20Hz', // -100Hz to +100Hz
    'outputFormat' => Constants::OUTPUT_FORMAT['AUDIO_24KHZ_96KBITRATE_MONO_MP3'],
    'timeout'      => 60,      // seconds, for this call only
]);

use Afaya\EdgeTTS\Exception\EmptyAudioException;

try {
    $tts->synthesize('这是一个测试', 'en-US-AriaNeural');
} catch (EmptyAudioException $e) {
    // Use a Chinese voice instead, such as zh-CN-XiaoxiaoNeural.
}

header('Content-Type: audio/mpeg');

foreach ($tts->stream('Long text to stream...', 'en-US-AriaNeural') as $chunk) {
    echo $chunk;
    flush();
}

$tts->synthesizeStream('Long text to stream...', 'en-US-AriaNeural', [], function (string $chunk) {
    file_put_contents('out.mp3', $chunk, FILE_APPEND);
});

$result = $tts->generate('¿Cómo estás? ¡Qué bien!', 'es-CO-SalomeNeural');

$result->audio;              // binary string
$result->format;             // 'mp3'
$result->outputFormat;       // 'audio-24khz-48kbitrate-mono-mp3'
$result->wordBoundaries;
$result->sentenceBoundaries;
$result->duration;           // estimated, in seconds

$result->toFile('greeting'); // greeting.mp3
$result->toSRT();            // or toVTT()

$raw    = $tts->toRaw();            // binary string
$base64 = $tts->toBase64();         // base64 string
$path   = $tts->toFile('out/audio'); // out/audio.mp3
$path   = $tts->toFile('out/audio.mp3'); // out/audio.mp3, the extension is not repeated
$path   = $tts->toFile('out/audio', 'bin'); // out/audio.bin
$stream = $tts->toStream();         // php://memory resource

$info = $tts->getAudioInfo();       // ['size' => 46944, 'format' => 'mp3', 'estimatedDuration' => 3.912]
$seconds = $tts->getDuration();     // estimated from the size and bitrate
$bytes = $tts->getSizeBytes();

foreach ($tts->getWordBoundaries() as $word) {
    // Offsets and durations are in 100-nanosecond units.
    printf("%.2fs %s\n", $word['offset'] / 1e7, $word['text']);
}

$tts->getSentenceBoundaries(); // the same, one per sentence
$tts->saveMetadata('words.json');

file_put_contents('speech.srt', $tts->toSRT()); // one cue per sentence
file_put_contents('speech.vtt', $tts->toVTT());

use Afaya\EdgeTTS\Service\Subtitles;

Subtitles::toSRT($tts->getWordBoundaries());    // one cue per word