forked from coqui-ai/STT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.i
100 lines (80 loc) · 2.97 KB
/
stt.i
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
%module model
%include "typemaps.i"
%{
#define SWIG_FILE_WITH_INIT
#include <string.h>
#include <node_buffer.h>
#include "coqui-stt.h"
using namespace v8;
using namespace node;
%}
// convert Node Buffer into a C ptr + length
%typemap(in) (short* IN_ARRAY1, int DIM1)
{
Local<Object> bufferObj = SWIGV8_TO_OBJECT($input);
char* bufferData = Buffer::Data(bufferObj);
size_t bufferLength = Buffer::Length(bufferObj);
if (bufferLength % 2 != 0) {
SWIG_exception_fail(SWIG_ERROR, "Buffer length must be even. Make sure your input audio is 16-bits per sample.");
}
$1 = ($1_ltype)bufferData;
$2 = ($2_ltype)(bufferLength / 2);
}
// apply to STT_FeedAudioContent and STT_SpeechToText
%apply (short* IN_ARRAY1, int DIM1) {(const short* aBuffer, unsigned int aBufferSize)};
// apply the buffer typemap to STT_CreateModelFromBuffer
%apply (short* IN_ARRAY1, int DIM1) {(const char *aModelBuffer, unsigned int aBufferSize)};
// make sure the string returned by SpeechToText is freed
%typemap(newfree) char* "STT_FreeString($1);";
%newobject STT_SpeechToText;
%newobject STT_IntermediateDecode;
%newobject STT_IntermediateDecodeFlushBuffers;
%newobject STT_FinishStream;
%newobject STT_Version;
%newobject STT_ErrorCodeToErrorMessage;
// convert double pointer retval in CreateModel to an output
%typemap(in, numinputs=0) ModelState **retval (ModelState *ret) {
ret = NULL;
$1 = &ret;
}
%typemap(argout) ModelState **retval {
$result = SWIGV8_ARRAY_NEW(0);
SWIGV8_AppendOutput($result, SWIG_From_int(result));
// owned by the application. NodeJS does not guarantee the finalizer will be called so applications must call FreeMetadata themselves.
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
}
// convert double pointer retval in CreateStream to an output
%typemap(in, numinputs=0) StreamingState **retval (StreamingState *ret) {
ret = NULL;
$1 = &ret;
}
%typemap(argout) StreamingState **retval {
$result = SWIGV8_ARRAY_NEW(0);
SWIGV8_AppendOutput($result, SWIG_From_int(result));
// not owned, STT_FinishStream deallocates StreamingState
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
}
%nodefaultctor ModelState;
%nodefaultdtor ModelState;
%typemap(out) TokenMetadata* %{
$result = SWIGV8_ARRAY_NEW(0);
for (int i = 0; i < arg1->num_tokens; ++i) {
SWIGV8_AppendOutput($result, SWIG_NewPointerObj(SWIG_as_voidptr(&result[i]), SWIGTYPE_p_TokenMetadata, 0));
}
%}
%typemap(out) CandidateTranscript* %{
$result = SWIGV8_ARRAY_NEW(0);
for (int i = 0; i < arg1->num_transcripts; ++i) {
SWIGV8_AppendOutput($result, SWIG_NewPointerObj(SWIG_as_voidptr(&result[i]), SWIGTYPE_p_CandidateTranscript, 0));
}
%}
%ignore Metadata::num_transcripts;
%ignore CandidateTranscript::num_tokens;
%nodefaultctor Metadata;
%nodefaultdtor Metadata;
%nodefaultctor CandidateTranscript;
%nodefaultdtor CandidateTranscript;
%nodefaultctor TokenMetadata;
%nodefaultdtor TokenMetadata;
%rename ("%(strip:[STT_])s") "";
%include "../coqui-stt.h"