File Coverage

blib/lib/Speech/Swift/Simple.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             #
2             # Speech::Swift - Swift Text-To-Speech for PERL
3             #
4             # Copyright (c) 2011, Mike Pultz .
5             # All rights reserved.
6             #
7             # Redistribution and use in source and binary forms, with or without
8             # modification, are permitted provided that the following conditions
9             # are met:
10             #
11             # # Redistributions of source code must retain the above copyright
12             # notice, this list of conditions and the following disclaimer.
13             #
14             # # Redistributions in binary form must reproduce the above copyright
15             # notice, this list of conditions and the following disclaimer in
16             # the documentation and/or other materials provided with the
17             # distribution.
18             #
19             # # Neither the name of Mike Pultz nor the names of his contributors
20             # may be used to endorse or promote products derived from this
21             # software without specific prior written permission.
22             #
23             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24             # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25             # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26             # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27             # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28             # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29             # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30             # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31             # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
32             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33             # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34             # POSSIBILITY OF SUCH DAMAGE.
35             #
36             # @author Mike Pultz
37             # @copyright 2011 Mike Pultz
38             # @license http://www.opensource.org/licenses/bsd-license.php BSD License
39             # @version SVN $id$
40             #
41             #
42              
43             package Speech::Swift::Simple;
44              
45 1     1   21105 use 5.010000;
  1         5  
  1         38  
46 1     1   5 use strict;
  1         2  
  1         32  
47 1     1   5 use warnings;
  1         6  
  1         35  
48 1     1   5 use Carp;
  1         2  
  1         112  
49              
50 1     1   449 use Speech::Swift;
  0            
  0            
