在学习Go时,我正在尝试正确读取Opus文件并将其发送到Discord的语音通道(仅支持Opus编解码器)。最初使用this脚本我能够传递WAV,脚本会将其编码为Opus,然后通过频道将其发送到Discord。相反,我想发送一个现成的Opus文件。然而,相反,我听到了乱码声,这意味着我读错了。
这是一个减少但完整的工作示例(减去电子邮件,密码和服务器ID),魔术发生在playSong
。我觉得它与我的音频缓冲区有关,但仍然在学习技巧。
读取opus文件并将其传递给频道的正确方法是什么?如果需要测试的opus文件,请找到一个here。任何帮助表示赞赏。谢谢!
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/oleiade/lane"
)
type voiceInstancesMap map[string]*VoiceInstance
var (
run *exec.Cmd
voiceInstances = voiceInstancesMap{}
)
const (
email string = ""
password string = ""
serverID string = ""
channels int = 2 // 1 for mono, 2 for stereo
frameRate int = 48000 // audio sampling rate
)
type VoiceInstance struct {
discord *discordgo.Session
queue *lane.Queue
serverID string
}
func (vi *VoiceInstance) playSong() {
f, err := os.Open("./test.opus")
defer f.Close()
audiobuf := make([]byte, 1024)
vi.discord.Voice.Speaking(true)
defer vi.discord.Voice.Speaking(false)
for {
err = binary.Read(f, binary.LittleEndian, &audiobuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
fmt.Println("error reading from ffmpeg stdout :", err)
break
}
fmt.Println("Sending audio")
vi.discord.Voice.OpusSend <- audiobuf
}
}
func (vi *VoiceInstance) connectVoice() {
vi.discord, _ = discordgo.New(email, password)
// Open the websocket and begin listening.
err := vi.discord.Open()
if err != nil {
fmt.Println(err)
}
channels, err := vi.discord.GuildChannels(vi.serverID)
var voiceChannel string
voiceChannels := []string{}
for _, channel := range channels {
if channel.Type == "voice" {
voiceChannels = append(voiceChannels, channel.ID)
if strings.Contains(strings.ToLower(channel.Name), "music") && voiceChannel == "" {
voiceChannel = channel.ID
}
}
}
if voiceChannel == "" {
fmt.Println("Selecting first channel")
voiceChannel = voiceChannels[0]
}
err = vi.discord.ChannelVoiceJoin(vi.serverID, voiceChannel, false, true)
if err != nil {
fmt.Println(err)
return
}
// Hacky loop to prevent sending on a nil channel.
// TODO: Find a better way.
for vi.discord.Voice.Ready == false {
runtime.Gosched()
}
}
func main() {
vi := new(VoiceInstance)
voiceInstances[serverID] = vi
fmt.Println("Connecting Voice...")
vi.serverID = serverID
vi.queue = lane.NewQueue()
vi.connectVoice()
vi.playSong()
}
答案 0 :(得分:0)
使用https://github.com/hraban/opus。来自该链接上的文档:
要解码.opus文件(或带有Opus数据的.ogg)或解码“ Opus流”(带有Opus数据的Ogg流),请使用Stream接口。它包装提供原始流字节的io.Reader并返回解码的Opus数据。
从.opus文件读取的粗略示例:
f, err := os.Open(fname)
if err != nil {
...
}
s, err := opus.NewStream(f)
if err != nil {
...
}
defer s.Close()
buf := make([]byte, 16384)
for {
n, err = s.Read(buf)
if err == io.EOF {
break
} else if err != nil {
...
}
pcm := buf[:n*channels]
// send pcm to audio device here, or write to a .wav file
}