Skip to content

Commit

Permalink
feat(dgvoice): replace send voice into discord voice channel by patch…
Browse files Browse the repository at this point in the history
…ed github.com/bwmarrin/dgvoice package
  • Loading branch information
Wittano committed May 15, 2024
1 parent 9d2fa18 commit ff86278
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bot/command/spock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"fmt"
"github.com/bwmarrin/dgvoice"
"github.com/bwmarrin/discordgo"
"github.com/wittano/komputer/bot/audio"
"github.com/wittano/komputer/bot/log"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (sc SpockCommand) playAudio(
}
defer cancel()

if err = voice.Play(playCtx, voiceChat, path, stopCh); err != nil {
if err = dgvoice.PlayAudioFile(playCtx, voiceChat, path, stopCh); err != nil {
loggerCtx.Logger.ErrorContext(playCtx, fmt.Sprintf("failed play '%s' audioPath", path), "error", err)
}
}
Expand Down
7 changes: 4 additions & 3 deletions bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module github.com/wittano/komputer/bot

go 1.22.2

replace github.com/wittano/komputer => ./..
replace github.com/bwmarrin/dgvoice => ./../dgvoice

require (
github.com/bwmarrin/dgvoice v0.0.0-20210225172318-caaac756e02e
github.com/bwmarrin/discordgo v0.28.1
github.com/google/uuid v1.6.0
github.com/jarcoal/httpmock v1.3.1
github.com/joho/godotenv v1.5.1
github.com/wittano/komputer v0.0.0-00010101000000-000000000000
go.mongodb.org/mongo-driver v1.15.0
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
Expand All @@ -29,4 +29,5 @@ require (
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32 // indirect
)
14 changes: 12 additions & 2 deletions bot/voice/play.go → dgvoice/dgvoice.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package voice
// Package dgvoice provides opus encoding and audio file playback for the
// Discordgo package.
package dgvoice

import (
"bufio"
Expand All @@ -12,6 +14,11 @@ import (
"syscall"
)

// NOTE: This API is not final and these are likely to change.

// Technically the below settings can be adjusted however that poses
// a lot of other problems that are not handled well at this time.
// These below values seem to provide the best overall performance
const (
channels int = 2 // 1 for mono, 2 for stereo
frameRate int = 48000 // audio sampling rate
Expand All @@ -20,7 +27,10 @@ const (
audioBufSize = 16384
)

func Play(ctx context.Context, vc *discordgo.VoiceConnection, path string, stop <-chan struct{}) (err error) {
// PlayAudioFile will play the given filename to the already connected
// Discord voice server/channel. voice websocket and udp socket
// must already be setup before this will work.
func PlayAudioFile(ctx context.Context, vc *discordgo.VoiceConnection, path string, stop <-chan struct{}) (err error) {
select {
case <-ctx.Done():
return context.Canceled
Expand Down
15 changes: 15 additions & 0 deletions dgvoice/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/wittano/komputer/dgvoice

go 1.22.2

require (
github.com/bwmarrin/discordgo v0.28.1
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32
)

require (
github.com/gorilla/websocket v1.5.1 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)
15 changes: 15 additions & 0 deletions dgvoice/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32 h1:/S1gOotFo2sADAIdSGk1sDq1VxetoCWr6f5nxOG0dpY=
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32/go.mod h1:yDtyzWZDFCVnva8NGtg38eH2Ns4J0D/6hD+MMeUGdF0=
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ go 1.22.2
use (
.
bot
dgvoice
)
9 changes: 9 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/wittano/komputer v1.1.0/go.mod h1:JReUad3NfC4PJFCjW/DOqqpZFD9+yWmVF6xoEMx42Sw=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
Expand Down

0 comments on commit ff86278

Please sign in to comment.