Remove diff mode for simplicity

This commit is contained in:
Seunghyun Lee 2025-10-12 22:59:29 +09:00
parent dd5fa23927
commit bc39e6060a
1 changed files with 8 additions and 80 deletions

View File

@ -11,7 +11,6 @@
#include <cstdio>
#include <fstream>
#include <string>
#include <cctype>
#include <thread>
#include <vector>
@ -38,7 +37,7 @@ struct whisper_params {
bool save_audio = false; // save audio to wav file
bool use_gpu = true;
bool flash_attn = true;
int file_mode = 0; // 0=raw, 1=newline, 2=diff
int file_mode = 0; // 0=raw, 1=newline
std::string language = "en";
std::string model = "models/ggml-base.en.bin";
@ -47,34 +46,12 @@ struct whisper_params {
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
static std::string trim_copy(const std::string & s) {
size_t b = 0;
while (b < s.size() && std::isspace(static_cast<unsigned char>(s[b]))) b++;
size_t e = s.size();
while (e > b && std::isspace(static_cast<unsigned char>(s[e - 1]))) e--;
return s.substr(b, e - b);
}
static std::vector<std::string> split_lines_keep_nl(const std::string & s) {
std::vector<std::string> lines;
size_t start = 0;
while (start < s.size()) {
size_t pos = s.find('\n', start);
if (pos == std::string::npos) {
lines.emplace_back(s.substr(start));
break;
}
lines.emplace_back(s.substr(start, pos - start + 1));
start = pos + 1;
}
return lines;
}
// no helpers needed for file write modes when using raw/newline only
static int file_mode_from_string(const std::string & mode) {
if (mode == "raw") return 0;
if (mode == "newline") return 1;
if (mode == "diff") return 2;
fprintf(stderr, "error: unknown --file-mode '%s' (expected: raw|newline|diff)\n", mode.c_str());
fprintf(stderr, "error: unknown --file-mode '%s' (expected: raw|newline)\n", mode.c_str());
exit(1);
}
@ -82,7 +59,6 @@ static const char * file_mode_to_cstr(int mode) {
switch (mode) {
case 0: return "raw";
case 1: return "newline";
case 2: return "diff";
default: return "raw";
}
}
@ -152,7 +128,7 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
fprintf(stderr, " -l LANG, --language LANG [%-7s] spoken language\n", params.language.c_str());
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
fprintf(stderr, " -f FNAME, --file FNAME [%-7s] text output file name\n", params.fname_out.c_str());
fprintf(stderr, " --file-mode MODE [%-7s] file write mode: raw|newline|diff (default raw)\n", file_mode_to_cstr(params.file_mode));
fprintf(stderr, " --file-mode MODE [%-7s] file write mode: raw|newline (default raw)\n", file_mode_to_cstr(params.file_mode));
fprintf(stderr, " -tdrz, --tinydiarize [%-7s] enable tinydiarize (requires a tdrz model)\n", params.tinydiarize ? "true" : "false");
fprintf(stderr, " -sa, --save-audio [%-7s] save the recorded audio to a file\n", params.save_audio ? "true" : "false");
fprintf(stderr, " -ng, --no-gpu [%-7s] disable GPU inference\n", params.use_gpu ? "false" : "true");
@ -256,7 +232,6 @@ int main(int argc, char ** argv) {
std::ofstream fout;
std::string file_buffer;
std::string last_flushed_output;
if (params.fname_out.length() > 0) {
fout.open(params.fname_out);
if (!fout.is_open()) {
@ -279,9 +254,6 @@ int main(int argc, char ** argv) {
printf("[Start speaking]\n");
if (params.fname_out.length() > 0) {
// Mirror the start signal into the file for parity with stdout
if (params.file_mode != 0) {
last_flushed_output += std::string("[Start speaking]\n");
}
fout << "[Start speaking]\n";
fout.flush();
}
@ -479,30 +451,8 @@ int main(int argc, char ** argv) {
if ((params.file_mode != 0) && params.fname_out.length() > 0) {
if (use_vad || (!use_vad && (n_iter % n_new_line) == 0)) {
if (!file_buffer.empty()) {
const std::string candidate = file_buffer;
if (params.file_mode == 2) { // diff
const auto cand_lines = split_lines_keep_nl(candidate);
const auto last_lines = split_lines_keep_nl(last_flushed_output);
size_t i = 0;
while (i < cand_lines.size() && i < last_lines.size() && cand_lines[i] == last_lines[i]) {
++i;
}
bool wrote_any = false;
for (; i < cand_lines.size(); ++i) {
const auto & ln = cand_lines[i];
if (!trim_copy(ln).empty()) {
fout << ln;
wrote_any = true;
} else {
if (wrote_any) fout << ln;
}
}
if (wrote_any) fout.flush();
} else { // newline
fout << candidate;
fout.flush();
}
last_flushed_output = candidate;
fout << file_buffer;
fout.flush();
file_buffer.clear();
}
}
@ -534,30 +484,8 @@ int main(int argc, char ** argv) {
audio.pause();
if ((params.file_mode != 0) && params.fname_out.length() > 0 && !file_buffer.empty()) {
const std::string candidate = file_buffer;
if (params.file_mode == 2) { // diff
const auto cand_lines = split_lines_keep_nl(candidate);
const auto last_lines = split_lines_keep_nl(last_flushed_output);
size_t i = 0;
while (i < cand_lines.size() && i < last_lines.size() && cand_lines[i] == last_lines[i]) {
++i;
}
bool wrote_any = false;
for (; i < cand_lines.size(); ++i) {
const auto & ln = cand_lines[i];
if (!trim_copy(ln).empty()) {
fout << ln;
wrote_any = true;
} else {
if (wrote_any) fout << ln;
}
}
if (wrote_any) fout.flush();
} else { // newline
fout << candidate;
fout.flush();
}
last_flushed_output = candidate;
fout << file_buffer;
fout.flush();
file_buffer.clear();
}