Repository: ggerganov/whisper.cpp
File: examples/server/server.cpp (C++)

Code before:
            return;
        }
        auto audio_file = req.get_file_value("file");

        // check non-required fields
        get_req_parameters(req, params);

        std::string filename{audio_file.filename};
        printf("Received request: %s\n", filename.c_str());

        // audio arrays
        std::vector<float> pcmf32;               // mono-channel F32 PCM
        std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM

        // write to temporary file
        const std::string temp_filename = "whisper_server_temp_file.wav";
        std::ofstream temp_file{temp_filename, std::ios::binary};
        temp_file << audio_file.content;
        temp_file.close();

        // if file is not wav, convert to wav
        
        if(sparams.ffmpeg_converter) {
            std::string error_resp = "{\"error\":\"Failed to execute ffmpeg command.\"}";
            bool isConverted = convert_to_WAV(temp_filename, error_resp);
            if(!isConverted) {
                res.set_content(error_resp, "application/json");
                whisper_mutex.unlock();
                return;
            }
        }

        // read wav content into pcmf32
        if (!::read_wav(temp_filename, pcmf32, pcmf32s, params.diarize)) {
            fprintf(stderr, "error: failed to read WAV file '%s'\n", temp_filename.c_str());
            const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
            res.set_content(error_resp, "application/json");
            std::remove(temp_filename.c_str());
            whisper_mutex.unlock();
            return;
        }
        // remove temp file
        std::remove(temp_filename.c_str());

        printf("Successfully loaded %s\n", filename.c_str());

        // print system information
        {
            fprintf(stderr, "\n");
            fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
                    params.n_threads*params.n_processors, std::thread::hardware_concurrency(), whisper_print_system_info());

Review comment (suggestion):
```suggestion
            const bool is_converted = convert_to_WAV(temp_filename, error_resp);
            if (!is_converted) {
```

Code after:
            return;
        }
        auto audio_file = req.get_file_value("file");

        // check non-required fields
        get_req_parameters(req, params);

        std::string filename{audio_file.filename};
        printf("Received request: %s\n", filename.c_str());

        // audio arrays
        std::vector<float> pcmf32;               // mono-channel F32 PCM
        std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM

        // write to temporary file
        const std::string temp_filename = "whisper_server_temp_file.wav";
        std::ofstream temp_file{temp_filename, std::ios::binary};
        temp_file << audio_file.content;
        temp_file.close();

        // if file is not wav, convert to wav
        
        if (sparams.ffmpeg_converter) {
            std::string error_resp = "{\"error\":\"Failed to execute ffmpeg command.\"}";
            const bool is_converted = convert_to_wav(temp_filename, error_resp);
            if (!is_converted) {
                res.set_content(error_resp, "application/json");
                whisper_mutex.unlock();
                return;
            }
        }

        // read wav content into pcmf32
        if (!::read_wav(temp_filename, pcmf32, pcmf32s, params.diarize)) {
            fprintf(stderr, "error: failed to read WAV file '%s'\n", temp_filename.c_str());
            const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
            res.set_content(error_resp, "application/json");
            std::remove(temp_filename.c_str());
            whisper_mutex.unlock();
            return;
        }
        // remove temp file
        std::remove(temp_filename.c_str());

        printf("Successfully loaded %s\n", filename.c_str());

        // print system information
        {
            fprintf(stderr, "\n");
            fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
                    params.n_threads*params.n_processors, std::thread::hardware_concurrency(), whisper_print_system_info());