Repository navigation
Expand file tree
/
Copy pathtext-to-speech.cpp
More file actions
125 lines (110 loc) · 4.08 KB
/
Copy pathtext-to-speech.cpp
File metadata and controls
125 lines (110 loc) · 4.08 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
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include "moonshine-cpp.h"
namespace {
void writeWav(const std::string &path, const std::vector<float> &samples,
int32_t sampleRate) {
FILE *file = std::fopen(path.c_str(), "wb");
if (!file) {
throw std::runtime_error("Failed to open output file: " + path);
}
const uint16_t num_channels = 1;
const uint16_t bits_per_sample = 16;
const uint32_t byte_rate = sampleRate * num_channels * bits_per_sample / 8;
const uint16_t block_align = num_channels * bits_per_sample / 8;
const uint32_t data_size =
static_cast<uint32_t>(samples.size()) * bits_per_sample / 8;
const uint32_t chunk_size = 36 + data_size;
// RIFF header
std::fwrite("RIFF", 1, 4, file);
std::fwrite(&chunk_size, 4, 1, file);
std::fwrite("WAVE", 1, 4, file);
// fmt chunk
std::fwrite("fmt ", 1, 4, file);
const uint32_t fmt_size = 16;
const uint16_t audio_format = 1; // PCM
std::fwrite(&fmt_size, 4, 1, file);
std::fwrite(&audio_format, 2, 1, file);
std::fwrite(&num_channels, 2, 1, file);
std::fwrite(&sampleRate, 4, 1, file);
std::fwrite(&byte_rate, 4, 1, file);
std::fwrite(&block_align, 2, 1, file);
std::fwrite(&bits_per_sample, 2, 1, file);
// data chunk
std::fwrite("data", 1, 4, file);
std::fwrite(&data_size, 4, 1, file);
for (size_t i = 0; i < samples.size(); ++i) {
float clamped = samples[i];
if (clamped > 1.0f) clamped = 1.0f;
if (clamped < -1.0f) clamped = -1.0f;
int16_t sample = static_cast<int16_t>(clamped * 32767.0f);
std::fwrite(&sample, sizeof(int16_t), 1, file);
}
std::fclose(file);
}
} // namespace
int main(int argc, char *argv[]) {
std::string asset_root = "../../core/moonshine-tts/data";
std::string language = "en_us";
std::string voice = "";
std::string text = "Hello! This is a test of the Moonshine text to speech.";
std::string output_path = "output.wav";
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-r" || arg == "--asset-root") {
asset_root = argv[++i];
} else if (arg == "-l" || arg == "--language") {
language = argv[++i];
} else if (arg == "-v" || arg == "--voice") {
voice = argv[++i];
} else if (arg == "-t" || arg == "--text") {
text = argv[++i];
} else if (arg == "-o" || arg == "--output") {
output_path = argv[++i];
} else {
std::cerr << "Unknown argument: " << arg << std::endl;
std::cerr << "Usage: " << argv[0]
<< " [-r asset_root] [-l language] [-v voice] [-t text] "
"[-o output.wav]"
<< std::endl;
return 1;
}
}
try {
std::vector<moonshine_option_t> options = {
{"g2p_root", asset_root.c_str()},
};
if (!voice.empty()) {
options.push_back({"voice", voice.c_str()});
}
std::cout << "Creating TTS synthesizer for language '" << language << "'"
<< std::endl;
moonshine::TextToSpeech tts(language, options);
moonshine::GraphemeToPhonemizer g2p(language,
{
{"g2p_root", asset_root.c_str()},
});
std::string ipa = g2p.toIpa(text);
std::cout << "IPA: " << ipa << " (" << ipa.size() << " characters)"
<< std::endl;
std::cout << "Synthesizing: \"" << text << "\"" << std::endl;
moonshine::TtsSynthesisResult result = tts.synthesize(text);
std::cout << "Got " << result.samples.size() << " samples at "
<< result.sampleRateHz << " Hz ("
<< (result.samples.size() /
static_cast<double>(result.sampleRateHz))
<< " seconds)" << std::endl;
writeWav(output_path, result.samples, result.sampleRateHz);
std::cout << "Written to " << output_path << std::endl;
} catch (const moonshine::MoonshineException &e) {
std::cerr << "Moonshine error: " << e.what() << std::endl;
return 1;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}