Directly access preprocessor directives

Closes issue hraban/opus#9. To be honest I'm not 100% why I didn't do it
this way in the first place. Three years have passed, something was
probably fixed in CGo, or maybe I was just wrong about this not being
possible, all along. Either way, it works on my machine, so that's all
the testing you need to do, right?
This commit is contained in:
Hraban Luyat 2018-03-11 18:37:16 +00:00
parent b85e55eab8
commit c490b1f079
2 changed files with 7 additions and 24 deletions

View file

@ -68,22 +68,12 @@ bridge_encoder_get_max_bandwidth(OpusEncoder *st, opus_int32 *max_bw)
return opus_encoder_ctl(st, OPUS_GET_MAX_BANDWIDTH(max_bw));
}
// Access the preprocessor from CGO
const int CONST_BANDWIDTH_NARROWBAND = OPUS_BANDWIDTH_NARROWBAND;
const int CONST_BANDWIDTH_MEDIUMBAND = OPUS_BANDWIDTH_MEDIUMBAND;
const int CONST_BANDWIDTH_WIDEBAND = OPUS_BANDWIDTH_WIDEBAND;
const int CONST_BANDWIDTH_SUPERWIDEBAND = OPUS_BANDWIDTH_SUPERWIDEBAND;
const int CONST_BANDWIDTH_FULLBAND = OPUS_BANDWIDTH_FULLBAND;
const int CONST_BITRATE_AUTO = OPUS_AUTO;
const int CONST_BITRATE_MAX = OPUS_BITRATE_MAX;
*/
import "C"
type Bandwidth int
var (
const (
// 4 kHz passband
Narrowband = Bandwidth(C.OPUS_BANDWIDTH_NARROWBAND)
// 6 kHz passband
@ -246,7 +236,7 @@ func (enc *Encoder) SetBitrate(bitrate int) error {
// SetBitrateToAuto will allow the encoder to automatically set the bitrate
func (enc *Encoder) SetBitrateToAuto() error {
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.CONST_BITRATE_AUTO))
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.OPUS_AUTO))
if res != C.OPUS_OK {
return Error(res)
}
@ -256,7 +246,7 @@ func (enc *Encoder) SetBitrateToAuto() error {
// SetBitrateToMax causes the encoder to use as much rate as it can. This can be
// useful for controlling the rate by adjusting the output buffer size.
func (enc *Encoder) SetBitrateToMax() error {
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.CONST_BITRATE_MAX))
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.OPUS_BITRATE_MAX))
if res != C.OPUS_OK {
return Error(res)
}

15
opus.go
View file

@ -8,25 +8,18 @@ package opus
// Link opus using pkg-config.
#cgo pkg-config: opus
#include <opus.h>
// Access the preprocessor from CGO
const int CONST_APPLICATION_VOIP = OPUS_APPLICATION_VOIP;
const int CONST_APPLICATION_AUDIO = OPUS_APPLICATION_AUDIO;
const int CONST_APPLICATION_RESTRICTED_LOWDELAY = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
*/
import "C"
type Application int
// These variables should be constants, but for interoperability with CGO
// they're var. Don't change them, though!
var (
const (
// Optimize encoding for VoIP
AppVoIP = Application(C.CONST_APPLICATION_VOIP)
AppVoIP = Application(C.OPUS_APPLICATION_VOIP)
// Optimize encoding for non-voice signals like music
AppAudio = Application(C.CONST_APPLICATION_AUDIO)
AppAudio = Application(C.OPUS_APPLICATION_AUDIO)
// Optimize encoding for low latency applications
AppRestrictedLowdelay = Application(C.CONST_APPLICATION_RESTRICTED_LOWDELAY)
AppRestrictedLowdelay = Application(C.OPUS_APPLICATION_RESTRICTED_LOWDELAY)
)
const (