1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-30 10:43:30 +00:00
arikawa/voice/testdata/testdata.go
Cléo Rebert d36955acea Move away from github.com/pkg/errors
Signed-off-by: Cléo Rebert <cleo.rebert@gmail.com>
2023-09-19 11:37:24 -07:00

48 lines
930 B
Go

package testdata
import (
"encoding/binary"
"fmt"
"io"
"os"
)
const Nico = "testdata/nico.dca"
// WriteOpus reads the given file containing the Opus frames into the give
// io.Writer.
func WriteOpus(w io.Writer, file string) error {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open %s: %w", file, err)
}
defer f.Close()
var lenbuf [4]byte
for {
_, err := io.ReadFull(f, lenbuf[:])
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("failed to read %s: %w", file, err)
}
// Read the integer
framelen := int64(binary.LittleEndian.Uint32(lenbuf[:]))
// Copy the frame.
_, err = io.CopyN(w, f, framelen)
if err != nil && err != io.EOF {
return fmt.Errorf("failed to write: %w", err)
}
}
}
// WriterFunc wraps f to be an io.Writer.
type WriterFunc func([]byte) (int, error)
func (w WriterFunc) Write(b []byte) (int, error) {
return w(b)
}