Lizerui9926
2023-04-26 b78d47f1efb3d0662fce1b8d45a9eb11b3caef02
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
#include "precomp.h"
#ifdef __cplusplus 
 
extern "C" {
#endif
 
    // APIs for funasr
    _FUNASRAPI FUNASR_HANDLE  FunASRInit(std::map<std::string, std::string>& model_path, int thread_num)
    {
        Model* mm = CreateModel(model_path, thread_num);
        return mm;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback)
    {
        Model* recog_obj = (Model*)handle;
        if (!recog_obj)
            return nullptr;
 
        int32_t sampling_rate = -1;
        Audio audio(1);
        if (!audio.LoadWav(sz_buf, n_len, &sampling_rate))
            return nullptr;
        if(recog_obj->UseVad()){
            audio.Split(recog_obj);
        }
 
        float* buff;
        int len;
        int flag=0;
        FUNASR_RECOG_RESULT* p_result = new FUNASR_RECOG_RESULT;
        p_result->snippet_time = audio.GetTimeLen();
        int n_step = 0;
        int n_total = audio.GetQueueSize();
        while (audio.Fetch(buff, len, flag) > 0) {
            string msg = recog_obj->Forward(buff, len, flag);
            p_result->msg += msg;
            n_step++;
            if (fn_callback)
                fn_callback(n_step, n_total);
        }
        if(recog_obj->UsePunc()){
            string punc_res = recog_obj->AddPunc((p_result->msg).c_str());
            p_result->msg = punc_res;
        }
 
        return p_result;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogPCMBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, int sampling_rate, FUNASR_MODE mode, QM_CALLBACK fn_callback)
    {
        Model* recog_obj = (Model*)handle;
        if (!recog_obj)
            return nullptr;
 
        Audio audio(1);
        if (!audio.LoadPcmwav(sz_buf, n_len, &sampling_rate))
            return nullptr;
        if(recog_obj->UseVad()){
            audio.Split(recog_obj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        FUNASR_RECOG_RESULT* p_result = new FUNASR_RECOG_RESULT;
        p_result->snippet_time = audio.GetTimeLen();
        int n_step = 0;
        int n_total = audio.GetQueueSize();
        while (audio.Fetch(buff, len, flag) > 0) {
            string msg = recog_obj->Forward(buff, len, flag);
            p_result->msg += msg;
            n_step++;
            if (fn_callback)
                fn_callback(n_step, n_total);
        }
        if(recog_obj->UsePunc()){
            string punc_res = recog_obj->AddPunc((p_result->msg).c_str());
            p_result->msg = punc_res;
        }
 
        return p_result;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogPCMFile(FUNASR_HANDLE handle, const char* sz_filename, int sampling_rate, FUNASR_MODE mode, QM_CALLBACK fn_callback)
    {
        Model* recog_obj = (Model*)handle;
        if (!recog_obj)
            return nullptr;
 
        Audio audio(1);
        if (!audio.LoadPcmwav(sz_filename, &sampling_rate))
            return nullptr;
        if(recog_obj->UseVad()){
            audio.Split(recog_obj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        FUNASR_RECOG_RESULT* p_result = new FUNASR_RECOG_RESULT;
        p_result->snippet_time = audio.GetTimeLen();
        int n_step = 0;
        int n_total = audio.GetQueueSize();
        while (audio.Fetch(buff, len, flag) > 0) {
            string msg = recog_obj->Forward(buff, len, flag);
            p_result->msg += msg;
            n_step++;
            if (fn_callback)
                fn_callback(n_step, n_total);
        }
        if(recog_obj->UsePunc()){
            string punc_res = recog_obj->AddPunc((p_result->msg).c_str());
            p_result->msg = punc_res;
        }
 
        return p_result;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogFile(FUNASR_HANDLE handle, const char* sz_wavfile, FUNASR_MODE mode, QM_CALLBACK fn_callback)
    {
        Model* recog_obj = (Model*)handle;
        if (!recog_obj)
            return nullptr;
        
        int32_t sampling_rate = -1;
        Audio audio(1);
        if(!audio.LoadWav(sz_wavfile, &sampling_rate))
            return nullptr;
        if(recog_obj->UseVad()){
            audio.Split(recog_obj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        int n_step = 0;
        int n_total = audio.GetQueueSize();
        FUNASR_RECOG_RESULT* p_result = new FUNASR_RECOG_RESULT;
        p_result->snippet_time = audio.GetTimeLen();
        while (audio.Fetch(buff, len, flag) > 0) {
            string msg = recog_obj->Forward(buff, len, flag);
            p_result->msg+= msg;
            n_step++;
            if (fn_callback)
                fn_callback(n_step, n_total);
        }
        if(recog_obj->UsePunc()){
            string punc_res = recog_obj->AddPunc((p_result->msg).c_str());
            p_result->msg = punc_res;
        }
    
        return p_result;
    }
 
    _FUNASRAPI const int FunASRGetRetNumber(FUNASR_RESULT result)
    {
        if (!result)
            return 0;
 
        return 1;
    }
 
 
    _FUNASRAPI const float FunASRGetRetSnippetTime(FUNASR_RESULT result)
    {
        if (!result)
            return 0.0f;
 
        return ((FUNASR_RECOG_RESULT*)result)->snippet_time;
    }
 
    _FUNASRAPI const char* FunASRGetResult(FUNASR_RESULT result,int n_index)
    {
        FUNASR_RECOG_RESULT * p_result = (FUNASR_RECOG_RESULT*)result;
        if(!p_result)
            return nullptr;
 
        return p_result->msg.c_str();
    }
 
    _FUNASRAPI void FunASRFreeResult(FUNASR_RESULT result)
    {
        if (result)
        {
            delete (FUNASR_RECOG_RESULT*)result;
        }
    }
 
    _FUNASRAPI void FunASRUninit(FUNASR_HANDLE handle)
    {
        Model* recog_obj = (Model*)handle;
 
        if (!recog_obj)
            return;
 
        delete recog_obj;
    }
 
#ifdef __cplusplus 
 
}
#endif