mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 09:32:50 +00:00
adding audio converter to handler
This commit is contained in:
@@ -7,17 +7,24 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sounds = make(map[string][][]byte, 0)
|
sounds = make(map[string]*AudioClip, 0)
|
||||||
|
|
||||||
soundPlayingLock = false
|
soundPlayingLock = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type AudioClip struct {
|
||||||
|
Name string
|
||||||
|
Extension string
|
||||||
|
Content [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
const SOUNDS_DIR string = "./sounds/"
|
const SOUNDS_DIR string = "./sounds/"
|
||||||
|
|
||||||
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
@@ -77,6 +84,26 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
func loadFile(fileName string) error {
|
func loadFile(fileName string) error {
|
||||||
fmt.Println("Loading file: " + fileName + ".dca")
|
fmt.Println("Loading file: " + fileName + ".dca")
|
||||||
|
|
||||||
|
// scan directory for file
|
||||||
|
files, _ := ioutil.ReadDir("./sounds")
|
||||||
|
var fextension string
|
||||||
|
var fname string
|
||||||
|
for _, f := range files {
|
||||||
|
fname = strings.Split(f.Name(), ".")[0]
|
||||||
|
fextension = "." + strings.Split(f.Name(), ".")[1]
|
||||||
|
|
||||||
|
if fname == fileName {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fname = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if fname == "" {
|
||||||
|
return errors.New("File not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// open file and load into memory
|
||||||
file, err := os.Open(SOUNDS_DIR + fileName + ".dca")
|
file, err := os.Open(SOUNDS_DIR + fileName + ".dca")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -84,7 +111,11 @@ func loadFile(fileName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sounds[fileName] = make([][]byte, 0)
|
sounds[fileName] = &AudioClip{
|
||||||
|
Content: make([][]byte, 0),
|
||||||
|
Name: fileName,
|
||||||
|
Extension: fextension,
|
||||||
|
}
|
||||||
|
|
||||||
var opuslen int16
|
var opuslen int16
|
||||||
|
|
||||||
@@ -102,11 +133,6 @@ func loadFile(fileName string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading from dca file :", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read encoded pcm from dca file.
|
// Read encoded pcm from dca file.
|
||||||
InBuf := make([]byte, opuslen)
|
InBuf := make([]byte, opuslen)
|
||||||
err = binary.Read(file, binary.LittleEndian, &InBuf)
|
err = binary.Read(file, binary.LittleEndian, &InBuf)
|
||||||
@@ -117,7 +143,7 @@ func loadFile(fileName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sounds[fileName] = append(sounds[fileName], InBuf)
|
sounds[fileName].Content = append(sounds[fileName].Content, InBuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -145,7 +171,7 @@ func playSound(s *discordgo.Session, guildID, channelID string, sound string) (e
|
|||||||
_ = vc.Speaking(true)
|
_ = vc.Speaking(true)
|
||||||
|
|
||||||
// Send the buffer data.
|
// Send the buffer data.
|
||||||
for _, buff := range sounds[sound] {
|
for _, buff := range sounds[sound].Content {
|
||||||
vc.OpusSend <- buff
|
vc.OpusSend <- buff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user