request->param('content'); if(empty($parmas)){ return $this->success('success'); } $chat=new ChatClient($this->app_id,$this->api_key,$this->api_secret); $client = new Client($chat->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat')); // 连接到 WebSocket 服务器 if ($client) { // 发送数据到 WebSocket 服务器 $data = $this->getBody($this->app_id,$parmas); $client->send($data); // 从 WebSocket 服务器接收数据 $answer = ""; while(true){ $response = $client->receive(); $resp = json_decode($response,true); $code = $resp["header"]["code"]; // echo "从服务器接收到的数据: " . $response; if(0 == $code){ $status = $resp["header"]["status"]; if($status != 2){ $content = $resp['payload']['choices']['text'][0]['content']; $answer .= $content; print($answer); ob_flush(); // 刷新输出缓冲区 flush(); // 刷新系统输出缓冲区 }else{ $content = $resp['payload']['choices']['text'][0]['content']; $answer .= $content; $total_tokens = $resp['payload']['usage']['text']['total_tokens']; print("\n本次消耗token用量:\n"); print($total_tokens); ob_flush(); // 刷新输出缓冲区 flush(); // 刷新系统输出缓冲区 break; } }else{ return $this->fail( "服务返回报错".$response); break; } } ob_flush(); // 刷新输出缓冲区 flush(); // 刷新系统输出缓冲区 return $this->success('success'); } else { return $this->fail('无法连接到 WebSocket 服务器'); } } //构造参数体 function getBody($appid,$question){ $header = array( "app_id" => $appid, "uid" => "1" ); $parameter = array( "chat" => array( "domain" => "generalv2", "temperature" => 0.5, "max_tokens" => 1024 ) ); $payload = array( "message" => array( "text" => array( array("role" => "user", "content" => $question) ) ) ); $json_string = json_encode(array( "header" => $header, "parameter" => $parameter, "payload" => $payload )); return $json_string; } //语音听写(流式版) public function iat() { header('X-Accel-Buffering: no'); $file = request()->file('audio'); if(empty($file)){ //return $this->fail('未上传音频文件'); } // 上传音频临时文件 $savename = \think\facade\Filesystem::putFile('audio', $file); $file = app()->getRootPath() . '/runtime/storage/' . $savename; if (!file_exists($file)) { //return $this->fail('未上传音频文件'); } $file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3"; $audioFile = fopen($file, 'rb'); if ($audioFile === false) { return $this->fail('音频文件异常'); } $words = ''; $iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat"; $iat = new IatClient($this->app_id,$this->api_key,$this->api_secret); $client = new Client($iat->assembleAuthUrl($iatHostUrl)); if ($client) { $frameSize = 1280; //每一帧的音频大小 $intervel = 20 * 1000; //发送音频间隔 $status = 0; while (true) { $len = fread($audioFile, $frameSize); if ($len === false) { break; } if ($len === '') { //文件读取完了 $status = 2; } switch ($status) { case 0: //发送第一帧音频,带business 参数 $frameData = array( 'common' => array( 'app_id' => $this->app_id //appid 必须带上,只需第一帧发送 ), 'business' => array( //business 参数,只需一帧发送 'language' => 'zh_cn', 'domain' => 'iat', 'accent' => 'mandarin' ), 'data' => array( 'status' => 0, 'format' => 'audio/L16;rate=16000', 'audio' => base64_encode($len), 'encoding' => 'lame' ) ); $client->send(json_encode($frameData)); $status = 1; break; case 1: $frameData = array( 'data' => array( 'status' => 1, 'format' => 'audio/L16;rate=16000', 'audio' => base64_encode($len), 'encoding' => 'raw' ) ); $client->send(json_encode($frameData)); break; case 2: $frameData = array( 'data' => array( 'status' => 2, 'format' => 'audio/L16;rate=16000', 'audio' => base64_encode($len), 'encoding' => 'raw' ) ); $client->send(json_encode($frameData)); break 2; } //模拟音频采样间隔 usleep($intervel); } while (true) { $response = $client->receive(); if ($response === null) { break; } $resp = json_decode($response,true); $code = $resp['code']; $message = $resp['message']; $data = $resp['data']; $result = $data['result']; $status = $data['status']; foreach($result['ws'] as $v) { $words .= $v['cw'][0]['w'] ?? ''; } if ($code != 0) { break; } if ($status === 2) { break; } } } else { return $this->fail('无法连接到 WebSocket 服务器'); } fclose($audioFile); // 删除临时音频文件 if (file_exists($file)) { unlink($file); } return $this->data(['words' => $words]); } //语音合成(流式版) public function tts() { header('X-Accel-Buffering: no'); $ttsHostUrl = "wss://tts-api.xfyun.cn/v2/tts"; $tts = new TtsClient($this->app_id,$this->api_key,$this->api_secret); $date = date('YmdHis', time()); $file_name = $date . '.mp3'; $audioxxxFile = app()->getRootPath() . '/runtime/storage/audio/' . date('Ymd') . '/' . $file_name; file_put_contents($audioxxxFile, $tts->request('欢迎使用科大讯飞语音能力,让我们一起用人工智能改变世界')->getBody()->getContents()); exit; $client = new Client($tts->assembleAuthUrl($ttsHostUrl)); $draft_content = '这是一段测试的音频文件,正常调试接口,这是一段测试的音频文件,正常调试接口'; $message = [ 'common' => [ 'app_id' => $this->app_id, ], 'business' => [ 'aue' => 'raw', 'auf' => 'audio/L16;rate=16000', 'vcn' => 'xiaoyan', 'tte' => 'UTF8', ], 'data' => [ 'status' => 2, 'text' => base64_encode($draft_content), ] ]; try { $client->send(json_encode($message, true)); //需要以追加的方式进行写文件 $audioxxxFile = app()->getRootPath() . '/runtime/storage/audio/' . date('Ymd') . '/' . $file_name; file_put_contents($audioxxxFile, $tts->request('欢迎使用科大讯飞语音能力,让我们一起用人工智能改变世界')->getBody()->getContents()); exit; $audio_file = fopen($audioxxxFile, 'ab'); $response = $client->receive(); $response = json_decode($response, true); do { //返回的音频需要进行base64解码 $audio = base64_decode($response['data']['audio']); fwrite($audio_file, $audio); //继续接收消息 $response = $client->receive(); $response = json_decode($response, true); } while ($response['data']['status'] != 2); fclose($audio_file); return $this->data([ 'audio_name' => $file_name, 'audio_url' => './audio/' . $file_name, ]); } catch (Exception $e) { return $this->fail($e->getMessage()); } finally { $client->close(); } return $this->fail('error'); } }