vad-sense-voice-c-api.c 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
// c-api-examples/vad-sense-voice-c-api.c
//
// Copyright (c)  2024  Xiaomi Corporation

//
// This file demonstrates how to use VAD + SenseVoice with sherpa-onnx's C API.
// clang-format off
//
// To use silero-vad:
//  wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx
//
// To use ten-vad:
//  wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx
//
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
//
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
// tar xvf sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
// rm sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
//
// clang-format on

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>

#include "sherpa-onnx/c-api/c-api.h"

// Function to normalize string: remove punctuation and spaces, convert to lowercase
void normalize_string(const char* input, char* output) {
    int i = 0, j = 0;
    while (input[i] != '\0') {
        // Skip punctuation characters and spaces (both English and Chinese)
        if (!ispunct((unsigned char)input[i]) && 
            !isspace((unsigned char)input[i]) &&
            !(input[i] >= 0x3000 && input[i] <= 0x303F) && // CJK punctuation
            !(input[i] >= 0xFF00 && input[i] <= 0xFF0F) && // Fullwidth forms
            !(input[i] >= 0xFF1A && input[i] <= 0xFF20) && // Fullwidth forms
            !(input[i] >= 0xFF3B && input[i] <= 0xFF40) && // Fullwidth forms
            !(input[i] >= 0xFF5B && input[i] <= 0xFF65)) { // Fullwidth forms
            
            // Convert to lowercase and add to output
            output[j++] = tolower((unsigned char)input[i]);
        }
        i++;
    }
    output[j] = '\0';
}

// Function to get the first meaningful character (non-punctuation, non-space)
char get_first_meaningful_char(const char* str) {
    int i = 0;
    while (str[i] != '\0') {
        if (!ispunct((unsigned char)str[i]) && 
            !isspace((unsigned char)str[i]) &&
            !(str[i] >= 0x3000 && str[i] <= 0x303F) &&
            !(str[i] >= 0xFF00 && str[i] <= 0xFF0F) &&
            !(str[i] >= 0xFF1A && str[i] <= 0xFF20) &&
            !(str[i] >= 0xFF3B && str[i] <= 0xFF40) &&
            !(str[i] >= 0xFF5B && str[i] <= 0xFF65)) {
            return tolower((unsigned char)str[i]);
        }
        i++;
    }
    return '\0';
}

// Function to check if two strings are effectively the same after normalization
int are_strings_effectively_same(const char* str1, const char* str2) {
    char norm1[1024], norm2[1024];
    normalize_string(str1, norm1);
    normalize_string(str2, norm2);
    return strcmp(norm1, norm2) == 0;
}

// 判断是否为 CJK 统一表意字符
static bool is_cjk_ideograph(uint32_t ch)
{
    return (ch >= 0x4E00  && ch <= 0x9FFF)  ||  // CJK Unified Ideographs
           (ch >= 0x3400  && ch <= 0x4DBF)  ||  // CJK Extension A
           (ch >= 0x20000 && ch <= 0x2A6DF) ||  // CJK Extension B
           (ch >= 0x2A700 && ch <= 0x2B73F) ||  // CJK Extension C
           (ch >= 0x2B740 && ch <= 0x2B81F) ||  // CJK Extension D
           (ch >= 0x2B820 && ch <= 0x2CEAF) ||  // CJK Extension E
           (ch >= 0x2CEB0 && ch <= 0x2EBEF) ||  // CJK Extension F
           (ch >= 0x3007 && ch <= 0x3007)  ||  // 〇
           (ch >= 0x3021 && ch <= 0x3029)  ||  // 〡〢〣〤〥〦〧〨〩
           (ch >= 0x3038 && ch <= 0x303B);      // 〸〹〺〻〼
}

