I'm trying to call Opus decode from Kotlin using t...
# kotlin-native
t
I'm trying to call Opus decode from Kotlin using the native binding. I'm struggling to figure out the right way to call the method. The thing that I'm not sure about is the pcm output field, which should be an array of shorts. Can somebody show me how to do this or point me to an example?
Copy code
public external expect fun opus_decode(
	st: kotlinx.cinterop.CValuesRef<cnames.structs.OpusDecoder>?, 
	data: kotlinx.cinterop.CValuesRef<kotlinx.cinterop.UByteVar /* = kotlinx.cinterop.UByteVarOf<kotlin.UByte> */>?, 
	len: cocoapods.OpusKit.opus_int32 /* = <http://kotlin.Int|kotlin.Int> */, 
	pcm: kotlinx.cinterop.CValuesRef<cocoapods.OpusKit.opus_int16Var /* = kotlinx.cinterop.ShortVarOf<cocoapods.OpusKit.opus_int16 /* = kotlin.Short */> */>?, 
	frame_size: <http://kotlin.Int|kotlin.Int>, decode_fec: <http://kotlin.Int|kotlin.Int>): <http://kotlin.Int|kotlin.Int> { /* compiled code */ }
Well, I got it to work, but this feels way to hard. Is there an easier way?
Copy code
val outputLen = _framesize * _channels * opus_int16.SIZE_BYTES
var opusOutData = allocArray<opus_int16Var>(outputLen)

val result = opus_decode(
    st = it.pointed.ptr,
    data = uByteArray.toCValues(),
    len = uByteArray.size,
    pcm = opusOutData,
    frame_size = _framesize,
    decode_fec = 0
)
if (result < OPUS_OK) {
    logErrorCode(result)
} else {
     val array = (0 until outputLen).map { opusOutData.get(it)}.toTypedArray()
    return array.toShortArray()
}