2022-11-18 21:18:12 +00:00
|
|
|
package discordspeaker
|
|
|
|
|
|
|
|
import (
|
2022-12-05 17:24:06 +00:00
|
|
|
"context"
|
2023-10-26 20:49:39 +00:00
|
|
|
"io"
|
2022-11-18 21:18:12 +00:00
|
|
|
"log"
|
|
|
|
"sync"
|
2022-11-28 09:52:23 +00:00
|
|
|
"time"
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-12-05 17:24:06 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/voice"
|
|
|
|
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
|
2023-10-26 20:49:39 +00:00
|
|
|
"github.com/gopxl/beep"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-10-26 20:49:39 +00:00
|
|
|
"golang.org/x/time/rate"
|
2022-12-01 09:48:25 +00:00
|
|
|
"gopkg.in/hraban/opus.v2"
|
2022-11-18 21:18:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-10-26 20:49:39 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
mixer beep.Mixer
|
|
|
|
samples [][2]float64
|
|
|
|
done chan struct{}
|
|
|
|
encoder *opus.Encoder
|
2022-11-18 21:18:12 +00:00
|
|
|
frameSize int = 960
|
|
|
|
channels int = 2
|
|
|
|
sampleRate int = 48000
|
|
|
|
maxBytes int = (frameSize * 2) * 2
|
|
|
|
buf []byte
|
2022-12-05 17:24:06 +00:00
|
|
|
session *voice.Session
|
2023-10-26 20:49:39 +00:00
|
|
|
pw *io.PipeWriter
|
|
|
|
pr *io.PipeReader
|
2023-11-02 20:30:29 +00:00
|
|
|
|
|
|
|
spklimit = rate.NewLimiter(rate.Every(5*time.Second), 1)
|
|
|
|
spk = true
|
2022-11-18 21:18:12 +00:00
|
|
|
)
|
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
var start time.Time
|
|
|
|
|
2023-10-26 20:49:39 +00:00
|
|
|
var Silence = [2]float64{}
|
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
var dmutex = sync.Mutex{}
|
|
|
|
|
|
|
|
func Init(dgv *voice.Session, bitrate int) error {
|
2022-11-18 21:18:12 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
Close()
|
|
|
|
|
2023-10-26 20:49:39 +00:00
|
|
|
pr, pw = io.Pipe()
|
2022-11-18 21:18:12 +00:00
|
|
|
buf = make([]byte, maxBytes)
|
2023-10-26 20:49:39 +00:00
|
|
|
mixer = beep.Mixer{}
|
2022-11-18 21:18:12 +00:00
|
|
|
samples = make([][2]float64, frameSize)
|
|
|
|
|
2022-12-05 17:24:06 +00:00
|
|
|
session = dgv
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
encoder, err = opus.NewEncoder(sampleRate, channels, opus.AppAudio)
|
|
|
|
encoder.SetBitrate(bitrate)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
go func() {
|
|
|
|
dmutex.Lock()
|
|
|
|
_, err := io.Copy(session, pr)
|
|
|
|
dmutex.Unlock()
|
2023-10-26 20:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-02 20:30:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2023-10-26 20:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:49:39 +00:00
|
|
|
func Speak(s bool) {
|
|
|
|
switch s {
|
|
|
|
case true:
|
|
|
|
|
|
|
|
case false:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
func CheckSilence(samples [][2]float64) {
|
2023-10-26 20:49:39 +00:00
|
|
|
if IsSilent(samples) {
|
|
|
|
if spk && spklimit.Allow() {
|
2022-12-05 17:24:06 +00:00
|
|
|
log.Println("Notspeaking")
|
|
|
|
session.Speaking(context.Background(), voicegateway.NotSpeaking)
|
|
|
|
spk = false
|
|
|
|
}
|
2023-11-02 20:30:29 +00:00
|
|
|
} else {
|
|
|
|
if !spk {
|
|
|
|
log.Println("Speaking")
|
|
|
|
session.Speaking(context.Background(), voicegateway.Microphone)
|
|
|
|
spk = true
|
|
|
|
spklimit.Reserve()
|
|
|
|
}
|
2022-11-28 09:52:23 +00:00
|
|
|
}
|
2023-11-02 20:30:29 +00:00
|
|
|
}
|
2022-11-28 09:52:23 +00:00
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
func update() {
|
|
|
|
start = time.Now()
|
|
|
|
mu.Lock()
|
|
|
|
mixer.Stream(samples)
|
|
|
|
mu.Unlock()
|
2022-12-05 17:24:06 +00:00
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
go CheckSilence(samples)
|
2023-10-26 20:49:39 +00:00
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
if !spk {
|
|
|
|
return
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
f32 := make([]float32, len(samples)*2)
|
|
|
|
var idx int
|
|
|
|
|
|
|
|
for i := 0; i < len(samples); i++ {
|
|
|
|
f32[idx] = float32(samples[i][0])
|
|
|
|
idx++
|
|
|
|
f32[idx] = float32(samples[i][1])
|
|
|
|
idx++
|
|
|
|
}
|
2023-10-26 20:49:39 +00:00
|
|
|
n, err := encoder.EncodeFloat32(f32, buf)
|
2022-12-05 17:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-26 20:49:39 +00:00
|
|
|
|
2023-11-02 20:30:29 +00:00
|
|
|
_, err = pw.Write(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//fmt.Println(time.Since(start), len(samples), n)
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-28 09:52:23 +00:00
|
|
|
|
2023-10-26 20:49:39 +00:00
|
|
|
func IsSilent(in [][2]float64) bool {
|
2022-11-28 09:52:23 +00:00
|
|
|
for _, v := range in {
|
2023-10-26 20:49:39 +00:00
|
|
|
if v != Silence {
|
2022-11-28 09:52:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|