You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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