Repository: ggerganov/whisper.cpp
File: examples/stream.wasm/emscripten.cpp (C++)

Code before:
    g_status = status;
}

void stream_main(size_t index, const std::string & lang) {
    stream_set_status("loading data ...");

    struct whisper_full_params wparams = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);
    bool is_multilingual = whisper_is_multilingual(g_contexts[index]);

    wparams.n_threads        = std::min(N_THREAD, (int) std::thread::hardware_concurrency());
    wparams.offset_ms        = 0;
    wparams.translate        = false;
    wparams.no_context       = true;
    wparams.single_segment   = true;
    wparams.print_realtime   = false;
    wparams.print_progress   = false;
    wparams.print_timestamps = true;
    wparams.print_special    = false;

    wparams.max_tokens       = 32;
    wparams.audio_ctx        = 768; // partial encoder context for better performance

    // disable temperature fallback
    wparams.temperature_inc  = -1.0f;

    wparams.language         = is_multilingual ? strdup(lang.c_str()) : "en";

    printf("stream: using %d threads\n", wparams.n_threads);

    std::vector<float> pcmf32;

    // whisper context
    auto & ctx = g_contexts[index];

    // 5 seconds interval
    const int64_t window_samples = 5*WHISPER_SAMPLE_RATE;

    while (g_running) {
        stream_set_status("waiting for audio ...");

        {
            std::unique_lock<std::mutex> lock(g_mutex);

            if (g_pcmf32.size() < 1024) {
                lock.unlock();

                std::this_thread::sleep_for(std::chrono::milliseconds(10));

                continue;
            }


Review comment (performance):
This would be a memory leak - maybe just store the string in a `std::string g_lang` global var.

Code after:
    g_status = status;
}

void stream_main(size_t index, const std::string & lang) {
    stream_set_status("loading data ...");

    struct whisper_full_params wparams = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);
    bool is_multilingual = whisper_is_multilingual(g_contexts[index]);

    wparams.n_threads        = std::min(N_THREAD, (int) std::thread::hardware_concurrency());
    wparams.offset_ms        = 0;
    wparams.translate        = false;
    wparams.no_context       = true;
    wparams.single_segment   = true;
    wparams.print_realtime   = false;
    wparams.print_progress   = false;
    wparams.print_timestamps = true;
    wparams.print_special    = false;

    wparams.max_tokens       = 32;
    wparams.audio_ctx        = 768; // partial encoder context for better performance

    // disable temperature fallback
    wparams.temperature_inc  = -1.0f;

    wparams.language         = is_multilingual ? lang.c_str() : "en";

    printf("stream: using %d threads\n", wparams.n_threads);

    std::vector<float> pcmf32;

    // whisper context
    auto & ctx = g_contexts[index];

    // 5 seconds interval
    const int64_t window_samples = 5*WHISPER_SAMPLE_RATE;

    while (g_running) {
        stream_set_status("waiting for audio ...");

        {
            std::unique_lock<std::mutex> lock(g_mutex);

            if (g_pcmf32.size() < 1024) {
                lock.unlock();

                std::this_thread::sleep_for(std::chrono::milliseconds(10));

                continue;
            }