2021-07-02 09:42:00 +00:00
|
|
|
package testdata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-09-19 15:23:25 +00:00
|
|
|
"fmt"
|
2021-07-02 09:42:00 +00:00
|
|
|
"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 {
|
2023-09-19 15:23:25 +00:00
|
|
|
return fmt.Errorf("failed to open %s: %w", file, err)
|
2021-07-02 09:42:00 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var lenbuf [4]byte
|
|
|
|
for {
|
|
|
|
_, err := io.ReadFull(f, lenbuf[:])
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-19 15:23:25 +00:00
|
|
|
return fmt.Errorf("failed to read %s: %w", file, err)
|
2021-07-02 09:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the integer
|
|
|
|
framelen := int64(binary.LittleEndian.Uint32(lenbuf[:]))
|
|
|
|
|
|
|
|
// Copy the frame.
|
|
|
|
_, err = io.CopyN(w, f, framelen)
|
|
|
|
if err != nil && err != io.EOF {
|
2023-09-19 15:23:25 +00:00
|
|
|
return fmt.Errorf("failed to write: %w", err)
|
2021-07-02 09:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|