AipSpeech.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. require_once 'lib/AipBase.php';
  18. /**
  19. * 百度语音
  20. */
  21. class AipSpeech extends AipBase{
  22. /**
  23. * url
  24. * @var string
  25. */
  26. public $asrUrl = 'http://vop.baidu.com/server_api';
  27. /**
  28. * url
  29. * @var string
  30. */
  31. public $ttsUrl = 'http://tsn.baidu.com/text2audio';
  32. /**
  33. * 判断认证是否有权限
  34. * @param array $authObj
  35. * @return boolean
  36. */
  37. protected function isPermission($authObj)
  38. {
  39. return true;
  40. }
  41. /**
  42. * 处理请求参数
  43. * @param string $url
  44. * @param array $params
  45. * @param array $data
  46. * @param array $headers
  47. */
  48. protected function proccessRequest($url, &$params, &$data, $headers){
  49. $token = isset($params['access_token']) ? $params['access_token'] : '';
  50. $data['cuid'] = md5($token);
  51. if($url === $this->asrUrl){
  52. $data['token'] = $token;
  53. $data = json_encode($data);
  54. }else{
  55. $data['tok'] = $token;
  56. }
  57. unset($params['access_token']);
  58. }
  59. /**
  60. * 格式化结果
  61. * @param $content string
  62. * @return mixed
  63. */
  64. protected function proccessResult($content){
  65. $obj = json_decode($content, true);
  66. if($obj === null){
  67. $obj = array(
  68. 'content' => $content
  69. );
  70. }
  71. return $obj;
  72. }
  73. /**
  74. * @param string $speech
  75. * @param string $format
  76. * @param int $rate
  77. * @param array $options
  78. * @return array
  79. */
  80. public function asr($speech, $format, $rate, $options=array()){
  81. $data = array();
  82. if(!empty($speech)){
  83. $data['speech'] = base64_encode($speech);
  84. $data['len'] = strlen($speech);
  85. }
  86. $data['format'] = $format;
  87. $data['rate'] = $rate;
  88. $data['channel'] = 1;
  89. $data = array_merge($data, $options);
  90. return $this->request($this->asrUrl, $data, array());
  91. }
  92. /**
  93. * @param string $text
  94. * @param string $lang
  95. * @param int $ctp
  96. * @param array $options
  97. * @return array
  98. */
  99. public function synthesis($text, $lang='zh', $ctp=1, $options=array()){
  100. $data = array();
  101. $data['tex'] = $text;
  102. $data['lan'] = $lang;
  103. $data['ctp'] = $ctp;
  104. $data = array_merge($data, $options);
  105. $result = $this->request($this->ttsUrl, $data, array());
  106. if(!isset($result['err_no'])){
  107. return $result['content'];
  108. }
  109. return $result;
  110. }
  111. }