mixAudioFile method

dynamic mixAudioFile({
  1. required MixItem mixItem,
  2. required double duration,
  3. required dynamic onSuccess(
    1. String
    ),
  4. required dynamic onBuild(),
  5. String pathCache = '',
  6. required dynamic onError(
    1. String
    ),
})

Implementation

mixAudioFile(
    {required MixItem mixItem,
      required double duration,
      required Function(String) onSuccess,
      required Function() onBuild,
      String pathCache = '',
      required Function(String) onError}) async {
  // FileManager.createFolder(extensionFile: mixItem.extension)
  //     .then((filemanager) async {
  Directory directory = await getApplicationDocumentsDirectory();



  var fileName =
      "${pathCache.isNotEmpty ? pathCache : directory.path}/${mixItem.fileName}.${mixItem.extension.toLowerCase()}";
  if (File(mixItem.request.first).existsSync()) {
    if(File(fileName).existsSync()){
      File(fileName).delete();
    }

    if (mixItem.request.length > 0 &&
        p.extension(mixItem.request.first).split(".")[1] ==
            mixItem.extension.toLowerCase()) {
      onError.call(
          "Default encoder for format ${p.extension(mixItem.request.first.split(".")[1])} (codec ${p.extension(mixItem.request.first.split(".")[1])}) is probably disabled. Please choose an encoder manually.");
    } else if (duration <= 0) {
      onError.call("Incorrect processing calculation time..");
    } else {
      onBuild.call();
      _onProcuessRenderToBufferSubject.add(1.0);
      List<String> pathArray =
      "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(",");
      String mixPath = "";
      String mixValue = '"';
      String mixPathEnd = "";
      String pitch = "aresample=${Hz}";
      //String speed = "atempo=1.0";
      int index = 0;
      mixItem.request.forEach((element) {
        String pan = "pan=stereo|c0=c0|c1=c1";
        if (mixItem.panPlayerConfig[index] > 0) {
          // pan =
          // "pan=stereo|c0=${(1.0 - (mixItem.panPlayerConfig[index] /20))}*c0|c1=${mixItem.panPlayerConfig[index] / 100}*c1";
          pan = "pan=stereo|c1=c1";
        } else if (mixItem.panPlayerConfig[index] < 0) {
          // pan =
          // "pan=stereo|c0=${(mixItem.panPlayerConfig[index]).abs() /20}*c0|c1=${(1.0 - (mixItem.panPlayerConfig[index]).abs() / 100)}*c1";
          pan = "pan=stereo|c0=c0";
        }

      if(mixItem.pitchConfig<0){

        final pitchConfig = pitchRate[((mixItem.pitchConfig.abs())-1).toInt()];
        pitch =  "asetrate=${Hz/pitchConfig},aresample=${Hz},atempo=${1*pitchConfig}";

      }else if(mixItem.pitchConfig>0){
        final pitchConfig = pitchRate[(mixItem.pitchConfig-1).toInt()];
        pitch =  "asetrate=${Hz*pitchConfig},aresample=${Hz},atempo=${1/pitchConfig}";
      }

        mixPath += " -i ${element}";
        mixValue +=
        "[${index}]volume=${mixItem.volumeConfig[index]/50},${pan},${pitch}[${pathArray[index]}];";
        mixPathEnd += "[${pathArray[index]}]";
        index++;
      });
      FFmpegKit.executeAsync(
          '${mixPath} -filter_complex ${mixValue}${mixPathEnd}amix=inputs=${mixItem.request.length}:duration=longest" ${fileName}',
              (session) async {
            final returnCode = await session.getReturnCode();

            if (ReturnCode.isSuccess(returnCode)) {
              _onProcuessRenderToBufferSubject.add(100.0);
              // SUCCESS
              print("FFmpegKit -> SUCCESS  outfile -> ${fileName}");
              onSuccess.call(fileName);
            } else if (ReturnCode.isCancel(returnCode)) {
              _onProcuessRenderToBufferSubject.add(100.0);
              // CANCEL
              // onError.call(
              //     "transaction failed There might be a file merging error. Please contact the developer.");
              print("FFmpegKit -> CANCEL ");
            } else {
              _onProcuessRenderToBufferSubject.add(100.0);
              // ERROR
              onError.call(
                  "transaction failed There might be a file merging error. Please contact the developer.");
              print("FFmpegKit ->CANCEL ");
            }
          }, (log) {
        print("FFmpegKit Log -> ${log.getMessage()}");
      }, (statistics) {
        _onProcuessRenderToBufferSubject.add((statistics.getTime() /
            Duration(seconds: duration.toInt()).inMilliseconds) *
            100);
      });
    }
  } else {
    onError.call("request Cannot open file, path ");
  }
  // });
}