Plagger/Plugin/Publish/Speech/MacOSX.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 98 CC 14 SCAR 34.4
CC
14
Cov
0%
feed
72.0
speak
20.0
voice_validate
12.0
CRAP 31.1
1 package Plagger::Plugin::Publish::Speech::MacOSX;
2 use base qw( Plagger::Plugin::Publish::Speech );
3  
4 use strict;
5 use DirHandle;
6 use Mac::Files;
7 use Mac::Speech;
8  
9 sub feed {
10 my ($self, $context, $args) = @_;
11  
12 # unless( $self->is_ascii( $args->{feed}->title_text ) ){
13 # Plagger->context->log(info => "Skip 2byte-included feed: " . $args->{feed}->title_text);
14 # return;
15 # }
16  
17 my $voice = $self->conf->{voice} || 'Vicki';
18 if( $self->voice_validate( $voice ) ){
19 Plagger->context->log(debug => "$voice is ready to speak.");
20 }
21 else{
22 Plagger->context->log(info => "Voice '$voice' not found, will be replaced to 'Vicki'.");
23 $voice = 'Vicki';
24 }
25  
26 my $speed;
27 if( $self->conf->{speed} =~ /^\d+(\.\d+)*$/o ){
28 $speed = $self->conf->{speed};
29 $speed = 4.0
30 if $speed > 4.0;
31 Plagger->context->log(debug => "Speed will be multiplied by $speed.")
32 if $speed != 1.0;
33 }
34  
35 my $pitch;
36 if( $self->conf->{pitch} =~ /^\d+(\.\d+)*$/o ){
37 $pitch = $self->conf->{pitch};
38 $pitch = 2.0
39 if $pitch > 2.0;
40 Plagger->context->log(debug => "Pitch will be multiplied by $pitch.")
41 if $pitch != 1.0;
42 }
43  
44 $self->speak( $voice, $speed, $pitch, $args->{feed}->title_text );
45  
46 for my $entry ($args->{feed}->entries) {
47 my $stuff = $entry->title_text . ' ' . $entry->body_text;
48  
49 # unless( $self->is_ascii( $stuff ) ){
50 # Plagger->context->log(info => "Can't speak 2byte-included entry, sorry.");
51 # next;
52 # }
53  
54 $self->speak( $voice, $speed, $pitch, $stuff );
55 }
56 }
57  
58 sub voice_validate {
59 my ($self, $voice) = @_;
60  
61 if( my $d = DirHandle->new( FindFolder(kOnSystemDisk, kVoicesFolderType) ) ){
62 while( defined ($_ = $d->read) ){
63 return 1
64 if /^$voice\.SpeechVoice$/i;
65 }
66 }
67 }
68  
69 sub is_ascii {
70 my ($self, $str) = @_;
71  
72 return $str =~ /[^\x00-\x7f]/o ? 0 : 1;
73 }
74  
75 sub speak {
76 my ($self, $voice, $speed, $pitch, $message) = @_;
77  
78 if( my $v = $Mac::Speech::Voice{ $voice } ){
79 my $channel = Mac::Speech::NewSpeechChannel( $v );
80  
81 SetSpeechRate( $channel, GetSpeechRate( $channel ) * $speed )
82 if $speed;
83  
84 SetSpeechPitch( $channel, GetSpeechPitch( $channel ) * $pitch )
85 if $pitch;
86  
87 SpeakText( $channel => $message );
88 sleep 1
89 while SpeechBusy;
90  
91 DisposeSpeechChannel( $channel );
92 }
93 else{
94 Plagger->context->log(error => "Voice sound file for '$voice' not found on your machine.");
95 return;
96 }
97 }
98  
99 sub finalize { }
100  
101 1;
102  
103 __END__