appId = $appId; $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; $this->requestConfig = new IgrConfig($requestConfig); } /** * 请求并返回结果 * * @param string $audioPath 待识别音频路径 * @return string * @throws Exception */ public function request($audioPath) { $ttsHandler = new WsHandler( $this->signUriV1(IgrConstants::URI, [ 'appId' => $this->appId, 'apiKey' => $this->apiKey, 'apiSecret' => $this->apiSecret, 'host' => IgrConstants::HOST, 'requestLine' => IgrConstants::REQUEST_LINE, ]), null ); $client = new WsClient([ 'handler' => $ttsHandler ]); // 音频上传 $frameNum = ceil(fileSize($audioPath) / IgrConstants::FRAME_SIZE); $fileStream = new Stream(fopen($audioPath, 'r')); // 发送第一帧 $client->send($this->generateAudioInput($fileStream->read(IgrConstants::FRAME_SIZE), true, false)); // 发送中间帧 for ($i = 1; $i < $frameNum; $i++) { $client->send($this->generateAudioInput($fileStream->read(IgrConstants::FRAME_SIZE), false, false)); usleep(4000); } // 发送最后一帧 $client->send($this->generateAudioInput('', false, true)); // 接受数据 $message = $this->jsonDecode($client->receive(), true); if ($message['code'] !== 0) { throw new Exception(json_encode($message)); } return $message['data']; } }