You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
low-carbon-platform-php/lib/StreamApiClient.php

103 lines
3.6 KiB

<?php
namespace OpenAPI\Client;
use Amp\Loop;
use Amp\Websocket\Client\Handshake;
use function Amp\Websocket\Client\connect;
use OpenAPI\Client\Model\SpeechResult;
use OpenAPI\Client\Model\SynthesizerParam;
// 流请求API客户端
class StreamApiClient
{
/**
* @var ApiClient api客户端
*/
protected ApiClient $apiClient;
/**
* @var Configuration api配置
*/
protected Configuration $config;
/**
* @param Configuration $config api配置
*/
function __construct(Configuration $config)
{
$this->config = $config;
$this->apiClient = new ApiClient($config);
}
/**
* 语音转文字
* @param string $binaryData 16k采样率的音频二进制数据
* @param StreamResponseHandler $handler 流响应处理器
* @return void
* @throws ApiException
*/
public function speechToText(string $binaryData, StreamResponseHandler $handler): void
{
$path = "/ws/speechToText";
$host = $this->config->getHost();
$host = str_replace(["http", "https"], ["ws", "wss"], $host) . $path;
$signParams = [
'payload' => null,
'path' => $path,
'charset' => $this->config->getCharset(),
'time' => date($this->config->getDataFormat(), time()),
'sign_type' => $this->config->getSignType(),
'access_token' => null,
'app_id' => $this->config->getAppid(),
'sign' => ''
];
$signParams['sign'] = $this->apiClient->requestSign($signParams);
Loop::run(function () use ($host, $signParams, $binaryData, $handler) {
$handshake = (new Handshake($host))->withHeaders($signParams);
$connection = yield connect($handshake);
yield $connection->sendBinary($binaryData);
while ($message = yield $connection->receive()) {
$payload = yield $message->buffer();
$content = json_decode($payload);
$result = ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\SpeechResult', []);
if ($result instanceof SpeechResult) {
$handler->speechToTextHandle($result);
}
}
});
}
/**
* 语音合成
* @param SynthesizerParam $synthesizerParam 语音合成参数
* @param StreamResponseHandler $handler 流响应处理器
* @return void
* @throws ApiException
*/
public function synthesizer(SynthesizerParam $synthesizerParam, StreamResponseHandler $handler): void
{
$path = "/ws/synthesis";
$host = $this->config->getHost();
$host = str_replace(["http", "https"], ["ws", "wss"], $host) . $path;
$signParams = [
'payload' => null,
'path' => $path,
'charset' => $this->config->getCharset(),
'time' => date($this->config->getDataFormat(), time()),
'sign_type' => $this->config->getSignType(),
'access_token' => null,
'app_id' => $this->config->getAppid(),
'sign' => ''
];
$signParams['sign'] = $this->apiClient->requestSign($signParams);
Loop::run(function () use ($host, $signParams, $synthesizerParam, $handler) {
$handshake = (new Handshake($host))->withHeaders($signParams);
$connection = yield connect($handshake);
yield $connection->send(json_encode($synthesizerParam));
while ($message = yield $connection->receive()) {
$payload = yield $message->buffer();
$handler->synthesizerHandle($payload);
}
});
}
}