From d941087903ae79d41264bd6d33281a4a87918d3f Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 29 Dec 2016 11:58:57 +0000 Subject: [PATCH 1/4] Export error type, CamelCase error names --- decoder.go | 6 +-- encoder.go | 6 +-- errors.go | 121 ++++++++++++++++++++++++++++++++++------------------- stream.go | 6 +-- 4 files changed, 86 insertions(+), 53 deletions(-) diff --git a/decoder.go b/decoder.go index 7d674f9..282ac25 100644 --- a/decoder.go +++ b/decoder.go @@ -51,7 +51,7 @@ func (dec *Decoder) Init(sample_rate int, channels int) error { C.opus_int32(sample_rate), C.int(channels)) if errno != 0 { - return opusError(errno) + return Error(errno) } return nil } @@ -76,7 +76,7 @@ func (dec *Decoder) Decode(data []byte, pcm []int16) (int, error) { C.int(cap(pcm)), 0)) if n < 0 { - return 0, opusError(n) + return 0, Error(n) } return n, nil } @@ -101,7 +101,7 @@ func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error) { C.int(cap(pcm)), 0)) if n < 0 { - return 0, opusError(n) + return 0, Error(n) } return n, nil } diff --git a/encoder.go b/encoder.go index a9ced73..683ce58 100644 --- a/encoder.go +++ b/encoder.go @@ -79,7 +79,7 @@ func (enc *Encoder) Init(sample_rate int, channels int, application Application) C.int(channels), C.int(application))) if errno != 0 { - return opusError(int(errno)) + return Error(int(errno)) } return nil } @@ -109,7 +109,7 @@ func (enc *Encoder) Encode(pcm []int16, data []byte) (int, error) { (*C.uchar)(&data[0]), C.opus_int32(cap(data)))) if n < 0 { - return 0, opusError(n) + return 0, Error(n) } return n, nil } @@ -137,7 +137,7 @@ func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error) { (*C.uchar)(&data[0]), C.opus_int32(cap(data)))) if n < 0 { - return 0, opusError(n) + return 0, Error(n) } return n, nil } diff --git a/errors.go b/errors.go index d932d67..169208f 100644 --- a/errors.go +++ b/errors.go @@ -44,82 +44,115 @@ const int CONST_OP_EBADTIMESTAMP = OP_EBADTIMESTAMP; */ import "C" -type opusError int +type Error int -var _ error = opusError(0) +var _ error = Error(0) // Libopus errors var ( - ERR_OPUS_OK = opusError(C.CONST_OPUS_OK) - ERR_OPUS_BAD_ARG = opusError(C.CONST_OPUS_BAD_ARG) - ERR_OPUS_BUFFER_TOO_SMALL = opusError(C.CONST_OPUS_BUFFER_TOO_SMALL) - ERR_OPUS_INTERNAL_ERROR = opusError(C.CONST_OPUS_INTERNAL_ERROR) - ERR_OPUS_INVALID_PACKET = opusError(C.CONST_OPUS_INVALID_PACKET) - ERR_OPUS_UNIMPLEMENTED = opusError(C.CONST_OPUS_UNIMPLEMENTED) - ERR_OPUS_INVALID_STATE = opusError(C.CONST_OPUS_INVALID_STATE) - ERR_OPUS_ALLOC_FAIL = opusError(C.CONST_OPUS_ALLOC_FAIL) + ErrOK = Error(C.CONST_OPUS_OK) + ErrBadArg = Error(C.CONST_OPUS_BAD_ARG) + ErrBufferTooSmall = Error(C.CONST_OPUS_BUFFER_TOO_SMALL) + ErrInternalError = Error(C.CONST_OPUS_INTERNAL_ERROR) + ErrInvalidPacket = Error(C.CONST_OPUS_INVALID_PACKET) + ErrUnimplemented = Error(C.CONST_OPUS_UNIMPLEMENTED) + ErrInvalidState = Error(C.CONST_OPUS_INVALID_STATE) + ErrAllocFail = Error(C.CONST_OPUS_ALLOC_FAIL) +) + +// DEPRECATED versions of the above variables. Will be removed next year. Please +// don't use. +var ( + ERR_OPUS_OK = Error(C.CONST_OPUS_OK) + ERR_OPUS_BAD_ARG = Error(C.CONST_OPUS_BAD_ARG) + ERR_OPUS_BUFFER_TOO_SMALL = Error(C.CONST_OPUS_BUFFER_TOO_SMALL) + ERR_OPUS_INTERNAL_ERROR = Error(C.CONST_OPUS_INTERNAL_ERROR) + ERR_OPUS_INVALID_PACKET = Error(C.CONST_OPUS_INVALID_PACKET) + ERR_OPUS_UNIMPLEMENTED = Error(C.CONST_OPUS_UNIMPLEMENTED) + ERR_OPUS_INVALID_STATE = Error(C.CONST_OPUS_INVALID_STATE) + ERR_OPUS_ALLOC_FAIL = Error(C.CONST_OPUS_ALLOC_FAIL) ) // Error string (in human readable format) for libopus errors. -func (e opusError) Error() string { +func (e Error) Error() string { return fmt.Sprintf("opus: %s", C.GoString(C.opus_strerror(C.int(e)))) } -type opusFileError int +type StreamError int -var _ error = opusFileError(0) +var _ error = StreamError(0) // Libopusfile errors. The names are copied verbatim from the libopusfile // library. var ( - ERR_OP_FALSE = opusFileError(C.CONST_OP_FALSE) - ERR_OP_EOF = opusFileError(C.CONST_OP_EOF) - ERR_OP_HOLE = opusFileError(C.CONST_OP_HOLE) - ERR_OP_EREAD = opusFileError(C.CONST_OP_EREAD) - ERR_OP_EFAULT = opusFileError(C.CONST_OP_EFAULT) - ERR_OP_EIMPL = opusFileError(C.CONST_OP_EIMPL) - ERR_OP_EINVAL = opusFileError(C.CONST_OP_EINVAL) - ERR_OP_ENOTFORMAT = opusFileError(C.CONST_OP_ENOTFORMAT) - ERR_OP_EBADHEADER = opusFileError(C.CONST_OP_EBADHEADER) - ERR_OP_EVERSION = opusFileError(C.CONST_OP_EVERSION) - ERR_OP_ENOTAUDIO = opusFileError(C.CONST_OP_ENOTAUDIO) - ERR_OP_EBADPACKET = opusFileError(C.CONST_OP_EBADPACKET) - ERR_OP_EBADLINK = opusFileError(C.CONST_OP_EBADLINK) - ERR_OP_ENOSEEK = opusFileError(C.CONST_OP_ENOSEEK) - ERR_OP_EBADTIMESTAMP = opusFileError(C.CONST_OP_EBADTIMESTAMP) + ErrStreamFalse = StreamError(C.CONST_OP_FALSE) + ErrStreamEOF = StreamError(C.CONST_OP_EOF) + ErrStreamHole = StreamError(C.CONST_OP_HOLE) + ErrStreamRead = StreamError(C.CONST_OP_EREAD) + ErrStreamFault = StreamError(C.CONST_OP_EFAULT) + ErrStreamImpl = StreamError(C.CONST_OP_EIMPL) + ErrStreamInval = StreamError(C.CONST_OP_EINVAL) + ErrStreamNotFormat = StreamError(C.CONST_OP_ENOTFORMAT) + ErrStreamBadHeader = StreamError(C.CONST_OP_EBADHEADER) + ErrStreamVersion = StreamError(C.CONST_OP_EVERSION) + ErrStreamNotAudio = StreamError(C.CONST_OP_ENOTAUDIO) + ErrStreamBadPacked = StreamError(C.CONST_OP_EBADPACKET) + ErrStreamBadLink = StreamError(C.CONST_OP_EBADLINK) + ErrStreamNoSeek = StreamError(C.CONST_OP_ENOSEEK) + ErrStreamBadTimestamp = StreamError(C.CONST_OP_EBADTIMESTAMP) +) + +// DEPRECATED versions of the above variables. Will be removed next year. Please +// don't use. +var ( + ERR_OP_FALSE = StreamError(C.CONST_OP_FALSE) + ERR_OP_EOF = StreamError(C.CONST_OP_EOF) + ERR_OP_HOLE = StreamError(C.CONST_OP_HOLE) + ERR_OP_EREAD = StreamError(C.CONST_OP_EREAD) + ERR_OP_EFAULT = StreamError(C.CONST_OP_EFAULT) + ERR_OP_EIMPL = StreamError(C.CONST_OP_EIMPL) + ERR_OP_EINVAL = StreamError(C.CONST_OP_EINVAL) + ERR_OP_ENOTFORMAT = StreamError(C.CONST_OP_ENOTFORMAT) + ERR_OP_EBADHEADER = StreamError(C.CONST_OP_EBADHEADER) + ERR_OP_EVERSION = StreamError(C.CONST_OP_EVERSION) + ERR_OP_ENOTAUDIO = StreamError(C.CONST_OP_ENOTAUDIO) + ERR_OP_EBADPACKET = StreamError(C.CONST_OP_EBADPACKET) + ERR_OP_EBADLINK = StreamError(C.CONST_OP_EBADLINK) + ERR_OP_ENOSEEK = StreamError(C.CONST_OP_ENOSEEK) + ERR_OP_EBADTIMESTAMP = StreamError(C.CONST_OP_EBADTIMESTAMP) ) -func (i opusFileError) Error() string { +func (i StreamError) Error() string { switch i { - case ERR_OP_FALSE: + case ErrStreamFalse: return "OP_FALSE" - case ERR_OP_EOF: + case ErrStreamEOF: return "OP_EOF" - case ERR_OP_HOLE: + case ErrStreamHole: return "OP_HOLE" - case ERR_OP_EREAD: + case ErrStreamRead: return "OP_EREAD" - case ERR_OP_EFAULT: + case ErrStreamFault: return "OP_EFAULT" - case ERR_OP_EIMPL: + case ErrStreamImpl: return "OP_EIMPL" - case ERR_OP_EINVAL: + case ErrStreamInval: return "OP_EINVAL" - case ERR_OP_ENOTFORMAT: + case ErrStreamNotFormat: return "OP_ENOTFORMAT" - case ERR_OP_EBADHEADER: + case ErrStreamBadHeader: return "OP_EBADHEADER" - case ERR_OP_EVERSION: + case ErrStreamVersion: return "OP_EVERSION" - case ERR_OP_ENOTAUDIO: + case ErrStreamNotAudio: return "OP_ENOTAUDIO" - case ERR_OP_EBADPACKET: + case ErrStreamBadPacked: return "OP_EBADPACKET" - case ERR_OP_EBADLINK: + case ErrStreamBadLink: return "OP_EBADLINK" - case ERR_OP_ENOSEEK: + case ErrStreamNoSeek: return "OP_ENOSEEK" - case ERR_OP_EBADTIMESTAMP: + case ErrStreamBadTimestamp: return "OP_EBADTIMESTAMP" default: return "libopusfile error: %d (unknown code)" diff --git a/stream.go b/stream.go index 4d84f99..1264760 100644 --- a/stream.go +++ b/stream.go @@ -111,7 +111,7 @@ func (s *Stream) Init(read io.Reader) error { 0, &errno) if errno != 0 { - return opusFileError(errno) + return StreamError(errno) } s.oggfile = oggfile return nil @@ -142,7 +142,7 @@ func (s *Stream) Read(pcm []int16) (int, error) { C.int(len(pcm)), nil) if n < 0 { - return 0, opusFileError(n) + return 0, StreamError(n) } if n == 0 { return 0, io.EOF @@ -166,7 +166,7 @@ func (s *Stream) ReadFloat32(pcm []float32) (int, error) { C.int(len(pcm)), nil) if n < 0 { - return 0, opusFileError(n) + return 0, StreamError(n) } if n == 0 { return 0, io.EOF From 84e6bd1031cdf77397b012e4bc1a549b1f6e6317 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 29 Dec 2016 12:05:22 +0000 Subject: [PATCH 2/4] CamelCase Application constants --- encoder_test.go | 8 ++++---- opus.go | 10 ++++++++++ opus_test.go | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/encoder_test.go b/encoder_test.go index 527a5e3..15d640c 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -7,11 +7,11 @@ package opus import "testing" func TestEncoderNew(t *testing.T) { - enc, err := NewEncoder(48000, 1, APPLICATION_VOIP) + enc, err := NewEncoder(48000, 1, AppVoIP) if err != nil || enc == nil { t.Errorf("Error creating new encoder: %v", err) } - enc, err = NewEncoder(12345, 1, APPLICATION_VOIP) + enc, err = NewEncoder(12345, 1, AppVoIP) if err == nil || enc != nil { t.Errorf("Expected error for illegal samplerate 12345") } @@ -30,7 +30,7 @@ func TestEncoderUnitialized(t *testing.T) { } func TestEncoderDTX(t *testing.T) { - enc, err := NewEncoder(8000, 1, APPLICATION_VOIP) + enc, err := NewEncoder(8000, 1, AppVoIP) if err != nil || enc == nil { t.Errorf("Error creating new encoder: %v", err) } @@ -47,7 +47,7 @@ func TestEncoderDTX(t *testing.T) { func TestEncoderSampleRate(t *testing.T) { sample_rates := []int{8000, 12000, 16000, 24000, 48000} for _, f := range sample_rates { - enc, err := NewEncoder(f, 1, APPLICATION_VOIP) + enc, err := NewEncoder(f, 1, AppVoIP) if err != nil || enc == nil { t.Fatalf("Error creating new encoder with sample_rate %d Hz: %v", f, err) } diff --git a/opus.go b/opus.go index 7bba634..1ec60b3 100644 --- a/opus.go +++ b/opus.go @@ -20,6 +20,16 @@ type Application int // These variables should be constants, but for interoperability with CGO // they're var. Don't change them, though! +var ( + // Optimize encoding for VoIP + AppVoIP = Application(C.CONST_APPLICATION_VOIP) + // Optimize encoding for non-voice signals like music + AppAudio = Application(C.CONST_APPLICATION_AUDIO) + // Optimize encoding for low latency applications + AppRestrictedLowdelay = Application(C.CONST_APPLICATION_RESTRICTED_LOWDELAY) +) + +// DEPRECATED -- Don't use these. Will be removed end of 2017. var ( // Optimize encoding for VOIP APPLICATION_VOIP = Application(C.CONST_APPLICATION_VOIP) diff --git a/opus_test.go b/opus_test.go index 87fe110..ae703a0 100644 --- a/opus_test.go +++ b/opus_test.go @@ -32,7 +32,7 @@ func TestCodec(t *testing.T) { const FRAME_SIZE_MS = 60 const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000 pcm := make([]int16, FRAME_SIZE) - enc, err := NewEncoder(SAMPLE_RATE, 1, APPLICATION_VOIP) + enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP) if err != nil || enc == nil { t.Fatalf("Error creating new encoder: %v", err) } @@ -66,7 +66,7 @@ func TestCodecFloat32(t *testing.T) { const FRAME_SIZE_MS = 60 const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000 pcm := make([]float32, FRAME_SIZE) - enc, err := NewEncoder(SAMPLE_RATE, 1, APPLICATION_VOIP) + enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP) if err != nil || enc == nil { t.Fatalf("Error creating new encoder: %v", err) } @@ -100,7 +100,7 @@ func TestStereo(t *testing.T) { const CHANNELS = 2 const FRAME_SIZE_MONO = SAMPLE_RATE * FRAME_SIZE_MS / 1000 - enc, err := NewEncoder(SAMPLE_RATE, CHANNELS, APPLICATION_VOIP) + enc, err := NewEncoder(SAMPLE_RATE, CHANNELS, AppVoIP) if err != nil || enc == nil { t.Fatalf("Error creating new encoder: %v", err) } From 9f1efc5ce0d91ffa6a60b38fe2a925feb4af43a3 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 29 Dec 2016 12:25:40 +0000 Subject: [PATCH 3/4] Use new error var names in tests --- opus_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opus_test.go b/opus_test.go index ae703a0..786f901 100644 --- a/opus_test.go +++ b/opus_test.go @@ -19,9 +19,9 @@ func TestOpusErrstr(t *testing.T) { // I scooped this -1 up from opus_defines.h, it's OPUS_BAD_ARG. Not pretty, // but it's better than not testing at all. Again, accessing #defines from // CGO is not possible. - if ERR_OPUS_BAD_ARG.Error() != "opus: invalid argument" { + if ErrBadArg.Error() != "opus: invalid argument" { t.Errorf("Expected \"invalid argument\" error message for error code %d: %v", - ERR_OPUS_BAD_ARG, ERR_OPUS_BAD_ARG) + ErrBadArg, ErrBadArg) } } From 9a6d0a2f5f6161b69c9d1dc22003213fd2e021ad Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 29 Dec 2016 12:30:29 +0000 Subject: [PATCH 4/4] Explicit about when deprecated names are removed --- errors.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index 169208f..5448434 100644 --- a/errors.go +++ b/errors.go @@ -60,8 +60,8 @@ var ( ErrAllocFail = Error(C.CONST_OPUS_ALLOC_FAIL) ) -// DEPRECATED versions of the above variables. Will be removed next year. Please -// don't use. +// DEPRECATED versions of the above variables. Will be removed end of 2017. +// Please don't use. var ( ERR_OPUS_OK = Error(C.CONST_OPUS_OK) ERR_OPUS_BAD_ARG = Error(C.CONST_OPUS_BAD_ARG) @@ -102,8 +102,8 @@ var ( ErrStreamBadTimestamp = StreamError(C.CONST_OP_EBADTIMESTAMP) ) -// DEPRECATED versions of the above variables. Will be removed next year. Please -// don't use. +// DEPRECATED versions of the above variables. Will be removed end of 2017. +// Please don't use. var ( ERR_OP_FALSE = StreamError(C.CONST_OP_FALSE) ERR_OP_EOF = StreamError(C.CONST_OP_EOF)