1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-11 01:22:48 +00:00

Merge pull request #19 from mgerb/development

fix(audio clipping): save separated audio streams
This commit is contained in:
2018-10-07 20:10:03 -05:00
committed by GitHub

View File

@@ -356,31 +356,31 @@ func writePacketsToFile(username string, packets chan *discordgo.Packet) {
os.Mkdir(config.Config.ClipsPath, os.ModePerm) os.Mkdir(config.Config.ClipsPath, os.ModePerm)
} }
// construct filename
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
filename := config.Config.ClipsPath + "/" + timestamp + "-" + username + ".wav"
// grab everything from the voice packet channel and dump it to the file // grab everything from the voice packet channel and dump it to the file
// close when there is nothing left // close when there is nothing left
pcmOut := make([]int16, 0) // split audio into specific voice streams
pcmOut := map[uint32][]int16{}
loop: loop:
for { for {
select { select {
case p := <-packets: case p := <-packets:
for _, pcm := range p.PCM { pcmOut[p.SSRC] = append(pcmOut[p.SSRC], p.PCM...)
pcmOut = append(pcmOut, pcm)
}
default: default:
break loop break loop
} }
} }
for key, pcmData := range pcmOut {
// construct filename
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
filename := config.Config.ClipsPath + "/" + timestamp + "-" + strconv.Itoa(int(key)) + "-" + username + ".wav"
cmd := exec.Command("ffmpeg", "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "-i", "pipe:0", filename) cmd := exec.Command("ffmpeg", "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "-i", "pipe:0", filename)
output := new(bytes.Buffer) output := new(bytes.Buffer)
binary.Write(output, binary.LittleEndian, pcmOut) binary.Write(output, binary.LittleEndian, pcmData)
cmd.Stdin = bytes.NewReader(output.Bytes()) cmd.Stdin = bytes.NewReader(output.Bytes())
err := cmd.Run() err := cmd.Run()
@@ -389,6 +389,7 @@ loop:
log.Error(err) log.Error(err)
} }
} }
}
// start listening to the voice channel // start listening to the voice channel
func (conn *AudioConnection) startAudioListener() { func (conn *AudioConnection) startAudioListener() {