Skip to content

Brad Fitzpatrick feedback on naming #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
121 changes: 77 additions & 44 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
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 end of 2017.
// 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)"
Expand Down
10 changes: 10 additions & 0 deletions opus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions opus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down