librapidasrapi.cpp 4.03 KB
Newer Older
mayong's avatar
mayong committed
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
#include "precomp.h"
#ifdef __cplusplus 



//  void __attribute__ ((visibility ("default"))) fun();
extern "C" {
#endif


	// APIs for qmasr
	_RAPIDASRAPI RPASR_HANDLE  RapidAsrInit(const char* szModelDir, int nThreadNum)
	{

#ifdef NDEBUG
		QMLIC_BOOL bMatched = QmLicCheckValid(QLFM_ASR);
		if (!bMatched) {
			return nullptr;
		}
#endif

		Model* mm = create_model(szModelDir, nThreadNum); 

		return mm;
	}


	_RAPIDASRAPI RPASR_RESULT RapidAsrRecogBuffer(RPASR_HANDLE handle, const char* szBuf, int nLen, RPASR_MODE Mode, QM_CALLBACK fnCallback)
	{


		Model* pRecogObj = (Model*)handle;

		if (!pRecogObj)
			return nullptr;

		Audio audio(1);
		audio.loadwav(szBuf,nLen);
		audio.split();

		float* buff;
		int len;
		int flag=0;
		RPASR_RECOG_RESULT* pResult = new RPASR_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) {
			pRecogObj->reset();
			string msg = pRecogObj->forward(buff, len, flag);
			pResult->msg += msg;
			nStep++;
			if (fnCallback)
				fnCallback(nStep, nTotal);
		}


		return pResult;
	}

	_RAPIDASRAPI RPASR_RESULT RapidAsrRecogPCMBuffer(RPASR_HANDLE handle, const char* szBuf, int nLen, RPASR_MODE Mode, QM_CALLBACK fnCallback)
	{

		Model* pRecogObj = (Model*)handle;

		if (!pRecogObj)
			return nullptr;

		Audio audio(1);
		audio.loadpcmwav(szBuf, nLen);
		audio.split();

		float* buff;
		int len;
		int flag = 0;
		RPASR_RECOG_RESULT* pResult = new RPASR_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) {
			pRecogObj->reset();
			string msg = pRecogObj->forward(buff, len, flag);
			pResult->msg += msg;
			nStep++;
			if (fnCallback)
				fnCallback(nStep, nTotal);
		}


		return pResult;

	}

	_RAPIDASRAPI RPASR_RESULT RapidAsrRecogPCMFile(RPASR_HANDLE handle, const char* szFileName, RPASR_MODE Mode, QM_CALLBACK fnCallback)
	{

		Model* pRecogObj = (Model*)handle;

		if (!pRecogObj)
			return nullptr;

		Audio audio(1);
		audio.loadpcmwav(szFileName);
		audio.split();

		float* buff;
		int len;
		int flag = 0;
		RPASR_RECOG_RESULT* pResult = new RPASR_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) {
			pRecogObj->reset();
			string msg = pRecogObj->forward(buff, len, flag);
			pResult->msg += msg;
			nStep++;
			if (fnCallback)
				fnCallback(nStep, nTotal);
		}


		return pResult;

	}

	_RAPIDASRAPI RPASR_RESULT RapidAsrRecogFile(RPASR_HANDLE handle, const char* szWavfile, RPASR_MODE Mode, QM_CALLBACK fnCallback)
	{
		Model* pRecogObj = (Model*)handle;

		if (!pRecogObj)
			return nullptr;

		Audio audio(1);
		if(!audio.loadwav(szWavfile))
			return nullptr;
		audio.split();

		float* buff;
		int len;
		int flag = 0;
		int nStep = 0;
		int nTotal = audio.get_queue_size();
		RPASR_RECOG_RESULT* pResult = new RPASR_RECOG_RESULT;
		pResult->snippet_time = audio.get_time_len();
		while (audio.fetch(buff, len, flag) > 0) {
			pRecogObj->reset();
			string msg = pRecogObj->forward(buff, len, flag);
			pResult->msg+= msg;
			nStep++;
			if (fnCallback)
				fnCallback(nStep, nTotal);
		}
	
	


		return pResult;
	}

	_RAPIDASRAPI const int RapidAsrGetRetNumber(RPASR_RESULT Result)
	{
		if (!Result)
			return 0;

		return 1;
		
	}


	_RAPIDASRAPI const float RapidAsrGetRetSnippetTime(RPASR_RESULT Result)
	{
		if (!Result)
			return 0.0f;

		return ((RPASR_RECOG_RESULT*)Result)->snippet_time;

	}

	_RAPIDASRAPI const char* RapidAsrGetResult(RPASR_RESULT Result,int nIndex)
	{
		RPASR_RECOG_RESULT * pResult = (RPASR_RECOG_RESULT*)Result;
		if(!pResult)
			return nullptr;

		return pResult->msg.c_str();
	
	}

	_RAPIDASRAPI void RapidAsrFreeResult(RPASR_RESULT Result)
	{

		if (Result)
		{
			delete (RPASR_RECOG_RESULT*)Result;

		}
	}

	_RAPIDASRAPI void RapidAsrUninit(RPASR_HANDLE handle)
	{

		Model* pRecogObj = (Model*)handle;


		if (!pRecogObj)
			return;

		delete pRecogObj;

	}



#ifdef __cplusplus 

}
#endif