更新语音

This commit is contained in:
yaooo 2023-10-10 14:00:57 +08:00
parent e11b7bbd01
commit 94ea58620c
2 changed files with 20 additions and 51 deletions

View File

@ -125,6 +125,7 @@ class XunFeiController extends BaseApiController
{
header('X-Accel-Buffering: no');
$file = request()->file('audio');
/*
if(empty($file)){
//return $this->fail('未上传音频文件');
}
@ -134,6 +135,7 @@ class XunFeiController extends BaseApiController
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) {
@ -239,61 +241,27 @@ class XunFeiController extends BaseApiController
{
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);
$text = request()->file('text');
if(empty($file)){
return $this->fail('未上传文本参数');
}
$business = [
'aue' => 'lame',
'vcn' => 'xiaoyan',
'auf' => 'audio/L16;rate=16000',
'speed' => 50,
'volume'=> 100,
'pitch' => 50,
'tte' => 'UTF8',
];
$tts = new TtsClient($this->app_id,$this->api_key,$this->api_secret, $business);
$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());
$audioFile = app()->getRootPath() . '/runtime/storage/audio/' . date('Ymd') . '/' . $file_name;
file_put_contents($audioxxxFile, $tts->request($text)->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');
}
}

View File

@ -130,7 +130,7 @@ class TtsConfig implements ConfigInterface
$config += [
'aue' => 'lame',
'sfl' => 1,
'auf' => null,
'auf' => 'audio/L16;rate=16000',
'vcn' => 'xiaoyan',
'speed' => 50,
'volume' => 50,
@ -154,6 +154,7 @@ class TtsConfig implements ConfigInterface
$this->reg = $config['reg'];
$this->rdn = $config['rdn'];
$this->ent = $config['ent'];
print_r($config);
}
/**