From b2eed4d9b95c7da49f5fed45e8d773c31d03b4a8 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Sun, 5 Jul 2015 12:15:17 +0100 Subject: [PATCH] Related code in separate files --- api.go | 89 ------------------------------------------------------ decoder.go | 63 ++++++++++++++++++++++++++++++++++++++ encoder.go | 57 ++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 89 deletions(-) create mode 100644 decoder.go create mode 100644 encoder.go diff --git a/api.go b/api.go index 0c00265..518399d 100644 --- 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 index 0000000..f51db8a --- /dev/null +++ b/decoder.go @@ -0,0 +1,63 @@ +// Copyright © 2015 Hraban Luyat +// +// 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 +*/ +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 index 0000000..483de73 --- /dev/null +++ b/encoder.go @@ -0,0 +1,57 @@ +// Copyright © 2015 Hraban Luyat +// +// 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 +*/ +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 +} -- 2.48.1