Compare commits
2 Commits
b770f341d0
...
ddf7ea2f15
Author | SHA1 | Date |
---|---|---|
Stein Ivar Berghei | ddf7ea2f15 | |
Stein Ivar Berghei | 2f3c14c3cb |
2
mpd.go
2
mpd.go
|
@ -24,7 +24,7 @@ type MPD struct {
|
|||
func init() {
|
||||
log.Println("mpd.go loading..")
|
||||
|
||||
f, err := os.OpenFile(config.GetString("mpd.config"), os.O_RDWR|os.O_CREATE, 0755)
|
||||
f, err := os.Create(config.GetString("mpd.config"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package discordspeaker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -10,7 +8,7 @@ import (
|
|||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/faiface/beep"
|
||||
"github.com/pkg/errors"
|
||||
"layeh.com/gopus"
|
||||
"gopkg.in/hraban/opus.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -18,15 +16,13 @@ var (
|
|||
mixer beep.Mixer
|
||||
samples [][2]float64
|
||||
done chan struct{}
|
||||
encoder *gopus.Encoder
|
||||
encoder *opus.Encoder
|
||||
voice *discordgo.VoiceConnection
|
||||
frameSize int = 960
|
||||
channels int = 2
|
||||
sampleRate int = 48000
|
||||
maxBytes int = (frameSize * 2) * 2
|
||||
pcm []int16
|
||||
buf []byte
|
||||
pause bool
|
||||
)
|
||||
|
||||
func Init(dgv *discordgo.VoiceConnection) error {
|
||||
|
@ -35,17 +31,15 @@ func Init(dgv *discordgo.VoiceConnection) error {
|
|||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
Close()
|
||||
|
||||
mixer = beep.Mixer{}
|
||||
|
||||
buf = make([]byte, maxBytes)
|
||||
pcm = make([]int16, frameSize*channels)
|
||||
samples = make([][2]float64, frameSize)
|
||||
|
||||
pause = true
|
||||
|
||||
voice = dgv
|
||||
|
||||
encoder, err = gopus.NewEncoder(sampleRate, channels, gopus.Audio)
|
||||
encoder, err = opus.NewEncoder(sampleRate, channels, opus.AppVoIP)
|
||||
encoder.SetBitrateToMax()
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to initialize speaker")
|
||||
|
@ -93,34 +87,24 @@ func update() {
|
|||
mu.Lock()
|
||||
mixer.Stream(samples)
|
||||
mu.Unlock()
|
||||
var f32 []float32
|
||||
|
||||
for i := range samples {
|
||||
for c := range samples[i] {
|
||||
val := samples[i][c]
|
||||
if val < -1 {
|
||||
val = -1
|
||||
}
|
||||
if val > +1 {
|
||||
val = +1
|
||||
}
|
||||
valInt16 := int16(val * (1<<15 - 1))
|
||||
low := byte(valInt16)
|
||||
high := byte(valInt16 >> 8)
|
||||
buf[i*4+c*2+0] = low
|
||||
buf[i*4+c*2+1] = high
|
||||
}
|
||||
for _, sample := range samples {
|
||||
for _, val := range sample {
|
||||
f32 = append(f32, float32(val))
|
||||
}
|
||||
|
||||
if Silence(buf) {
|
||||
}
|
||||
|
||||
if Silence(f32) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return
|
||||
}
|
||||
|
||||
binary.Read(bytes.NewReader(buf), binary.LittleEndian, &pcm)
|
||||
|
||||
opus, err := encoder.Encode(pcm, frameSize, maxBytes)
|
||||
n, err := encoder.EncodeFloat32(f32, buf)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -128,10 +112,10 @@ func update() {
|
|||
return
|
||||
}
|
||||
|
||||
voice.OpusSend <- opus
|
||||
voice.OpusSend <- buf[:n]
|
||||
}
|
||||
|
||||
func Silence(in []byte) bool {
|
||||
func Silence(in []float32) bool {
|
||||
for _, v := range in {
|
||||
if v != 0 {
|
||||
return false
|
||||
|
|
Loading…
Reference in New Issue