-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
335 lines (273 loc) · 8.73 KB
/
test.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "picoapi.h"
#include <alsa/asoundlib.h>
const int MEM_SIZE = 1024 * 1024 * 5;
const int BUF_SIZE = 160000;
void playback(short *buf, int len) {
int i;
int err;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_open (&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
"default",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
// int rate = 44100;
int rate = 16000;
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
printf("Playing at rate: %d\n", rate);
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 1)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_writei (playback_handle, buf, len)) != len) {
fprintf (stderr, "write to audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_close (playback_handle);
}
// Error handling.
#define CHECKED_SYS(RET) checked_sys(system, RET, 0)
#define CHECKED_SYS_REC(RET) checked_sys(system, RET, depth + 1)
void checked_sys(pico_System system, pico_Status ret, int depth) {
// We are going to just assume call related to warnings don't error,
// since missing warnings isn't the end of the world anyways
int num_warnings = 0;
pico_getNrSystemWarnings(
system,
&num_warnings
);
for( pico_Int32 i = 0; i < num_warnings; i++ ) {
pico_Retstring message;
pico_Status code;
pico_getSystemWarning(
system,
i,
&code,
message
);
fprintf(stderr, "Pico warning %d: %s\n", code, message);
}
if( ret != 0 ) {
// Recursive error handling may not be the best idea ever
if( depth > 3 ) {
fprintf(stderr, "Recursive errors\n");
exit(1);
}
pico_Retstring message;
checked_sys(
system,
pico_getSystemStatusMessage(
system,
ret,
message
),
depth + 1
);
fprintf(stderr, "pico error %d: %s\n", ret, message);
exit(1);
}
}
// This is all just copy and pasted from the above with system replaced by engine.
#define CHECKED_ENGINE(RET) checked_engine(engine, RET, 0)
#define CHECKED_ENGINE_REC(RET) checked_engine(engine, RET, depth + 1)
void checked_engine(pico_Engine engine, pico_Status ret, int depth) {
// We are going to just assume call related to warnings don't error,
// since missing warnings isn't the end of the world anyways
int num_warnings = 0;
pico_getNrEngineWarnings(
engine,
&num_warnings
);
for( pico_Int32 i = 0; i < num_warnings; i++ ) {
pico_Retstring message;
pico_Status code;
pico_getEngineWarning(
engine,
i,
&code,
message
);
fprintf(stderr, "Pico warning %d: %s\n", code, message);
}
if( ret != 0 ) {
// Recursive error handling may not be the best idea ever
if( depth > 3 ) {
fprintf(stderr, "Recursive errors\n");
exit(1);
}
pico_Retstring message;
checked_engine(
engine,
pico_getEngineStatusMessage(
engine,
ret,
message
),
depth + 1
);
fprintf(stderr, "pico error %d: %s\n", ret, message);
exit(1);
}
}
int main(int argc, char **argv) {
char *text;
if( argc > 1 ) {
text = argv[1];
}
else {
text = "Hello world!";
}
pico_System system;
// Pico asks us to initialize memory, and won't otherwise allocate memory.
// Unfortunately it doesn't tell us exactly how much memory it's going to need.
// The documentation suggests they had a target of 200KB when creating it,
// but it crashes at 1MB. So we just give it 5MB.
CHECKED_SYS(pico_initialize(
malloc(MEM_SIZE),
MEM_SIZE,
// Defined as an 'out' variable in the header, this initializes system
&system
));
// Resource files include the actual data needed for tts.
// "Text analysis" lingware resource file
pico_Resource res_ta;
CHECKED_SYS(pico_loadResource(
system,
"lang/en-GB_ta.bin",
&res_ta
));
// For whatever reason the resource is sometimes referred to by name instead
// of by handle
pico_Retstring res_ta_name;
CHECKED_SYS(pico_getResourceName(
system,
res_ta,
res_ta_name
));
printf("Loaded resource with name: '%s'\n", res_ta_name);
// "Signal generation" lingware resource file
pico_Resource res_sg;
CHECKED_SYS(pico_loadResource(
system,
"lang/en-GB_kh0_sg.bin",
&res_sg
));
pico_Retstring res_sg_name;
CHECKED_SYS(pico_getResourceName(
system,
res_sg,
res_sg_name
));
printf("Loaded resource with name: '%s'\n", res_sg_name);
// This creates a name which we can use for later reference
CHECKED_SYS(pico_createVoiceDefinition(
system,
"PicoVoice"
));
// This attatches voice data to the name
CHECKED_SYS(pico_addResourceToVoiceDefinition(
system,
"PicoVoice",
res_ta_name
));
CHECKED_SYS(pico_addResourceToVoiceDefinition(
system,
"PicoVoice",
res_sg_name
));
// Note that if you incorrectly load resourceData
// (i.e. only load ta) this segfautls in the error
// handling code, but gdb will show you the error
// message if you compile everything with debugging
// symbols.
pico_Engine engine;
CHECKED_SYS(pico_newEngine(
system,
"PicoVoice",
&engine
));
pico_Int16 bytes_copied;
CHECKED_ENGINE(pico_putTextUtf8(
engine,
text,
strlen(text) + 1,
&bytes_copied
));
printf("bytes copied: %d/%d\n", bytes_copied, strlen(text) + 1);
FILE *audio_f = fopen("/dev/audio", "w");
short *buf = malloc(BUF_SIZE * 4);
int buf_len = 0;
pico_Int16 bytes_received = 0;
pico_Int16 dtype = 0;
pico_Status ret;
do {
ret = pico_getData(
engine,
buf + buf_len,
1000,
&bytes_received,
&dtype
);
// We are assuming that we get shorts back... which
// is currently the only supported dtype anyways but
// we might as well check.
if( dtype != PICO_DATA_PCM_16BIT ) {
fprintf(stderr, "Unknown dtype\n");
exit(1);
}
buf_len += bytes_received / 2;
// The 3 * part is just to try and make sure we will have space
// for the next batch of bytes...
if( BUF_SIZE - buf_len - 3 * bytes_received < 0 ) {
printf("Early finish, %d\n", buf_len);
playback(buf, buf_len);
buf_len = 0;
}
}
while( ret == PICO_STEP_BUSY );
playback(buf, buf_len);
// We could cleanup here, but program termination will do it for us anyways.
return 0;
}