Skip to content

Commit d5695ad

Browse files
authored
Fix voice example for DCA0 audio (#477)
1 parent fb4115d commit d5695ad

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

_examples/voice/nico.dca

-206 Bytes
Binary file not shown.

_examples/voice/voice.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func play(client *bot.Client, closeChan chan os.Signal) {
7878
closeChan <- syscall.SIGTERM
7979
}
8080

81+
// writeOpus reads from the included `nico.dca` file in the [DCA0] file format
82+
// and writes its Opus frames to the io.Writer.
83+
//
84+
// [DCA0]: https://github.com/bwmarrin/dca/wiki/DCA0-specification
8185
func writeOpus(w io.Writer) {
8286
file, err := os.Open("nico.dca")
8387
if err != nil {
@@ -86,9 +90,10 @@ func writeOpus(w io.Writer) {
8690
ticker := time.NewTicker(time.Millisecond * 20)
8791
defer ticker.Stop()
8892

89-
var lenBuf [4]byte
90-
for range ticker.C {
91-
_, err = io.ReadFull(file, lenBuf[:])
93+
var frameLen int16
94+
// Don't wait for the first tick, run immediately.
95+
for ; true; <-ticker.C {
96+
err = binary.Read(file, binary.LittleEndian, &frameLen)
9297
if err != nil {
9398
if err == io.EOF {
9499
_ = file.Close()
@@ -98,14 +103,11 @@ func writeOpus(w io.Writer) {
98103
return
99104
}
100105

101-
// Read the integer
102-
frameLen := int64(binary.LittleEndian.Uint32(lenBuf[:]))
103-
104106
// Copy the frame.
105-
_, err = io.CopyN(w, file, frameLen)
107+
_, err = io.CopyN(w, file, int64(frameLen))
106108
if err != nil && err != io.EOF {
107109
_ = file.Close()
108110
return
109111
}
110112
}
111-
}
113+
}

0 commit comments

Comments
 (0)