encodeFloat method

Uint8List encodeFloat(
  1. Float32List input, {
  2. int? inputSize,
})

将 PCM 样本编码为 Opus 数据包(32 位浮点数输入)

input PCM 样本数据(32 位浮点数,范围通常在 -1.0, 1.0 之间) inputSize 输入样本的数量(不是字节数)

返回编码后的 Opus 数据包 抛出 OpusException 如果编码失败

Implementation

Uint8List encodeFloat(Float32List input, {int? inputSize}) {
  final encoder = _encoder;
  if (encoder == null) {
    throw StateError('Encoder has been disposed');
  }

  final size = inputSize ?? input.length;
  final output = calloc<Uint8>(4000);
  final encodedSize = calloc<UintPtr>();
  final error = calloc<OpusError>();

  try {
    final inputPtr = calloc<Float>(size);
    inputPtr.asTypedList(size).setAll(0, input);

    final res = bindings.encode_float(
      encoder,
      inputPtr,
      size,
      output,
      4000,
      encodedSize,
      error,
    );

    calloc.free(inputPtr);

    if (res != 0) {
      final errorMsg = extractErrorMessage(error);
      throw OpusException(error.ref.code, errorMsg);
    }

    final encodedLen = encodedSize.value;
    final result = Uint8List(encodedLen);
    final outputList = output.asTypedList(encodedLen);
    result.setAll(0, outputList);

    return result;
  } finally {
    freeError(error);
    calloc.free(output);
    calloc.free(encodedSize);
  }
}