lyblsgo
2023-04-20 ae3e2567602546e66c0f358463617e560fc70e20
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
#include "precomp.h"
#ifdef __cplusplus 
 
extern "C" {
#endif
 
    // APIs for funasr
    _FUNASRAPI FUNASR_HANDLE  FunASRInit(const char* szModelDir, int nThreadNum, bool quantize, bool use_vad)
    {
        Model* mm = create_model(szModelDir, nThreadNum, quantize, use_vad);
        return mm;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogBuffer(FUNASR_HANDLE handle, const char* szBuf, int nLen, FUNASR_MODE Mode, QM_CALLBACK fnCallback, bool use_vad)
    {
        Model* pRecogObj = (Model*)handle;
        if (!pRecogObj)
            return nullptr;
 
        int32_t sampling_rate = -1;
        Audio audio(1);
        if (!audio.loadwav(szBuf, nLen, &sampling_rate))
            return nullptr;
        if(use_vad){
            audio.split(pRecogObj);
        }
 
        float* buff;
        int len;
        int flag=0;
        FUNASR_RECOG_RESULT* pResult = new FUNASR_RECOG_RESULT;
        pResult->snippet_time = audio.get_time_len();
        int nStep = 0;
        int nTotal = audio.get_queue_size();
        while (audio.fetch(buff, len, flag) > 0) {
            string msg = pRecogObj->forward(buff, len, flag);
            pResult->msg += msg;
            nStep++;
            if (fnCallback)
                fnCallback(nStep, nTotal);
        }
 
        return pResult;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogPCMBuffer(FUNASR_HANDLE handle, const char* szBuf, int nLen, int sampling_rate, FUNASR_MODE Mode, QM_CALLBACK fnCallback, bool use_vad)
    {
        Model* pRecogObj = (Model*)handle;
        if (!pRecogObj)
            return nullptr;
 
        Audio audio(1);
        if (!audio.loadpcmwav(szBuf, nLen, &sampling_rate))
            return nullptr;
        if(use_vad){
            audio.split(pRecogObj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        FUNASR_RECOG_RESULT* pResult = new FUNASR_RECOG_RESULT;
        pResult->snippet_time = audio.get_time_len();
        int nStep = 0;
        int nTotal = audio.get_queue_size();
        while (audio.fetch(buff, len, flag) > 0) {
            string msg = pRecogObj->forward(buff, len, flag);
            pResult->msg += msg;
            nStep++;
            if (fnCallback)
                fnCallback(nStep, nTotal);
        }
 
        return pResult;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogPCMFile(FUNASR_HANDLE handle, const char* szFileName, int sampling_rate, FUNASR_MODE Mode, QM_CALLBACK fnCallback, bool use_vad)
    {
        Model* pRecogObj = (Model*)handle;
        if (!pRecogObj)
            return nullptr;
 
        Audio audio(1);
        if (!audio.loadpcmwav(szFileName, &sampling_rate))
            return nullptr;
        if(use_vad){
            audio.split(pRecogObj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        FUNASR_RECOG_RESULT* pResult = new FUNASR_RECOG_RESULT;
        pResult->snippet_time = audio.get_time_len();
        int nStep = 0;
        int nTotal = audio.get_queue_size();
        while (audio.fetch(buff, len, flag) > 0) {
            string msg = pRecogObj->forward(buff, len, flag);
            pResult->msg += msg;
            nStep++;
            if (fnCallback)
                fnCallback(nStep, nTotal);
        }
 
        return pResult;
    }
 
    _FUNASRAPI FUNASR_RESULT FunASRRecogFile(FUNASR_HANDLE handle, const char* szWavfile, FUNASR_MODE Mode, QM_CALLBACK fnCallback, bool use_vad)
    {
        Model* pRecogObj = (Model*)handle;
        if (!pRecogObj)
            return nullptr;
        
        int32_t sampling_rate = -1;
        Audio audio(1);
        if(!audio.loadwav(szWavfile, &sampling_rate))
            return nullptr;
        if(use_vad){
            audio.split(pRecogObj);
        }
 
        float* buff;
        int len;
        int flag = 0;
        int nStep = 0;
        int nTotal = audio.get_queue_size();
        FUNASR_RECOG_RESULT* pResult = new FUNASR_RECOG_RESULT;
        pResult->snippet_time = audio.get_time_len();
        while (audio.fetch(buff, len, flag) > 0) {
            string msg = pRecogObj->forward(buff, len, flag);
            pResult->msg+= msg;
            nStep++;
            if (fnCallback)
                fnCallback(nStep, nTotal);
        }
    
        return pResult;
    }
 
    _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 nIndex)
    {
        FUNASR_RECOG_RESULT * pResult = (FUNASR_RECOG_RESULT*)Result;
        if(!pResult)
            return nullptr;
 
        return pResult->msg.c_str();
    }
 
    _FUNASRAPI void FunASRFreeResult(FUNASR_RESULT Result)
    {
        if (Result)
        {
            delete (FUNASR_RECOG_RESULT*)Result;
        }
    }
 
    _FUNASRAPI void FunASRUninit(FUNASR_HANDLE handle)
    {
        Model* pRecogObj = (Model*)handle;
 
        if (!pRecogObj)
            return;
 
        delete pRecogObj;
    }
 
#ifdef __cplusplus 
 
}
#endif