Skip to content

Commit db5b99c

Browse files
Maximilian BruneMaximilian Brune
authored andcommitted
Did some cleaning up and changed way to send Messages
1 parent a65ba6b commit db5b99c

File tree

10 files changed

+46
-73
lines changed

10 files changed

+46
-73
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ mumble library written in go. Intended for writing client side music bots.
1515

1616
## TODO
1717
- implement more than just youtube videos as source for music
18+
- be more or less OS independent (I am only using Linux and have not tested it on other Operating Systems)
19+
- make library capable of using TLS certificates
1820

1921
## Notes
2022
If you want to use this library be aware that this Project is still very much experimental. I appreciate and welcome any Issue or pull request or feature request.

gomble/audioformats/opus.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const (
2424
// Best for most VoIP/videoconference applications where listening quality and intelligibility matter most
2525
OPUS_APPICATION_VOIP = 2048
2626

27-
// Best for broadcast/high-fidelity application where the decoded audio sho uld be as close as possible to the input
28-
OPUS_APPLICATION_VOICE = 2049
27+
// Best for broadcast/high-fidelity application where the decoded audio should be as close as possible to the input
28+
OPUS_APPLICATION_AUDIO = 2049
2929

3030
// Only use when lowest-achievable latency is what matters most. Voice-optimized modes cannot be used.
3131
OPUS_APPLICATION_RESTRICTEDLOWDELAY = 2051
@@ -58,7 +58,7 @@ const (
5858
//OPUS_MAX_PACKET_SIZE = 1275
5959

6060
// The Application to use for opus. libopus will automatically
61-
OPUS_APPLICATION = OPUS_APPLICATION_VOICE
61+
OPUS_APPLICATION = OPUS_APPLICATION_AUDIO
6262

6363
// Variable Bit Rate, if set to one libopus will automatically change the bitrate as it sees fit.
6464
OPUS_VBR = 0

gomble/audiopackagewriter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ func sendAudioPacket(opusPayload []byte, opusPayloadlen uint16, last bool) {
7272
var all []byte = make([]byte, len(header)+len(opusPayload))
7373
copy(all[:len(header)], header[:])
7474
copy(all[len(header):], opusPayload[:])
75-
logger.Debugf("header size: %d\n", len(header))
76-
logger.Debugf("payloa size: %d\n", len(opusPayload))
77-
logger.Debugf("entire size %d\n", len(all))
75+
logger.Debugf("header size: %d, payload size: %d, entire size %d\n", len(header), len(opusPayload), len(all))
7876

7977
// Do we tunnel audio over TCP or do we send it over UDP
8078
if audiocryptoconfig.tcpTunnelMode {

gomble/channel.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

gomble/eventhandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ var Listener listener
1818
type listener struct {
1919
OnPrivateMessageReceived func(e PrivateMessageReceivedEvent)
2020
OnChannelMessageReceived func(e ChannelMessageReceivedEvent)
21-
OnTrackPaused func(e TrackPausedEvent)
22-
OnTrackStopped func(e TrackStoppedEvent)
23-
OnTrackException func(e TrackExceptionEvent)
24-
OnTrackFinished func(e TrackFinishedEvent)
21+
OnTrackPaused func(e TrackPausedEvent)
22+
OnTrackStopped func(e TrackStoppedEvent)
23+
OnTrackException func(e TrackExceptionEvent)
24+
OnTrackFinished func(e TrackFinishedEvent)
2525
}
2626

2727
// conn is our tcp connection to the server. It is used by packagereader.go to read packages from mumble-server and by packagewriter to write packages to the mumble-server.

gomble/packagereader.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ func handlePacket(pckType uint16, data []byte) { // {{{
8888
case 9:
8989
var pck mumbleproto.UserState
9090
proto.Unmarshal(data, &pck)
91+
if (pck.GetName() == "gomble-bot") {
92+
// We got our own UserState
93+
BotUserState.ChannelId = pck.GetChannelId()
94+
BotUserState.Name = pck.GetName()
95+
BotUserState.Session = pck.GetSession()
96+
}
9197
break
9298
// Server sync
9399
case 5:

gomble/track.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func audioroutine(t *Track) { // {{{
9999
return
100100
}
101101

102-
opusbuf := make(chan []byte, t.buffer_ms / audioformats.OPUS_FRAME_DURATION)
102+
opusbuf := make(chan []byte, t.buffer_ms / audioformats.OPUS_FRAME_DURATION) // make channel with buffer_ms/frame_duration number of frames as buffer
103103
// our producer
104104
go func () {
105105
for true {
@@ -156,7 +156,7 @@ func audioroutine(t *Track) { // {{{
156156
}
157157
default:
158158
}
159-
logger.Debugf("Get next opus frame\n")
159+
logger.Debugf("Get next opus frame, number of frames in buffer: %d\n", len(opusbuf))
160160
lastTime := time.Now()
161161
opusPayload, ok := <-opusbuf
162162
elapsed := time.Since(lastTime)

gomble/tracksources/youtube/youtubedl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ type YoutubedlFormat struct {
3737
}
3838

3939
func NewYoutubedlVideo(path string) (*YoutubedlVideo, error) {// {{{
40+
var video YoutubedlVideo
41+
4042
cmd := exec.Command("youtube-dl", "-j", path)
4143
buf, err := cmd.Output()
4244
if err != nil {
4345
_, file, line, _ := runtime.Caller(0)
4446
return nil, fmt.Errorf("NewYoutubeVideo(%s:%d): %w", file, line, err)
4547
}
46-
var video YoutubedlVideo
4748
err = json.Unmarshal(buf, &video)
4849
if err != nil {
4950
_, file, line, _ := runtime.Caller(0)

gomble/user.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,39 @@ package gomble
33
import "github.com/CodingVoid/gomble/logger"
44
import "github.com/CodingVoid/gomble/mumbleproto"
55

6-
type user struct {
7-
id uint32
8-
//name string
6+
type UserState struct {
7+
Name string
8+
Session uint32
9+
ChannelId uint32
910
}
1011

11-
//var users = map[uint32]string{}
12-
13-
func GetUser(userid uint32) *user {
14-
//username := users[userid]
15-
//if username == "" {
16-
// return nil
17-
//}
18-
return &user{
19-
id: userid,
20-
//name: username,
21-
}
22-
}
23-
24-
//func CheckUserExists(userid uint32) bool {
25-
// if users[userid] != "" {
26-
// return true
27-
// }
28-
// return false
29-
//}
12+
var BotUserState UserState
3013

3114
// send Message to the user
32-
func (u user) SendMessage(msg string) {
15+
func SendMessageToUser(msg string, userid uint32) {
3316
pck := mumbleproto.TextMessage{
3417
Actor: nil, //uint32
3518
Message: &msg,
3619
TreeId: nil, //[]uint32 {},
37-
Session: []uint32{u.id},
20+
Session: []uint32{userid},
3821
ChannelId: nil, //[]uint32{},
3922
}
40-
logger.Infof("Sending Message to user %d\n", u.id)
23+
logger.Infof("Sending Message to user with session-ID: %d\n", userid)
24+
if err := writeProto(&pck); err != nil {
25+
panic(err.Error())
26+
}
27+
}
28+
29+
// send Message to the user
30+
func SendMessageToChannel(msg string, channelid uint32) {
31+
pck := mumbleproto.TextMessage{
32+
Actor: nil, // uint32
33+
Message: &msg,
34+
TreeId: nil, // []uint32 {},
35+
Session: nil, // []uint32{},
36+
ChannelId: []uint32{channelid},
37+
}
38+
logger.Infof("Sending Message to Channel %d\n", channelid)
4139
if err := writeProto(&pck); err != nil {
4240
panic(err.Error())
4341
}

main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
// queue of tracks
1111
var queue []*gomble.Track
1212

13-
// current Channel the bot is in
14-
var currentChannel gomble.Channel
15-
1613
func main() {
1714
gomble.Init(logger.TRACE, "127.0.0.1:64738", false)
1815
gomble.Listener.OnPrivateMessageReceived = OnPrivateMessageReceived
@@ -26,12 +23,12 @@ func main() {
2623
}
2724

2825
func OnPrivateMessageReceived(e gomble.PrivateMessageReceivedEvent) {
29-
gomble.GetUser(e.Actor).SendMessage("Send Back Private")
26+
gomble.SendMessageToUser("Send Back Private", e.Actor)
3027
}
3128

3229
func OnChannelMessageReceived(e gomble.ChannelMessageReceivedEvent) {
3330
// set current channel
34-
currentChannel = gomble.GetChannel(e.Channel)
31+
//gomble.SwitchChannel(e.Channel) TODO
3532
if strings.HasPrefix(e.Message, "#play ") {
3633
logger.Debugf(e.Message + "\n")
3734
yt, err := gomble.LoadTrack(e.Message)
@@ -71,7 +68,7 @@ func startNextTrack() {
7168
t := queue[0]
7269
// returns false if a track is already playing (or t == nil). returns true if starting was successful
7370
if gomble.Play(t) {
74-
currentChannel.SendMessage("Start playing Track " + t.GetTitle())
71+
gomble.SendMessageToChannel("Start playing Track " + t.GetTitle(), gomble.BotUserState.ChannelId)
7572
// If successful remove the track from the queue
7673
queue = queue[1:]
7774
}

0 commit comments

Comments
 (0)