// 反向解码一个 UTF-8 字符,返回其长度(字节)和码点
static int prev_utf8_char(const char *s, int pos, uint32_t *out_ch)
{
    int start = pos;
    // 找到当前字符起始字节
    while (start > 0 && (s[start] & 0xC0) == 0x80)
        --start;
    // 解码
    const unsigned char *p = (const unsigned char *)&s[start];
    if ((*p & 0x80) == 0) {              // 1-byte
        *out_ch = *p;
    } else if ((*p & 0xE0) == 0xC0) {    // 2-byte
        *out_ch = ((p[0] & 0x1F) << 6) | (p[1] & 0x3F);
    } else if ((*p & 0xF0) == 0xE0) {    // 3-byte
        *out_ch = ((p[0] & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F);
    } else if ((*p & 0xF8) == 0xF0) {    // 4-byte
        *out_ch = ((p[0] & 0x07) << 18) | ((p[1] & 0x3F) << 12) |
                  ((p[2] & 0x3F) << 6)  | (p[3] & 0x3F);
    } else {
        *out_ch = 0xFFFD; // 非法序列,用替换字符
    }
    return pos - start + 1; // 返回字节长度
}

// 新实现:按“中日文单字 / 英文整词”取最后 n 个语义单元
void get_last_n_words(const char *str, int n, char *output)
{
    if (!str || !output || n <= 0) {
        *output = '\0';
        return;
    }

    int len = strlen(str);
    if (len == 0) {
        *output = '\0';
        return;
    }

    // 用来存反向收集到的单元
    char units[256][256];
    int  unit_cnt = 0;

    int pos = len; // 从 '\0' 前一个位置开始
    while (pos > 0 && unit_cnt < n) {
        uint32_t ch;
        int char_len = prev_utf8_char(str, pos - 1, &ch);
        pos -= char_len;

        if (ch < 128 && ((ch | 32) - 'a' < 26)) {
            // ===== 英文单词 =====
            int word_end = pos + char_len;
            int word_start = pos;
            // 向前找单词起始
            while (word_start > 0) {
                uint32_t tmp;
                int tmp_len = prev_utf8_char(str, word_start - 1, &tmp);
                if (tmp < 128 && ((tmp | 32) - 'a' < 26))
                    word_start -= tmp_len;
                else
                    break;
            }
            // 拷贝整个单词
            int wlen = word_end - word_start;
            if (wlen >= (int)sizeof(units[unit_cnt])) wlen = sizeof(units[unit_cnt]) - 1;
            memcpy(units[unit_cnt], str + word_start, wlen);
            units[unit_cnt][wlen] = '\0';
            ++unit_cnt;
            pos = word_start; // 继续向前扫描
        } else if (is_cjk_ideograph(ch) || ch > 0xFF00) {
            // ===== CJK 或全角符号 =====
            if (char_len >= (int)sizeof(units[unit_cnt])) char_len = sizeof(units[unit_cnt]) - 1;
            memcpy(units[unit_cnt], str + pos, char_len);
            units[unit_cnt][char_len] = '\0';
            ++unit_cnt;
        }
        // 其他标点/空格直接跳过
    }

    // 反向拼回 output
    output[0] = '\0';
    for (int i = unit_cnt - 1; i >= 0; --i) {
        if (i < unit_cnt - 1) strcat(output, " ");
        strcat(output, units[i]);
    }
}

// 在第二个字符串中查找锚点文本的位置
const char *find_anchor_end_position(const char *str, const char *anchor) {
  if (!anchor || !*anchor) return str;
  
  char normalized_str[1024] = {0};
  char normalized_anchor[1024] = {0};
  
  // 规范化两个字符串
  normalize_string(str, normalized_str);
  normalize_string(anchor, normalized_anchor);
  
  // 在规范化后的字符串中查找锚点
  char *found = strstr(normalized_str, normalized_anchor);
  if (!found) return str; // 如果找不到锚点,返回整个字符串
  
  // 计算锚点的结束位置
  int anchor_end_offset = found - normalized_str + strlen(normalized_anchor);
  
  // 计算在原始字符串中的对应位置
  int normalized_count = 0;
  const char *ptr = str;
  
  while (*ptr != '\0' && normalized_count < anchor_end_offset) {
    if (!ispunct((unsigned char)*ptr) && !isspace((unsigned char)*ptr)) {
      normalized_count++;
    }
    ptr++;
  }
  
  return ptr;
}
// 找到下一个单词的开始位置
const char *find_next_word_start(const char *str) {
  // 跳过所有标点和空格
  while (*str != '\0' && 
         (ispunct((unsigned char)*str) || isspace((unsigned char)*str))) {
    str++;
  }
  return str;
}

// 获取基于锚点的差异文本(从锚点后的第一个完整单词开始)
char *get_difference_after_anchor(const char *str1, const char *str2, int num_anchor_words) {
    if (are_strings_effectively_same(str1, str2)) {
        return strdup("");
    }

    // 获取语义单元级的锚点文本
    char semantic_anchor[256] = {0};
    get_last_n_words(str1, num_anchor_words, semantic_anchor);

    if (strlen(semantic_anchor) == 0) {
        return strdup(str2);
    }

    // 关键:对语义锚点再做一次字符级规范化,匹配 find_anchor_end_position 的行为
    char normalized_anchor[256] = {0};
    normalize_string(semantic_anchor, normalized_anchor);

    // 使用规范化后的锚点查找位置
    const char *anchor_end = find_anchor_end_position(str2, normalized_anchor);
    const char *next_word_start = find_next_word_start(anchor_end);

    return strdup(next_word_start);
}

// Structure to store previous segment information
typedef struct {
    float* samples;
    int32_t n;
    int32_t start;
    char* text;
} PreviousSegment;

void free_previous_segment(PreviousSegment* seg) {
    if (seg) {
        if (seg->samples) free(seg->samples);
        if (seg->text) free(seg->text);
        free(seg);
    }
}

PreviousSegment* copy_segment(const SherpaOnnxSpeechSegment* segment, const char* text) {
    PreviousSegment* prev = (PreviousSegment*)malloc(sizeof(PreviousSegment));
    if (!prev) return NULL;
    
    prev->n = segment->n;
    prev->start = segment->start;
    prev->samples = (float*)malloc(segment->n * sizeof(float));
    if (!prev->samples) {
        free(prev);
        return NULL;
    }
    memcpy(prev->samples, segment->samples, segment->n * sizeof(float));
    
    prev->text = strdup(text);
    if (!prev->text) {
        free(prev->samples);
        free(prev);
        return NULL;
    }
    
    return prev;
}

int32_t main() {
    setlocale(LC_ALL, ""); // Set locale for wide character handling

    const char *wav_filename = "./lei-jun-test.wav";
    if (!SherpaOnnxFileExists(wav_filename)) {
        fprintf(stderr, "Please download %s\n", wav_filename);
        return -1;
    }

    const char *vad_filename;
    int32_t use_silero_vad = 0;
    int32_t use_ten_vad = 0;

    if (SherpaOnnxFileExists("./silero_vad.onnx")) {
        printf("Use silero-vad\n");
        vad_filename = "./silero_vad.onnx";
        use_silero_vad = 1;
    } else if (SherpaOnnxFileExists("./ten-vad.onnx")) {
        printf("Use ten-vad\n");
        vad_filename = "./ten-vad.onnx";
        use_ten_vad = 1;
    } else {
        fprintf(stderr, "Please provide either silero_vad.onnx or ten-vad.onnx\n");
        return -1;
    }

    const char *model_filename =
        "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.onnx";
    const char *tokens_filename =
        "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt";
    const char *language = "auto";
    const char *provider = "cpu";
    int32_t use_inverse_text_normalization = 1;

    const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename);
    if (wave == NULL) {
        fprintf(stderr, "Failed to read %s\n", wav_filename);
        return -1;
    }

    if (wave->sample_rate != 16000) {
        fprintf(stderr, "Expect the sample rate to be 16000. Given: %d\n",
                wave->sample_rate);
        SherpaOnnxFreeWave(wave);
        return -1;
    }

    SherpaOnnxOfflineSenseVoiceModelConfig sense_voice_config;
    memset(&sense_voice_config, 0, sizeof(sense_voice_config));
    sense_voice_config.model = model_filename;
    sense_voice_config.language = language;
    sense_voice_config.use_itn = use_inverse_text_normalization;

    // Offline model config
    SherpaOnnxOfflineModelConfig offline_model_config;
    memset(&offline_model_config, 0, sizeof(offline_model_config));
    offline_model_config.debug = 0;
    offline_model_config.num_threads = 1;
    offline_model_config.provider = provider;
    offline_model_config.tokens = tokens_filename;
    offline_model_config.sense_voice = sense_voice_config;

    // Recognizer config
    SherpaOnnxOfflineRecognizerConfig recognizer_config;
    memset(&recognizer_config, 0, sizeof(recognizer_config));
    recognizer_config.decoding_method = "greedy_search";
    recognizer_config.model_config = offline_model_config;

    const SherpaOnnxOfflineRecognizer *recognizer =
        SherpaOnnxCreateOfflineRecognizer(&recognizer_config);

    if (recognizer == NULL) {
        fprintf(stderr, "Please check your recognizer config!\n");
        SherpaOnnxFreeWave(wave);
        return -1;
    }

    SherpaOnnxVadModelConfig vadConfig;
    memset(&vadConfig, 0, sizeof(vadConfig));

    if (use_silero_vad) {
        vadConfig.silero_vad.model = vad_filename;
        vadConfig.silero_vad.threshold = 0.25;
        vadConfig.silero_vad.min_silence_duration = 1.5;
        vadConfig.silero_vad.min_speech_duration = 0.3;
        vadConfig.silero_vad.max_speech_duration = 20;
        vadConfig.silero_vad.window_size = 512;
    } else if (use_ten_vad) {
        vadConfig.ten_vad.model = vad_filename;
        vadConfig.ten_vad.threshold = 0.25;
        vadConfig.ten_vad.min_silence_duration = 0.5;
        vadConfig.ten_vad.min_speech_duration = 0.5;
        vadConfig.ten_vad.max_speech_duration = 10;
        vadConfig.ten_vad.window_size = 256;
    }

    vadConfig.sample_rate = 16000;
    vadConfig.num_threads = 1;
    vadConfig.debug = 1;

    const SherpaOnnxVoiceActivityDetector *vad =
        SherpaOnnxCreateVoiceActivityDetector(&vadConfig, 30);

    if (vad == NULL) {
        fprintf(stderr, "Please check your recognizer config!\n");
        SherpaOnnxFreeWave(wave);
        SherpaOnnxDestroyOfflineRecognizer(recognizer);
        return -1;
    }

    int32_t window_size = use_silero_vad ? vadConfig.silero_vad.window_size
                                         : vadConfig.ten_vad.window_size;
    int32_t i = 0;
    int is_eof = 0;

    // Variables to store previous segment information
    PreviousSegment *prev_segment = NULL;

    while (!is_eof) {
        if (i + window_size < wave->num_samples) {
            SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad, wave->samples + i,
                                                          window_size);
        } else {
            SherpaOnnxVoiceActivityDetectorFlush(vad);
            is_eof = 1;
        }

        while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) {
            const SherpaOnnxSpeechSegment *segment =
                SherpaOnnxVoiceActivityDetectorFront(vad);

            float duration = segment->n / 16000.0f;
            
            // Process the current segment
            const SherpaOnnxOfflineStream *stream =
                SherpaOnnxCreateOfflineStream(recognizer);
            
            SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate,
                                          segment->samples, segment->n);
            SherpaOnnxDecodeOfflineStream(recognizer, stream);
            const SherpaOnnxOfflineRecognizerResult *result =
                SherpaOnnxGetOfflineStreamResult(stream);
            
            float start = segment->start / 16000.0f;
            float stop = start + duration;
            
            if (duration < 1.5f && prev_segment != NULL) {
                // Current segment is less than 1 second and we have a previous segment
                // Merge with previous segment
                
                // Create merged samples
                int32_t merged_n = prev_segment->n + segment->n;
                float *merged_samples = (float*)malloc(merged_n * sizeof(float));
                memcpy(merged_samples, prev_segment->samples, prev_segment->n * sizeof(float));
                memcpy(merged_samples + prev_segment->n, segment->samples, segment->n * sizeof(float));
                
                // Create stream for merged segment
                const SherpaOnnxOfflineStream *merged_stream =
                    SherpaOnnxCreateOfflineStream(recognizer);
                SherpaOnnxAcceptWaveformOffline(merged_stream, wave->sample_rate,
                                              merged_samples, merged_n);
                SherpaOnnxDecodeOfflineStream(recognizer, merged_stream);
                const SherpaOnnxOfflineRecognizerResult *merged_result =
                    SherpaOnnxGetOfflineStreamResult(merged_stream);
                
                // Get the meaningful difference starting from first character
                char *diff_text = get_difference_after_anchor(prev_segment->text, merged_result->text, 3);
                
                if (strlen(diff_text) == 0) {
                    fprintf(stderr, "%.3f -- %.3f: %s (short segment, no meaningful difference)\n", 
                            start, stop, merged_result->text);
                } else {
                    fprintf(stderr, "%.3f -- %.3f: %s (short segment, meaningful diff: %s)\n", 
                            start, stop, merged_result->text, diff_text);
                }
                
                // Don't update prev_segment for short segments (requirement 1)
                // Only update if the current segment is >= 1 second
                
                SherpaOnnxDestroyOfflineRecognizerResult(merged_result);
                SherpaOnnxDestroyOfflineStream(merged_stream);
                free(merged_samples);
                free(diff_text);
                
            } else {
                // Normal processing for segments >= 1 second
                fprintf(stderr, "%.3f -- %.3f: %s\n", start, stop, result->text);
                
                // Store current segment and result only if duration >= 1 second (requirement 1)
                if (duration >= 1.5f) {
                    if (prev_segment != NULL) {
                        free_previous_segment(prev_segment);
                    }
                    prev_segment = copy_segment(segment, result->text);
                } else {
                    // Short segment, don't store as previous
                    if (prev_segment != NULL) {
                        free_previous_segment(prev_segment);
                        prev_segment = NULL;
                    }
                }
            }
            
            SherpaOnnxDestroyOfflineRecognizerResult(result);
            SherpaOnnxDestroyOfflineStream(stream);
            SherpaOnnxDestroySpeechSegment(segment);
            SherpaOnnxVoiceActivityDetectorPop(vad);
        }
        i += window_size;
    }

    // Clean up
    if (prev_segment != NULL) {
        free_previous_segment(prev_segment);
    }
    
    SherpaOnnxDestroyOfflineRecognizer(recognizer);
    SherpaOnnxDestroyVoiceActivityDetector(vad);
    SherpaOnnxFreeWave(wave);

    return 0;
}