summaryrefslogtreecommitdiff
path: root/utils/music/voice.go
blob: 736ecf98d4a7d2fe5167400f71cab8f3dcc4cf79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package music

import (
	"ai/types"
	"ai/utils/logger"
	"encoding/binary"
	"fmt"
	"io"
	"os"
	"os/exec"
	"sync"
	"time"

	"github.com/bwmarrin/discordgo"
	"layeh.com/gopus"
)

const (
	channels  int = 2
	frameRate int = 48000
	frameSize int = 960
	maxBytes  int = (frameSize * 2) * 2
)

type VoiceInstance struct {
	GuildID        string
	ChannelID      string
	Connection     *discordgo.VoiceConnection
	Playing        bool
	StopChannel    chan bool
	OpusEncoder    *gopus.Encoder
	mu             sync.Mutex
	CurrentTrackID string
}

var (
	VoiceConnection = make(map[string]*VoiceInstance)
	VoiceMutex      = &sync.Mutex{}
)

func (v *VoiceInstance) Stop() {
	v.mu.Lock()
	defer v.mu.Unlock()

	if v.Playing {
		select {
		case v.StopChannel <- true:
		default:
		}
		close(v.StopChannel)
		v.StopChannel = make(chan bool, 1)
		v.Playing = false
	}
}

func JoinVoiceChannel(s *discordgo.Session, guildID, channelID string) (*VoiceInstance, error) {
	VoiceMutex.Lock()
	defer VoiceMutex.Unlock()

	if voice, exists := VoiceConnection[guildID]; exists {
		return voice, nil
	}

	vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
	if err != nil {
		return nil, err
	}

	encoder, err := gopus.NewEncoder(frameRate, channels, gopus.Audio)
	if err != nil {
		vc.Disconnect()
		return nil, err
	}

	voiceInstance := &VoiceInstance{
		GuildID:     guildID,
		ChannelID:   channelID,
		Connection:  vc,
		Playing:     false,
		StopChannel: make(chan bool, 1),
		OpusEncoder: encoder,
	}

	VoiceConnection[guildID] = voiceInstance
	return voiceInstance, nil
}

func LeaveVoiceChannel(guildID string) error {
	VoiceMutex.Lock()
	defer VoiceMutex.Unlock()

	voice, exists := VoiceConnection[guildID]
	if !exists {
		return nil
	}

	voice.Stop()

	err := voice.Connection.Disconnect()
	if err != nil {
		return err
	}

	delete(VoiceConnection, guildID)
	return nil
}

func IsUserInSameVC(s *discordgo.Session, guildID, userID string) (bool, string) {
	guild, err := s.State.Guild(guildID)
	if err != nil {
		return false, ""
	}

	var userChannelID string
	for _, vs := range guild.VoiceStates {
		if vs.UserID == userID {
			userChannelID = vs.ChannelID
			break
		}
	}

	if userChannelID == "" {
		return false, ""
	}

	VoiceMutex.Lock()
	defer VoiceMutex.Unlock()

	voice, exists := VoiceConnection[guildID]
	if !exists {
		return true, userChannelID
	}

	return voice.ChannelID == userChannelID, userChannelID
}

