0869ed4aecae2d48e86f7b162849d342ddaca5a2
[clearscm.git] / lib / Speak.pm
1 =pod
2
3 =head1 NAME $RCSfile: Speak.pm,v $
4
5 Convert text to speach using Google's engine and play it on speakers
6
7 =head1 VERSION
8
9 =over
10
11 =item Author
12
13 Andrew DeFaria <Andrew@DeFaria.com>
14
15 =item Revision
16
17 $Revision: 1.0 $
18
19 =item Created
20
21 Wed 24 Feb 2021 11:05:36 AM PST
22
23 =item Modified
24
25
26 =back
27
28 =head1 SYNOPSIS
29
30 This module offers subroutines to convert text into speach and speak them.
31
32 =head2 DESCRIPTION
33
34 This module exports subroutines to process text to speach and speak them.
35
36 =head1 ROUTINES
37
38 The following routines are exported:
39
40 =cut
41
42 package Speak;
43
44 use strict;
45 use warnings;
46
47 use base 'Exporter';
48
49 use FindBin;
50
51 use lib "$FindBin::Bin/../lib";
52
53 use Display;
54 use Logger;
55 use Utils;
56
57 our @EXPORT = qw(speak);
58
59 sub speak (;$$) {
60   my ($msg, $log) = @_;
61
62 =pod
63
64 =head2 speak($msg, $log)
65
66 Convert $msg to speach.
67
68 Note this currently uses an external script to do the conversion. I intend to
69 re-write that into Perl here eventually.
70
71 Parameters:
72
73 =for html <blockquote>
74
75 =over
76
77 =item $msg:
78
79 Message to speak. If $msg is defined and scalar then that is the message
80 to speak. If it is a file handle then the text will be read from that file.
81 Otherwise the text in the clipboard will be used.
82
83 =item $log
84
85 If provided, errors and messages will be logged to the logfile, otherwise stdout
86
87 =back
88
89 =for html </blockquote>
90
91 Returns:
92
93 =for html <blockquote>
94
95 =over
96
97 =item Nothing
98
99 =back
100
101 =for html </blockquote>
102
103 =cut
104
105   if (-f "$FindBin::Bin/shh") {
106     if ($log) {
107       $log->msg("Not speaking because we were asked to be quiet - $msg");
108     } else {
109       verbose "Not speaking because we were asked to be quiet - $msg";
110     } # if
111
112     return;
113   } # if
114
115   # Handle the case where $msg is a filehandle
116   $msg = <$msg> if ref $msg eq 'GLOB';
117
118   # We can't have two speakers going at the same time so if we have an error
119   # backoff a little and try again.
120   my $attempts   = 0;
121   my $maxretries = 3;
122   my $backoff    = 2;
123
124   my ($status, @output);
125
126   while ($attempts++ < $maxretries) {
127     ($status, @output) = Execute "/usr/local/bin/gt \"$msg\"";
128
129     if ($status) {
130       my $errmsg = "Unable to speak (Status: $status) - " . join "\n", @output;
131
132       if ($log) {
133         $log->err($errmsg);
134       } else {
135         error $errmsg;
136       } # if
137
138       sleep $backoff++;
139     } else {
140       return; # We said our piece...
141     } # if
142   } # while
143
144   my $errmsg = 'Maximum retries exceeded - terminating';
145
146   if ($log) {
147     $log->err($errmsg, $status);
148   } else {
149     error $errmsg, $status;
150   } # if
151
152   return;
153 } # speak
154
155 1;
156
157 =pod
158
159 =head1 CONFIGURATION AND ENVIRONMENT
160
161 DEBUG: If set then $debug is set to this level.
162
163 VERBOSE: If set then $verbose is set to this level.
164
165 TRACE: If set then $trace is set to this level.
166
167 =head1 DEPENDENCIES
168
169 =head2 Perl Modules
170
171 L<File::Spec|File::Spec>
172
173 L<Term::ANSIColor|Term::ANSIColor>
174
175 =head1 INCOMPATABILITIES
176
177 None yet...
178
179 =head1 BUGS AND LIMITATIONS
180
181 There are no known bugs in this module.
182
183 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
184
185 =head1 LICENSE AND COPYRIGHT
186
187 This Perl Module is freely available; you can redistribute it and/or
188 modify it under the terms of the GNU General Public License as
189 published by the Free Software Foundation; either version 2 of the
190 License, or (at your option) any later version.
191
192 This Perl Module is distributed in the hope that it will be useful,
193 but WITHOUT ANY WARRANTY; without even the implied warranty of
194 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
195 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
196 details.
197
198 You should have received a copy of the GNU General Public License
199 along with this Perl Module; if not, write to the Free Software Foundation,
200 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
201 reserved.
202
203 =cut