Optimize saving speed in android

I am making a voice changer app, which means the input is an audio file, after being processed in a function, it should generate an effect audio file with DSP

extern "C" JNIEXPORT jint JNICALL
Java_io_microshow_aisound_AiSound_saveMixSound(JNIEnv *env, jclass clazz, jstring main_audio,
                                               jstring sub_audio, jstring output_audio,
                                               jint type,
                                               jfloat main_audio_volume,
                                               jfloat sub_audio_volume,
                                               jobject on_progress_listener) {
    System *system = nullptr;
    Sound *mainSound = nullptr, *subSound = nullptr;
    Channel *mainChannel = nullptr, *subChannel = nullptr;
    bool isMainSoundPlaying = true;
    jint result = 1;
    unsigned int length = 0, position = 0;
    DSP *dsp;
    float mFrequency;
    JNIEnv *mEnv = env;
    System_Create(&system);
    system->setSoftwareFormat(22050, FMOD_SPEAKERMODE_STEREO, 1);
    const char *cstr_main_audio = mEnv->GetStringUTFChars(main_audio, nullptr);
    const char *cstr_sub_audio = mEnv->GetStringUTFChars(sub_audio, nullptr);
    const char *cstr_output_audio = mEnv->GetStringUTFChars(output_audio, nullptr);
    char cDest[200];
    strcpy(cDest, cstr_output_audio);
    system->setOutput(FMOD_OUTPUTTYPE_WAVWRITER_NRT);
    system->init(1024, FMOD_INIT_STREAM_FROM_UPDATE | FMOD_INIT_PROFILE_ENABLE, cDest);
    try {
        system->createSound(cstr_main_audio,FMOD_CREATESAMPLE, nullptr,
                            &mainSound);
        switch (type) {
            case TYPE_NORMAL:
                system->playSound(mainSound, nullptr, false, &mainChannel);
                break;
          
            default:
                break;
        }
        if (env->GetStringLength(sub_audio) != 0) {
            LOGI("%s", "Saving background")
            system->createSound(cstr_sub_audio,
                                FMOD_LOOP_NORMAL | FMOD_CREATESAMPLE  , nullptr,
                                &subSound);

            system->playSound(subSound, nullptr, false, &subChannel);
            subChannel->setVolume(sub_audio_volume);
        }
    } catch (...) {
        LOGE("%s", "catch exception...")
        result = 0;
    }

    mainSound->getLength(&length, FMOD_TIMEUNIT_MS);
    mainChannel->setVolume(main_audio_volume);
    try {
        const float CALLBACK_THRESHOLD = 0.01f;
        float lastCallbackProgress = 0.0f;
        float lengthReciprocal = 1.0f / float(length);
        while (isMainSoundPlaying) {
            usleep(1000);
            mainChannel->isPlaying(&isMainSoundPlaying);
            mainChannel->getPosition(&position, FMOD_TIMEUNIT_MS);
            float progress = float(position) * lengthReciprocal;
            if (progress - lastCallbackProgress >= CALLBACK_THRESHOLD) {
                callJNICallBack(env, on_progress_listener, progress);
                lastCallbackProgress = progress;
            }
            system->update();
        }
        callJNICallBack(env, on_progress_listener, 1.0);
        env->ReleaseStringUTFChars(main_audio, cstr_main_audio);
        env->ReleaseStringUTFChars(sub_audio, cstr_sub_audio);
        env->ReleaseStringUTFChars(output_audio, cstr_output_audio);
        mainSound->release();
        subSound->release();
        system->close();
        system->release();
    }
    catch (...) {
        LOGE("%s", "FILE DID NOT SAVE")
        result = 0;
    }
    return result;
}

But the issue here is the file takes a long time to generate, I want to optimize the saving speed as fast as possible

Hi,

What version of FMOD are you using?

What is the average length of the audio file and how long are they taking to export?