51             require Speech::Swift::Simple::Wav;
52             require Exporter;
53              
54             our @ISA = qw(Exporter);
55             our $VERSION = '1.1';
56              
57             sub new
58             {
59             my ($_class, %_conf) = @_;
60             my %values = ();
61              
62             #
63             # create the params object
64             #
65             my $params = Speech::Swift::swift_params_new()
66             or croak("failed to initialize Swift params.");
67              
68             if (defined($_conf{encoding}))
69             {
70             if ($_conf{encoding} == Speech::Swift::AUDIO_ENCODING_PCM16)
71             {
72             Speech::Swift::swift_params_set_string($params, "audio/encoding", "pcm16");
73             } elsif ($_conf{encoding} == Speech::Swift::AUDIO_ENCODING_PCM8)
74             {
75             Speech::Swift::swift_params_set_string($params, "audio/encoding", "pcm8");
76             } elsif ($_conf{encoding} == Speech::Swift::AUDIO_ENCODING_ULAW)
77             {
78             Speech::Swift::swift_params_set_string($params, "audio/encoding", "ulaw");
79             } elsif ($_conf{encoding} == Speech::Swift::AUDIO_ENCODING_ALAW)
80             {
81             Speech::Swift::swift_params_set_string($params, "audio/encoding", "alaw");
82             } else
83             {
84             croak("Speech::Swift::Simple: invalid encoding type: " . $_conf{encoding});
85             }
86             } else
87             {
88             Speech::Swift::swift_params_set_string($params, "audio/encoding", "pcm16");
89             }
90             if (defined($_conf{channels}))
91             {
92             if ( ($_conf{channels} == 1) || ($_conf{channels} == 2) )
93             {
94             Speech::Swift::swift_params_set_int($params, "audio/channels", $_conf{channels});
95             } else
96             {
97             croak("Speech::Swift::Simple: channel setting must be 1 or 2");
98             }
99             } else
100             {
101             Speech::Swift::swift_params_set_int($params, "audio/channels", 1);
102             }
103             if (defined($_conf{deadair}))
104             {
105             if ( ($_conf{deadair} == 0) || ($_conf{deadair} == 1) )
106             {
107             Speech::Swift::swift_params_set_int($params, "audio/deadair", $_conf{deadair});
108             } else
109             {
110             croak("Speech::Swift::Simple: deadair value must be 1 or 0");
111             }
112             } else
113             {
114             Speech::Swift::swift_params_set_int($params, "audio/deadair", 0);
115             }
116            
117             #
118             # create the swift engine and port
119             #
120             my $engine = Speech::Swift::swift_engine_open($params)
121             or croak("failed to initialize Swift engine.");
122              
123             my $port = Speech::Swift::swift_port_open($engine, $params)
124             or croak("failed to initialize Swift port.");
125              
126             #
127             # store the engine, params and port objects
128             #
129             $values{engine} = $engine;
130             $values{params} = $params;
131             $values{port} = $port;
132              
133             return bless({%values}, $_class);
134             }
135             sub DESTROY
136             {
137             my ($_self) = @_;
138              
139             if ($_self->{port})
140             {
141             Speech::Swift::swift_port_close($_self->{port});
142             }
143             if ($_self->{engine})
144             {
145             Speech::Swift::swift_engine_close($_self->{engine});
146             }
147             }
148             sub set_voice
149             {
150             my ($_self, $_voice) = @_;
151              
152             if (!$_self->{port})
153             {
154             croak("invalid swift port defined");
155             }
156              
157             #
158             # lookup the voice
159             #
160             my $voice = Speech::Swift::swift_port_find_first_voice($_self->{port}, "speaker/name=" . $_voice, "");
161             if (!$voice)
162             {
163             croak("unknown voice $_voice");
164             } else
165             {
166             #
167             # set the voice by name
168             #
169             my $res = Speech::Swift::swift_port_set_voice($_self->{port}, $voice);
170             if (Speech::Swift::swift_failed($res))
171             {
172             croak("failed to set voice to $_voice");
173             }
174              
175             $_self->{voice} = $voice;
176             }
177              
178             1;
179             }
180             sub get_voices
181             {
182             my ($_self) = @_;
183              
184             if (!$_self->{port})
185             {
186             croak("invalid swift port defined");
187             }
188              
189             my $voices = ();
190              
191             #
192             # get the first voice in the list
193             #
194             my $voice = Speech::Swift::swift_port_find_first_voice($_self->{port}, "", "");
195             if ($voice)
196             {
197             #
198             # go through each voice
199             #
200             while($voice)
201             {
202             #
203             # get the attributes for this voice, and load it into a hash
204             #
205             my $name = Speech::Swift::swift_voice_get_attribute($voice, "name");
206              
207             $voices->{$name}->{id} = Speech::Swift::swift_voice_get_attribute($voice, "id");
208             $voices->{$name}->{name} = Speech::Swift::swift_voice_get_attribute($voice, "name");
209             $voices->{$name}->{path} = Speech::Swift::swift_voice_get_attribute($voice, "path");
210             $voices->{$name}->{version} = Speech::Swift::swift_voice_get_attribute($voice, "version");
211             $voices->{$name}->{buildstamp} = Speech::Swift::swift_voice_get_attribute($voice, "buildstamp");
212             $voices->{$name}->{sample_rate} = Speech::Swift::swift_voice_get_attribute($voice, "sample-rate");
213             $voices->{$name}->{license_key} = Speech::Swift::swift_voice_get_attribute($voice, "license/key");
214             $voices->{$name}->{language_tag} = Speech::Swift::swift_voice_get_attribute($voice, "language/tag");
215             $voices->{$name}->{language_name} = Speech::Swift::swift_voice_get_attribute($voice, "language/name");
216             $voices->{$name}->{language_version} = Speech::Swift::swift_voice_get_attribute($voice, "language/version");
217             $voices->{$name}->{lexicon_name} = Speech::Swift::swift_voice_get_attribute($voice, "lexicon/name");
218             $voices->{$name}->{lexicon_version} = Speech::Swift::swift_voice_get_attribute($voice, "lexicon/version");
219             $voices->{$name}->{speaker_name} = Speech::Swift::swift_voice_get_attribute($voice, "speaker/name");
220             $voices->{$name}->{speaker_gender} = Speech::Swift::swift_voice_get_attribute($voice, "speaker/gender");
221             $voices->{$name}->{speaker_age} = Speech::Swift::swift_voice_get_attribute($voice, "speaker/age");
222              
223             #
224             # get the next voice
225             #
226             $voice = Speech::Swift::swift_port_find_next_voice($_self->{port});
227             }
228             }
229              
230             return $voices;
231             }
232             sub generate
233             {
234             my ($_self, $_text) = @_;
235              
236             if (!$_self->{port})
237             {
238             croak("invalid swift port defined");
239             }
240              
241             #
242             # create a new waveform
243             #
244             my $wav = Speech::Swift::swift_waveform_new()
245             or croak("failed to create new waveform object");
246              
247             #
248             # do the TTS generation, and get the waveform
249             #
250             $wav = Speech::Swift::swift_port_get_wave($_self->{port}, $_text)
251             or croak("failed to generate wavefrom from text");
252              
253             return new Speech::Swift::Simple::Wav($wav);
254             }
255              
256             1;
257             __END__