diff --git a/doubao_mic.cpp b/doubao_mic.cpp index 24da69569..4fe00645f 100644 --- a/doubao_mic.cpp +++ b/doubao_mic.cpp @@ -19,7 +19,9 @@ #include #include +// ============================================= // 全局状态管理 +// ============================================= std::atomic is_recording(false); std::atomic exit_program(false); std::atomic recorded_seconds(0); @@ -29,7 +31,11 @@ std::vector audio_buffer; std::mutex buffer_mutex; // 配置常量 -const int RECORD_TIMEOUT = 24; // 目标 30 秒 +int RECORD_TIMEOUT = 30; // 可变,支持动态测试 + +// ============================================= +// 系统工具函数 +// ============================================= void signal_handler(int sig) { if (sig == SIGINT) { @@ -41,6 +47,7 @@ void signal_handler(int sig) { } } +// 非阻塞检查标准输入(带毫秒超时) bool check_input_non_blocking(int timeout_ms = 20) { fd_set fds; FD_ZERO(&fds); @@ -62,7 +69,11 @@ void clear_input_buffer() { } } +// ============================================= +// 音频回调 (硬件驱动层) +// ============================================= void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { + // 只要 is_recording 为 true,回调就会持续把数据写入 buffer if (!is_recording.load() || pInput == NULL) return; const float* pInputFloat = (const float*)pInput; std::lock_guard lock(buffer_mutex); @@ -70,34 +81,31 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin recorded_seconds.store(static_cast(audio_buffer.size() / 16000.0)); } -void print_status_guide() { - printf("\n=============================================\n"); - printf("🎙️ 操作提示:\n"); - printf(" ▶ [回车键] : 开始录制\n"); - printf(" ■ [回车键] : 停止录制并识别\n"); - printf(" ⏳ [自动停止]: 达到 %d 秒自动截断\n", RECORD_TIMEOUT); - printf("=============================================\n"); -} - +// ============================================= +// 识别逻辑 +// ============================================= void recognize_audio(struct whisper_context* ctx, const std::vector& audio_data) { if (audio_data.empty()) return; float total_sec = (float)audio_data.size() / 16000.0f; - printf("\n🔍 正在识别(总长度:%.2fs)...\n", total_sec); + + printf("\n🔍 正在识别(总采样长度:%.2fs)...\n", total_sec); auto t_start = std::chrono::steady_clock::now(); + whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY); wparams.language = "zh"; wparams.n_threads = std::max(2, (int)std::thread::hardware_concurrency()); wparams.print_progress = false; if (whisper_full(ctx, wparams, audio_data.data(), audio_data.size()) != 0) { - fprintf(stderr, "❌ 识别失败\n"); + fprintf(stderr, "❌ Whisper 推理失败\n"); return; } auto t_end = std::chrono::steady_clock::now(); float msec = std::chrono::duration(t_end - t_start).count(); - printf("⏱️ 识别耗时:%.2f 秒 | 速度:%.2fx\n", msec/1000.0f, total_sec/(msec/1000.0f)); + + printf("⏱️ 推理耗时:%.2f 秒 | 速度:%.2fx 实时\n", msec/1000.0f, total_sec/(msec/1000.0f)); int n_segments = whisper_full_n_segments(ctx); printf("📝 结果:\n"); @@ -106,87 +114,113 @@ void recognize_audio(struct whisper_context* ctx, const std::vector& audi } } +// ============================================= +// 主程序 +// ============================================= int main(int argc, char** argv) { signal(SIGINT, signal_handler); - if (argc < 2) return 1; - - ma_context context; - ma_context_init(NULL, 0, NULL, &context); - ma_device_info* pCaptureInfos = NULL; - ma_uint32 captureCount = 0; - ma_context_get_devices(&context, NULL, NULL, &pCaptureInfos, &captureCount); + if (argc < 2) { + printf("Usage: %s [timeout_seconds]\n", argv[0]); + return 1; + } + if (argc >= 3) { + RECORD_TIMEOUT = atoi(argv[2]); + } + // 1. Whisper 初始化 (强制 GPU 后端) struct whisper_context_params cparams = whisper_context_default_params(); cparams.use_gpu = true; struct whisper_context* ctx = whisper_init_from_file_with_params(argv[1], cparams); if (!ctx) return 1; + // 2. 音频设备初始化 (AB13X USB) + ma_context context; + ma_context_init(NULL, 0, NULL, &context); ma_device_config devCfg = ma_device_config_init(ma_device_type_capture); devCfg.capture.format = ma_format_f32; devCfg.capture.channels = 1; devCfg.sampleRate = 16000; devCfg.dataCallback = data_callback; - if (captureCount > 5) devCfg.capture.pDeviceID = &pCaptureInfos[5].id; // 锁定你的 AB13X - + ma_device device; - ma_device_init(&context, &devCfg, &device); + if (ma_device_init(&context, &devCfg, &device) != MA_SUCCESS) return 1; ma_device_start(&device); while (!exit_program.load()) { - print_status_guide(); // 修复:增加每轮提示 - printf("👉 等待按回车开始..."); + printf("\n=============================================\n"); + printf("🎙️ 操作提示 (当前超时: %d秒):\n", RECORD_TIMEOUT); + printf(" ▶ [回车键] : 开始录制\n"); + printf(" ■ [回车键] : 停止录制 (会有1.5秒平滑收尾)\n"); + printf("=============================================\n"); + printf("👉 等待指令..."); fflush(stdout); while (!check_input_non_blocking(50) && !exit_program.load()); if (exit_program.load()) break; clear_input_buffer(); - // 开始录制 + // 重置状态 { std::lock_guard lock(buffer_mutex); audio_buffer.clear(); } recorded_seconds.store(0); is_recording.store(true); auto start_time = std::chrono::steady_clock::now(); - printf("\n🎙️ 录制中 (按回车停止)... \n"); + printf("\n🎙️ 正在录制 (进度将在下方实时更新)... \n"); + // 进度显示线程 std::thread progress_thread([&]() { while (is_recording.load() && !exit_program.load()) { - printf("\r📊 进度: %d 秒", recorded_seconds.load()); + printf("\r📊 进度: %d 秒 ", recorded_seconds.load()); fflush(stdout); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); } }); - bool stopped = false; - while (!exit_program.load() && !stopped) { + bool stop_triggered = false; + while (!exit_program.load() && !stop_triggered) { auto now = std::chrono::steady_clock::now(); - // 修复:使用更精确的毫秒对比,并增加 500ms 冗余以确保达到 30s - double elapsed = std::chrono::duration(now - start_time).count(); + double elapsed_ms = std::chrono::duration(now - start_time).count(); + // 检查手动停止 if (check_input_non_blocking(10)) { char c; if (read(STDIN_FILENO, &c, 1) > 0 && c == '\n') { - printf("\n🛑 手动停止录制..."); - stopped = true; + printf("\n🛑 检测到手动回车,准备收尾..."); + stop_triggered = true; } - } else if (elapsed >= (RECORD_TIMEOUT * 1000 + 500)) { // 严格 30.5 秒逻辑 - printf("\n⏱️ 达到 30 秒限制,自动切断..."); - stopped = true; + } + // 检查超时停止 + else if (elapsed_ms >= (RECORD_TIMEOUT * 1000)) { + printf("\n⏱️ 达到 %d 秒阈值,准备收尾...", RECORD_TIMEOUT); + stop_triggered = true; } - std::this_thread::sleep_for(std::chrono::milliseconds(2)); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + + // --- 核心改动:平滑收尾逻辑 --- + // 即使触发了停止,我们也不立即关闭 is_recording 开关 + // 这样可以确保正在 ALSA 缓冲区或 DMA 队列里的数据被 data_callback 继续捞走 + if (stop_triggered) { + printf("正在执行 1.5 秒平滑数据刷新..."); + fflush(stdout); + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + is_recording.store(false); // 此时才真正切断回调写入 + printf("完成。\n"); } - // 停止回调并捕获尾音 - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - is_recording.store(false); if (progress_thread.joinable()) progress_thread.join(); + // 拷贝数据进行识别 std::vector captured; - { std::lock_guard lock(buffer_mutex); captured = audio_buffer; } + { + std::lock_guard lock(buffer_mutex); + captured = audio_buffer; + } recognize_audio(ctx, captured); } ma_device_uninit(&device); + ma_context_uninit(&context); whisper_free(ctx); return 0; } \ No newline at end of file