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); } }); } }