// SPDX-License-Identifier: Apache-2.0 #pragma once #include #include namespace nemo_speech::tts { // Retain previous encoder outputs across short windows. A later chunk may need // more history than the immediately preceding window happened to contain. inline bool append_longform_text_context( const std::vector& text_context, int text_len, int current_chunk_len, int n_embd, int max_history_tokens, std::vector& history) { if (n_embd <= 0 || max_history_tokens <= 0 || current_chunk_len < 0 || text_len < current_chunk_len || text_context.size() != static_cast(text_len) * n_embd || history.size() % n_embd != 0) { return false; } const size_t maximum = static_cast(max_history_tokens) * n_embd; const size_t added = static_cast(current_chunk_len) * n_embd; if (added >= maximum) { history.assign(text_context.end() - maximum, text_context.end()); } else { const size_t keep = maximum - added; if (history.size() > keep) history.erase(history.begin(), history.end() - keep); history.insert(history.end(), text_context.end() - added, text_context.end()); } return true; } } // namespace nemo_speech::tts