From 10761f623b1944bd299e8c9435f1cddb7b4e4fef Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Mon, 10 Oct 2016 14:18:10 +0100 Subject: [PATCH] Make opus errors wrapped ints, expose names Allows comparing error values by value instead of just by human readable string. --- decoder.go | 6 +++--- encoder.go | 6 +++--- errors.go | 45 ++++++++++++++++++++++++++++++++++++++------- opus_test.go | 8 ++++---- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/decoder.go b/decoder.go index c77a5ba..3674894 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 opuserr(int(errno)) + return opusError(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, opuserr(n) + return 0, opusError(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, opuserr(n) + return 0, opusError(n) } return n, nil } diff --git a/encoder.go b/encoder.go index 976091b..bd50405 100644 --- a/encoder.go +++ b/encoder.go @@ -77,7 +77,7 @@ func (enc *Encoder) Init(sample_rate int, channels int, application Application) C.int(channels), C.int(application))) if errno != 0 { - return opuserr(int(errno)) + return opusError(int(errno)) } return nil } @@ -101,7 +101,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, opuserr(n) + return 0, opusError(n) } return n, nil } @@ -125,7 +125,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, opuserr(n) + return 0, opusError(n) } return n, nil } diff --git a/errors.go b/errors.go index 928bec6..3c47287 100644 --- a/errors.go +++ b/errors.go @@ -14,6 +14,18 @@ import ( #include // Access the preprocessor from CGO + +// Errors for libopus +const int CONST_OPUS_OK = OPUS_OK; +const int CONST_OPUS_BAD_ARG = OPUS_BAD_ARG; +const int CONST_OPUS_BUFFER_TOO_SMALL = OPUS_BUFFER_TOO_SMALL; +const int CONST_OPUS_INTERNAL_ERROR = OPUS_INTERNAL_ERROR; +const int CONST_OPUS_INVALID_PACKET = OPUS_INVALID_PACKET; +const int CONST_OPUS_UNIMPLEMENTED = OPUS_UNIMPLEMENTED; +const int CONST_OPUS_INVALID_STATE = OPUS_INVALID_STATE; +const int CONST_OPUS_ALLOC_FAIL = OPUS_ALLOC_FAIL; + +// Errors for libopusfile const int CONST_OP_FALSE = OP_FALSE; const int CONST_OP_EOF = OP_EOF; const int CONST_OP_HOLE = OP_HOLE; @@ -32,8 +44,33 @@ const int CONST_OP_EBADTIMESTAMP = OP_EBADTIMESTAMP; */ import "C" +type opusError int + +var _ error = opusError(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) +) + +// Error string (in human readable format) for libopus errors. +func (e opusError) Error() string { + return fmt.Sprintf("opus: %s", C.GoString(C.opus_strerror(C.int(e)))) +} + type opusFileError int +var _ error = opusFileError(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) @@ -85,12 +122,6 @@ func (i opusFileError) Error() string { case ERR_OP_EBADTIMESTAMP: return "OP_EBADTIMESTAMP" default: - return "libopus error: %d (unknown code)" + return "libopusfile error: %d (unknown code)" } } - -// opuserr translates libopus (not libopusfile) error codes to human readable -// strings -func opuserr(code int) error { - return fmt.Errorf("opus: %s", C.GoString(C.opus_strerror(C.int(code)))) -} diff --git a/opus_test.go b/opus_test.go index b600abe..230f426 100644 --- a/opus_test.go +++ b/opus_test.go @@ -62,13 +62,13 @@ func TestDecoderUnitialized(t *testing.T) { } } -func TestOpuserr(t *testing.T) { +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. - err := opuserr(-1) - if err.Error() != "opus: invalid argument" { - t.Errorf("Expected \"invalid argument\" error message for error code -1: %v") + if ERR_OPUS_BAD_ARG.Error() != "opus: invalid argument" { + t.Errorf("Expected \"invalid argument\" error message for error code %d: %v", + ERR_OPUS_BAD_ARG, ERR_OPUS_BAD_ARG) } } -- 2.48.1