2022-11-18 21:18:12 +00:00
|
|
|
package discordspeaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync"
|
2022-11-28 09:52:23 +00:00
|
|
|
"time"
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/faiface/beep"
|
|
|
|
"github.com/pkg/errors"
|
2022-12-01 09:48:25 +00:00
|
|
|
"gopkg.in/hraban/opus.v2"
|
2022-11-18 21:18:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
|
|
|
mixer beep.Mixer
|
|
|
|
samples [][2]float64
|
|
|
|
done chan struct{}
|
2022-12-01 09:48:25 +00:00
|
|
|
encoder *opus.Encoder
|
2022-11-18 21:18:12 +00:00
|
|
|
voice *discordgo.VoiceConnection
|
|
|
|
frameSize int = 960
|
|
|
|
channels int = 2
|
|
|
|
sampleRate int = 48000
|
|
|
|
maxBytes int = (frameSize * 2) * 2
|
|
|
|
buf []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init(dgv *discordgo.VoiceConnection) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
Close()
|
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
mixer = beep.Mixer{}
|
2022-11-18 21:18:12 +00:00
|
|
|
buf = make([]byte, maxBytes)
|
|
|
|
samples = make([][2]float64, frameSize)
|
|
|
|
|
|
|
|
voice = dgv
|
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
encoder, err = opus.NewEncoder(sampleRate, channels, opus.AppVoIP)
|
|
|
|
encoder.SetBitrateToMax()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to initialize speaker")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
update()
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Lock() {
|
|
|
|
mu.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the speaker. Call after modifying any currently playing Streamer.
|
|
|
|
func Unlock() {
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Play(s ...beep.Streamer) {
|
|
|
|
mu.Lock()
|
|
|
|
mixer.Add(s...)
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Clear() {
|
|
|
|
mu.Lock()
|
|
|
|
mixer.Clear()
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func update() {
|
|
|
|
mu.Lock()
|
|
|
|
mixer.Stream(samples)
|
|
|
|
mu.Unlock()
|
2022-12-01 09:48:25 +00:00
|
|
|
var f32 []float32
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
for _, sample := range samples {
|
|
|
|
for _, val := range sample {
|
|
|
|
f32 = append(f32, float32(val))
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-12-01 09:48:25 +00:00
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
if Silence(f32) {
|
2022-11-28 09:52:23 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
n, err := encoder.EncodeFloat32(f32, buf)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2022-12-01 09:48:25 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-23 07:38:55 +00:00
|
|
|
if !voice.Ready || voice.OpusSend == nil {
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-28 09:52:23 +00:00
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
voice.OpusSend <- buf[:n]
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-28 09:52:23 +00:00
|
|
|
|
2022-12-01 09:48:25 +00:00
|
|
|
func Silence(in []float32) bool {
|
2022-11-28 09:52:23 +00:00
|
|
|
for _, v := range in {
|
|
|
|
if v != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|