1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 08:32:48 +00:00
This commit is contained in:
2018-05-30 19:27:09 -05:00
parent af8448e028
commit 1f9706060a
3 changed files with 87 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
@@ -18,6 +19,7 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/mgerb/go-discord-bot/server/config"
"github.com/mgerb/go-discord-bot/server/rtp"
log "github.com/sirupsen/logrus"
)
@@ -25,6 +27,7 @@ const (
channels int = 2 // 1 for mono, 2 for stereo
sampleRate int = 48000 // audio sampling rate - apparently a standard for opus
frameSize int = 960 // uint16 size of each audio frame
frameSizeMs int = 20 // 20 milliseconds between each frame
maxBytes int = (frameSize * 2) * 2 // max size of opus data
maxSoundQueue int = 10 // max amount of sounds that can be queued at one time
voiceClipQueuePacketSize int = 2000 // this packet size equates to roughly 40 seconds of audio
@@ -32,7 +35,6 @@ const (
// store our connection objects in a map tied to a guild id
var activeConnections = make(map[string]*AudioConnection)
var speakers = make(map[uint32]*gopus.Decoder)
// AudioConnection -
type AudioConnection struct {
@@ -351,6 +353,9 @@ func (conn *AudioConnection) startAudioListener() {
}
}()
var speakers = make(map[uint32]*gopus.Decoder)
realTimeAudio := rtp.New()
loop:
for {
@@ -373,11 +378,16 @@ loop:
}
opusChannel.PCM, err = speakers[opusChannel.SSRC].Decode(opusChannel.Opus, frameSize, false)
if err != nil {
log.Error("Error decoding opus data", err)
continue
}
realTimeAudio.PushPacket(opusChannel)
fmt.Println(realTimeAudio.GetStream(opusChannel.SSRC).GetTimeOffset(realTimeAudio.StartTime.Unix()))
// if channel is full trim off from beginning
if len(conn.VoiceClipQueue) == cap(conn.VoiceClipQueue) {
<-conn.VoiceClipQueue

36
server/rtp/rtp.go Normal file
View File

@@ -0,0 +1,36 @@
// rtp packet processing
package rtp
import (
"time"
"github.com/bwmarrin/discordgo"
)
// RTP -
type RTP struct {
StartTime time.Time
Streams map[uint32]*Stream
}
// New -
func New() *RTP {
return &RTP{
StartTime: time.Now(),
Streams: map[uint32]*Stream{},
}
}
// PushPacket -
func (r *RTP) PushPacket(packet *discordgo.Packet) {
if _, ok := r.Streams[packet.SSRC]; !ok {
r.Streams[packet.SSRC] = new(Stream)
}
r.Streams[packet.SSRC].PushPacket(packet)
}
// GetStream -
func (r *RTP) GetStream(ssrc uint32) *Stream {
return r.Streams[ssrc]
}

40
server/rtp/stream.go Normal file
View File

@@ -0,0 +1,40 @@
package rtp
import (
"time"
"github.com/bwmarrin/discordgo"
)
// Stream -
type Stream struct {
StartTime time.Time
StartingTimestamp uint32
LatestTimestamp uint32
Packets []*discordgo.Packet
}
// GetTimeDiff -
func (s *Stream) GetTimeDiff() uint32 {
return s.LatestTimestamp - s.StartingTimestamp
}
func (s *Stream) GetTimeOffset(startTime int64) int64 {
return s.StartTime.Unix() - startTime
}
// PushPacket -
func (s *Stream) PushPacket(packet *discordgo.Packet) {
if s.StartTime.Unix() == 0 {
s.StartTime = time.Now()
}
if s.StartingTimestamp == 0 && packet.Timestamp != 0 {
s.StartingTimestamp = packet.Timestamp
}
s.LatestTimestamp = packet.Timestamp
s.Packets = append(s.Packets, packet)
}