PHP code example of saviorlv / yii2-bd-aipspeech
1. Go to this page and download the library: Download saviorlv/yii2-bd-aipspeech library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
saviorlv / yii2-bd-aipspeech example snippets
// 配置文件里修改
'components' => [
......
'aipSpeech' => [
'class' => 'Saviorlv\Baidu\BdSpeech',
'app_id' => 'xxxxxx', // 百度语音 App ID
'api_key' => 'xxxxxxx', // 百度语音 API Key
'secret_key' => 'xxxxxx', // 百度语音 Secret Key
'path' => Yii::getAlias('@tmp'.'/audios/') //可以不填写 默认在 runtime
],
......
],
//请求
$aipSpeech = Yii::$app->get('aipSpeech');
$file = Yii::getAlias('@tmp'.'/audios/').'16k.pcm';
$x = $aipSpeech->recognize($file,'');
var_dump($x);
//响应
[
'success' =>true,
'msg' => '语音识别成功',
'data' =>[
......
]
]
//or
[
'success' =>false,
'msg' => '语音文件路径错误',
]
//请求
$aipSpeech = Yii::$app->get('aipSpeech');
$x = $aipSpeech->combine('您好,世界');
var_dump($x);
//响应
[
'success' =>true,
'msg' => '语音合成成功',
'data' =>'/webwww/yii2-bd/tmp/audios/5c4575feeb70d.mp3'
]
//or
[
'success' =>false,
'msg' => '语音合成失败',
]
/**
* 语音识别
*
* @param $filePath string 语音文件本地路径,优先使用此项
* @param $url string 语音文件URL路径
* @param $userID string 用户唯一标识
* @param $format string 语音文件格式 ['pcm', 'wav', 'opus', 'speex', 'amr']
* @param $rate integer 采样率 [8000, 16000]
* @param $dev_pid int 语音语言 [1536,1537,1737,1637,1837,1936]
* @return array
*/
public function recognize($filePath, $url, $format = 'wav', $dev_pid = 1536, $userID = null, $rate = 16000)
{}
/**
* 语音合成
*
* @param $text string 合成的文本
* @param $userID string 用户唯一标识
* @param $lan string 语音 ['zh']
* @param $speed integer 语速,取值0-9,默认为5中语速
* @param $pitch integer 音调,取值0-9,默认为5中语调
* @param $volume integer 音量,取值0-15,默认为5中音量
* @param $person integer 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
* @param $fileName string 存储文件路径名称
* @return array
*/
public function combine($text, $userID = null, $lan = 'zh', $speed = 5, $pitch = 5, $volume = 5, $person = 0, $fileName = null){}