AipSpeech.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = 'https://vop.baidu.com/pro_api';
  27. /**
  28. * url
  29. * @var string
  30. */
  31. public $ttsUrl = 'http://tsn.baidu.com/text2audio';
  32. public $ttsLongUrl = 'https://aip.baidubce.com/rpc/2.0/tts/v1/create';
  33. /**
  34. * 判断认证是否有权限
  35. * @param array $authObj
  36. * @return boolean
  37. */
  38. protected function isPermission($authObj)
  39. {
  40. return true;
  41. }
  42. /**
  43. * 处理请求参数
  44. * @param string $url
  45. * @param array $params
  46. * @param array $data
  47. * @param array $headers
  48. */
  49. protected function proccessRequest($url, &$params, &$data, $headers){
  50. $token = isset($params['access_token']) ? $params['access_token'] : '';
  51. if(empty($data['cuid'])){
  52. $data['cuid'] = md5($token);
  53. }
  54. if($url === $this->asrUrl){
  55. $data['token'] = $token;
  56. $data = json_encode($data);
  57. }else{
  58. $data['tok'] = $token;
  59. }
  60. unset($params['access_token']);
  61. }
  62. /**
  63. * 格式化结果
  64. * @param $content string
  65. * @return mixed
  66. */
  67. protected function proccessResult($content){
  68. $obj = json_decode($content, true);
  69. if($obj === null){
  70. $obj = array(
  71. '__json_decode_error' => $content
  72. );
  73. }
  74. return $obj;
  75. }
  76. /**
  77. * @param string $speech
  78. * @param string $format
  79. * @param int $rate
  80. * @param array $options
  81. * @return array
  82. */
  83. public function asr($speech, $format, $rate, $options=array()){
  84. $data = array();
  85. if(!empty($speech)){
  86. $data['speech'] = base64_encode($speech);
  87. $data['len'] = strlen($speech);
  88. }
  89. $data['format'] = $format;
  90. $data['rate'] = $rate;
  91. $data['channel'] = 1;
  92. $data['cuid'] = "123456PHP";
  93. $data['dev_pid'] = 80001;
  94. $data = array_merge($data, $options);
  95. return $this->request($this->asrUrl, $data, array());
  96. }
  97. /**
  98. * @param string $text
  99. * @param string $lang
  100. * @param int $ctp
  101. * @param array $options
  102. * @return array
  103. */
  104. public function synthesis($text, $lang='zh', $ctp=1, $options=array()){
  105. $data = array();
  106. $data['tex'] = $text;
  107. $data['lan'] = $lang;
  108. $data['ctp'] = $ctp;
  109. $data = array_merge($data, $options);
  110. $result = $this->request($this->ttsUrl, $data, array());
  111. if(isset($result['__json_decode_error'])){
  112. return $result['__json_decode_error'];
  113. }
  114. return $result;
  115. }
  116. /**
  117. * @param string $text
  118. * @param string $lang
  119. * @param int $ctp
  120. * @param array $options
  121. * @return array
  122. */
  123. public function synthesis2($text, $lang='zh', $options=array()){
  124. $data = array();
  125. $data['text'][0] = $text;
  126. // $data['lang'] = $lang;
  127. $data = array_merge($data, $options);
  128. // print_r($data);
  129. $result = $this->request($this->ttsLongUrl, $data, array());
  130. if(isset($result['__json_decode_error'])){
  131. return $result['__json_decode_error'];
  132. }
  133. return $result;
  134. }
  135. }