I am trying to read a buffered stream of signed 16 bit integers (wav format), but the bufio.Read method only accepts an array of bytes. My question is a 2-parter:
- Can I preformat the byte stream into a buffered int16 array?
If I can't, whats the best way of post-processing the byte array into int16 array? My initial thought is to use tmp arrays and keep pushing/processing them, but I was curious if there was a more idiomatic way of doing this?
package main import ( "bufio" "io" "log" "os/exec" ) func main() { app := "someapp" cmd := exec.Command(app) stdout, err := cmd.StdoutPipe() r := bufio.NewReader(stdout) if err != nil { log.Fatal(err) } if err := cmd.Start(); err != nil { log.Fatal(err) } //"someapp" outputs signed 16bit integers (little endian)) buf := make([]byte, 0, 4*1024) for { n, err := r.Read(buf[:cap(buf)]) //r.Read only accepts type []byte buf = buf[:n] if n == 0 { if err == nil { continue } if err == io.EOF { break } log.Fatal(err) } log.Printf("%x\n", buf) //process buf here if err != nil && err != io.EOF { log.Fatal(err) } } }