-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
vosk_bindings.c
211 lines (167 loc) · 6.52 KB
/
vosk_bindings.c
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
#define CAML_NAME_SPACE
#include "include/vosk_api.h"
#include "include/jsmn.h"
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <string.h>
#include <stdio.h>
VoskModel *model;
int write_vosk_json_to_files(const char* vosk_json, const char* text_s, const char* timestamp_s, int total_chars_printed);
CAMLprim value make_model(value str_model) {
vosk_set_log_level(-1);
model = vosk_model_new(String_val(str_model));
return Val_int(0);
}
CAMLprim value delete_model(value int_v) {
vosk_model_free(model);
return Val_int(0);
}
CAMLprim value total_duration(value audio_file, value duration_file) {
FILE* wavin = fopen(String_val(audio_file), "rb");
fseek(wavin, 0, SEEK_END);
int size = ftell(wavin);
FILE *total_dur = fopen(String_val(duration_file), "w");
fprintf(total_dur, "%d\n", size);
fclose(total_dur);
return Val_int(0);
}
CAMLprim value transcribe(value audio,
value transcript,
value timestamp,
value total_duration,
value current_duration) {
FILE *wavin, *output;
char buf[3200];
int nread, final;
VoskRecognizer *recognizer = vosk_recognizer_new(model, 16000.0);
vosk_recognizer_set_words(recognizer, 1);
wavin = fopen(String_val(audio), "rb");
output = fopen(String_val(transcript), "w");
FILE *current_dur = fopen(String_val(current_duration), "w");
fseek(wavin, 0, SEEK_SET);
int chars_printed = 0;
fseek(wavin, 44, SEEK_SET);
while (!feof(wavin)) {
nread = fread(buf, 1, sizeof(buf), wavin);
final = vosk_recognizer_accept_waveform(recognizer, buf, nread);
if (final) {
chars_printed = write_vosk_json_to_files(vosk_recognizer_final_result(recognizer),
String_val(transcript),
String_val(timestamp),
chars_printed);
} else {
/* fprintf(output, "%s\n", vosk_recognizer_partial_result(recognizer)); */
}
fseek(current_dur, 0, SEEK_SET);
fprintf(current_dur, "%ld\n", ftell(wavin));
}
/* fprintf(output, "%s\n", vosk_recognizer_final_result(recognizer)); */
fseek(current_dur, 0, SEEK_SET);
/* fprintf(current_dur, "%s\n", "DONE"); */
vosk_recognizer_free(recognizer);
fclose(wavin);
fclose(current_dur);
return Val_int(1);
}
/* Writes the json that vosk outputs to the cached files.
Parses inputs formatted like this:
{
"result" : [{
"conf" : 0.280911,
"end" : 7.408000,
"start" : 7.348000,
"word" : "i"
}, {
"conf" : 0.280911,
"end" : 7.557229,
"start" : 7.408000,
"word" : "think"
}]
}
*/
int jsoneq (const char* vosk_json, jsmntok_t* tokens, int tok_index, const char *s) {
jsmntok_t tok = tokens[tok_index];
if ((tok.type == JSMN_STRING)
&& ((int)strlen(s) == tok.end - tok.start)
&& (strncmp(vosk_json + tok.start, s, tok.end - tok.start) == 0)) {
return 1;
}
return 0;
}
int write_vosk_json_to_files(const char* vosk_json, const char* text_s, const char* timestamp_s, int total_chars_printed) {
const int VOSK_JSON_MAX_TOKENS = 1024;
jsmntok_t tokens[VOSK_JSON_MAX_TOKENS];
FILE *text = fopen(text_s, "a");
FILE *timestamp = fopen(timestamp_s, "a");
int tokens_read;
{
jsmn_parser parser;
jsmn_init(&parser);
tokens_read = jsmn_parse(&parser, vosk_json, strlen(vosk_json), tokens, VOSK_JSON_MAX_TOKENS);
if ((tokens_read <= 0) || (tokens[0].type != JSMN_OBJECT)) {
printf("%s\n", "JSON PARSE FAILURE. EXITING");
exit(1);
}
}
char conversion_buffer[16]; // small buffer to put strings from vosk into while converting them to timestamp format.
for (int i = 1; i < tokens_read; i++) {
/* SKIP IRRELEVANT FIELDS */
if (jsoneq(vosk_json, tokens, i, "result")) {
if (tokens[i + 1].type != JSMN_ARRAY) {
printf("%s\n", "JSON PARSE FAILURE. EXITING");
exit(1);
}
/* LOOP OVER WORD STRUCTS */
for (int j = 0, obj = 0; obj < tokens[i + 1].size; j++) {
if (tokens[i + 1 + j].type != JSMN_OBJECT) {
continue; // shouldn't happen
}
/* LOOP THROUGH STRUCT FIELDS */
for (int k = 0, field = 0; field < tokens[i + 1 + j].size; k++) {
if (jsoneq(vosk_json, tokens, i + 1 + j + 1 + k, "word")) {
jsmntok_t* word_token = &tokens[i + 1 + j + 1 + k + 1];
const char* WRITE_FORMAT = "%.*s ";
char format_buf[500];
snprintf(format_buf, 500, WRITE_FORMAT, word_token->end-word_token->start, vosk_json+word_token->start);
fprintf(text, "%s", format_buf);
total_chars_printed += strlen(format_buf);
/* printf("just printed||%s||, the total chars is now %d", format_buf, total_chars_printed); */
}
if (jsoneq(vosk_json, tokens, i + 1 + j + 1 + k, "start")) {
jsmntok_t* start_timestamp_token = &tokens[i + 1 + j + 1 + k + 1];
/* SLICE OUT NUMBER FROM VOSK OUTPUT */
strncpy(conversion_buffer, vosk_json+start_timestamp_token->start, start_timestamp_token->end-start_timestamp_token->start);
conversion_buffer[start_timestamp_token->end-start_timestamp_token->start] = '\0';
/* CONSTANTS FOR TIME UNITS */
const int SECOND = 1;
const int MINUTE = 60*SECOND;
const int HOUR = 60*MINUTE;
/* GET TIME UNITS FROM JSON STRING */
float raw = atof(conversion_buffer);
int total_seconds = (int)raw; if (total_seconds > raw) total_seconds--; // fast replacement for floor()
int hours = total_seconds / HOUR;
total_seconds = total_seconds % HOUR;
int minutes = total_seconds / MINUTE;
total_seconds = total_seconds % MINUTE;
int seconds = total_seconds;
/* FORMAT AS TIMESTAMP */
if (hours > 0) {
fprintf(timestamp, "[%.2i:%.2i:%.2i](%d)", hours, minutes, seconds, total_chars_printed);
} else {
fprintf(timestamp, "[%.2i:%.2i](%d)", minutes, seconds, total_chars_printed);
}
}
/* ignoring end time and confidence values for now... */
field++;
k++; // skip
}
obj++;
j += tokens[i + 1 + j].size;
}
break; // break if we already looped over results;
}
}
fclose(text);
fclose(timestamp);
return total_chars_printed;
}