-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeaking.cpp
62 lines (53 loc) · 1.52 KB
/
speaking.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
//
// Created by prostoichelovek on 01.05.19.
//
#include "speaking.h"
audio_player::audio_player(const std::string &path) {
if (!path.empty()) {
stream.set_backend(RHVoice::audio::backend_file);
stream.set_device(path);
}
}
bool audio_player::set_sample_rate(int sample_rate) {
try {
if (stream.is_open() && (stream.get_sample_rate() != sample_rate))
stream.close();
stream.set_sample_rate(sample_rate);
return true;
}
catch (...) {
return false;
}
}
bool audio_player::play_speech(const short *samples, size_t count) {
try {
if (!stream.is_open())
stream.open();
stream.write(samples, count);
return true;
}
catch (...) {
stream.close();
return false;
}
}
void audio_player::finish() {
if (stream.is_open())
stream.drain();
}
RHSpeaker::RHSpeaker(std::string voice) {
eng = RHVoice::smart_ptr<RHVoice::engine>(new RHVoice::engine);
setVoice(std::move(voice));
eng->quality.set_from_string("max");
eng->text_settings.stress_marker = '>';
}
void RHSpeaker::setVoice(std::string voice) {
profile = eng->create_voice_profile(voice);
}
void RHSpeaker::say(std::string text) {
std::unique_ptr<RHVoice::document> doc = RHVoice::document::create_from_plain_text(eng, text.begin(), text.end(), RHVoice::content_text,
profile);
doc->set_owner(player);
doc->synthesize();
player.finish();
}