func NewDecoder(sampleRate, channels int) (*Decoder, error) { decoder := &Decoder{} decoder.data = make([]byte, int(C.opus_decoder_get_size(C.int(channels)))) decoder.cDecoder = (*C.struct_OpusDecoder)(unsafe.Pointer(&decoder.data[0])) ret := C.opus_decoder_init(decoder.cDecoder, C.opus_int32(sampleRate), C.int(channels)) if err := getErr(ret); err != nil { return nil, err } decoder.channels = channels return decoder, nil }
func (dec *Decoder) Init(sample_rate int, channels int) error { if dec.p != nil { return fmt.Errorf("opus decoder already initialized") } if channels != 1 && channels != 2 { return fmt.Errorf("Number of channels must be 1 or 2: %d", channels) } size := C.opus_decoder_get_size(C.int(channels)) dec.sample_rate = sample_rate dec.mem = make([]byte, size) dec.p = (*C.OpusDecoder)(unsafe.Pointer(&dec.mem[0])) errno := C.opus_decoder_init( dec.p, C.opus_int32(sample_rate), C.int(channels)) if errno != 0 { return opuserr(int(errno)) } return nil }