@@ -478,6 +478,32 @@ std::string trim_words(const std::string& input, const std::string& separator, s
478478 return result.str ();
479479}
480480
481+ static std::string TruncateToFirstNumberWords (const std::string& input, int limit) {
482+ static const std::regex wordRegex (R"( \b[\w'-]+\b)" );
483+ std::sregex_iterator words_begin (input.begin (), input.end (), wordRegex);
484+ std::sregex_iterator words_end;
485+ int count = 0 ;
486+ std::size_t cutoffPos = std::string::npos;
487+ if (limit<=0 )
488+ {
489+ return " " ;
490+ }
491+ for (auto it = words_begin; it != words_end; ++it) {
492+ ++count;
493+ if (count >= limit) {
494+ // position AFTER the last matched word
495+ cutoffPos = it->position () + it->length ();
496+ break ;
497+ }
498+ }
499+ if (cutoffPos == std::string::npos) {
500+ // fewer than N words, return original
501+ return input;
502+ }
503+ // Preserve everything up to and including the Nth word
504+ return input.substr (0 , cutoffPos);
505+ }
506+
481507static llama_context * ttc_ctx = nullptr ; // text to codes ctx
482508static llama_context * cts_ctx = nullptr ; // codes to speech
483509
@@ -562,6 +588,7 @@ bool ttstype_load_model(const tts_load_model_inputs inputs)
562588 }
563589
564590 ttsdebugmode = inputs.debugmode ;
591+ tts_max_len = inputs.ttsmaxlen ;
565592
566593 // tts init
567594 if (is_ttscpp_file) {
@@ -577,8 +604,6 @@ bool ttstype_load_model(const tts_load_model_inputs inputs)
577604
578605 nthreads = inputs.threads ;
579606
580- tts_max_len = inputs.ttsmaxlen ;
581-
582607 tts_model_params.use_mmap = false ;
583608 tts_model_params.use_mlock = false ;
584609 tts_model_params.n_gpu_layers = inputs.gpulayers ; // offload if possible
@@ -692,6 +717,11 @@ static tts_generation_outputs ttstype_generate_ttscpp(const tts_generation_input
692717 }
693718 }
694719
720+ if (tts_max_len>0 )
721+ {
722+ prompt = TruncateToFirstNumberWords (prompt,tts_max_len);
723+ }
724+
695725 if (ttsdebugmode==1 && !tts_is_quiet)
696726 {
697727 printf (" \n Using Speaker ID: %d, Voice: %s" , speaker_seed, voiceused.c_str ());
0 commit comments