Curie/Component/TTSWindow.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 126 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use Renard::Incunabula::Common::Setup;
2 package Renard::Curie::Component::TTSWindow;
3 # ABSTRACT: Component used to control speech synthesis
4 $Renard::Curie::Component::TTSWindow::VERSION = '0.005';
5 use Moo;
6 use Module::Load;
7 use Renard::Block::NLP;
8 use Renard::Incunabula::Common::Types qw(InstanceOf Bool Str);
9 use List::AllUtils qw(first);
10 use IO::Async::Function;
11 use Try::Tiny;
12  
13 use Pango;
14  
15 has view_manager => (
16 is => 'ro',
17 required => 1,
18 isa => InstanceOf['Renard::Curie::ViewModel::ViewManager'],
19 handles => {
20 view => current_view =>,
21 },
22 );
23  
24 has synth_param => (
25 is => 'lazy', # _build_synth_param
26 );
27  
28 has synth_function => (
29 is => 'lazy',
30 );
31  
32 classmethod can_load_speech_synthesis() {
33 if($^O eq 'linux') {
34 return try {
35 load 'Speech::Synthesis';
36 1;
37 } catch {
38 0;
39 };
40 }
41  
42 return 0;
43 }
44  
45 method BUILD(@) {
46 return unless $self->can_load_speech_synthesis;
47  
48 $self->builder->get_object('tts-window')
49 ->signal_connect(
50 'delete-event'
51 # TODO do nothing for now
52 => sub { undef; } );
53 $self->builder->get_object('button-play')
54 ->signal_connect(
55 clicked =>
56 \&on_clicked_button_play_cb, $self );
57 $self->builder->get_object('button-next')
58 ->signal_connect(
59 clicked =>
60 \&on_clicked_button_next_cb, $self );
61 $self->builder->get_object('button-previous')
62 ->signal_connect(
63 clicked =>
64 \&on_clicked_button_previous_cb, $self );
65  
66 $self->builder->get_object('tts-text')
67 ->modify_font(Pango::FontDescription->from_string('Monospace 32'));
68  
69 }
70  
71 method show_all() {
72 return unless $self->can_load_speech_synthesis;
73  
74 $self->builder->get_object('tts-window')->show_all;
75 }
76  
77 method speak( (Str) $text ) { # uncoverable subroutine
78 $self->synth->speak($text); # uncoverable statement
79 }
80  
81 callback on_clicked_button_play_cb( $button, $self ) {
82 $self->view_manager->tts_playing( ! $self->view_manager->tts_playing );
83 $self->builder->get_object('button-play')
84 ->set_label(
85 $self->view_manager->tts_playing
86 ? 'gtk-media-pause'
87 : 'gtk-media-play'
88 );
89 $self->update;
90 }
91  
92 method update() {
93 return unless defined $self->view_manager->current_document;
94 my $text = $self->view_manager->current_text_page;
95 $self->builder->get_object('label-sentence-count')
96 ->set_text(
97 "@{[ @$text == 0 ? 0 : $self->view_manager->current_sentence_number + 1 ]} / @{[ scalar @$text ]}"
98 );
99 my $current_sentence_text =
100 $text->[$self->view_manager->current_sentence_number]{sentence} // '';
101 $self->builder->get_object('tts-text')
102 ->get_buffer
103 ->set_text($current_sentence_text);
104 if( $self->view_manager->tts_playing && @$text > 0 ) {
105 # NOTE This error occurs if you send UTF-8:
106 # *** Wide character in syswrite at .../Festival/Client/Async.pm line 127.
107  
108 my $preproc_tts = Renard::Block::NLP::preprocess_for_tts(
109 "" . $current_sentence_text
110 );
111 $self->synth_function->call(
112 args => [
113 $self->synth_param,
114 $preproc_tts,
115 ],
116 on_result => sub {
117 Glib::Timeout->add(0, sub {
118 $self->view_manager->choose_next_sentence;
119 $self->update;
120 return 0;
121 });
122 },
123 );
124 }
125 }
126  
127 callback on_clicked_button_previous_cb( $button, $self ) {
128 $self->view_manager->choose_previous_sentence;
129 $self->update;
130 }
131  
132 callback on_clicked_button_next_cb( $button, $self ) {
133 $self->view_manager->choose_next_sentence;
134 $self->update;
135 }
136  
137 sub _build_synth_param {
138 # no $self : subprocess
139 my $engine;
140 my $preferred_voice_name;
141 if( $^O eq 'linux' ) {
142 $engine = 'Festival';
143 $preferred_voice_name = 'nitech_us_awb_arctic_hts';
144 } elsif( $^O eq 'darwin' ) {
145 $engine = "MacSpeech";
146 } elsif( $^O eq 'MSWin32' ) {
147 $engine = 'SAPI5';
148 $preferred_voice_name = 'Microsoft Zira Desktop';
149 }
150 my @voices = Speech::Synthesis->InstalledVoices(engine => $engine);
151 my @avatars = Speech::Synthesis->InstalledAvatars(engine => $engine);
152 my $voice = ( first {
153 $_->{name} eq $preferred_voice_name
154 } @voices ) // $voices[-1];
155 my %params = (
156 engine => $engine,
157 avatar => undef,
158 language => $voice->{language},
159 voice => $voice->{id},
160 async => 0
161 );
162 \%params;
163 }
164  
165 sub _build_synth_function {
166 # no $self : subprocess
167 IO::Async::Function->new(
168 code => sub {
169 my ( $synth_param, $text ) = @_;
170 Speech::Synthesis
171 ->new(%$synth_param)
172 ->speak($text);
173 }
174 );
175 }
176  
177  
178 with qw(
179 Intertangle::API::Gtk3::Component::Role::FromBuilder
180 Intertangle::API::Gtk3::Component::Role::UIFileFromPackageName
181 Renard::Curie::Component::Role::HasParentMainWindow
182 );
183  
184 1;
185  
186 __END__