func (v *VoiceInstance) PlayYouTube(videoURL, videoID string) error {
	logger.Log("Starting to play: "+videoURL, types.LogOptions{
		Prefix: "Music Player",
		Level:  types.Info,
	})

	var oldStopChan chan bool

	v.mu.Lock()
	if v.Playing {
		logger.Log("Stopping current playback", types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Info,
		})
		oldStopChan = v.StopChannel
		v.StopChannel = make(chan bool, 1)
	} else {
		v.StopChannel = make(chan bool, 1)
	}

	v.Playing = true
	v.CurrentTrackID = videoID
	stopChan := v.StopChannel
	v.mu.Unlock()

	if oldStopChan != nil {
		select {
		case oldStopChan <- true:
			logger.Log("Stop signal sent to previous playback", types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Debug,
			})
		default:
			logger.Log("Could not send stop signal", types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Debug,
			})
		}
		time.Sleep(100 * time.Millisecond)
	}

	err := os.MkdirAll("./temp", 0755)
	if err != nil {
		logger.Log("Failed to create temp directory: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}

	fileName := fmt.Sprintf("./temp/%s_%d.mp3", videoID, time.Now().Unix())
	logger.Log("Downloading to: "+fileName, types.LogOptions{
		Prefix: "Music Player",
		Level:  types.Debug,
	})

	var downloadCmd *exec.Cmd
	cookiesFile := "./cookies/cookies.txt"
	if _, err := os.Stat(cookiesFile); err == nil {
		logger.Log("Using cookies file: "+cookiesFile, types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Debug,
		})
		downloadCmd = exec.Command("yt-dlp", "--no-warnings", "--quiet", "-x", "--audio-format", "mp3",
			"--audio-quality", "0", "--no-playlist", "--cookies", cookiesFile, "--output", fileName, videoURL)
	} else {
		logger.Log("No cookies file found, downloading without cookies", types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Debug,
		})
		downloadCmd = exec.Command("yt-dlp", "--no-warnings", "--quiet", "-x", "--audio-format", "mp3",
			"--audio-quality", "0", "--no-playlist", "--output", fileName, videoURL)
	}

	// Create logs for stdout and stderr to capture yt-dlp output
	stdout, err := downloadCmd.StdoutPipe()
	if err != nil {
		logger.Log("Error creating StdoutPipe: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}
	stderr, err := downloadCmd.StderrPipe()
	if err != nil {
		logger.Log("Error creating StderrPipe: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}

	// Start the download process
	err = downloadCmd.Start()
	if err != nil {
		logger.Log("Error starting yt-dlp command: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}

	// Log the stdout and stderr
	go func() {
		stdoutLogs := make([]byte, 1024)
		for {
			n, err := stdout.Read(stdoutLogs)
			if err != nil {
				break
			}
			logger.Log("yt-dlp stdout: "+string(stdoutLogs[:n]), types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Debug,
			})
		}
	}()
	go func() {
		stderrLogs := make([]byte, 1024)
		for {
			n, err := stderr.Read(stderrLogs)
			if err != nil {
				break
			}
			logger.Log("yt-dlp stderr: "+string(stderrLogs[:n]), types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Error,
			})
		}
	}()

	err = downloadCmd.Wait()
	if err != nil {
		logger.Log("Download error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		v.mu.Lock()
		v.Playing = false
		v.mu.Unlock()
		return err
	}

	logger.Log("Download complete, starting playback", types.LogOptions{
		Prefix: "Music Player",
		Level:  types.Info,
	})

	fileInfo, err := os.Stat(fileName)
	if err != nil {
		logger.Log("File stat error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		v.mu.Lock()
		v.Playing = false
		v.mu.Unlock()
		return err
	}

	logger.Log(fmt.Sprintf("File size: %d bytes", fileInfo.Size()), types.LogOptions{
		Prefix: "Music Player",
		Level:  types.Debug,
	})

	defer os.Remove(fileName)

	err = v.playAudioFile(fileName, stopChan)
	if err != nil {
		logger.Log("Playback error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
	}

	v.mu.Lock()
	v.Playing = false
	v.mu.Unlock()

	return err
}

func (v *VoiceInstance) playAudioFile(filename string, stopChan chan bool) error {
	v.Connection.Speaking(false)
	time.Sleep(50 * time.Millisecond)

	err := v.Connection.Speaking(true)
	if err != nil {
		logger.Log("Speaking error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}
	defer v.Connection.Speaking(false)

	ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "quiet", "-i", filename, "-f", "s16le", "-ar", "48000", "-ac", "2", "pipe:1")
	ffmpegout, err := ffmpeg.StdoutPipe()
	if err != nil {
		logger.Log("FFmpeg pipe error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}

	ffmpeg.Stderr = nil
	err = ffmpeg.Start()
	if err != nil {
		logger.Log("FFmpeg start error: "+err.Error(), types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Error,
		})
		return err
	}

	ffmpegProcess := ffmpeg.Process
	defer func() {
		ffmpegProcess.Kill()
		ffmpeg.Wait()
	}()

	buf := make([]int16, frameSize*channels)

	playbackDone := make(chan error, 1)
	go func() {
		for {
			err = binary.Read(ffmpegout, binary.LittleEndian, &buf)
			if err == io.EOF || err == io.ErrUnexpectedEOF {
				playbackDone <- nil
				return
			}
			if err != nil {
				playbackDone <- err
				return
			}

			opus, err := v.OpusEncoder.Encode(buf, frameSize, maxBytes)
			if err != nil {
				playbackDone <- err
				return
			}

			select {
			case v.Connection.OpusSend <- opus:
			case <-stopChan:
				playbackDone <- nil
				return
			}
		}
	}()

	select {
	case err := <-playbackDone:
		if err != nil {
			logger.Log("Playback error: "+err.Error(), types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Error,
			})
		} else {
			logger.Log("Playback completed", types.LogOptions{
				Prefix: "Music Player",
				Level:  types.Success,
			})
		}
		return err
	case <-stopChan:
		logger.Log("Playback stopped by request", types.LogOptions{
			Prefix: "Music Player",
			Level:  types.Info,
		})
		return nil
	}
}