]> Sergey Matveev's repositories - go-opus.git/commitdiff
Make opus errors wrapped ints, expose names
authorHraban Luyat <hraban@0brg.net>
Mon, 10 Oct 2016 13:18:10 +0000 (14:18 +0100)
committerHraban Luyat <hraban@0brg.net>
Mon, 10 Oct 2016 13:18:10 +0000 (14:18 +0100)
Allows comparing error values by value instead of just by human readable
string.

decoder.go
encoder.go
errors.go
opus_test.go

index c77a5ba064d7c3aea614ef1c7555346a1d0f9c07..367489453f1bcb518a581f8594bd51d56a31f74d 100644 (file)
@@ -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
 }
index 976091b73eafb27db3c1ab36b84be7d6f1223993..bd50405633684c7ce30ff9e5d68561a50e9bbbda 100644 (file)
@@ -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
 }
index 928bec64934bace5e29fa51678e21721bb96db75..3c472875e1098c5dbda273499be98064788884ea 100644 (file)
--- a/errors.go
+++ b/errors.go
@@ -14,6 +14,18 @@ import (
 #include <opusfile.h>
 
 // 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))))
-}
index b600abe5c38c5f6012eb20aac1c92b967a11c5cb..230f4265e78f7de866173dd7a8efc33650fa2500 100644 (file)
@@ -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)
        }
 }