gpt4 book ai didi

go - 我应该如何在 golang 中编码用于 Google Voice 识别的音频流?

转载 作者:行者123 更新时间:2023-12-01 20:23:03 24 4
gpt4 key购买 nike

我正在构建一个使用 Opus 流的 discord 机器人。我尝试了各种方法,例如直接发送 Opus 数据包,将 OPUS 流解码为 PCM 并将其编码为字节数组,以及将 PCM 直接转换为字节数组。在所有情况下,我得到:

无法识别:代码:11 消息:“音频数据传输速度太慢。请大约实时传输音频数据。”

我在 20 毫秒帧时尝试了 8kHz-48kHz 频率。我还尝试使用最大比特率对转换后的 PCM 进行编码。我已经成功运行了示例代码,所以我这边没有连接问题。我应该在哪里寻找解决方案?

package main

import (
"fmt"
//"io"
"log"
"os"
"flag"

speech "cloud.google.com/go/speech/apiv1"
"golang.org/x/net/context"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"

"github.com/bwmarrin/discordgo"
"layeh.com/gopus"
)

// OnError gets called by dgvoice when an error is encountered.
// By default logs to STDERR
var OnError = func(str string, err error) {
prefix := "dgVoice: " + str

if err != nil {
os.Stderr.WriteString(prefix + ": " + err.Error())
} else {
os.Stderr.WriteString(prefix)
}
}

var stream speechpb.Speech_StreamingRecognizeClient

func main() {
var (
Token = flag.String("t", "", "Discord bot token.")
// Email = flag.String("e", "", "Discord account email.")
// Password = flag.String("p", "", "Discord account password.")
GuildID = flag.String("g", "", "Guild ID")
ChannelID = flag.String("c", "", "Channel ID")
)
flag.Parse()


fmt.Println("Connecting to Discord...")
// Connect to Discord
discord, err := discordgo.New(*Token)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Opening Socket...")
// Open Websocket
err = discord.Open()
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Joining Channel...")
// Connect to voice channel.
// NOTE: Setting mute to false, deaf to true.
dgv, err := discord.ChannelVoiceJoin(*GuildID, *ChannelID, false, false)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Connecting to Google Speech Recognition API...")
ctx := context.Background()

// [START speech_streaming_mic_recognize]
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
stream, err = client.StreamingRecognize(ctx)
if err != nil {
log.Fatal(err)
}
// Send the initial configuration message.
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_StreamingConfig{
StreamingConfig: &speechpb.StreamingRecognitionConfig{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "en-US",
},
//InterimResults: true,
SingleUtterance: true,
},

},
}); err != nil {
log.Fatal(err)
}


recv := make(chan *discordgo.Packet, 2)
go Receive(dgv, recv)

send := make(chan []int16, 2)
go Send(dgv, send)

// dgv.Speaking(true)
// defer dgv.Speaking(false)

go func() {
for {

p, ok := <-recv
if !ok {
fmt.Println("Not OK")
return
}

send <- p.PCM
}

} ()

for {
resp, err := stream.Recv()
//fmt.Printf("%+v\n",resp)
if err != nil {
log.Fatalf("Cannot stream results: %v", err)
}
if err := resp.Error; err != nil {
log.Fatalf("Could not recognize: %v", err)
}
for _, result := range resp.Results {
fmt.Printf("Result: %+v\n", result)
}
}

// Close connections
dgv.Close()
discord.Close()

return
}

func Receive(v *discordgo.VoiceConnection, c chan *discordgo.Packet) {
var speakers map[uint32]*gopus.Decoder
if c == nil {
return
}

var err error
for {
p, ok := <-v.OpusRecv
if !ok {
return
}

if speakers == nil {
speakers = make(map[uint32]*gopus.Decoder)
}

_, ok = speakers[p.SSRC]
if !ok {
speakers[p.SSRC], err = gopus.NewDecoder(16000, 1)
if err != nil {
OnError("error creating opus decoder", err)
continue
}
}
p.PCM, err = speakers[p.SSRC].Decode(p.Opus, 320, false)
if err != nil {
OnError("Error decoding opus data", err)
continue
}

// try encoding pcm frame with Opus

c <- p
}
}

func Send(v *discordgo.VoiceConnection, pcm <- chan []int16) {
for {

// read pcm from chan, exit if channel is closed.
recv, ok := <-pcm
if !ok {
OnError("PCM Channel closed", nil)
return
}


buf := make([]byte,2*len(recv))

for i := 0; i < len(recv); i+=2 {
var h, l uint8 = uint8(i>>8), uint8(i&0xff)
buf[i] = h
buf[i+1] = l
}


stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: buf,
},
});
}

最佳答案

Google Speech-to-Text 文档有 a fully working example of streaming speech recognition in Go .

“音频数据传输速度太慢” 由服务器在未实时接收音频时发送。在这种情况下,上面的代码包含一个错误,导致在发送循环的每次迭代中只发送半个 PCM 帧:

        for i := 0; i < len(recv); i+=2 {
var h, l uint8 = uint8(i>>8), uint8(i&0xff)
buf[i] = h
buf[i+1] = l
}

recv 是 int16 值的一部分,因此它应该一次迭代一个值,而不是 i+=2,它会跳过所有其他值。 buf 是一个 uint8 slice ,因此它的索引是有效的。


关于go - 我应该如何在 golang 中编码用于 Google Voice 识别的音频流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com