mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
No need to store outfile just open it when needed
This commit is contained in:
parent
8b31293600
commit
dd13c03bfc
@ -396,7 +396,6 @@ class Synthesizer {
|
|||||||
Marks current_marks;
|
Marks current_marks;
|
||||||
int32_t last_reported_mark_index;
|
int32_t last_reported_mark_index;
|
||||||
std::atomic<id_type> current_cmd_id;
|
std::atomic<id_type> current_cmd_id;
|
||||||
std::ofstream outfile;
|
|
||||||
|
|
||||||
Revokers revoker;
|
Revokers revoker;
|
||||||
std::recursive_mutex recursive_lock;
|
std::recursive_mutex recursive_lock;
|
||||||
@ -411,8 +410,8 @@ class Synthesizer {
|
|||||||
void on_cue_entered(id_type cmd_id, const winrt::hstring &label, const SpeechCue &cue);
|
void on_cue_entered(id_type cmd_id, const winrt::hstring &label, const SpeechCue &cue);
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
winrt::fire_and_forget save(id_type cmd_id, std::wstring_view const &text, bool is_ssml, std::vector<wchar_t> &&buf, std::ofstream &&outfile);
|
winrt::fire_and_forget save(id_type cmd_id, std::wstring_view const &text, bool is_ssml, std::vector<wchar_t> &&buf, std::filesystem::path path);
|
||||||
void start_save_stream(SpeechSynthesisStream const &&stream, id_type cmd_id);
|
void start_save_stream(SpeechSynthesisStream const &&stream, std::filesystem::path path, id_type cmd_id);
|
||||||
|
|
||||||
void initialize() {
|
void initialize() {
|
||||||
synth = SpeechSynthesizer();
|
synth = SpeechSynthesizer();
|
||||||
@ -438,7 +437,6 @@ class Synthesizer {
|
|||||||
current_text_storage = std::vector<wchar_t>();
|
current_text_storage = std::vector<wchar_t>();
|
||||||
current_marks = Marks();
|
current_marks = Marks();
|
||||||
last_reported_mark_index = -1;
|
last_reported_mark_index = -1;
|
||||||
if (outfile.is_open()) outfile.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -672,14 +670,19 @@ handle_speak(id_type cmd_id, std::vector<std::wstring_view> &parts) {
|
|||||||
|
|
||||||
// Save {{{
|
// Save {{{
|
||||||
static winrt::fire_and_forget
|
static winrt::fire_and_forget
|
||||||
save_stream(SpeechSynthesisStream const &&stream, std::ofstream &&outfile, id_type cmd_id) {
|
save_stream(SpeechSynthesisStream const &&stream, std::filesystem::path path, id_type cmd_id) {
|
||||||
unsigned long long stream_size = stream.Size(), bytes_read = 0;
|
unsigned long long stream_size = stream.Size(), bytes_read = 0;
|
||||||
DataReader reader(stream);
|
DataReader reader(stream);
|
||||||
unsigned int n;
|
unsigned int n;
|
||||||
const static unsigned int chunk_size = 16 * 1024;
|
const static unsigned int chunk_size = 16 * 1024;
|
||||||
uint8_t buf[chunk_size];
|
uint8_t buf[chunk_size];
|
||||||
while (bytes_read < stream_size) {
|
std::ofstream outfile;
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
|
try {
|
||||||
|
outfile.open(path.string(), std::ios::out | std::ios::trunc);
|
||||||
|
ok = true;
|
||||||
|
} CATCH_ALL_EXCEPTIONS("Failed to create file: " + path.string(), cmd_id);
|
||||||
|
while (ok && bytes_read < stream_size) {
|
||||||
try {
|
try {
|
||||||
n = co_await reader.LoadAsync(chunk_size);
|
n = co_await reader.LoadAsync(chunk_size);
|
||||||
ok = true;
|
ok = true;
|
||||||
@ -697,26 +700,24 @@ save_stream(SpeechSynthesisStream const &&stream, std::ofstream &&outfile, id_ty
|
|||||||
if (!ok) break;
|
if (!ok) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outfile.close();
|
|
||||||
output(cmd_id, "saved", {{"size", bytes_read}});
|
output(cmd_id, "saved", {{"size", bytes_read}});
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Synthesizer::start_save_stream(SpeechSynthesisStream const &&stream, id_type cmd_id) {
|
Synthesizer::start_save_stream(SpeechSynthesisStream const &&stream, std::filesystem::path path, id_type cmd_id) {
|
||||||
std::scoped_lock sl(recursive_lock);
|
std::scoped_lock sl(recursive_lock);
|
||||||
try {
|
try {
|
||||||
save_stream(std::move(stream), std::move(outfile), cmd_id);
|
save_stream(std::move(stream), path, cmd_id);
|
||||||
} CATCH_ALL_EXCEPTIONS("Failed to save loaded stream", cmd_id);
|
} CATCH_ALL_EXCEPTIONS("Failed to save loaded stream", cmd_id);
|
||||||
stop_current_activity();
|
stop_current_activity();
|
||||||
}
|
}
|
||||||
|
|
||||||
winrt::fire_and_forget Synthesizer::save(id_type cmd_id, std::wstring_view const &text, bool is_ssml, std::vector<wchar_t> &&buf, std::ofstream &&out) {
|
winrt::fire_and_forget Synthesizer::save(id_type cmd_id, std::wstring_view const &text, bool is_ssml, std::vector<wchar_t> &&buf, std::filesystem::path path) {
|
||||||
SpeechSynthesisStream stream{nullptr};
|
SpeechSynthesisStream stream{nullptr};
|
||||||
{ std::scoped_lock sl(recursive_lock);
|
{ std::scoped_lock sl(recursive_lock);
|
||||||
stop_current_activity();
|
stop_current_activity();
|
||||||
current_cmd_id.store(cmd_id);
|
current_cmd_id.store(cmd_id);
|
||||||
current_text_storage = std::move(buf);
|
current_text_storage = std::move(buf);
|
||||||
outfile = std::move(out);
|
|
||||||
synth.Options().IncludeSentenceBoundaryMetadata(false);
|
synth.Options().IncludeSentenceBoundaryMetadata(false);
|
||||||
synth.Options().IncludeWordBoundaryMetadata(false);
|
synth.Options().IncludeWordBoundaryMetadata(false);
|
||||||
}
|
}
|
||||||
@ -729,7 +730,7 @@ winrt::fire_and_forget Synthesizer::save(id_type cmd_id, std::wstring_view const
|
|||||||
if (ok) {
|
if (ok) {
|
||||||
if (main_loop_is_running.load()) {
|
if (main_loop_is_running.load()) {
|
||||||
try {
|
try {
|
||||||
sx.start_save_stream(std::move(stream), cmd_id);
|
sx.start_save_stream(std::move(stream), path, cmd_id);
|
||||||
} CATCH_ALL_EXCEPTIONS("Failed to load synthesized stream for save", cmd_id);
|
} CATCH_ALL_EXCEPTIONS("Failed to load synthesized stream for save", cmd_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -752,10 +753,8 @@ handle_save(id_type cmd_id, std::vector<std::wstring_view> &parts) {
|
|||||||
*((wchar_t*)text.data() + text.size()) = 0; // ensure NULL termination
|
*((wchar_t*)text.data() + text.size()) = 0; // ensure NULL termination
|
||||||
auto filename = join(parts);
|
auto filename = join(parts);
|
||||||
auto path = std::filesystem::absolute(filename);
|
auto path = std::filesystem::absolute(filename);
|
||||||
std::ofstream outfile(path.string(), std::ios::out | std::ios::trunc);
|
|
||||||
if (!outfile.good()) throw "Failed to create: " + path.string();
|
|
||||||
output(cmd_id, "saving", {{"ssml", is_ssml}, {"output_path", path.string()}});
|
output(cmd_id, "saving", {{"ssml", is_ssml}, {"output_path", path.string()}});
|
||||||
sx.save(cmd_id, text, is_ssml, std::move(buf), std::move(outfile));
|
sx.save(cmd_id, text, is_ssml, std::move(buf), path);
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user