build static method

Future<Track?> build({
  1. required Sequence sequence,
  2. required Instrument instrument,
})

Creates a track in the underlying sequencer engine.

Implementation

static Future<Track?> build(
    {required Sequence sequence, required Instrument instrument}) async {
  int? id;

  if (instrument is Sf2Instrument) {
    id = await NativeBridge.addTrackSf2(
        instrument.idOrPath, instrument.isAsset, instrument.presetIndex);
  } else if (instrument is SfzInstrument) {
    final sfzFile = File(instrument.idOrPath);
    String? normalizedSfzPath;

    if (instrument.isAsset) {
      final normalizedSfzDir =
          await NativeBridge.normalizeAssetDir(sfzFile.parent.path);

      if (normalizedSfzDir == null)
        throw Exception(
            'Could not normalize asset dir for ${sfzFile.parent.path}');
      normalizedSfzPath = '$normalizedSfzDir/${p.basename(sfzFile.path)}';
    } else {
      normalizedSfzPath = sfzFile.path;
    }

    id = await NativeBridge.addTrackSfz(
        normalizedSfzPath, instrument.tuningPath);
  } else if (instrument is RuntimeSfzInstrument) {
    final sfzContent = instrument.sfz.buildString();
    String? normalizedSampleRoot;

    if (instrument.isAsset) {
      normalizedSampleRoot =
          await NativeBridge.normalizeAssetDir(instrument.sampleRoot);

      if (normalizedSampleRoot == null)
        throw Exception(
            'Could not normalize asset dir for ${instrument.sampleRoot}');
    } else {
      normalizedSampleRoot = instrument.sampleRoot;
    }

    // Sfizz uses the parent path of this (line 73 of Parser.cpp)
    final fakeSfzDir = '$normalizedSampleRoot/does_not_exist.sfz';

    id = await NativeBridge.addTrackSfzString(
        fakeSfzDir, sfzContent, instrument.tuningString);
  } else if (instrument is AudioUnitInstrument) {
    id = await NativeBridge.addTrackAudioUnit(instrument.idOrPath);
  } else {
    throw Exception('Instrument not recognized');
  }

  if (id == -1) return null;

  return Track._withId(
    sequence: sequence,
    id: id!,
    instrument: instrument,
  );
}