ask gemini to add timeout hint back
This commit is contained in:
parent
95df75f360
commit
8eb7806471
212
doubao_mic.cpp
212
doubao_mic.cpp
|
|
@ -1,214 +1,154 @@
|
|||
#include "whisper.h"
|
||||
#include "common.h"
|
||||
|
||||
#define MINIAUDIO_IMPLEMENTATION
|
||||
#include "miniaudio.h"
|
||||
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
// =============================================
|
||||
// 1. 工程常量定义 (拒绝 Magic Numbers)
|
||||
// =============================================
|
||||
struct RecordingConfig {
|
||||
static constexpr int SAMPLE_RATE = 16000;
|
||||
static constexpr int PROGRESS_REFRESH_MS = 100; // 进度条刷新频率
|
||||
static constexpr int UI_WAIT_MS = 10; // 循环等待步长
|
||||
static constexpr int INPUT_CHECK_MS = 20; // 输入检测超时
|
||||
static constexpr int POST_STOP_BUFFER_MS = 1500; // 停止后的平滑采样缓冲
|
||||
static constexpr int CLOCK_TOLERANCE_MS = 200; // 系统时钟容差(确保跳到目标秒数)
|
||||
};
|
||||
|
||||
// 全局状态管理
|
||||
std::atomic<bool> is_recording(false);
|
||||
std::atomic<bool> exit_program(false);
|
||||
std::atomic<int> recorded_seconds(0);
|
||||
|
||||
// 音频缓冲区与锁
|
||||
std::vector<float> audio_buffer;
|
||||
std::mutex buffer_mutex;
|
||||
int g_timeout_setting = 30; // 用户设定的超时秒数
|
||||
|
||||
// 配置常量
|
||||
int RECORD_TIMEOUT = 30;
|
||||
|
||||
// 信号处理
|
||||
// =============================================
|
||||
// 系统辅助函数
|
||||
// =============================================
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGINT) {
|
||||
printf("\n\n🛑 收到退出信号,正在清理资源...\n");
|
||||
exit_program.store(true);
|
||||
is_recording.store(false);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
// 非阻塞检查标准输入
|
||||
bool check_input_non_blocking(int timeout_ms = 20) {
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout_ms * 1000;
|
||||
int ret;
|
||||
do {
|
||||
ret = select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
return ret > 0;
|
||||
bool check_input_non_blocking(int timeout_ms = RecordingConfig::INPUT_CHECK_MS) {
|
||||
fd_set fds; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds);
|
||||
struct timeval tv = {0, timeout_ms * 1000};
|
||||
return select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) > 0;
|
||||
}
|
||||
|
||||
void clear_input_buffer() {
|
||||
while (check_input_non_blocking(5)) {
|
||||
char c;
|
||||
read(STDIN_FILENO, &c, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 音频采集回调
|
||||
// =============================================
|
||||
// 音频回调
|
||||
// =============================================
|
||||
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) {
|
||||
if (!is_recording.load() || pInput == NULL) return;
|
||||
const float* pInputFloat = (const float*)pInput;
|
||||
std::lock_guard<std::mutex> lock(buffer_mutex);
|
||||
audio_buffer.insert(audio_buffer.end(), pInputFloat, pInputFloat + frameCount);
|
||||
recorded_seconds.store(static_cast<int>(audio_buffer.size() / 16000.0));
|
||||
}
|
||||
|
||||
// 识别逻辑
|
||||
void recognize_audio(struct whisper_context* ctx, const std::vector<float>& audio_data) {
|
||||
if (audio_data.empty()) return;
|
||||
float total_sec = (float)audio_data.size() / 16000.0f;
|
||||
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, "❌ Whisper 推理失败\n");
|
||||
return;
|
||||
}
|
||||
|
||||
auto t_end = std::chrono::steady_clock::now();
|
||||
float msec = std::chrono::duration<float, std::milli>(t_end - t_start).count();
|
||||
printf("⏱️ 推理耗时:%.2f 秒 | 速度:%.2fx\n", msec/1000.0f, total_sec/(msec/1000.0f));
|
||||
|
||||
int n_segments = whisper_full_n_segments(ctx);
|
||||
printf("📝 结果:\n");
|
||||
for (int i = 0; i < n_segments; ++i) {
|
||||
printf(" %s\n", whisper_full_get_segment_text(ctx, i));
|
||||
}
|
||||
audio_buffer.insert(audio_buffer.end(), (float*)pInput, (float*)pInput + frameCount);
|
||||
recorded_seconds.store(static_cast<int>(audio_buffer.size() / (float)RecordingConfig::SAMPLE_RATE));
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 主逻辑
|
||||
// =============================================
|
||||
int main(int argc, char** argv) {
|
||||
signal(SIGINT, signal_handler);
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <model_path> [timeout_seconds]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc >= 3) RECORD_TIMEOUT = atoi(argv[2]);
|
||||
if (argc < 2) return 1;
|
||||
if (argc >= 3) g_timeout_setting = atoi(argv[2]);
|
||||
|
||||
// 1. 初始化 Whisper
|
||||
// 初始化 Whisper
|
||||
struct whisper_context_params cparams = whisper_context_default_params();
|
||||
cparams.use_gpu = true;
|
||||
cparams.use_gpu = true;
|
||||
struct whisper_context* ctx = whisper_init_from_file_with_params(argv[1], cparams);
|
||||
if (!ctx) return 1;
|
||||
|
||||
// 2. 【补回】设备选择逻辑
|
||||
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);
|
||||
// 麦克风设备枚举与选择
|
||||
ma_context context; ma_context_init(NULL, 0, NULL, &context);
|
||||
ma_device_info* pCapInfos; ma_uint32 capCount;
|
||||
ma_context_get_devices(&context, NULL, NULL, &pCapInfos, &capCount);
|
||||
for (ma_uint32 i = 0; i < capCount; ++i) printf("[%u] %s\n", i, pCapInfos[i].name);
|
||||
printf("👉 请输入设备 ID: ");
|
||||
ma_uint32 dev_id; scanf("%u", &dev_id);
|
||||
while (getchar() != '\n');
|
||||
|
||||
printf("\n📜 可用麦克风设备列表:\n");
|
||||
for (ma_uint32 i = 0; i < captureCount; ++i) {
|
||||
printf(" [%u] %s\n", i, pCaptureInfos[i].name);
|
||||
}
|
||||
|
||||
ma_uint32 device_id = 0;
|
||||
printf("\n👉 请输入要使用的麦克风 ID (默认0): ");
|
||||
if (scanf("%u", &device_id) != 1) device_id = 0;
|
||||
clear_input_buffer();
|
||||
|
||||
// 3. 配置并启动设备
|
||||
ma_device_config devCfg = ma_device_config_init(ma_device_type_capture);
|
||||
devCfg.capture.format = ma_format_f32;
|
||||
devCfg.capture.format = ma_format_f32;
|
||||
devCfg.capture.channels = 1;
|
||||
devCfg.sampleRate = 16000;
|
||||
devCfg.sampleRate = RecordingConfig::SAMPLE_RATE;
|
||||
devCfg.dataCallback = data_callback;
|
||||
if (device_id < captureCount) devCfg.capture.pDeviceID = &pCaptureInfos[device_id].id;
|
||||
if (dev_id < capCount) devCfg.capture.pDeviceID = &pCapInfos[dev_id].id;
|
||||
|
||||
ma_device device;
|
||||
if (ma_device_init(&context, &devCfg, &device) != MA_SUCCESS) return 1;
|
||||
ma_device device; ma_device_init(&context, &devCfg, &device);
|
||||
ma_device_start(&device);
|
||||
|
||||
while (!exit_program.load()) {
|
||||
printf("\n=============================================\n");
|
||||
printf("🎙️ 操作提示 (超时设置: %d秒):\n", RECORD_TIMEOUT);
|
||||
printf(" ▶ [回车] : 开始录制\n");
|
||||
printf(" ■ [回车] : 停止录制 (1.5s 平滑收尾)\n");
|
||||
printf("=============================================\n");
|
||||
printf("👉 等待指令...");
|
||||
fflush(stdout);
|
||||
|
||||
printf("\n[回车] 录制 | [回车] 停止\n👉 等待指令...");
|
||||
while (!check_input_non_blocking(50) && !exit_program.load());
|
||||
if (exit_program.load()) break;
|
||||
clear_input_buffer();
|
||||
while (check_input_non_blocking(0)) getchar();
|
||||
|
||||
{ std::lock_guard<std::mutex> 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");
|
||||
|
||||
// 进度显示线程
|
||||
std::thread progress_thread([&]() {
|
||||
while (is_recording.load() && !exit_program.load()) {
|
||||
while (is_recording.load()) {
|
||||
printf("\r📊 进度: %d 秒 ", recorded_seconds.load());
|
||||
fflush(stdout);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(RecordingConfig::PROGRESS_REFRESH_MS));
|
||||
}
|
||||
});
|
||||
|
||||
bool stop_triggered = false;
|
||||
while (!exit_program.load() && !stop_triggered) {
|
||||
while (!stop_triggered && !exit_program.load()) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
double elapsed_ms = std::chrono::duration<double, std::milli>(now - start_time).count();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
|
||||
|
||||
if (check_input_non_blocking(10)) {
|
||||
char c;
|
||||
if (read(STDIN_FILENO, &c, 1) > 0 && c == '\n') {
|
||||
printf("\n🛑 手动停止触发,准备平滑刷新...");
|
||||
stop_triggered = true;
|
||||
if (check_input_non_blocking(RecordingConfig::UI_WAIT_MS)) {
|
||||
if (getchar() == '\n') {
|
||||
printf("\n🛑 手动停止 (进入平滑刷新模式)...");
|
||||
stop_triggered = true;
|
||||
}
|
||||
} else if (elapsed_ms >= (RECORD_TIMEOUT * 1000 + 200)) {
|
||||
printf("\r📊 进度: %d 秒", RECORD_TIMEOUT); // 强制补全显示
|
||||
printf("\n⏱️ 达到设定阈值 (%d秒),准备平滑刷新...", RECORD_TIMEOUT);
|
||||
}
|
||||
// 修正边界:加上 CLOCK_TOLERANCE_MS 确保进度条在视觉上能显示到设定的秒数
|
||||
else if (elapsed >= (g_timeout_setting * 1000 + RecordingConfig::CLOCK_TOLERANCE_MS)) {
|
||||
printf("\r📊 进度: %d 秒 ", g_timeout_setting); // 强制补完最后一秒显示
|
||||
printf("\n⏱️ 超时停止 (%d秒,进入平滑刷新模式)...", g_timeout_setting);
|
||||
stop_triggered = true;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
if (stop_triggered) {
|
||||
// “宁多不少”核心:延时 1.5s 确保 ALSA/DMA 缓冲区数据全部入库
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
|
||||
is_recording.store(false);
|
||||
}
|
||||
|
||||
// 平滑刷新:等待硬件缓冲区数据入库,防止丢字
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(RecordingConfig::POST_STOP_BUFFER_MS));
|
||||
is_recording.store(false);
|
||||
|
||||
if (progress_thread.joinable()) progress_thread.join();
|
||||
|
||||
// 识别逻辑
|
||||
std::vector<float> captured;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(buffer_mutex);
|
||||
captured = audio_buffer;
|
||||
}
|
||||
recognize_audio(ctx, captured);
|
||||
{ std::lock_guard<std::mutex> lock(buffer_mutex); captured = audio_buffer; }
|
||||
|
||||
printf("\n🔍 识别中 (音频长: %.2fs)...", (float)captured.size()/RecordingConfig::SAMPLE_RATE);
|
||||
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
||||
wparams.language = "zh";
|
||||
wparams.n_threads = 4;
|
||||
whisper_full(ctx, wparams, captured.data(), captured.size());
|
||||
|
||||
int n_segments = whisper_full_n_segments(ctx);
|
||||
for (int i = 0; i < n_segments; ++i) printf("\n📝 %s", whisper_full_get_segment_text(ctx, i));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
ma_device_uninit(&device);
|
||||
ma_context_uninit(&context);
|
||||
whisper_free(ctx);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue