|
|
<?php
|
|
|
|
|
|
namespace xunfei\ise;
|
|
|
|
|
|
use Monolog\Logger;
|
|
|
use xunfei\Base;
|
|
|
|
|
|
class Ise extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 语音合成
|
|
|
* @param string $text 需要合成语音的文本
|
|
|
* @param array $tts_config 配置参数
|
|
|
* @param string $local_file_path 保存本地文件的路径
|
|
|
* @date 2022-05-06
|
|
|
*/
|
|
|
public function tts($text, $tts_config, $local_file_path = '')
|
|
|
{
|
|
|
|
|
|
|
|
|
//没有定义本地存储路径的时候,自动生成本地路径,mp3格式
|
|
|
if(!$local_file_path){
|
|
|
$local_file_path = './temp/' . md5(microtime(true)) . '.mp3';
|
|
|
}
|
|
|
|
|
|
// $logger是psr3的日志实例,需要打印日志可以传入,缺省为null,此项为v2.0.1之后可用
|
|
|
// $logger = new Logger('TTSLOG');
|
|
|
// $logger->pushHandler(new StreamHandler(__DIR__ . '/tts.log', Logger::DEBUG));
|
|
|
|
|
|
$client = new \IFlytek\Xfyun\Speech\TtsClient($this->config['app_id'], $this->config['api_key'], $this->config['api_secret'], $tts_config);
|
|
|
|
|
|
// 返回格式为音频文件的二进制数组,可以直接通过file_put_contents存入本地文件
|
|
|
try {
|
|
|
$result = $client->request($text)->getBody()->getContents();
|
|
|
file_put_contents($local_file_path, $result);
|
|
|
return $local_file_path;
|
|
|
} catch (\Exception $e) {
|
|
|
$message = $e->getMessage();
|
|
|
|
|
|
$message = json_decode($message, true);
|
|
|
return $message;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 语音评测
|
|
|
* @param string $file 录音文件
|
|
|
* @param string $text 需要评测的文本
|
|
|
* @param array $tts_config 配置参数
|
|
|
* @date 2022-05-12
|
|
|
*/
|
|
|
public function ise($file, $text, $tts_config)
|
|
|
{
|
|
|
|
|
|
// 这里的$app_id、$api_key、$api_secret是在开放平台控制台获得
|
|
|
$client = new \IFlytek\Xfyun\Speech\IseClient($this->config['app_id'], $this->config['api_key'], $this->config['api_secret'], $tts_config);
|
|
|
|
|
|
// 返回识别结果
|
|
|
try {
|
|
|
$result = $client->request($file, $text);
|
|
|
$result = xml_to_array($result);
|
|
|
|
|
|
return $result;
|
|
|
} catch (\Exception $e) {
|
|
|
$message = $e->getMessage();
|
|
|
|
|
|
$message = json_decode($message, true);
|
|
|
return $message;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} |