@@ -57,7 +57,7 @@ static bool g_started = false;
57
57
static bool g_stopped = false ;
58
58
static bool g_killed = false ;
59
59
60
- static void AudioGeneratedCallback (const float *s, int32_t n) {
60
+ static int32_t AudioGeneratedCallback (const float *s, int32_t n) {
61
61
if (n > 0 ) {
62
62
Samples samples;
63
63
samples.data = std::vector<float >{s, s + n};
@@ -66,6 +66,10 @@ static void AudioGeneratedCallback(const float *s, int32_t n) {
66
66
g_buffer.samples .push (std::move (samples));
67
67
g_started = true ;
68
68
}
69
+ if (g_killed) {
70
+ return 0 ;
71
+ }
72
+ return 1 ;
69
73
}
70
74
71
75
static int PlayCallback (const void * /* in*/ , void *out,
@@ -324,6 +328,7 @@ BEGIN_MESSAGE_MAP(CNonStreamingTextToSpeechDlg, CDialogEx)
324
328
ON_WM_PAINT()
325
329
ON_WM_QUERYDRAGICON()
326
330
ON_BN_CLICKED(IDOK, &CNonStreamingTextToSpeechDlg::OnBnClickedOk)
331
+ ON_BN_CLICKED(IDC_STOP, &CNonStreamingTextToSpeechDlg::OnBnClickedStop)
327
332
END_MESSAGE_MAP()
328
333
329
334
@@ -492,11 +497,18 @@ void CNonStreamingTextToSpeechDlg::Init() {
492
497
if (tts_) {
493
498
SherpaOnnxDestroyOfflineTts (tts_);
494
499
}
500
+ if (generate_thread_ && generate_thread_->joinable ()) {
501
+ generate_thread_->join ();
502
+ }
503
+
504
+ if (play_thread_ && play_thread_->joinable ()) {
505
+ play_thread_->join ();
506
+ }
495
507
}
496
508
497
509
498
510
static std::string ToString (const CString &s) {
499
- CT2CA pszConvertedAnsiString ( s);
511
+ CT2CA pszConvertedAnsiString (s);
500
512
return std::string (pszConvertedAnsiString);
501
513
}
502
514
@@ -510,7 +522,7 @@ void CNonStreamingTextToSpeechDlg::OnBnClickedOk() {
510
522
}
511
523
512
524
speed_.GetWindowText (s);
513
- float speed = static_cast <float >(_ttof (s));
525
+ float speed = static_cast <float >(_ttof (s));
514
526
if (speed < 0 ) {
515
527
AfxMessageBox (Utf8ToUtf16 (" Please input a valid speed" ).c_str (), MB_OK);
516
528
return ;
@@ -541,28 +553,40 @@ void CNonStreamingTextToSpeechDlg::OnBnClickedOk() {
541
553
// for simplicity
542
554
play_thread_ = std::make_unique<std::thread>(StartPlayback, SherpaOnnxOfflineTtsSampleRate (tts_));
543
555
544
- generate_btn_.EnableWindow (FALSE );
545
-
546
- const SherpaOnnxGeneratedAudio *audio =
547
- SherpaOnnxOfflineTtsGenerateWithCallback (tts_, ss.c_str (), speaker_id, speed, &AudioGeneratedCallback);
548
-
549
- generate_btn_.EnableWindow (TRUE );
556
+ if (generate_thread_ && generate_thread_->joinable ()) {
557
+ generate_thread_->join ();
558
+ }
550
559
551
560
output_filename_.GetWindowText (s);
552
561
std::string filename = ToString (s);
553
562
554
- int ok = SherpaOnnxWriteWave (audio-> samples , audio-> n , audio-> sample_rate ,
555
- filename. c_str ()) ;
563
+ generate_thread_ = std::make_unique<std::thread>([ss, this ,filename, speaker_id, speed]() {
564
+ std::string text = ss ;
556
565
557
- SherpaOnnxDestroyOfflineTtsGeneratedAudio (audio );
566
+ // generate_btn_.EnableWindow(FALSE );
558
567
559
- if (ok) {
560
- // AfxMessageBox(Utf8ToUtf16(std::string("Saved to ") + filename + " successfully").c_str(), MB_OK);
561
- AppendLineToMultilineEditCtrl (my_hint_, std::string (" Saved to " ) + filename + " successfully" );
562
- } else {
563
- // AfxMessageBox(Utf8ToUtf16(std::string("Failed to save to ") + filename).c_str(), MB_OK);
564
- AppendLineToMultilineEditCtrl (my_hint_, std::string (" Failed to saved to " ) + filename);
565
- }
568
+ const SherpaOnnxGeneratedAudio *audio =
569
+ SherpaOnnxOfflineTtsGenerateWithCallback (tts_, text.c_str (), speaker_id, speed, &AudioGeneratedCallback);
570
+ // generate_btn_.EnableWindow(TRUE);
571
+ g_stopped = true ;
572
+
573
+ int ok = SherpaOnnxWriteWave (audio->samples , audio->n , audio->sample_rate ,
574
+ filename.c_str ());
575
+
576
+ SherpaOnnxDestroyOfflineTtsGeneratedAudio (audio);
577
+
578
+ if (ok) {
579
+ // AfxMessageBox(Utf8ToUtf16(std::string("Saved to ") + filename + " successfully").c_str(), MB_OK);
580
+
581
+ // AppendLineToMultilineEditCtrl(my_hint_, std::string("Saved to ") + filename + " successfully");
582
+ } else {
583
+ // AfxMessageBox(Utf8ToUtf16(std::string("Failed to save to ") + filename).c_str(), MB_OK);
584
+
585
+ // AppendLineToMultilineEditCtrl(my_hint_, std::string("Failed to saved to ") + filename);
586
+ }
587
+ });
566
588
567
589
// CDialogEx::OnOK();
568
590
}
591
+
592
+ void CNonStreamingTextToSpeechDlg::OnBnClickedStop () { g_killed = true ; }
0 commit comments