Speech.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |ZBPHP[基于ThinkPHP5.1开发]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016-2019 http://www.benbenwangluo.com
  6. // +----------------------------------------------------------------------
  7. // | Author 似水星辰 [ 2630481389@qq.com ]
  8. // +----------------------------------------------------------------------
  9. // | 中犇软件 技术六部 出品
  10. // +----------------------------------------------------------------------
  11. namespace addons\BaiduAi\controller;
  12. use app\common\controller\Common;
  13. require_once(dirname(dirname(__FILE__))."/sdk/AipSpeech.php");
  14. /**
  15. * 语音
  16. * @author 晓风<215628355@qq.com>
  17. * @package plugins\Sms\controller
  18. */
  19. class Speech extends Common
  20. {
  21. public $client;
  22. public function __construct(){
  23. $config = addons_config('BaiduAi');
  24. $this->client = new \AipSpeech($config['app_id'], $config['api_key'], $config['secret_key']);
  25. }
  26. public function setConnectionTimeoutInMillis($ms){
  27. $this->client->setConnectionTimeoutInMillis($ms);
  28. }
  29. public function setSocketTimeoutInMillis($ms){
  30. $this->client->setSocketTimeoutInMillis($ms);
  31. }
  32. public function __call($method, $args){
  33. $ret = call_user_func_array([$this->client, $method], $args);
  34. if(isset($ret['error_code']) && $ret['error_code'] != 0){
  35. $error_msg = $this->getErrorMsg($ret['error_code'])?:$ret['error_msg'];
  36. exception($error_msg);
  37. }else{
  38. return $ret;
  39. }
  40. }
  41. public function getErrorMsg($errorCode){
  42. $errors = [
  43. '500' => '不支持的输入',
  44. '501' => '输入参数不正确',
  45. '502' => 'token验证失败',
  46. '503' => '合成后端错误',
  47. '3300' => '输入参数不正确', // 请仔细核对文档及参照demo,核对输入参数
  48. '3301' => '音频质量过差', //请上传清晰的音频
  49. '3302' => '鉴权失败', // token字段校验失败。请使用正确的API_KEY 和 SECRET_KEY生成
  50. '3303' => '语音服务器后端问题', // 请将api返回结果反馈至论坛或者QQ群
  51. '3304' => '用户的请求QPS超限', // 请降低识别api请求频率 (qps以appId计算,移动端如果共用则累计)
  52. '3305' => '用户的日pv(日请求量)超限', // 请“申请提高配额”,如果暂未通过,请降低日请求量
  53. '3307' => '语音服务器后端识别出错问题', // 目前请确保16000的采样率音频时长低于30s,8000的采样率音频时长低于60s。如果仍有问题,请将api返回结果反馈至论坛或者QQ群
  54. '3308' => '音频过长', // 音频时长不超过60s,请将音频时长截取为60s以下
  55. '3309' => '音频数据问题', // 服务端无法将音频转为pcm格式,可能是长度问题,音频格式问题等。 请将输入的音频时长截取为60s以下,并核对下音频的编码,是否是8K或者16K, 16bits,单声道。
  56. '3310' => '输入的音频文件过大', // 语音文件共有3种输入方式: json 里的speech 参数(base64后); 直接post 二进制数据,及callback参数里url。 分别对应三种情况:json超过10M;直接post的语音文件超过10M;callback里回调url的音频文件超过10M
  57. '3311' => '采样率rate参数不在选项里', // 目前rate参数仅提供8000,16000两种,填写4000即会有此错误
  58. '3312' => '音频格式format参数不在选项里', // 目前格式仅仅支持pcm,wav或amr,如填写mp3即会有此错误
  59. ];
  60. return $errors[$errorCode]?: '';
  61. }
  62. }