GlintEncoder constructor

GlintEncoder({
  1. int sampleRate = 44100,
  2. int channels = 1,
  3. int bitrate = 128,
  4. int? mode,
  5. int quality = 1,
  6. int path = 0,
  7. int? vbrQuality,
})

Create a new MP3 encoder.

Throws ArgumentError if the configuration is not supported. Throws StateError if the encoder could not be created. mode: 0 mono, 1 dual, 2 joint, 3 stereo (default: mono for 1 ch, joint for 2). quality: 0 speed / 1 normal / 2 best. path: 0 default, 1 double, 2 fixed. vbrQuality 0..9 selects VBR.

Implementation

GlintEncoder({
  int sampleRate = 44100,
  int channels = 1,
  int bitrate = 128,
  int? mode,
  int quality = 1,
  int path = 0,
  int? vbrQuality,
}) : channels = channels {
  _lib = _loadLibrary();

  final checkConfig =
      _lib.lookupFunction<GlintCheckConfigNative, GlintCheckConfig>(
          'glint_check_config');
  final create =
      _lib.lookupFunction<GlintCreateNative, GlintCreate>('glint_create');
  final spfFn =
      _lib.lookupFunction<GlintSamplesPerFrameNative, GlintSamplesPerFrame>(
          'glint_samples_per_frame');

  _glintEncode =
      _lib.lookupFunction<GlintEncodeNative, GlintEncode>('glint_encode');
  _glintEncodeFloat =
      _lib.lookupFunction<GlintEncodeFloatNative, GlintEncodeFloat>(
          'glint_encode_float');
  _glintFlush =
      _lib.lookupFunction<GlintFlushNative, GlintFlush>('glint_flush');
  _glintDestroy =
      _lib.lookupFunction<GlintDestroyNative, GlintDestroy>('glint_destroy');

  // Validate configuration
  final rc = checkConfig(sampleRate, bitrate);
  if (rc != 0) {
    throw ArgumentError(
        'Unsupported sample rate ($sampleRate) or bitrate ($bitrate)');
  }

  // Allocate and fill config struct
  final cfgPtr = calloc<GlintConfig>();
  cfgPtr.ref.sampleRate = sampleRate;
  cfgPtr.ref.numChannels = channels;
  cfgPtr.ref.mode = mode ?? (channels == 1 ? 0 : 2); // MONO or JOINT
  cfgPtr.ref.bitrate = bitrate;
  cfgPtr.ref.path = path;
  cfgPtr.ref.simd = 0;
  cfgPtr.ref.quality = quality;
  if (vbrQuality != null) {
    cfgPtr.ref.vbr = 1;
    cfgPtr.ref.vbrQuality = vbrQuality;
  }
  // vbr fields otherwise zero (CBR); calloc zeroed the struct.

  _handle = create(cfgPtr);
  calloc.free(cfgPtr);

  if (_handle == nullptr) {
    throw StateError('glint_create returned null');
  }

  samplesPerFrame = spfFn(_handle);
}