-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExample.cpp
More file actions
56 lines (45 loc) · 1.75 KB
/
Copy pathExample.cpp
File metadata and controls
56 lines (45 loc) · 1.75 KB
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
#include <cstdio>
#include <vector>
#include "platform/lib/SharpVox.h"
using SharpVox::SharpVoxSpeaker;
using SharpVox::PhonemeEvent;
static void write_wav_streaming(const char* path, SharpVoxSpeaker& speaker, const char* text) {
FILE* f = fopen(path, "wb");
auto w32 = [f](int v) { fwrite(&v, 4, 1, f); };
auto w16 = [f](short v) { fwrite(&v, 2, 1, f); };
int sr = speaker.SampleRate;
fwrite("RIFF", 4, 1, f); w32(0);
fwrite("WAVEfmt ", 8, 1, f); w32(16); w16(1); w16(1);
w32(sr); w32(sr * 2); w16(2); w16(16);
fwrite("data", 4, 1, f); w32(0);
struct WavCtx { FILE* f; int total; };
WavCtx ctx { f, 0 };
speaker.Speak(text, [](SharpVoxSpeaker* /*speaker*/, const short* buf, int len, void* ud) {
auto* c = static_cast<WavCtx*>(ud);
fwrite(buf, 2, len, c->f);
c->total += len;
}, &ctx);
int data = ctx.total * 2;
fseek(f, 4, SEEK_SET); w32(36 + data);
fseek(f, 40, SEEK_SET); w32(data);
fclose(f);
}
static void speak_with_phonemes(SharpVoxSpeaker& speaker, const char* text) {
std::vector<short> samples;
speaker.SpeakWithEvents(text,
[](SharpVoxSpeaker* /*speaker*/, const short* buf, int len,
const PhonemeEvent* events, int32_t count, void* ud) {
auto* s = static_cast<std::vector<short>*>(ud);
s->insert(s->end(), buf, buf + len);
for (int32_t i = 0; i < count; i++) {
printf(" phoneme %d at %.3fs\n", events[i].Phoneme, events[i].TimeSeconds);
}
},
&samples);
printf("total samples: %d\n", (int)samples.size());
}
int main() {
SharpVoxSpeaker speaker;
write_wav_streaming("hello.wav", speaker, "hello world");
speak_with_phonemes(speaker, "hello world");
}