lyblsgo
2023-04-11 6e37612b61094e0ab08b38d35f588c2b17eae267
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
 
#ifndef _WIN32
#include <sys/time.h>
#else
#include <win_func.h>
#endif
 
#include "libfunasrapi.h"
 
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
 
int main(int argc, char *argv[])
{
 
    if (argc < 4)
    {
        printf("Usage: %s /path/to/model_dir /path/to/wav.scp quantize(true or false) \n", argv[0]);
        exit(-1);
    }
 
    // read wav.scp
    vector<string> wav_list;
    ifstream in(argv[2]);
    if (!in.is_open()) {
        printf("Failed to open file: %s", argv[2]);
        return 0;
    }
    string line;
    while(getline(in, line))
    {
        istringstream iss(line);
        string column1, column2;
        iss >> column1 >> column2;
        wav_list.push_back(column2); 
    }
    in.close();
 
    // model init
    struct timeval start, end;
    gettimeofday(&start, NULL);
    int nThreadNum = 1;
    // is quantize
    bool quantize = false;
    istringstream(argv[3]) >> boolalpha >> quantize;
 
    FUNASR_HANDLE AsrHanlde=FunASRInit(argv[1], nThreadNum, quantize);
    if (!AsrHanlde)
    {
        printf("Cannot load ASR Model from: %s, there must be files model.onnx and vocab.txt", argv[1]);
        exit(-1);
    }
    gettimeofday(&end, NULL);
    long seconds = (end.tv_sec - start.tv_sec);
    long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
    printf("Model initialization takes %lfs.\n", (double)modle_init_micros / 1000000);
 
    // warm up
    for (size_t i = 0; i < 30; i++)
    {
        FUNASR_RESULT Result=FunASRRecogFile(AsrHanlde, wav_list[0].c_str(), RASR_NONE, NULL);
    }
 
    // forward
    float snippet_time = 0.0f;
    float total_length = 0.0f;
    long total_time = 0.0f;
    
    for (size_t i = 0; i < wav_list.size(); i++)
    {
        gettimeofday(&start, NULL);
        FUNASR_RESULT Result=FunASRRecogFile(AsrHanlde, wav_list[i].c_str(), RASR_NONE, NULL);
        gettimeofday(&end, NULL);
        seconds = (end.tv_sec - start.tv_sec);
        long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
        total_time += taking_micros;
 
        if(Result){
            string msg = FunASRGetResult(Result, 0);
            printf("Result: %s \n", msg);
 
            snippet_time = FunASRGetRetSnippetTime(Result);
            total_length += snippet_time;
            FunASRFreeResult(Result);
        }else{
            cout <<"No return data!";
        }
    }
 
    printf("total_time_wav %ld ms.\n", (long)(total_length * 1000));
    printf("total_time_comput %ld ms.\n", total_time / 1000);
    printf("total_rtf %05lf .\n", (double)total_time/ (total_length*1000000));
 
    FunASRUninit(AsrHanlde);
    return 0;
}