Beispiel #1
0
// EncodeFloat encodes an Opus frame from floating point input.
//
// pcm: the 32-bit float PCM input signal (interleaved if 2 channels). Whose
// length is frameSize*channels. Samples have a normal range of +/-1.0, but can
// have a range beyond that (but they will be clipped by decoders using the
// integer API and should only be used if it is known that the far end supports
// extended dynamic range).
//
// frameSize: the number of samples per channel in the input signal. This must
// be an Opus frame size for the encoder's sampling rate. For example at 48kHz
// the permitted values are 120, 240, 480, 960, 1920, and 2880. Passing in a
// duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder
// from using the LPC or hybrid modes.
//
// data: the slice to store the output payload in, containing len(data) bytes
// of space. The length of the slice may be used to impose an upper limit on
// the instant bitrate, but should not be used as the only bitrate control. Use
// SET_BITRATE to control the bitrate.
//
// returns: a slice of the data slice representing the encoded packet, OR nil
// and an error.
func (e *Encoder) EncodeFloat(pcm []float32, frameSize int, data []byte) ([]byte, error) {
	n := C.opus_encode_float(
		e.cptr(),
		(*C.float)(unsafe.Pointer(&pcm[0])),
		C.int(frameSize),
		(*C.uchar)(unsafe.Pointer(&data[0])),
		C.opus_int32(len(data)),
	)
	if n > 0 {
		return data[:n], nil
	}
	return nil, opusError(C.int(-n))
}
Beispiel #2
0
// Encode raw PCM data and store the result in the supplied buffer. On success,
// returns the number of bytes used up by the encoded data.
func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error) {
	if enc.p == nil {
		return 0, errEncUninitialized
	}
	if len(pcm) == 0 {
		return 0, fmt.Errorf("opus: no data supplied")
	}
	if len(data) == 0 {
		return 0, fmt.Errorf("opus: no target buffer")
	}
	n := int(C.opus_encode_float(
		enc.p,
		(*C.float)(&pcm[0]),
		C.int(len(pcm)),
		(*C.uchar)(&data[0]),
		C.opus_int32(cap(data))))
	if n < 0 {
		return 0, opuserr(n)
	}
	return n, nil
}