func NewEncoder(sampleRate, channels int, application Application) (*Encoder, error) {
	encoder := &Encoder{}
	encoder.data = make([]byte, int(C.opus_encoder_get_size(C.int(channels))))
	encoder.cEncoder = (*C.struct_OpusEncoder)(unsafe.Pointer(&encoder.data[0]))

	ret := C.opus_encoder_init(encoder.cEncoder, C.opus_int32(sampleRate), C.int(channels), C.int(application))
	if err := getErr(ret); err != nil {
		return nil, err
	}
	return encoder, nil
}
Example #2
0
func (this *Encoder) Init(sampleRate int, channels int, application Application) {
	C.opus_encoder_init(
		this.ptr,
		C.opus_int32(C.int(sampleRate)),
		C.int(channels),
		C.int(application),
	)
}
Example #3
0
// Allocates and initializes an encoder state. There are three coding modes:
//
//  1. APPLICATION_VOIP gives best quality at a given bitrate for voice
//  signals. It enhances the  input signal by high-pass filtering and
//  emphasizing formants and harmonics. Optionally  it includes in-band forward
//  error correction to protect against packet loss. Use this mode for typical
//  VoIP applications. Because of the enhancement, even at high bitrates the
//  output may sound different from the input.
//
//  2. APPLICATION_AUDIO gives best quality at a given bitrate for most
//  non-voice signals like music. Use this mode for music and mixed
//  (music/voice) content, broadcast, and applications requiring less than 15
//  ms of coding delay.
//
//  3. APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that disables
//  the speech-optimized mode in exchange for slightly reduced delay. This mode
//  can only be set on an newly initialized or freshly reset encoder because it
//  changes the codec delay. This is useful when the caller knows that the
//  speech-optimized modes will not be needed (use with caution).
//
// Fs: Sampling rate of input signal (Hz). This must be one of 8000, 12000,
// 16000, 24000, or 48000.
//
// channels: Number of channels (1 or 2) in input signal
//
// application: Coding mode (APPLICATION_VOIP/APPLICATION_AUDIO/APPLICATION_RESTRICTED_LOWDELAY)
//
// note: Regardless of the sampling rate and number channels selected, the Opus
// encoder can switch to a lower audio bandwidth or number of channels if the
// bitrate selected is too low. This also means that it is safe to always use
// 48 kHz stereo input and let the encoder optimize the encoding.
func NewEncoder(Fs, channels, application int) (*Encoder, error) {
	enc := new(Encoder)
	err := opusError(C.opus_encoder_init(
		enc.cptr(),
		C.opus_int32(Fs),
		C.int(channels),
		C.int(application),
	))
	if err != nil {
		return nil, err
	}
	return enc, nil
}
Example #4
0
// Init initializes a pre-allocated opus encoder. Unless the encoder has been
// created using NewEncoder, this method must be called exactly once in the
// life-time of this object, before calling any other methods.
func (enc *Encoder) Init(sample_rate int, channels int, application Application) error {
	if enc.p != nil {
		return fmt.Errorf("opus encoder already initialized")
	}
	if channels != 1 && channels != 2 {
		return fmt.Errorf("Number of channels must be 1 or 2: %d", channels)
	}
	size := C.opus_encoder_get_size(C.int(channels))
	enc.mem = make([]byte, size)
	enc.p = (*C.OpusEncoder)(unsafe.Pointer(&enc.mem[0]))
	errno := int(C.opus_encoder_init(
		enc.p,
		C.opus_int32(sample_rate),
		C.int(channels),
		C.int(application)))
	if errno != 0 {
		return opuserr(int(errno))
	}
	return nil
}