doubao needs to use same logic handle both key enter and timeout
This commit is contained in:
parent
bb17b83555
commit
588bf73a67
|
|
@ -23,12 +23,23 @@
|
|||
std::atomic<bool> is_recording(false);
|
||||
std::atomic<bool> exit_program(false);
|
||||
std::atomic<int> recorded_seconds(0);
|
||||
// 新增:记录最后一次音频回调的时间(毫秒)
|
||||
std::atomic<long long> last_callback_time(0);
|
||||
// 音频缓冲区(加锁保护)
|
||||
std::vector<float> audio_buffer;
|
||||
std::mutex buffer_mutex;
|
||||
// 配置常量
|
||||
const int RECORD_TIMEOUT = 30; // 超时时间(秒)
|
||||
const int FINISH_WAIT_MS = 2000; // 停止前收尾等待时间(毫秒)
|
||||
const int RECORD_TIMEOUT = 30; // 基础超时时间(秒)
|
||||
const int TIMEOUT_GRACE_MS = 1500; // 超时后宽限1.5秒(等最后帧)
|
||||
const int FINISH_WAIT_MS = 2000; // 停止前收尾等待时间
|
||||
const int FRAME_INTERVAL_MS = 100; // 音频帧间隔(ms)
|
||||
|
||||
// 获取当前时间戳(毫秒)
|
||||
long long get_current_time_ms() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch()
|
||||
).count();
|
||||
}
|
||||
|
||||
// 信号处理:Ctrl+C 优雅退出
|
||||
void signal_handler(int sig) {
|
||||
|
|
@ -36,13 +47,12 @@ void signal_handler(int sig) {
|
|||
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 = 100) {
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
|
|
@ -50,14 +60,13 @@ bool check_input_non_blocking(int timeout_ms = 100) {
|
|||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout_ms * 1000; // 转换为微秒
|
||||
tv.tv_usec = timeout_ms * 1000;
|
||||
|
||||
return select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) > 0;
|
||||
}
|
||||
|
||||
// 清空输入缓冲区(避免残留)
|
||||
// 清空输入缓冲区
|
||||
void clear_input_buffer() {
|
||||
// 使用非阻塞读取清空缓冲区
|
||||
while (check_input_non_blocking(10)) {
|
||||
char c;
|
||||
ssize_t ret = read(STDIN_FILENO, &c, 1);
|
||||
|
|
@ -65,19 +74,23 @@ void clear_input_buffer() {
|
|||
}
|
||||
}
|
||||
|
||||
// 音频回调(确保完整接收音频帧)
|
||||
// 音频回调(关键:记录最后回调时间,确保每帧都写入)
|
||||
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;
|
||||
if (pInputFloat == NULL) return;
|
||||
|
||||
// 更新最后回调时间(关键:标记有新数据)
|
||||
last_callback_time.store(get_current_time_ms());
|
||||
|
||||
std::lock_guard<std::mutex> lock(buffer_mutex);
|
||||
// 安全保护:最多录制35秒(超时+5秒缓冲)
|
||||
const size_t max_memory = 16000 * (RECORD_TIMEOUT + 5);
|
||||
if (audio_buffer.size() < max_memory) {
|
||||
// 逐帧写入,确保不丢帧
|
||||
audio_buffer.insert(audio_buffer.end(), pInputFloat, pInputFloat + frameCount);
|
||||
// 更新实时时长(精确到0.1秒)
|
||||
// 精确计算录制时长(按实际采样数)
|
||||
recorded_seconds.store(static_cast<int>(audio_buffer.size() / 16000.0));
|
||||
}
|
||||
}
|
||||
|
|
@ -88,11 +101,11 @@ int trim_silence(const float* audio_data, int audio_len, float threshold = 0.001
|
|||
while (start < audio_len && fabs(audio_data[start]) < threshold) {
|
||||
start++;
|
||||
}
|
||||
// 关键:不裁剪末尾,确保最后几个字完整
|
||||
return std::max(audio_len - start, 16000); // 至少保留1秒
|
||||
// 完全保留末尾,哪怕是静音
|
||||
return std::max(audio_len - start, 16000);
|
||||
}
|
||||
|
||||
// 列出系统音频设备(修复参数类型:第三个参数为引用)
|
||||
// 列出系统音频设备
|
||||
void list_audio_devices(ma_context& context, ma_device_info** pCaptureInfos, ma_uint32& captureCount) {
|
||||
printf("\n📜 系统可用麦克风设备列表:\n");
|
||||
printf("=============================================\n");
|
||||
|
|
@ -116,11 +129,11 @@ void list_audio_devices(ma_context& context, ma_device_info** pCaptureInfos, ma_
|
|||
// 提示信息
|
||||
void print_usage() {
|
||||
printf("=============================================\n");
|
||||
printf("🎤 语音识别程序(终极稳定版)\n");
|
||||
printf("🎤 语音识别程序(防丢帧终极版)\n");
|
||||
printf("操作说明:\n");
|
||||
printf(" 1. 按下【回车键】开始录制\n");
|
||||
printf(" 2. 说话完成后按回车停止(会自动收尾)\n");
|
||||
printf(" 3. 录制超过%d秒自动停止并识别\n", RECORD_TIMEOUT);
|
||||
printf(" 3. 录制超过%d秒后宽限1.5秒自动停止\n", RECORD_TIMEOUT);
|
||||
printf(" 4. 录制中实时显示时长\n");
|
||||
printf(" 5. Ctrl+C 退出程序\n");
|
||||
printf("=============================================\n");
|
||||
|
|
@ -130,10 +143,9 @@ void print_usage() {
|
|||
void print_cpu_optimize_tips() {
|
||||
printf("⚡ CPU优化配置说明:\n");
|
||||
printf(" ✅ 已启用多线程识别(自动适配CPU核心数)\n");
|
||||
printf(" ✅ 停止前预留2秒缓冲,不丢最后音频\n");
|
||||
printf(" ✅ 修复线程死锁,手动停止立即响应\n");
|
||||
printf(" ✅ 超时宽限1.5秒,确保最后音频帧不丢\n");
|
||||
printf(" ✅ 记录音频回调时间,实时检测数据写入\n");
|
||||
printf(" 📌 模型优化:推荐使用 ggml-medium-q4_0.bin(量化版)\n");
|
||||
printf(" 📌 编译优化:已用 -O3 最高级优化\n");
|
||||
printf("=============================================\n");
|
||||
}
|
||||
|
||||
|
|
@ -203,10 +215,10 @@ int main(int argc, char** argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// 2. 枚举麦克风设备(修复参数传递:直接传变量,而非指针)
|
||||
// 2. 枚举麦克风设备
|
||||
ma_device_info* pCaptureInfos = NULL;
|
||||
ma_uint32 captureCount = 0;
|
||||
list_audio_devices(context, &pCaptureInfos, captureCount); // 这里直接传captureCount(引用)
|
||||
list_audio_devices(context, &pCaptureInfos, captureCount);
|
||||
|
||||
// 3. 选择麦克风设备
|
||||
ma_uint32 device_id = 0;
|
||||
|
|
@ -216,12 +228,12 @@ int main(int argc, char** argv) {
|
|||
fprintf(stderr, "❌ 输入无效,使用默认设备ID 0\n");
|
||||
device_id = 0;
|
||||
}
|
||||
clear_input_buffer(); // 清空缓冲区
|
||||
clear_input_buffer();
|
||||
}
|
||||
|
||||
// 4. 初始化 Whisper 模型
|
||||
struct whisper_context_params cparams = whisper_context_default_params();
|
||||
cparams.use_gpu = false; // 强制CPU
|
||||
cparams.use_gpu = false;
|
||||
|
||||
printf("\n🚀 正在加载模型:%s\n", model_path);
|
||||
struct whisper_context* ctx = whisper_init_from_file_with_params(model_path, cparams);
|
||||
|
|
@ -267,11 +279,11 @@ int main(int argc, char** argv) {
|
|||
|
||||
print_usage();
|
||||
|
||||
// 主循环(彻底修复死锁逻辑)
|
||||
// 主循环
|
||||
while (!exit_program.load()) {
|
||||
printf("\n👉 按下回车键开始录制...\n");
|
||||
|
||||
// 阻塞等待用户回车(确保由用户控制开始)
|
||||
// 阻塞等待用户回车
|
||||
char input_char = 0;
|
||||
while (!check_input_non_blocking() && !exit_program.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
|
@ -280,7 +292,7 @@ int main(int argc, char** argv) {
|
|||
|
||||
ssize_t ret1 = read(STDIN_FILENO, &input_char, 1);
|
||||
(void)ret1;
|
||||
clear_input_buffer(); // 清空其他残留输入
|
||||
clear_input_buffer();
|
||||
|
||||
if (exit_program.load()) break;
|
||||
if (input_char != '\n') {
|
||||
|
|
@ -288,9 +300,10 @@ int main(int argc, char** argv) {
|
|||
continue;
|
||||
}
|
||||
|
||||
// 重置录制状态
|
||||
// 重置录制状态(关键:清空最后回调时间)
|
||||
is_recording.store(true);
|
||||
recorded_seconds.store(0);
|
||||
last_callback_time.store(get_current_time_ms());
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(buffer_mutex);
|
||||
audio_buffer.clear();
|
||||
|
|
@ -308,18 +321,19 @@ int main(int argc, char** argv) {
|
|||
|
||||
bool is_timeout = false;
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
bool manual_stop = false;
|
||||
long long timeout_deadline_ms = get_current_time_ms() + (RECORD_TIMEOUT * 1000) + TIMEOUT_GRACE_MS;
|
||||
|
||||
// 核心循环 - 修复死锁:在设flag前等待,不阻塞主线程
|
||||
// 核心循环 - 防丢帧逻辑
|
||||
while (is_recording.load() && !exit_program.load()) {
|
||||
// 检查超时
|
||||
auto duration = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::steady_clock::now() - start_time).count();
|
||||
long long current_ms = get_current_time_ms();
|
||||
|
||||
if (duration >= RECORD_TIMEOUT) {
|
||||
printf("\n⏱️ 录制超时(%d秒),正在收尾...", RECORD_TIMEOUT);
|
||||
// 超时判断:1. 超过总时限 且 2. 最后回调超过帧间隔(无新数据)
|
||||
bool timeout_1 = current_ms >= timeout_deadline_ms;
|
||||
bool timeout_2 = (current_ms - last_callback_time.load()) > (FRAME_INTERVAL_MS * 2);
|
||||
if (timeout_1 && timeout_2) {
|
||||
printf("\n⏱️ 录制超时(%d秒+宽限1.5秒),正在等待最后帧写入...", RECORD_TIMEOUT);
|
||||
fflush(stdout);
|
||||
// 关键:先等2秒让数据写完,再停标志
|
||||
// 等待最后帧写完(哪怕多等一点)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(FINISH_WAIT_MS));
|
||||
is_recording.store(false);
|
||||
is_timeout = true;
|
||||
|
|
@ -327,7 +341,7 @@ int main(int argc, char** argv) {
|
|||
break;
|
||||
}
|
||||
|
||||
// 检查手动输入(非阻塞)
|
||||
// 手动停止(正常逻辑)
|
||||
if (check_input_non_blocking(100)) {
|
||||
char c;
|
||||
ssize_t ret2 = read(STDIN_FILENO, &c, 1);
|
||||
|
|
@ -335,19 +349,17 @@ int main(int argc, char** argv) {
|
|||
if (c == '\n') {
|
||||
printf("\n🛑 已手动停止录制,正在收尾...");
|
||||
fflush(stdout);
|
||||
// 关键:先sleep,让音频写完,再停标志
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(FINISH_WAIT_MS));
|
||||
is_recording.store(false);
|
||||
manual_stop = true;
|
||||
printf("完成\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 更短轮询,减少延迟
|
||||
}
|
||||
|
||||
// 等待进度线程退出(此时线程应该已经自然退出)
|
||||
// 等待进度线程退出
|
||||
progress_thread.join();
|
||||
|
||||
if (exit_program.load()) break;
|
||||
|
|
|
|||
Loading…
Reference in New Issue