-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlibc_deepspeech.cpp
95 lines (78 loc) · 2.05 KB
/
libc_deepspeech.cpp
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
#include <stdlib.h>
#include <string>
// Deepspeech 0.9.3 library header file.
#include "../libdeepspeech_0.9.3/deepspeech.h"
// This library header file.
#include "libc_deepspeech.h"
char *deepspeech_verison(void)
{
char *version = DS_Version();
return version;
}
void deepspeech_free_str(char *string)
{
if (string != NULL)
{
DS_FreeString(string);
}
}
void *create_model(char *model_path)
{
if (model_path == NULL)
{
return NULL;
}
ModelState *ctx;
int status = DS_CreateModel(model_path, &ctx);
return (void *)ctx;
}
void free_model(void *model_state)
{
if (model_state != NULL)
{
ModelState *ptr = (ModelState *)model_state;
DS_FreeModel(ptr);
}
}
uint64_t model_sample_rate(void *model_state)
{
if (model_state == NULL)
{
return 0;
}
ModelState *ptr = (ModelState *)model_state;
int sample_rate = DS_GetModelSampleRate(ptr);
return sample_rate;
}
char *speech_to_text(void *model_state, char *buffer, uint64_t buffer_size)
{
Metadata *result = DS_SpeechToTextWithMetadata((ModelState *)model_state, (short *)buffer, buffer_size / 2, 3);
const CandidateTranscript *transcript = &result->transcripts[0];
std::string retval = "";
for (int i = 0; i < transcript->num_tokens; i++)
{
const TokenMetadata &token = transcript->tokens[i];
retval += token.text;
}
char *encoded = strdup(retval.c_str());
DS_FreeMetadata(result);
return encoded;
}
int enable_external_scorer(void *model_state, char *model_path)
{
ModelState *ptr = (ModelState *)model_state;
int status = DS_EnableExternalScorer(ptr, model_path);
return status;
}
int disable_external_scorer(void *model_state)
{
ModelState *ptr = (ModelState *)model_state;
int status = DS_DisableExternalScorer(ptr);
return status;
}
int set_scorer_alpha_beta(void *model_state, float alpha, float beta)
{
ModelState *ptr = (ModelState *)model_state;
int status = DS_SetScorerAlphaBeta(ptr, alpha, beta);
return status;
}