func SpeechToText(file, format string, sampleRate int) ([]string, error) {
	client := NewVoiceClient(config.BaiduVoiceAPIKey, config.BaiduVoiceAPISecret)
	if err := client.Auth(); err != nil {
		return nil, err
	}
	f, err := os.OpenFile(file, os.O_RDONLY, 0666)
	if err != nil {
		return nil, err
	}
	rs, err := client.SpeechToText(
		f,
		Format(format),
		Channel(1),
		Rate(sampleRate),
	)
	if err != nil {
		return nil, err
	}
	return rs, nil
}
Voice Recognition
ATTENTION: the .wav file must be 8k or 16k rate with single(mono) channel.
FYI: you can use QuickTime to record voice and Fission converting to .wav
package baidu
import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/countstarlight/homo/cmd/webview/config"
	"github.com/countstarlight/homo/module/com"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"os"
)
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 ASRParams struct {
	Format   string `json:"format"`   
	Rate     int    `json:"rate"`     
	Channel  int    `json:"channel"`  
	Cuid     string `json:"cuid"`     
	Token    string `json:"token"`    
	Language string `json:"lan"`      
	Speech   string `json:"speech"`   
	Length   int    `json:"len"`      
}
type ASRParam func(params *ASRParams)
func Format(fmt string) ASRParam {
	if fmt != "pcm" && fmt != "wav" && fmt != "amr" {
		fmt = "pcm"
	}
	return func(params *ASRParams) {
		params.Format = fmt
	}
}
func Rate(rate int) ASRParam {
	if rate != 8000 && rate != 16000 {
		rate = 8000
	}
	return func(params *ASRParams) {
		params.Rate = rate
	}
}
func Channel(c int) ASRParam {
	return func(params *ASRParams) {
		params.Channel = 1  
	}
}
func Language(lang string) ASRParam {
	if lang != "zh" && lang != "ct" && lang != "en" {
		lang = "zh"
	}
	return func(params *ASRParams) {
		params.Language = lang
	}
}
func (vc *VoiceClient) SpeechToText(reader io.Reader, params ...ASRParam) ([]string, error) {
	content, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	if len(content) > 10*MB {
		return nil, fmt.Errorf("文件大小不能超过10M")
	}
	if err := vc.Auth(); err != nil {
		return nil, err
	}
	spch := base64.StdEncoding.EncodeToString(content)
	var cuid string
	netitfs, err := net.Interfaces()
	if err != nil {
		cuid = "anonymous"
	} else {
		for _, itf := range netitfs {
			if cuid = itf.HardwareAddr.String(); len(cuid) > 0 {
				break
			}
		}
	}
	asrParams := &ASRParams{
		Format:   "pcm",
		Rate:     8000,
		Channel:  1,
		Cuid:     cuid,
		Token:    vc.AccessToken,
		Language: "zh",
		Speech:   spch,
		Length:   len(content),
	}
	for _, fn := range params {
		fn(asrParams)
	}
	asrParamsBuffer := new(bytes.Buffer)
	err = json.NewEncoder(asrParamsBuffer).Encode(asrParams)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("POST", config.BaiduASRAPI, asrParamsBuffer)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer com.IOClose("Post baidu ASR api", resp.Body)
	var asrResponse *ASRResponse
	err = json.NewDecoder(resp.Body).Decode(&asrResponse)
	if err != nil {
		return nil, err
	}
	if asrResponse.ErrNo != 0 {
		if asrResponse.ErrMsg == "speech quality error." {
			return nil, ErrSpeechQuality{ErrNo: asrResponse.ErrNo, ErrMsg: asrResponse.ErrMsg}
		} else {
			return nil, fmt.Errorf("调用服务失败：%s", asrResponse.ErrMsg)
		}
	}
	return asrResponse.Result, nil
}