Skip to content

Commit a86cd98

Browse files
committed
llama-tts
* enabled optional cmdline argument '-o' on tts to specify an output filename. It defaults to 'output.wav'. * program now returns ENOENT in case of file write failure
1 parent af7747c commit a86cd98

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# NOTES
2+
NOTES-*
3+
14
# Extensions
25

36
*.a
@@ -33,7 +36,7 @@
3336
.vs/
3437
.vscode/
3538
nppBackup
36-
39+
*.code-workspace
3740

3841
# Coverage
3942

common/arg.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,13 +1858,15 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
18581858
? params.lora_outfile.c_str()
18591859
: ex == LLAMA_EXAMPLE_CVECTOR_GENERATOR
18601860
? params.cvector_outfile.c_str()
1861-
: params.out_file.c_str()),
1861+
: ex == LLAMA_EXAMPLE_TTS
1862+
? params.ttss_outfile.c_str()
1863+
: params.out_file.c_str()),
18621864
[](common_params & params, const std::string & value) {
18631865
params.out_file = value;
18641866
params.cvector_outfile = value;
18651867
params.lora_outfile = value;
18661868
}
1867-
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA}));
1869+
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_TTS}));
18681870
add_opt(common_arg(
18691871
{"-ofreq", "--output-frequency"}, "N",
18701872
string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),

common/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ struct common_params {
410410

411411
bool spm_infill = false; // suffix/prefix/middle pattern for infill
412412

413-
std::string lora_outfile = "ggml-lora-merged-f16.gguf";
413+
// default output filenames
414+
std::string
415+
lora_outfile = "ggml-lora-merged-f16.gguf",
416+
ttss_outfile = "output.wav";
414417

415418
// batched-bench params
416419
bool batched_bench_output_jsonl = false;

examples/tts/tts.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ struct wav_header {
7979
uint32_t data_size;
8080
};
8181

82-
static void save_wav16(const std::string & fname, const std::vector<float> & data, int sample_rate) {
82+
static bool save_wav16(const std::string & fname, const std::vector<float> & data, int sample_rate) {
8383
std::ofstream file(fname, std::ios::binary);
8484
if (!file) {
85-
LOG_ERR("%s: Failed to open file '%s' for writing", __func__, fname.c_str());
86-
return;
85+
LOG_ERR("%s: Failed to open file '%s' for writing.\n", __func__, fname.c_str());
86+
return false;
8787
}
8888

8989
wav_header header;
@@ -100,7 +100,8 @@ static void save_wav16(const std::string & fname, const std::vector<float> & dat
100100
file.write(reinterpret_cast<const char*>(&pcm_sample), sizeof(pcm_sample));
101101
}
102102

103-
file.close();
103+
//file.close();
104+
return file.good();
104105
}
105106

106107
static void fill_hann_window(int length, bool periodic, float * output) {
@@ -464,6 +465,8 @@ int main(int argc, char ** argv) {
464465
params.sampling.top_k = 4;
465466
params.sampling.samplers = { COMMON_SAMPLER_TYPE_TOP_K, };
466467

468+
params.out_file = params.ttss_outfile;
469+
467470
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_TTS, print_usage)) {
468471
return 1;
469472
}
@@ -951,8 +954,6 @@ lovely<|t_0.56|><|code_start|><|634|><|596|><|1766|><|1556|><|1306|><|1285|><|14
951954
}
952955
#endif
953956

954-
const std::string fname = "output.wav";
955-
956957
const int n_sr = 24000; // sampling rate
957958

958959
// zero out first 0.25 seconds
@@ -963,11 +964,18 @@ lovely<|t_0.56|><|code_start|><|634|><|596|><|1766|><|1556|><|1306|><|1285|><|14
963964
LOG_INF("%s: time for spectral ops: %.3f ms\n", __func__, (ggml_time_us() - t_spec_start) / 1000.0f);
964965
LOG_INF("%s: total time: %.3f ms\n", __func__, (ggml_time_us() - t_main_start) / 1000.0f);
965966

966-
save_wav16(fname, audio, n_sr);
967+
int retval(0);
967968

968-
LOG_INF("%s: audio written to file '%s'\n", __func__, fname.c_str());
969+
if (save_wav16(params.out_file, audio, n_sr)) {
970+
LOG_INF("%s: audio written to file '%s'\n", __func__, params.out_file.c_str());
971+
}
972+
973+
else {
974+
retval=ENOENT;
975+
LOG_ERR("Check path exists, directory write permissions, free disk space.\n");
976+
}
969977

970978
llama_backend_free();
971979

972-
return 0;
980+
return retval;
973981
}

0 commit comments

Comments
 (0)