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);
});
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.