diff --git a/examples/command/command.cpp b/examples/command/command.cpp index 51d800a2f2e..7db8d4df578 100644 --- a/examples/command/command.cpp +++ b/examples/command/command.cpp @@ -336,6 +336,15 @@ int process_command_list(struct whisper_context * ctx, audio_async &audio, const std::vector pcmf32_cur; std::vector pcmf32_prompt; + FILE *out_stream = NULL; + if (!params.fname_out.empty()) { + out_stream = fopen(params.fname_out.c_str(), "a"); + if (out_stream == NULL) { + fprintf(stderr, "%s: error: opening of \"%s\" failed!\n", __func__, params.fname_out.c_str()); + perror(NULL); + return 5; + } + } // main loop while (is_running) { @@ -451,6 +460,11 @@ int process_command_list(struct whisper_context * ctx, audio_async &audio, const "\033[1m", allowed_commands[index].c_str(), "\033[0m", prob, (int) std::chrono::duration_cast(t_end - t_start).count()); fprintf(stdout, "\n"); + + if (out_stream != NULL) { + fprintf(out_stream, "%f\t%s\n", prob, allowed_commands[index].c_str()); + fflush(out_stream); + } } } @@ -458,6 +472,9 @@ int process_command_list(struct whisper_context * ctx, audio_async &audio, const } } + if (out_stream != NULL) { + fclose(out_stream); + } return 0; } @@ -559,6 +576,15 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au std::vector pcmf32_cur; std::vector pcmf32_prompt; + FILE *out_stream = NULL; + if (!params.fname_out.empty()) { + out_stream = fopen(params.fname_out.c_str(), "a"); + if (out_stream == NULL) { + fprintf(stderr, "%s: error: opening of \"%s\" failed!\n", __func__, params.fname_out.c_str()); + perror(NULL); + return 1; + } + } std::string k_prompt = "Ok Whisper, start listening for commands."; if (!params.prompt.empty()) { @@ -665,6 +691,10 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au const std::string command = ::trim(txt.substr(best_len)); fprintf(stdout, "%s: Command '%s%s%s', (t = %d ms)\n", __func__, "\033[1m", command.c_str(), "\033[0m", (int) t_ms); + if (out_stream != NULL) { + fprintf(out_stream, "%f\t%s\n", p/100.0, command.c_str()); + fflush(out_stream); + } } fprintf(stdout, "\n"); @@ -674,6 +704,10 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au } } } + if (out_stream != NULL) { + fclose(out_stream); + } + return 0; }