Esempio n. 1
0
// Decode decodes a single Opus frame to floating point format. Stereo
// output will be interleaved. A null input frame indicates packet loss.
func (dec *Decoder) Decode(frame []byte, pksize int, isFec bool) (pcm []float32, err error) {
	var input *C.uchar
	if frame != nil {
		input = (*C.uchar)(&frame[0])
	}

	isfNum := 0
	if isFec {
		isfNum = 1
	}

	pcm = make([]float32, pksize*dec.chcnt)
	num := C.opus_decode_float(&dec.cee, input, C.opus_int32(len(frame)),
		(*C.float)(&pcm[0]), C.int(pksize), C.int(isfNum))
	if num < 0 {
		pcm = nil
		err = ErrUnspecified
		switch num {
		case C.OPUS_BAD_ARG:
			fmt.Println("OPUS_BAD_ARG")
		case C.OPUS_BUFFER_TOO_SMALL:
			fmt.Println("OPUS_BUFFER_TOO_SMALL")
		case C.OPUS_INVALID_PACKET:
			fmt.Println("OPUS_INVALID_PACKET")
		}
		return
	}
	pcm = pcm[:num*C.int(dec.chcnt)]
	return
}
Esempio n. 2
0
// Decode encoded Opus data into the supplied buffer. On success, returns the
// number of samples correctly written to the target buffer.
func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error) {
	if dec.p == nil {
		return 0, errDecUninitialized
	}
	if len(data) == 0 {
		return 0, fmt.Errorf("opus: no data supplied")
	}
	if len(pcm) == 0 {
		return 0, fmt.Errorf("opus: target buffer empty")
	}
	n := int(C.opus_decode_float(
		dec.p,
		(*C.uchar)(&data[0]),
		C.opus_int32(len(data)),
		(*C.float)(&pcm[0]),
		C.int(cap(pcm)),
		0))
	if n < 0 {
		return 0, opuserr(n)
	}
	return n, nil
}