AipSpeech.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * Http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. namespace PhpLife\Library;
  18. use AipBase;
  19. require_once 'BaiduVoice/AipBase.php';
  20. /**
  21. * 百度语音
  22. */
  23. class AipSpeech extends AipBase{
  24. /**
  25. * url
  26. * @var string
  27. */
  28. public $asrUrl = 'http://vop.baidu.com/server_api';
  29. /**
  30. * url
  31. * @var string
  32. */
  33. public $ttsUrl = 'http://tsn.baidu.com/text2audio';
  34. /**
  35. * 判断认证是否有权限
  36. * @param array $authObj
  37. * @return boolean
  38. */
  39. protected function isPermission($authObj)
  40. {
  41. return true;
  42. }
  43. /**
  44. * 处理请求参数
  45. * @param string $url
  46. * @param array $params
  47. * @param array $data
  48. * @param array $headers
  49. */
  50. protected function proccessRequest($url, &$params, &$data, $headers){
  51. $token = isset($params['access_token']) ? $params['access_token'] : '';
  52. $data['cuid'] = md5($token);
  53. if($url === $this->asrUrl){
  54. $data['token'] = $token;
  55. $data = json_encode($data);
  56. }else{
  57. $data['tok'] = $token;
  58. }
  59. unset($params['access_token']);
  60. }
  61. /**
  62. * 格式化结果
  63. * @param $content string
  64. * @return mixed
  65. */
  66. protected function proccessResult($content){
  67. $obj = json_decode($content, true);
  68. if($obj === null){
  69. $obj = array(
  70. 'content' => $content
  71. );
  72. }
  73. return $obj;
  74. }
  75. /**
  76. * @param string $speech
  77. * @param string $format
  78. * @param int $rate
  79. * @param array $options
  80. * @return array
  81. */
  82. public function asr($speech, $format, $rate, $options=array()){
  83. $data = array();
  84. if(!empty($speech)){
  85. $data['speech'] = base64_encode($speech);
  86. $data['len'] = strlen($speech);
  87. }
  88. $data['format'] = $format;
  89. $data['rate'] = $rate;
  90. $data['channel'] = 1;
  91. $data = array_merge($data, $options);
  92. return $this->request($this->asrUrl, $data, array());
  93. }
  94. /**
  95. * @param string $text
  96. * @param string $lang
  97. * @param int $ctp
  98. * @param array $options
  99. * @return array
  100. */
  101. public function synthesis($text, $lang='zh', $ctp=1, $options=array()){
  102. $data = array();
  103. $data['tex'] = $text;
  104. $data['lan'] = $lang;
  105. $data['ctp'] = $ctp;
  106. $data = array_merge($data, $options);
  107. $result = $this->request($this->ttsUrl, $data, array());
  108. if(!isset($result['err_no'])){
  109. return $result['content'];
  110. }
  111. return $result;
  112. }
  113. }