From: Hraban Luyat Date: Fri, 19 May 2017 21:39:16 +0000 (+0100) Subject: README: Example for file & stream handling X-Git-Tag: v2.0.0~28 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=cc6c1c4f3838411ff3ce5a8d598d8eb8c9ad8e25;p=go-opus.git README: Example for file & stream handling --- diff --git a/README.md b/README.md index d81e148..40c3962 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ xiph.org opus libs: * encoders * decoders -* stream handlers +* files & streams ### Import @@ -114,6 +114,33 @@ To decode a .opus file (or .ogg with Opus data), or to decode a "Opus stream" (which is a Ogg stream with Opus data), use the `Stream` interface. It wraps an io.Reader providing the raw stream bytes and returns the decoded Opus data. +A crude example for reading from a .opus file: + +```go +f, err := os.Open(fname) +if err != nil { + ... +} +s, err := opus.NewStream(f) +if err != nil { + ... +} +defer s.Close() +buf := make([]byte, 16384) +for { + n, err = s.Read(buf) + if err == io.EOF { + break + } else if err != nil { + ... + } + pcm := buf[:n*channels] + + // send pcm to audio device here, or write to a .wav file + +} +``` + See https://godoc.org/gopkg.in/hraban/opus.v2#Stream for further info. ### API Docs