voice

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

Installation

go get github.com/chenqinghe/baidu-ai-go-sdk/voice

Usage

  • 创建VoiceClient
    	var apiKey,apiSecret string 
	apiKey = "XXXXXXX"
	apiSecret = "XXXXXXX"
	client := voice.NewVoiceClient(apiKey,apiSecret)
  • 语音合成
	bts,err:=client.TextToSpeech("你好")
	if err!=nil{
		log.Fatal(err)
	}
	if err:=writeBytesToFile(bts,filename);err!=nil{//writeBytesToFile需要自己实现
		log.Fatal(err)
	}
  • 语音识别
	var ap ASRParams = ASRParams{
		Format:  "wav",
		Rate:    16000,
		Channel: 1,
		Cuid:    "mac address",
		Token:   client.AccessToken,
		Lan:     "zh",
		Speech:  afterBase64Str,
		Len:     fiLen,
	}
	strs,err:=client.SpeechToText(ap)
	if err!=nil{
		log.Fatal(err)
	}	
	fmt.Println(strs)//[]string,翻译候选结果,5个。

Documentation

Overview

语音处理 利用百度RESTFul API 进行语音及文字的相互转换

Index

Constants

View Source
const (
	B = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
)
View Source
const ASR_URL = "http://vop.baidu.com/server_api"
View Source
const TTS_URL = "http://tsn.baidu.com/text2audio"

Variables

View Source
var (
	ErrTextTooLong = errors.New("The input string is too long")
)

Functions

This section is empty.

Types

type ASRParam added in v0.2.0

type ASRParam func(params *ASRParams)

func Channel added in v0.2.0

func Channel(c int) ASRParam

func Format added in v0.2.0

func Format(fmt string) ASRParam

func Language added in v0.2.0

func Language(lang string) ASRParam

func Rate added in v0.2.0

func Rate(rate int) ASRParam

type ASRParams

type ASRParams struct {
	Format   string `json:"format"`  //语音的格式,pcm 或者 wav 或者 amr。不区分大小写
	Rate     int    `json:"rate"`    //采样率,支持 8000 或者 16000
	Channel  int    `json:"channel"` //声道数,仅支持单声道,请填写固定值 1
	Cuid     string `json:"cuid"`    //用户唯一标识,用来区分用户,计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内
	Token    string `json:"token"`   //开放平台获取到的开发者access_token
	Language string `json:"lan"`     //语种选择,默认中文(zh)。 中文=zh、粤语=ct、英文=en,不区分大小写
	Speech   string `json:"speech"`  //真实的语音数据 ,需要进行base64 编码。与len参数连一起使用
	Length   int    `json:"len"`     //原始语音长度,单位字节
}

语音识别参数

type ASRResponse

type ASRResponse struct {
	CorpusNo string   `json:"corpus_no"`
	ERRMSG   string   `json:"err_msg"`
	ERRNO    int      `json:"err_no"`
	Result   []string `json:"result"`
	SN       string   `json:"sn"`
}

语音识别响应信息

type TTSParam added in v0.2.0

type TTSParam func(params *TTSParams)

func Person added in v0.2.0

func Person(per int) TTSParam

func Pitch added in v0.2.0

func Pitch(pit int) TTSParam

func Speed added in v0.2.0

func Speed(spd int) TTSParam

func Volume added in v0.2.0

func Volume(vol int) TTSParam

type TTSParams added in v0.2.0

type TTSParams struct {
	Text       string `json:"tex"`
	Token      string `json:"tok"`
	Cuid       string `json:"cuid"`
	ClientType int    `json:"ctp"`
	Language   string `json:"lan"`
	Speed      int    `json:"spd"`
	Pitch      int    `json:"pit"`
	Volume     int    `json:"vol"`
	Person     int    `json:"per"`
}

type VoiceClient

type VoiceClient struct {
	*gosdk.Client
}

func NewVoiceClient

func NewVoiceClient(apiKey, apiSecret string) *VoiceClient

func (*VoiceClient) SpeechToText

func (vc *VoiceClient) SpeechToText(reader io.Reader, params ...ASRParam) ([]string, error)

//SpeechToText 语音识别,将语音翻译成文字

func (*VoiceClient) TextToSpeech

func (vc *VoiceClient) TextToSpeech(txt string, params ...TTSParam) ([]byte, error)

TextToSpeech 语音合成,将文字转换为语音

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL