]> Sergey Matveev's repositories - go-opus.git/commitdiff
Related code in separate files
authorHraban Luyat <hraban@0brg.net>
Sun, 5 Jul 2015 11:15:17 +0000 (12:15 +0100)
committerHraban Luyat <hraban@0brg.net>
Sun, 5 Jul 2015 11:15:17 +0000 (12:15 +0100)
api.go
decoder.go [new file with mode: 0644]
encoder.go [new file with mode: 0644]

diff --git a/api.go b/api.go
index 0c00265005497126a5087b215b25df1c7babb389..518399ddcdaccdb2cc3f7c5d059a23951be906ce 100644 (file)
--- a/api.go
+++ b/api.go
@@ -6,7 +6,6 @@ package opus
 
 import (
        "fmt"
-       "unsafe"
 )
 
 /*
@@ -48,94 +47,6 @@ func Version() string {
        return C.GoString(C.opus_get_version_string())
 }
 
-type Encoder struct {
-       p *C.struct_OpusEncoder
-}
-
 func opuserr(code int) error {
        return fmt.Errorf("opus: %s", C.GoString(C.opus_strerror(C.int(code))))
 }
-
-func NewEncoder(sample_rate int, channels int, application Application) (*Encoder, error) {
-       var errno int
-       p := C.opus_encoder_create(C.opus_int32(sample_rate), C.int(channels), C.int(application), (*C.int)(unsafe.Pointer(&errno)))
-       if errno != 0 {
-               return nil, opuserr(errno)
-       }
-       return &Encoder{p: p}, nil
-}
-
-func (enc *Encoder) EncodeFloat32(pcm []float32) ([]byte, error) {
-       if pcm == nil || len(pcm) == 0 {
-               return nil, fmt.Errorf("opus: no data supplied")
-       }
-       // I never know how much to allocate
-       data := make([]byte, 10000)
-       n := int(C.opus_encode_float(
-               enc.p,
-               (*C.float)(&pcm[0]),
-               C.int(len(pcm)),
-               (*C.uchar)(&data[0]),
-               C.opus_int32(cap(data))))
-       if n < 0 {
-               return nil, opuserr(n)
-       }
-       return data[:n], nil
-}
-
-// Returns an error if the encoder was already closed
-func (enc *Encoder) Close() error {
-       if enc.p == nil {
-               return fmt.Errorf("opus: encoder already closed")
-       }
-       C.opus_encoder_destroy(enc.p)
-       enc.p = nil
-       return nil
-}
-
-type Decoder struct {
-       p           *C.struct_OpusDecoder
-       sample_rate int
-}
-
-func NewDecoder(sample_rate int, channels int) (*Decoder, error) {
-       var errno int
-       p := C.opus_decoder_create(C.opus_int32(sample_rate), C.int(channels), (*C.int)(unsafe.Pointer(&errno)))
-       if errno != 0 {
-               return nil, opuserr(errno)
-       }
-       dec := &Decoder{
-               p:           p,
-               sample_rate: sample_rate,
-       }
-       return dec, nil
-}
-
-func (dec *Decoder) DecodeFloat32(data []byte) ([]float32, error) {
-       if data == nil || len(data) == 0 {
-               return nil, fmt.Errorf("opus: no data supplied")
-       }
-       // I don't know how big this frame will be, but this is the limit
-       pcm := make([]float32, xMAX_FRAME_SIZE_MS*dec.sample_rate/1000)
-       n := int(C.opus_decode_float(
-               dec.p,
-               (*C.uchar)(&data[0]),
-               C.opus_int32(len(data)),
-               (*C.float)(&pcm[0]),
-               C.int(cap(pcm)),
-               0))
-       if n < 0 {
-               return nil, opuserr(n)
-       }
-       return pcm[:n], nil
-}
-
-// Returns an error if the encoder was already closed
-func (dec *Decoder) Close() error {
-       if dec.p == nil {
-               return fmt.Errorf("opus: decoder already closed")
-       }
-       C.opus_decoder_destroy(dec.p)
-       dec.p = nil
-       return nil
-}
diff --git a/decoder.go b/decoder.go
new file mode 100644 (file)
index 0000000..f51db8a
--- /dev/null
@@ -0,0 +1,63 @@
+// Copyright © 2015 Hraban Luyat <hraban@0brg.net>
+//
+// License for use of this code is detailed in the LICENSE file
+
+package opus
+
+import (
+       "fmt"
+       "unsafe"
+)
+
+/*
+#cgo CFLAGS: -std=c99 -Wall -Werror -pedantic -Ibuild/include
+#include <opus/opus.h>
+*/
+import "C"
+
+type Decoder struct {
+       p           *C.struct_OpusDecoder
+       sample_rate int
+}
+
+func NewDecoder(sample_rate int, channels int) (*Decoder, error) {
+       var errno int
+       p := C.opus_decoder_create(C.opus_int32(sample_rate), C.int(channels), (*C.int)(unsafe.Pointer(&errno)))
+       if errno != 0 {
+               return nil, opuserr(errno)
+       }
+       dec := &Decoder{
+               p:           p,
+               sample_rate: sample_rate,
+       }
+       return dec, nil
+}
+
+func (dec *Decoder) DecodeFloat32(data []byte) ([]float32, error) {
+       if data == nil || len(data) == 0 {
+               return nil, fmt.Errorf("opus: no data supplied")
+       }
+       // I don't know how big this frame will be, but this is the limit
+       pcm := make([]float32, xMAX_FRAME_SIZE_MS*dec.sample_rate/1000)
+       n := int(C.opus_decode_float(
+               dec.p,
+               (*C.uchar)(&data[0]),
+               C.opus_int32(len(data)),
+               (*C.float)(&pcm[0]),
+               C.int(cap(pcm)),
+               0))
+       if n < 0 {
+               return nil, opuserr(n)
+       }
+       return pcm[:n], nil
+}
+
+// Returns an error if the encoder was already closed
+func (dec *Decoder) Close() error {
+       if dec.p == nil {
+               return fmt.Errorf("opus: decoder already closed")
+       }
+       C.opus_decoder_destroy(dec.p)
+       dec.p = nil
+       return nil
+}
diff --git a/encoder.go b/encoder.go
new file mode 100644 (file)
index 0000000..483de73
--- /dev/null
@@ -0,0 +1,57 @@
+// Copyright © 2015 Hraban Luyat <hraban@0brg.net>
+//
+// License for use of this code is detailed in the LICENSE file
+
+package opus
+
+import (
+       "fmt"
+       "unsafe"
+)
+
+/*
+#cgo CFLAGS: -std=c99 -Wall -Werror -pedantic -Ibuild/include
+#include <opus/opus.h>
+*/
+import "C"
+
+type Encoder struct {
+       p *C.struct_OpusEncoder
+}
+
+func NewEncoder(sample_rate int, channels int, application Application) (*Encoder, error) {
+       var errno int
+       p := C.opus_encoder_create(C.opus_int32(sample_rate), C.int(channels), C.int(application), (*C.int)(unsafe.Pointer(&errno)))
+       if errno != 0 {
+               return nil, opuserr(errno)
+       }
+       return &Encoder{p: p}, nil
+}
+
+func (enc *Encoder) EncodeFloat32(pcm []float32) ([]byte, error) {
+       if pcm == nil || len(pcm) == 0 {
+               return nil, fmt.Errorf("opus: no data supplied")
+       }
+       // I never know how much to allocate
+       data := make([]byte, 10000)
+       n := int(C.opus_encode_float(
+               enc.p,
+               (*C.float)(&pcm[0]),
+               C.int(len(pcm)),
+               (*C.uchar)(&data[0]),
+               C.opus_int32(cap(data))))
+       if n < 0 {
+               return nil, opuserr(n)
+       }
+       return data[:n], nil
+}
+
+// Returns an error if the encoder was already closed
+func (enc *Encoder) Close() error {
+       if enc.p == nil {
+               return fmt.Errorf("opus: encoder already closed")
+       }
+       C.opus_encoder_destroy(enc.p)
+       enc.p = nil
+       return nil
+}